Skip to content

Commit d21fa34

Browse files
authored
Reorganize MPI sources (#526)
* Reorganize MPI sources * Update build documentation
1 parent 309e124 commit d21fa34

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+174
-382
lines changed

doc/sphinx/build.rst

-3
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,6 @@ The CMake package defines the following variables and targets:
133133
+----------------------------+------------------------------------------+
134134
| caliper | The Caliper runtime library (target) |
135135
+----------------------------+------------------------------------------+
136-
| caliper-serial | Caliper runtime library without MPI |
137-
| | dependencies (target) |
138-
+----------------------------+------------------------------------------+
139136
| caliper-tools-util | Utilities for caliper tools (target) |
140137
+----------------------------+------------------------------------------+
141138

src/CMakeLists.txt

-38
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@ add_subdirectory(reader)
1717
add_subdirectory(services)
1818
add_subdirectory(caliper)
1919

20-
if (CALIPER_HAVE_MPI)
21-
add_subdirectory(mpi)
22-
endif()
23-
2420
set(CALIPER_COMMON_OBJS
2521
$<TARGET_OBJECTS:caliper-common>
2622
$<TARGET_OBJECTS:caliper-interface>
@@ -30,38 +26,9 @@ set(CALIPER_COMMON_OBJS
3026
${CALIPER_SERVICES_LIBS}
3127
${CALIPER_EXTERNAL_SOURCES})
3228

33-
list(APPEND CALIPER_SERIAL_OBJS
34-
${CALIPER_COMMON_OBJS})
3529
list(APPEND CALIPER_ALL_OBJS
3630
${CALIPER_COMMON_OBJS})
3731

38-
# Make (non-mpi) libcaliper-serial
39-
40-
add_library(caliper-serial ${CALIPER_SERIAL_OBJS})
41-
42-
set_target_properties(caliper-serial PROPERTIES SOVERSION ${CALIPER_MAJOR_VERSION})
43-
set_target_properties(caliper-serial PROPERTIES VERSION ${CALIPER_VERSION})
44-
45-
target_compile_features(caliper-serial PUBLIC cxx_std_11)
46-
47-
target_include_directories(
48-
caliper-serial
49-
INTERFACE
50-
"$<INSTALL_INTERFACE:include>")
51-
52-
list(APPEND CALIPER_EXTERNAL_LIBS
53-
Threads::Threads)
54-
55-
foreach (_extlib ${CALIPER_EXTERNAL_LIBS})
56-
target_link_libraries(caliper-serial PRIVATE ${_extlib})
57-
endforeach()
58-
59-
if (WIN32)
60-
target_link_libraries(caliper-serial PUBLIC ws2_32)
61-
endif()
62-
63-
# Make the combined libcaliper
64-
6532
add_library(caliper ${CALIPER_ALL_OBJS})
6633

6734
set_target_properties(caliper PROPERTIES SOVERSION ${CALIPER_MAJOR_VERSION})
@@ -89,11 +56,6 @@ if (WITH_TOOLS)
8956
add_subdirectory(tools)
9057
endif()
9158

92-
install(TARGETS caliper-serial
93-
EXPORT caliper-targets
94-
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
95-
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
96-
9759
install(TARGETS caliper
9860
EXPORT caliper-targets
9961
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}

src/caliper/CMakeLists.txt

+16-14
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,33 @@ set(CALIPER_RUNTIME_SOURCES
1616
cali_datatracker.cpp
1717
config_sanity_check.cpp)
1818

19+
if (CALIPER_HAVE_MPI)
20+
list(APPEND CALIPER_RUNTIME_SOURCES
21+
aggregate_over_mpi.cpp
22+
collective_flush.cpp
23+
CollectiveOutputChannel.cpp
24+
OutputCommMpi.cpp
25+
MpiChannelManager.cpp
26+
mpi_flush.cpp
27+
machine_mpi.cpp
28+
setup_mpi.cpp)
29+
else()
30+
list(APPEND CALIPER_RUNTIME_SOURCES
31+
machine_serial.cpp
32+
setup_serial.cpp)
33+
endif()
34+
1935
add_subdirectory(controllers)
2036

2137
add_library(caliper-runtime OBJECT ${CALIPER_RUNTIME_SOURCES})
2238

2339
target_compile_options(caliper-runtime PRIVATE ${Wall_flag})
2440
target_compile_features(caliper-runtime PUBLIC cxx_std_11)
2541

26-
list(APPEND CALIPER_SERIAL_OBJS
27-
caliper/machine_serial.cpp
28-
caliper/setup_serial.cpp)
29-
3042
if (CALIPER_HAVE_MPI)
3143
target_include_directories(caliper-runtime PRIVATE ${MPI_CXX_INCLUDE_PATH})
32-
list(APPEND CALIPER_ALL_OBJS
33-
caliper/machine_mpi.cpp
34-
caliper/setup_mpi.cpp)
35-
else()
36-
list(APPEND CALIPER_ALL_OBJS
37-
caliper/machine_serial.cpp
38-
caliper/setup_serial.cpp)
3944
endif()
4045

41-
set(CALIPER_SERIAL_OBJS ${CALIPER_SERIAL_OBJS} PARENT_SCOPE)
42-
set(CALIPER_ALL_OBJS ${CALIPER_ALL_OBJS} PARENT_SCOPE)
43-
4446
if (BUILD_TESTING)
4547
add_subdirectory(test)
4648
endif()
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

src/caliper/machine_mpi.cpp

+54-9
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,58 @@
77

88
#include "caliper/common/Log.h"
99

10-
#ifdef CALIPER_HAVE_MPI
11-
#include "../mpi/mpi_machine.h"
12-
#endif
10+
#include <mpi.h>
11+
12+
#include <unistd.h>
13+
14+
#include <cstdint>
15+
#include <string>
16+
#include <vector>
17+
18+
namespace
19+
{
20+
21+
int get_rank_for_hash(uint64_t hash, MPI_Comm comm)
22+
{
23+
int myrank = 0;
24+
int ccsize = 0;
25+
26+
PMPI_Comm_rank(comm, &myrank);
27+
PMPI_Comm_size(comm, &ccsize);
28+
29+
std::vector<uint64_t> res(ccsize, 0);
30+
31+
int ret = PMPI_Allgather(&hash, 1, MPI_UINT64_T,
32+
res.data(), 1, MPI_UINT64_T, comm);
33+
34+
if (ret != MPI_SUCCESS)
35+
return -1;
36+
37+
int hrank = 0;
38+
39+
for (int i = 0; i < myrank; ++i)
40+
if (hash == res[i])
41+
++hrank;
42+
43+
return hrank;
44+
}
45+
46+
int get_rank_for_node()
47+
{
48+
constexpr std::size_t max_len = 1024;
49+
char hostname[max_len];
50+
if (gethostname(hostname, max_len) < 0)
51+
return -1;
52+
53+
int initialized = 0;
54+
PMPI_Initialized(&initialized);
55+
if (!initialized)
56+
return 0;
57+
58+
return ::get_rank_for_hash(std::hash<std::string>{}(std::string(hostname)), MPI_COMM_WORLD);
59+
}
60+
61+
} // namespace [anonymous]
1362

1463
namespace cali
1564
{
@@ -23,11 +72,7 @@ int get_rank_for(MachineLevel level)
2372
case MachineLevel::Process:
2473
return 0;
2574
case MachineLevel::Node:
26-
#ifdef CALIPER_HAVE_MPI
27-
return mpi::get_rank_for_node();
28-
#else
29-
return 0;
30-
#endif
75+
return ::get_rank_for_node();
3176
default:
3277
Log(0).stream() << "machine::get_rank_for(MachineLevel): level "
3378
<< level << " is not supported"
@@ -37,6 +82,6 @@ int get_rank_for(MachineLevel level)
3782
return -1;
3883
}
3984

40-
}
85+
} // namespace machine
4186

4287
}
File renamed without changes.

