Chi-Tech
chi_mpi_utils.cc
Go to the documentation of this file.
1#include "chi_mpi_utils.h"
2
3namespace chi_mpi_utils
4{
5
6/**Returns the current rank on the specified communicator.*/
7int GetLocationID(MPI_Comm mpi_comm)
8{
9 int location_id;
10 MPI_Comm_rank(mpi_comm, &location_id);
11
12 return location_id;
13}
14
15/**Returns the total number of ranks on the specified communicator.*/
16int GetProcessCount(MPI_Comm mpi_comm)
17{
18 int process_count;
19 MPI_Comm_size(mpi_comm, &process_count);
20
21 return process_count;
22}
23
24/**Given each location's local size (of items), builds a vector (dimension
25* comm-size plus 1) of where each location's global indices start and end.
26* Example: location i starts at extents[i] and ends at extents[i+1]*/
27std::vector<uint64_t> BuildLocationExtents(uint64_t local_size, MPI_Comm comm)
28{
29 const int process_count = GetProcessCount(comm);
30 // Get the local vector sizes per process
31 std::vector<uint64_t> local_sizes(process_count, 0);
32 MPI_Allgather(&local_size, // sendbuf
33 1,
34 MPI_UINT64_T, // sendcount + sendtype
35 local_sizes.data(), // recvbuf
36 1,
37 MPI_UINT64_T, // recvcount + recvtype
38 comm); // communicator
39
40 // With the vector sizes per processor, now the offsets for each
41 // processor can be defined using a cumulative sum per processor.
42 // This allows for the determination of whether a global index is
43 // locally owned or not.
44 std::vector<uint64_t> extents(process_count + 1, 0);
45 for (size_t locJ = 1; locJ < process_count; ++locJ)
46 extents[locJ] = extents[locJ - 1] + local_sizes[locJ - 1];
47 extents[process_count] = extents[process_count - 1] + local_sizes.back();
48
49 return extents;
50}
51}
int GetProcessCount(MPI_Comm mpi_comm)
std::vector< uint64_t > BuildLocationExtents(uint64_t local_size, MPI_Comm comm)
int GetLocationID(MPI_Comm mpi_comm)
Definition: chi_mpi_utils.cc:7