libROM  v1.0
Data-driven physical simulation library
mpi_utils.cpp
1 
11 // Description: Utility functions for MPI distribution.
12 
13 #include "mpi_utils.h"
14 #include "Utilities.h"
15 
16 #include <iomanip>
17 #include <stdlib.h>
18 #include <sys/stat.h>
19 
20 namespace CAROM {
21 
22 int
23 split_dimension(const int dim, const MPI_Comm &comm)
24 {
25  int mpi_init;
26  MPI_Initialized(&mpi_init);
27  CAROM_VERIFY(mpi_init != 0);
28 
29  int d_num_procs, d_rank;
30  MPI_Comm_rank(comm, &d_rank);
31  MPI_Comm_size(comm, &d_num_procs);
32 
33  int local_dim = dim / d_num_procs;
34  if (dim % d_num_procs > d_rank)
35  local_dim++;
36 
37  return local_dim;
38 }
39 
40 int
41 get_global_offsets(const int local_dim, std::vector<int> &offsets,
42  const MPI_Comm &comm)
43 {
44  int mpi_init;
45  MPI_Initialized(&mpi_init);
46  CAROM_VERIFY(mpi_init != 0);
47 
48  int d_num_procs, d_rank;
49  MPI_Comm_rank(comm, &d_rank);
50  MPI_Comm_size(comm, &d_num_procs);
51 
52  offsets.resize(d_num_procs + 1);
53  offsets[d_rank] = local_dim;
54  CAROM_VERIFY(MPI_Allgather(MPI_IN_PLACE, 1, MPI_INT,
55  &offsets[0], 1, MPI_INT,
56  comm) == MPI_SUCCESS);
57 
58  int dim = 0;
59  for (int i = 0; i < d_num_procs; i++)
60  dim += offsets[i];
61  offsets[d_num_procs] = dim;
62 
63  for (int i = d_num_procs - 1; i >= 0; i--)
64  offsets[i] = offsets[i + 1] - offsets[i];
65 
66  CAROM_VERIFY(offsets[0] == 0);
67 
68  return dim;
69 }
70 
71 bool
72 is_same(int x, const MPI_Comm &comm) {
73  int p[2] = {-x, x};
74  MPI_Allreduce(MPI_IN_PLACE, p, 2, MPI_INT, MPI_MIN, comm);
75  return (p[0] == -p[1]);
76 }
77 
78 }
int split_dimension(const int dim, const MPI_Comm &comm)
Distribute the global size dim into MPI processes as equally as possible.
Definition: mpi_utils.cpp:23
bool is_same(int x, const MPI_Comm &comm)
Check if an integer is equal over all MPI processes.
Definition: mpi_utils.cpp:72
int get_global_offsets(const int local_dim, std::vector< int > &offsets, const MPI_Comm &comm)
Save integer offsets for each MPI rank under MPI communicator comm, where each rank as the size of lo...
Definition: mpi_utils.cpp:41