Skip to content

Commit

Permalink
#378 Creation of DYNAWO_USE_MPI macro
Browse files Browse the repository at this point in the history
Signed-off-by: Dimitri Baron <[email protected]>
  • Loading branch information
barondim authored and gautierbureau committed May 13, 2024
1 parent 9f95fbc commit 9fc6d3e
Show file tree
Hide file tree
Showing 17 changed files with 269 additions and 188 deletions.
3 changes: 2 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@
{ "name": "DYNAWO_BOOST_HOME", "value" : "${workspaceFolder}/../dynawo/build-code/deploy/Debug/dynawo" },
{ "name": "DYNAWO_LIBXML_HOME", "value" : "${workspaceFolder}/../dynawo/build-code/deploy/Debug/dynawo" },
{ "name": "DYNAWO_NICSLU_INSTALL_DIR", "value" : "${workspaceFolder}/../dynawo/build-code/deploy/Debug/dynawo" },
{ "name": "LD_LIBRARY_PATH", "value" : "${env:LD_LIBRARY_PATH}:${workspaceFolder}/build-code/install/3rdParty/Debug/gperftools/lib:${workspaceFolder}/build-code/install/3rdParty/Debug/mpich/lib:${workspaceFolder}/build-code/install/dynawo-algorithms/Debug/lib:${workspaceFolder}/../dynawo/build-code/deploy/Debug/dynawo/lib" }
{ "name": "LD_LIBRARY_PATH", "value" : "${env:LD_LIBRARY_PATH}:${workspaceFolder}/build-code/install/3rdParty/Debug/gperftools/lib:${workspaceFolder}/build-code/install/3rdParty/Debug/mpich/lib:${workspaceFolder}/build-code/install/dynawo-algorithms/Debug/lib:${workspaceFolder}/../dynawo/build-code/deploy/Debug/dynawo/lib:${workspaceFolder}/build-code/build/dynawo-algorithms/Debug/sources/Test" },
{ "name": "PATH", "value": "${env:PATH}:${workspaceFolder}/../dynawo/build-code/deploy/Debug/dynawo/OpenModelica/bin" }
],
"externalConsole": false,
"MIMode": "gdb",
Expand Down
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
"TMP_DIR": "${workspaceFolder}/build-code/build/3rdParty/${buildType}/tmp",
"DOWNLOAD_DIR": "${workspaceFolder}/build-code/build/3rdParty/${buildType}/src",
"GTEST_ROOT": "${workspaceFolder}/../dynawo/build-code/deploy/Debug/dynawo",
"GMOCK_HOME": "${workspaceFolder}/../dynawo/build-code/deploy/Debug/dynawo"
"GMOCK_HOME": "${workspaceFolder}/../dynawo/build-code/deploy/Debug/dynawo",
"USE_MPI": "YES"
},

"files.exclude": {
Expand Down
6 changes: 4 additions & 2 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"DYNAWO_ALGORITHMS_DEPLOY_DIR": "${workspaceFolder}/build-code/deploy/Debug/dynawo-algorithms",
"DYNAWO_ALGORITHMS_BUILD_DIR": "${workspaceFolder}/build-code/build/dynawo-algorithms/Debug",
"DYNAWO_ALGORITHMS_THIRD_PARTY_BUILD_DIR": "${workspaceFolder}/build-code/build/3rdParty/Debug/build",
"DYNAWO_ALGORITHMS_THIRD_PARTY_INSTALL_DIR": "${workspaceFolder}/build-code/install/3rdParty/Debug"
"DYNAWO_ALGORITHMS_THIRD_PARTY_INSTALL_DIR": "${workspaceFolder}/build-code/install/3rdParty/Debug",
"DYNAWO_USE_MPI": "YES"
}
}
},
Expand All @@ -37,7 +38,8 @@
"DYNAWO_ALGORITHMS_DEPLOY_DIR": "${workspaceFolder}/build-code/deploy/Release/dynawo-algorithms",
"DYNAWO_ALGORITHMS_BUILD_DIR": "${workspaceFolder}/build-code/build/dynawo-algorithms/Release",
"DYNAWO_ALGORITHMS_THIRD_PARTY_BUILD_DIR": "${workspaceFolder}/build-code/build/3rdParty/Release/build",
"DYNAWO_ALGORITHMS_THIRD_PARTY_INSTALL_DIR": "${workspaceFolder}/build-code/install/3rdParty/Release"
"DYNAWO_ALGORITHMS_THIRD_PARTY_INSTALL_DIR": "${workspaceFolder}/build-code/install/3rdParty/Release",
"DYNAWO_USE_MPI": "YES"
}
}
}
Expand Down
74 changes: 38 additions & 36 deletions 3rdParty/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,42 +61,44 @@ if(UNIX)
endif()
endif()

