25#ifndef VULPES_UTILS_MPI_WRAPPERS_HPP_
26#define VULPES_UTILS_MPI_WRAPPERS_HPP_
57 if constexpr (std::is_same_v<T, unsigned char>) {
58 return MPI_UNSIGNED_CHAR;
59 }
else if constexpr (std::is_same_v<T, char>) {
61 }
else if constexpr (std::is_same_v<T, short>) {
63 }
else if constexpr (std::is_same_v<T, int>) {
65 }
else if constexpr (std::is_same_v<T, long>) {
67 }
else if constexpr (std::is_same_v<T, long long>) {
69 }
else if constexpr (std::is_same_v<
71 return MPI_UNSIGNED_SHORT;
72 }
else if constexpr (std::is_same_v<T, unsigned int>) {
74 }
else if constexpr (std::is_same_v<
76 return MPI_UNSIGNED_LONG;
77 }
else if constexpr (std::is_same_v<
78 T,
unsigned long long>) {
79 return MPI_UNSIGNED_LONG_LONG;
80 }
else if constexpr (std::is_same_v<T, float>) {
82 }
else if constexpr (std::is_same_v<T, double>) {
84 }
else if constexpr (std::is_same_v<T, long double>) {
85 return MPI_LONG_DOUBLE;
87 static_assert(
sizeof(T) * 0 &&
"Unknown conversion to MPI type.");
93 int mpi_is_initialized;
94 const int mpi_err = MPI_Initialized(&mpi_is_initialized);
95 return (mpi_err == MPI_SUCCESS) && mpi_is_initialized;
99 PetscBool petsc_is_initialized;
100 PetscCall(PetscInitialized(&petsc_is_initialized));
101 return petsc_is_initialized;
105 int mpi_is_finalized;
106 const int mpi_err = MPI_Finalized(&mpi_is_finalized);
107 return (mpi_err == MPI_SUCCESS) && mpi_is_finalized;
111 PetscBool petsc_is_finalized;
112 PetscCall(PetscFinalized(&petsc_is_finalized));
113 return petsc_is_finalized;
117 static int world_rank = -1;
118 if (world_rank == -1) {
119 MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
125 static int world_size = -1;
126 if (world_size == -1) {
127 MPI_Comm_size(MPI_COMM_WORLD, &world_size);
153 MPI_Comm communicator = MPI_COMM_WORLD) {
169 const T& data,
int destination, MPI_Comm communicator = MPI_COMM_WORLD) {
184 const std::vector<T>& data,
186 MPI_Comm communicator = MPI_COMM_WORLD) {
187 const std::size_t count = data.size();
205 const std::string& data,
207 MPI_Comm communicator = MPI_COMM_WORLD) {
208 const std::size_t count = data.size();
227template <
typename T, std::
size_t N>
229 const std::vector<std::array<T, N>>& data,
231 MPI_Comm communicator = MPI_COMM_WORLD) {
232 const std::size_t count = data.size();
239 data[0].data(), data.size() * N,
getMpiType<T>(), destination,
256template <
typename T,
int N,
int M,
int Options>
258 const std::vector<Eigen::Matrix<T, N, M, Options>>& data,
260 MPI_Comm communicator = MPI_COMM_WORLD) {
261 static_assert(N > 0 && M > 0);
262 const std::size_t count = data.size();
269 data[0].data(), data.size() * N,
getMpiType<T>(), destination,
287 IndexType RowSize = IndexType(-1)>
291 MPI_Comm communicator = MPI_COMM_WORLD) {
292 send(map.
data(), destination, communicator);
315 MPI_Comm communicator = MPI_COMM_WORLD,
316 MPI_Status* status = MPI_STATUS_IGNORE) {
335 MPI_Comm communicator = MPI_COMM_WORLD,
336 MPI_Status* status = MPI_STATUS_IGNORE) {
338 recv(&data, 1, source, communicator, status);
353 std::vector<T>& data,
355 MPI_Comm communicator = MPI_COMM_WORLD,
356 MPI_Status* status = MPI_STATUS_IGNORE) {
380 MPI_Comm communicator = MPI_COMM_WORLD,
381 MPI_Status* status = MPI_STATUS_IGNORE) {
390 communicator, status);
404template <
typename T, std::
size_t N>
406 std::vector<std::array<T, N>>& data,
408 MPI_Comm communicator = MPI_COMM_WORLD,
409 MPI_Status* status = MPI_STATUS_IGNORE) {
419 communicator, status);
436template <
typename T,
int N,
int M,
int Options>
438 std::vector<Eigen::Matrix<T, N, M, Options>>& data,
440 MPI_Comm communicator = MPI_COMM_WORLD,
441 MPI_Status* status = MPI_STATUS_IGNORE) {
442 static_assert(N > 0 && M > 0);
452 communicator, status);
470 IndexType RowSize = IndexType(-1)>
474 MPI_Comm communicator = MPI_COMM_WORLD,
475 MPI_Status* status = MPI_STATUS_IGNORE) {
477 recv(map.
data(), source, communicator, status);
496 T* data,
int count,
int root, MPI_Comm communicator = MPI_COMM_WORLD) {
510void bcast(T& data,
int root, MPI_Comm communicator = MPI_COMM_WORLD) {
525 std::vector<T>& data,
int root, MPI_Comm communicator = MPI_COMM_WORLD) {
526 std::size_t count = 0;
545 std::string& data,
int root, MPI_Comm communicator = MPI_COMM_WORLD) {
546 std::size_t count = data.size();
569template <
typename T,
int N,
int M,
int Options>
571 Eigen::Matrix<T, N, M, Options>& data,
573 MPI_Comm communicator = MPI_COMM_WORLD) {
574 std::size_t nrows = 0, ncols = 0;
583 data.resize(nrows, ncols);
611 MPI_Comm communicator = MPI_COMM_WORLD) {
632 MPI_Comm communicator = MPI_COMM_WORLD) {
659 std::vector<int32_t> displacements(comm_size), counts(comm_size);
661 const int32_t chunck_sz = count / comm_size;
663 for (int32_t i = 0; i < comm_size; i++) {
664 counts[i] = stride * (chunck_sz + (i < (count % comm_size)));
667 displacements[0] = 0;
668 for (int32_t i = 1; i < comm_size; i++) {
669 displacements[i] = displacements[i - 1] + counts[i - 1];
672 return std::pair{displacements, counts};
698 MPI_Comm communicator = MPI_COMM_WORLD) {
701 send_data, send_count,
getMpiType<T>(), recv_data, recv_count,
719 const std::vector<T>& send_data,
720 std::vector<T>& recv_data,
722 MPI_Comm communicator = MPI_COMM_WORLD) {
723 int send_count = send_data.size();
724 int recv_count = recv_data.size() /
size();
727 send_data.data(), send_count,
getMpiType<T>(), recv_data.data(),
753 MPI_Comm communicator = MPI_COMM_WORLD) {
756 send_data, send_count,
getMpiType<T>(), recv_data, recv_count, displs,
784 MPI_Comm communicator = MPI_COMM_WORLD) {
787 send_data, send_count,
getMpiType<T>(), recv_data, recv_count,
805 const std::vector<T>& send_data,
806 std::vector<T>& recv_data,
808 MPI_Comm communicator = MPI_COMM_WORLD) {
809 int recv_count = recv_data.size();
810 int send_count = send_data.size() /
size();
813 send_data.data(), send_count,
getMpiType<T>(), recv_data.data(),
842 MPI_Comm mpi_comm = MPI_COMM_WORLD,
847 send_data, send_counts, displacements, send_t, recv_data, recv_count,
848 recv_t,
root, mpi_comm);
867 int* provided =
nullptr) {
868 return init(&argc, &argv, required, provided);
873 char*** argv =
nullptr,
875 int* provided =
nullptr) {
878 if (required == MPI_THREAD_SINGLE) {
879 mpi_res = MPI_Init(argc, argv);
881 mpi_res = MPI_Init_thread(argc, argv, required, provided);
883 if (mpi_res != MPI_SUCCESS) {
884 throw std::runtime_error(
"MPI_Init failed");
889 PetscCall(PetscInitialize(argc, argv,
nullptr,
nullptr));
908 PetscCall(PetscFinalize());
static void startInterval(const char *label)
startInterval(label) when recording is on.
Definition profiler.hpp:96
static void stopInterval(const char *label)
stopInterval(label) when recording is on.
Definition profiler.hpp:102
Definition crs_map.hpp:72
const auto & indices() const
Constant accessor to the indices buffer.
Definition crs_map.hpp:442
const auto & data() const
Constant accessor to the data buffer.
Definition crs_map.hpp:438
MpiHandle()=default
Prevent direct construction of objects of this class.
static constexpr int s_default_thread_required
Default level of thread support for MPI_Init_thread.
Definition mpi_wrappers.hpp:899
static int init(int &argc, char **&argv, int required=s_default_thread_required, int *provided=nullptr)
Singleton creation with Mpi::init(argc, argv).
Definition mpi_wrappers.hpp:863
static int init(int *argc=nullptr, char ***argv=nullptr, int required=s_default_thread_required, int *provided=nullptr)
Singleton creation with Mpi::init().
Definition mpi_wrappers.hpp:871
~MpiHandle()
finalize MPI.
Definition mpi_wrappers.hpp:916
static int finalize()
finalize MPI and PETSc.
Definition mpi_wrappers.hpp:906
static MpiHandle & singleton()
initialize the Mpi singleton.
Definition mpi_wrappers.hpp:901
Definition mpi_wrappers.hpp:44
void recv(T *data, int count, int source, MPI_Comm communicator=MPI_COMM_WORLD, MPI_Status *status=MPI_STATUS_IGNORE)
Receive count contiguous values of type T from a source rank.
Definition mpi_wrappers.hpp:311
static int rank()
Return the MPI rank in MPI_COMM_WORLD.
Definition mpi_wrappers.hpp:116
void gatherv(const T *send_data, int send_count, T *recv_data, int *recv_count, int *displs, int root, MPI_Comm communicator=MPI_COMM_WORLD)
Gather variable-length contiguous values of type T from every rank into recv_data on root.
Definition mpi_wrappers.hpp:746
static int size()
Return the size of MPI_COMM_WORLD.
Definition mpi_wrappers.hpp:124
static bool root()
Return true if the rank in MPI_COMM_WORLD is zero.
Definition mpi_wrappers.hpp:132
static bool isPetscFinalized()
Return true if PETSc has been finalized.
Definition mpi_wrappers.hpp:110
static bool isFinalized()
Return true if MPI has been finalized.
Definition mpi_wrappers.hpp:104
void send(const T *data, int count, int destination, MPI_Comm communicator=MPI_COMM_WORLD)
Send count contiguous values of type T to a destination rank.
Definition mpi_wrappers.hpp:149
void scatter(const T *send_data, int send_count, T *recv_data, int recv_count, int root, MPI_Comm communicator=MPI_COMM_WORLD)
Scatter recv_count contiguous values of type T from root to every rank.
Definition mpi_wrappers.hpp:778
void reduce(const T *send_data, T *recv_data, int count, MPI_Op op, int root, MPI_Comm communicator=MPI_COMM_WORLD)
Reduce count contiguous values of type T onto root.
Definition mpi_wrappers.hpp:605
static bool isPetscInitialized()
Return true if PETSc has been initialized.
Definition mpi_wrappers.hpp:98
void scatterv(const T *send_data, int *send_counts, int *displacements, T *recv_data, int recv_count, int root, MPI_Comm mpi_comm=MPI_COMM_WORLD, MPI_Datatype send_t=getMpiType< T >(), MPI_Datatype recv_t=getMpiType< T >())
Scatter variable-length contiguous values of type T from root to every rank.
Definition mpi_wrappers.hpp:835
void gather(const T *send_data, int send_count, T *recv_data, int recv_count, int root, MPI_Comm communicator=MPI_COMM_WORLD)
Gather send_count contiguous values of type T from every rank into recv_data on root.
Definition mpi_wrappers.hpp:692
static bool isInitialized()
Return true if MPI has been initialized.
Definition mpi_wrappers.hpp:92
MPI_Datatype getMpiType()
Map a C++ arithmetic type to its corresponding MPI_Datatype.
Definition mpi_wrappers.hpp:56
void bcast(T *data, int count, int root, MPI_Comm communicator=MPI_COMM_WORLD)
Broadcast count contiguous values of type T from root.
Definition mpi_wrappers.hpp:495
auto computeChuncks(int32_t count, int32_t stride=1)
Split count elements into near-equal contiguous chunks, one per rank in the current communicator.
Definition mpi_wrappers.hpp:657
static constexpr int s_default_tag
Default MPI message tag used by all wrappers in this file.
Definition mpi_wrappers.hpp:47
void configureVtkThreading()
Configure the VTK SMP thread budget.
Definition threading.cpp:190
void checkOpenBlasThreading()
Validate the active OpenBLAS thread count.
Definition threading.cpp:195
Definition cell_manager.hpp:38
Thread configuration and runtime validation utilities.