Loading CMakeLists.txt 0 → 100644 +72 −0 Original line number Diff line number Diff line # Create an "mpitest" package containing: # - an mpiwrap library and header file # - an mpitest executable that uses the library # cmake_minimum_required(VERSION 3.17) project(mpitest VERSION 1.0 LANGUAGES CXX) option(BUILD_SHARED_LIBS "Build using shared libraries" ON) find_package(MPI REQUIRED) ########## TARGETS ################################################ add_library(mpiwrap src/wrapper.cc) add_executable(mpitest src/bcast.cc) ##################################################################### target_compile_features(mpitest PUBLIC cxx_std_14) target_compile_features(mpiwrap PUBLIC cxx_std_14) target_link_libraries(mpiwrap PUBLIC MPI::MPI_CXX) target_include_directories(mpiwrap PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> $<INSTALL_INTERFACE:include> ) # lib needs PIC when BUILD_SHARED_LIBS=ON set_target_properties(mpiwrap PROPERTIES POSITION_INDEPENDENT_CODE ${BUILD_SHARED_LIBS} ) target_link_libraries(mpitest mpiwrap) ### Installation Instructions ### set(installable_bin mpitest) install(TARGETS ${installable_bin} DESTINATION bin) # Attach these libraries to the mpitestTargets list of exported libs. set(installable_libs mpiwrap) install(TARGETS ${installable_libs} DESTINATION lib EXPORT mpitestTargets) set(MPIWRAP_HEADERS include/mpiwrap.hh) install(FILES ${MPIWRAP_HEADERS} DESTINATION include) # Note: we choose the following location for cmake dependency info: # <prefix>/lib/cmake/mpitest/ # install the targets to export install(EXPORT mpitestTargets FILE mpitestTargets.cmake NAMESPACE mpitest:: DESTINATION lib/cmake/mpitest ) # Create a config helper so others can call find_package(mpitest::mpiwrap) include(CMakePackageConfigHelpers) configure_package_config_file(${CMAKE_CURRENT_SOURCE_DIR}/Config.cmake.in "${CMAKE_CURRENT_BINARY_DIR}/mpitestConfig.cmake" INSTALL_DESTINATION "lib/cmake/mpitest" NO_SET_AND_CHECK_MACRO ) # generate the version file for the config file write_basic_package_version_file( "${CMAKE_CURRENT_BINARY_DIR}/mpitestConfigVersion.cmake" VERSION "${mpitest_VERSION_MAJOR}.${mpitest_VERSION_MINOR}" COMPATIBILITY AnyNewerVersion ) # install the configuration file install(FILES ${CMAKE_CURRENT_BINARY_DIR}/mpitestConfig.cmake ${CMAKE_CURRENT_BINARY_DIR}/mpitestConfigVersion.cmake DESTINATION lib/cmake/mpitest ) Config.cmake.in 0 → 100644 +7 −0 Original line number Diff line number Diff line @PACKAGE_INIT@ include ( "${CMAKE_CURRENT_LIST_DIR}/mpitestTargets.cmake" ) find_dependency(MPI REQUIRED) check_required_components(mpitest) include/mpiwrap.hh 0 → 100644 +23 −0 Original line number Diff line number Diff line #include <mpi.h> #include <stdlib.h> #include <assert.h> #include <stdio.h> #define MPICHECK(cmd) do { \ int e = cmd; \ if( e != MPI_SUCCESS ) { \ printf("Failed: MPI error %s:%d '%d'\n", \ __FILE__,__LINE__, e); \ exit(EXIT_FAILURE); \ } \ } while(0) struct MPIH { int ranks, rank; MPI_Comm comm; MPIH(int *argc, char **argv[]); ~MPIH(); }; src/bcast.cc 0 → 100644 +14 −0 Original line number Diff line number Diff line #include <mpiwrap.hh> #include <stdio.h> int main(int argc, char *argv[]) { MPIH mpi(&argc, &argv); double buf[128]; int count = sizeof(buf)/sizeof(double); if(mpi.rank == 0) printf("Broadcasting %d doubles from root.\n", count); MPI_Bcast(buf, count, MPI_DOUBLE, 0, mpi.comm); return 0; } src/wrapper.cc 0 → 100644 +12 −0 Original line number Diff line number Diff line #include <mpiwrap.hh> MPIH::MPIH(int *argc, char **argv[]) : comm(MPI_COMM_WORLD) { int provided; MPICHECK( MPI_Init_thread(argc, argv, MPI_THREAD_FUNNELED, &provided) ); assert(provided >= MPI_THREAD_FUNNELED); MPICHECK( MPI_Comm_size( comm, &ranks) ); MPICHECK( MPI_Comm_rank( comm, &rank ) ); } MPIH::~MPIH() { MPI_Finalize(); } Loading
CMakeLists.txt 0 → 100644 +72 −0 Original line number Diff line number Diff line # Create an "mpitest" package containing: # - an mpiwrap library and header file # - an mpitest executable that uses the library # cmake_minimum_required(VERSION 3.17) project(mpitest VERSION 1.0 LANGUAGES CXX) option(BUILD_SHARED_LIBS "Build using shared libraries" ON) find_package(MPI REQUIRED) ########## TARGETS ################################################ add_library(mpiwrap src/wrapper.cc) add_executable(mpitest src/bcast.cc) ##################################################################### target_compile_features(mpitest PUBLIC cxx_std_14) target_compile_features(mpiwrap PUBLIC cxx_std_14) target_link_libraries(mpiwrap PUBLIC MPI::MPI_CXX) target_include_directories(mpiwrap PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> $<INSTALL_INTERFACE:include> ) # lib needs PIC when BUILD_SHARED_LIBS=ON set_target_properties(mpiwrap PROPERTIES POSITION_INDEPENDENT_CODE ${BUILD_SHARED_LIBS} ) target_link_libraries(mpitest mpiwrap) ### Installation Instructions ### set(installable_bin mpitest) install(TARGETS ${installable_bin} DESTINATION bin) # Attach these libraries to the mpitestTargets list of exported libs. set(installable_libs mpiwrap) install(TARGETS ${installable_libs} DESTINATION lib EXPORT mpitestTargets) set(MPIWRAP_HEADERS include/mpiwrap.hh) install(FILES ${MPIWRAP_HEADERS} DESTINATION include) # Note: we choose the following location for cmake dependency info: # <prefix>/lib/cmake/mpitest/ # install the targets to export install(EXPORT mpitestTargets FILE mpitestTargets.cmake NAMESPACE mpitest:: DESTINATION lib/cmake/mpitest ) # Create a config helper so others can call find_package(mpitest::mpiwrap) include(CMakePackageConfigHelpers) configure_package_config_file(${CMAKE_CURRENT_SOURCE_DIR}/Config.cmake.in "${CMAKE_CURRENT_BINARY_DIR}/mpitestConfig.cmake" INSTALL_DESTINATION "lib/cmake/mpitest" NO_SET_AND_CHECK_MACRO ) # generate the version file for the config file write_basic_package_version_file( "${CMAKE_CURRENT_BINARY_DIR}/mpitestConfigVersion.cmake" VERSION "${mpitest_VERSION_MAJOR}.${mpitest_VERSION_MINOR}" COMPATIBILITY AnyNewerVersion ) # install the configuration file install(FILES ${CMAKE_CURRENT_BINARY_DIR}/mpitestConfig.cmake ${CMAKE_CURRENT_BINARY_DIR}/mpitestConfigVersion.cmake DESTINATION lib/cmake/mpitest )
Config.cmake.in 0 → 100644 +7 −0 Original line number Diff line number Diff line @PACKAGE_INIT@ include ( "${CMAKE_CURRENT_LIST_DIR}/mpitestTargets.cmake" ) find_dependency(MPI REQUIRED) check_required_components(mpitest)
include/mpiwrap.hh 0 → 100644 +23 −0 Original line number Diff line number Diff line #include <mpi.h> #include <stdlib.h> #include <assert.h> #include <stdio.h> #define MPICHECK(cmd) do { \ int e = cmd; \ if( e != MPI_SUCCESS ) { \ printf("Failed: MPI error %s:%d '%d'\n", \ __FILE__,__LINE__, e); \ exit(EXIT_FAILURE); \ } \ } while(0) struct MPIH { int ranks, rank; MPI_Comm comm; MPIH(int *argc, char **argv[]); ~MPIH(); };
src/bcast.cc 0 → 100644 +14 −0 Original line number Diff line number Diff line #include <mpiwrap.hh> #include <stdio.h> int main(int argc, char *argv[]) { MPIH mpi(&argc, &argv); double buf[128]; int count = sizeof(buf)/sizeof(double); if(mpi.rank == 0) printf("Broadcasting %d doubles from root.\n", count); MPI_Bcast(buf, count, MPI_DOUBLE, 0, mpi.comm); return 0; }
src/wrapper.cc 0 → 100644 +12 −0 Original line number Diff line number Diff line #include <mpiwrap.hh> MPIH::MPIH(int *argc, char **argv[]) : comm(MPI_COMM_WORLD) { int provided; MPICHECK( MPI_Init_thread(argc, argv, MPI_THREAD_FUNNELED, &provided) ); assert(provided >= MPI_THREAD_FUNNELED); MPICHECK( MPI_Comm_size( comm, &ranks) ); MPICHECK( MPI_Comm_rank( comm, &rank ) ); } MPIH::~MPIH() { MPI_Finalize(); }