set(mpich_version "3.4.2")
if(DEFINED ENV{DYNAWO_ALGORITHMS_MPICH_DOWNLOAD_URL})
set(mpich_prefix_url $ENV{DYNAWO_ALGORITHMS_MPICH_DOWNLOAD_URL})
else()
set(mpich_prefix_url http://www.mpich.org/static/downloads)
endif()
set(mpich_url ${mpich_prefix_url}/${mpich_version}/mpich-${mpich_version}.tar.gz)
set(mpich_md5 6ee1cfff98728e5160c6e78bdb1986ca)
if(USE_MPI STREQUAL "YES")
set(mpich_version "3.4.2")
if(DEFINED ENV{DYNAWO_ALGORITHMS_MPICH_DOWNLOAD_URL})
set(mpich_prefix_url $ENV{DYNAWO_ALGORITHMS_MPICH_DOWNLOAD_URL})
else()
set(mpich_prefix_url http://www.mpich.org/static/downloads)
endif()
set(mpich_url ${mpich_prefix_url}/${mpich_version}/mpich-${mpich_version}.tar.gz)
set(mpich_md5 6ee1cfff98728e5160c6e78bdb1986ca)

find_package(MPI QUIET)
if(MPI_FOUND)
add_custom_target(mpich)
else()
ExternalProject_Add(mpich
INSTALL_DIR ${CMAKE_INSTALL_PREFIX}/mpich
DOWNLOAD_DIR ${CMAKE_CURRENT_SOURCE_DIR}/mpich
TMP_DIR ${TMP_DIR}
STAMP_DIR ${DOWNLOAD_DIR}/mpich-stamp
SOURCE_DIR ${DOWNLOAD_DIR}/mpich
URL ${mpich_url}
URL_MD5 ${mpich_md5}
BUILD_IN_SOURCE 1
find_package(MPI QUIET)
if(MPI_FOUND)
add_custom_target(mpich)
else()
ExternalProject_Add(mpich
INSTALL_DIR ${CMAKE_INSTALL_PREFIX}/mpich
DOWNLOAD_DIR ${CMAKE_CURRENT_SOURCE_DIR}/mpich
TMP_DIR ${TMP_DIR}
STAMP_DIR ${DOWNLOAD_DIR}/mpich-stamp
SOURCE_DIR ${DOWNLOAD_DIR}/mpich
URL ${mpich_url}
URL_MD5 ${mpich_md5}
BUILD_IN_SOURCE 1

CONFIGURE_COMMAND <SOURCE_DIR>/configure
--prefix=<INSTALL_DIR>
"CC=${CMAKE_C_COMPILER}"
"CXX=${CMAKE_CXX_COMPILER}"
"CFLAGS=-m64 -O3 -DNDEBUG"
"CXXFLAGS=-m64 -O3 -DNDEBUG"
"FCFLAGS=-m64 -O3 -DNDEBUG"
"LDFLAGS=-m64 -O3 -DNDEBUG"
--with-device=ch3:nemesis
--disable-fortran
--enable-fast=all,O3,ndebug
BUILD_COMMAND make -j ${CPU_COUNT}
)
ExternalProject_Get_Property(mpich install_dir)
set(MPI_HOME ${install_dir})
CONFIGURE_COMMAND <SOURCE_DIR>/configure
--prefix=<INSTALL_DIR>
"CC=${CMAKE_C_COMPILER}"
"CXX=${CMAKE_CXX_COMPILER}"
"CFLAGS=-m64 -O3 -DNDEBUG"
"CXXFLAGS=-m64 -O3 -DNDEBUG"
"FCFLAGS=-m64 -O3 -DNDEBUG"
"LDFLAGS=-m64 -O3 -DNDEBUG"
--with-device=ch3:nemesis
--disable-fortran
--enable-fast=all,O3,ndebug
BUILD_COMMAND make -j ${CPU_COUNT}
)
ExternalProject_Get_Property(mpich install_dir)
set(MPI_HOME ${install_dir})
endif()
endif()
13 changes: 8 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,14 @@ find_package(Dynawo 1.7.0 REQUIRED)
find_package(XercesC REQUIRED)
# end of hack

set(MPI_HOME ${DYNAWO_ALGORITHMS_THIRD_PARTY_DIR}/mpich)
find_package(MPI REQUIRED)
if(MPI_FOUND)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${MPI_C_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${MPI_CXX_FLAGS}")
if(USE_MPI STREQUAL "YES")
set(MPI_HOME ${DYNAWO_ALGORITHMS_THIRD_PARTY_DIR}/mpich)
find_package(MPI REQUIRED)
if(MPI_FOUND)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${MPI_C_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${MPI_CXX_FLAGS}")
endif()
add_compile_definitions(_MPI_)
endif()

if(BUILD_TESTS_COVERAGE)
Expand Down
12 changes: 8 additions & 4 deletions sources/Common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ set(DYN_ALGO_COMMON_SOURCES
DYNLoadIncrease.cpp
DYNSimulationResult.cpp
DYNLoadIncreaseResult.cpp
DYNMPIContext.cpp
DYNMultiProcessingContext.cpp
${CPP_KEYS}
)

Expand All @@ -61,11 +61,15 @@ set(DYN_ALGO_COMMON_HEADERS
DYNScenarios.h
DYNSimulationResult.h
DYNLoadIncreaseResult.h
DYNMPIContext.h
DYNMPIContext.hpp
DYNMultiProcessingContext.h
${INCLUDE_KEYS}
)

if (USE_MPI STREQUAL "YES")
list(APPEND DYN_ALGO_COMMON_HEADERS
DYNMultiProcessingContext.hpp)
endif()

add_library(dynawo_algorithms_Common SHARED ${DYN_ALGO_COMMON_SOURCES})
add_dependencies(dynawo_algorithms_Common create_keys_files)
install(FILES ${DYN_ALGO_COMMON_HEADERS} DESTINATION ${INCLUDEDIR_NAME})
Expand All @@ -80,7 +84,7 @@ target_include_directories(dynawo_algorithms_Common

target_link_libraries(dynawo_algorithms_Common
Dynawo::dynawo_Common
MPI::MPI_CXX
$<$<STREQUAL:${USE_MPI},YES>:MPI::MPI_CXX>
)

install(TARGETS dynawo_algorithms_Common
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,26 @@
// of simulation tools for power systems.
//

#include "DYNMPIContext.h"
#include "DYNMultiProcessingContext.h"

#include <iostream>
#include <numeric>

namespace DYNAlgorithms {
namespace mpi {
namespace multiprocessing {

Context* Context::instance_ = nullptr;
bool Context::finalized_ = false;

Context&
Context::instance() {
if (!instance_) {
std::cerr << "No MPI context has been instantiated" << std::endl;
std::cerr << "No multiprocessing context has been instantiated" << std::endl;
std::exit(EXIT_FAILURE);
}

if (finalized_) {
std::cerr << "MPI context has already been finalized" << std::endl;
std::cerr << "Multiprocessing context has already been finalized" << std::endl;
std::exit(EXIT_FAILURE);
}

Expand All @@ -39,10 +39,11 @@ Context::instance() {

Context::Context() {
if (instance_) {
std::cerr << "MPI context should only be instantiated once per process in the main thread" << std::endl;
std::cerr << "Multiprocessing context should only be instantiated once per process in the main thread" << std::endl;
std::exit(EXIT_FAILURE);
}

#ifdef _MPI_
int ret = MPI_Init(NULL, NULL);
if (ret != MPI_SUCCESS) {
std::cerr << "MPI initialization error" << std::endl;
Expand All @@ -60,25 +61,29 @@ Context::Context() {
MPI_Finalize();
std::exit(EXIT_FAILURE);
}
#endif

instance_ = this;
}

Context::~Context() {
finalized_ = true;
#ifdef _MPI_
MPI_Finalize();
#endif
}

void
forEach(unsigned int iStart, unsigned int size, const std::function<void(unsigned int)>& func) {
auto& context = mpi::context();
for (unsigned int i = iStart; i < size; i++) {
if (i % context.nbProcs() == context.rank()) {
#ifdef _MPI_
if (i % multiprocessing::context().nbProcs() == multiprocessing::context().rank())
#endif
func(i);
}
}
}

#ifdef _MPI_
template<>
void
Context::gatherImpl(Tag<bool>, const bool& data, std::vector<bool>& recvData) const {
Expand Down Expand Up @@ -150,7 +155,8 @@ Context::broadcastImpl(Tag<std::vector<bool> >, std::vector<bool>& data) const {
std::transform(dataInt.begin(), dataInt.end(), std::back_inserter(data), [](unsigned int value) { return static_cast<bool>(value); });
}
}
#endif

} // namespace mpi
} // namespace multiprocessing

} // namespace DYNAlgorithms
Loading

0 comments on commit 9fc6d3e

Please sign in to comment.