src/caliper/setup_mpi.cpp

+79-12
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,102 @@
33

44
#include "caliper/caliper-config.h"
55

6+
#include "OutputCommMpi.h"
7+
8+
#include "caliper/CaliperService.h"
9+
#include "caliper/CustomOutputController.h"
10+
11+
#include "caliper/common/Log.h"
12+
#include "caliper/common/OutputStream.h"
13+
614
#include "../services/Services.h"
715

8-
namespace cali
16+
#include <mpi.h>
17+
18+
#include <sstream>
19+
20+
using namespace cali;
21+
22+
using COC = cali::internal::CustomOutputController;
23+
24+
namespace
925
{
1026

11-
#ifdef CALIPER_HAVE_MPI
12-
namespace mpi
27+
void
28+
setup_log_prefix()
1329
{
30+
static bool done = false;
31+
32+
if (done)
33+
return;
1434

15-
extern void setup_mpi();
16-
extern void add_mpi_controllers_and_services();
35+
int mpi_is_initialized = 0;
36+
MPI_Initialized(&mpi_is_initialized);
1737

38+
if (mpi_is_initialized && Log::is_initialized()) {
39+
int rank = 0;
40+
41+
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
42+
43+
// Disable (most) logging on non-0 ranks by default
44+
std::ostringstream logprefix;
45+
logprefix << "(" << rank << "): ";
46+
47+
Log::add_prefix(logprefix.str());
48+
49+
if (rank > 0)
50+
Log::set_verbosity(0);
51+
52+
done = true;
53+
}
1854
}
19-
#endif
55+
56+
// Implement flush over MPI for CustomOutputController objects
57+
void
58+
custom_output_controller_flush_mpi(COC* controller)
59+
{
60+
Log(2).stream() << controller->name() << ": CustomOutputController::flush(): using MPI" << std::endl;
61+
62+
OutputCommMpi comm;
63+
OutputStream stream;
64+
65+
controller->collective_flush(stream, comm);
66+
}
67+
68+
} // namespace [anonymous]
69+
70+
namespace cali
71+
{
72+
73+
extern CaliperService mpiflush_service;
2074

2175
void add_submodule_controllers_and_services()
2276
{
77+
static const CaliperService mpi_services[] = {
78+
mpiflush_service, { nullptr, nullptr }
79+
};
80+
81+
services::add_service_specs(mpi_services);
2382
services::add_default_service_specs();
24-
#ifdef CALIPER_HAVE_MPI
25-
mpi::add_mpi_controllers_and_services();
26-
#endif
2783
}
2884

2985
void init_submodules()
3086
{
31-
#ifdef CALIPER_HAVE_MPI
32-
mpi::setup_mpi();
33-
#endif
87+
::setup_log_prefix();
88+
COC::set_flush_fn(::custom_output_controller_flush_mpi);
89+
3490
add_submodule_controllers_and_services();
3591
}
3692

3793
} // namespace cali
94+
95+
extern "C"
96+
{
97+
98+
void
99+
cali_mpi_init()
100+
{
101+
::setup_log_prefix();
102+
}
103+
104+
}

src/caliper/test/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ set(CALIPER_TEST_SOURCES
1313

1414
add_executable(test_caliper ${CALIPER_TEST_SOURCES})
1515

16-
target_link_libraries(test_caliper caliper-serial gtest_main)
16+
target_link_libraries(test_caliper caliper gtest_main)
1717

1818
set(CALIPER_TEST_JSON_FILES
1919
test_single_config.json

src/mpi/CMakeLists.txt

-60
This file was deleted.

src/mpi/README.md

-5
This file was deleted.

0 commit comments

Comments
 (0)