svMultiPhysics
Loading...
Searching...
No Matches
CmMod.h
1// SPDX-FileCopyrightText: Copyright (c) Stanford University, The Regents of the University of California, and others.
2// SPDX-License-Identifier: BSD-3-Clause
3
4// The classes defined here duplicate the data structures in the Fortran CMMOD module
5// defined in COMU.f.
6
7#ifndef CMMOD_H
8#define CMMOD_H
9
10#include "Array.h"
11#include "Array3.h"
12#include "Vector.h"
13
14#include "mpi.h"
15#include "consts.h"
16
17#include <string>
18
19namespace cm_mod {
20
21using MpiCommWorldType = MPI_Comm;
22//using MpiCommWorldType = decltype(MPI_COMM_WORLD);
23
24// Set MPI data type names.
25const decltype(MPI_CXX_BOOL) mplog = MPI_CXX_BOOL;
26//const decltype(MPI_LOGICAL) mplog = MPI_LOGICAL;
27const decltype(MPI_INTEGER) mpint = MPI_INTEGER;
28const decltype(MPI_UNSIGNED) mpuint = MPI_UNSIGNED;
29const decltype(MPI_DOUBLE_PRECISION) mpreal = MPI_DOUBLE_PRECISION;
30const decltype(MPI_CHARACTER) mpchar = MPI_CHARACTER;
31};
32
33/// @brief The CmMod class duplicates the data structures in the Fortran CMMOD module
34/// defined in COMU.f.
35///
36/// The data members here are the global variables exposed by the CMMOD module.
37class CmMod {
38
39 public:
40 CmMod();
41 ~CmMod();
42
43 // Size of blocks for openMP communications.
44 int mpBs = 1000;
45
46 // master is assumed to have zero ID
47 int master = 0;
48
49 // Abstracted MPI names.
50 decltype(MPI_LOGICAL) mplog = MPI_LOGICAL;
51 decltype(MPI_INTEGER) mpint = MPI_INTEGER;
52 decltype(MPI_DOUBLE_PRECISION) mpreal = MPI_DOUBLE_PRECISION;
53 decltype(MPI_CHARACTER) mpchar = MPI_CHARACTER;
54};
55
56/// @brief The cmType class stores data and defines methods used for mpi communication.
57class cmType {
58 public:
59 cmType();
60 ~cmType();
61
62 // Communicator handle.
63 decltype(MPI_COMM_WORLD) cHndl;
64
65 // Processors ID.
66 int taskId = 0;
67
68 // Number of openMP threads in this cm
69 int nThreads = 0;
70
71 // Number of processors
72 int nProcs = 0;
73
74 //----- M e t h o d s -----//
75
76 void bcast(const CmMod& cm_mod, bool* data) const;
77 void bcast(const CmMod& cm_mod, std::vector<bool>& data) const;
78
79 void bcast(const CmMod& cm_mod, std::string& data) const;
80
81 void bcast(const CmMod& cm_mod, double* data) const;
82 void bcast(const CmMod& cm_mod, Vector<double>& data, const std::string& name="") const;
83 void bcast(const CmMod& cm_mod, Array<double>& data, const std::string& name="") const;
84 void bcast(const CmMod& cm_mod, Array3<double>& data, const std::string& name="") const;
85
86 void bcast(const CmMod& cm_mod, int* data) const;
87 void bcast(const CmMod& cm_mod, Vector<int>& data) const;
88
89 void bcast(const CmMod &cm_mod, unsigned int *data) const;
90
91 void bcast(const CmMod& cm_mod, Array<int>& data, const std::string& name="") const;
92
93 // Gather operations
94 void gather(const CmMod& cm_mod, const int* send_data, int send_count, int* recv_data, int recv_count, int root) const;
95 void gather(const CmMod& cm_mod, const double* send_data, int send_count, double* recv_data, int recv_count, int root) const;
96 void gather(const CmMod& cm_mod, const Vector<int>& send_data, Vector<int>& recv_data, int root) const;
97 void gather(const CmMod& cm_mod, const Vector<double>& send_data, Vector<double>& recv_data, int root) const;
98
99 // Gatherv operations
100 void gatherv(const CmMod& cm_mod, const Vector<int>& send_data, Vector<int>& recv_data, const Vector<int>& recv_counts, const Vector<int>& displs, int root) const;
101 void gatherv(const CmMod& cm_mod, const Vector<double>& send_data, Vector<double>& recv_data, const Vector<int>& recv_counts, const Vector<int>& displs, int root) const;
102
103 // Scatterv operations
104 void scatterv(const CmMod& cm_mod, const Vector<int>& send_data, const Vector<int>& send_counts, const Vector<int>& displs, Vector<int>& recv_data, int root) const;
105 void scatterv(const CmMod& cm_mod, const Vector<double>& send_data, const Vector<int>& send_counts, const Vector<int>& displs, Vector<double>& recv_data, int root) const;
106
107 // Scatter operations
108 void scatter(const CmMod& cm_mod, const int* send_data, int send_count, int* recv_data, int recv_count, int root) const;
109 void scatter(const CmMod& cm_mod, const double* send_data, int send_count, double* recv_data, int recv_count, int root) const;
110 void scatter(const CmMod& cm_mod, const Vector<int>& send_data, Vector<int>& recv_data, int root) const;
111 void scatter(const CmMod& cm_mod, const Vector<double>& send_data, Vector<double>& recv_data, int root) const;
112
113 //------------
114 // bcast_enum
115 //------------
116 //
117 template <typename T>
118 void bcast_enum(const CmMod& cm_mod, T* data) const
119 {
120 int idata = static_cast<int>(*data);
121 //std::cout << "[bcast_enum] idata in: " << idata << std::endl;
122 MPI_Bcast(&idata, 1, cm_mod::mpint, cm_mod.master, com());
123 //std::cout << "[bcast_enum] idata out: " << idata << std::endl;
124 *data = static_cast<T>(idata);
125 }
126
127 //------------
128 // bcast_prop
129 //------------
130 //
131 template <typename T>
132 void bcast_prop(const CmMod& cm_mod, std::map<T,double>& props) const
133 {
134 static const int MAX_SIZE = 100;
135
136 if (2*props.size() > MAX_SIZE) {
137 throw std::runtime_error("bcast prop is larger than " + std::to_string(MAX_SIZE) + ".");
138 }
139
140 double prop_array[MAX_SIZE];
141 std::fill_n(prop_array, MAX_SIZE, -1.0);
142
143 int n = 0;
144 for (auto& entry : props) {
145 prop_array[n++] = static_cast<int>(entry.first);
146 prop_array[n++] = entry.second;
147 }
148
149 MPI_Bcast(prop_array, MAX_SIZE, cm_mod::mpreal, cm_mod.master, com());
150
151 props.clear();
152 int num_props = MAX_SIZE / 2;;
153
154 for (int i = 0; i < num_props; i++) {
155 int iprop = static_cast<int>(prop_array[2*i]);
156 if (iprop == -1) {
157 break;
158 }
159 auto prop = static_cast<T>(iprop);
160 props[prop] = prop_array[2*i+1];
161 }
162 }
163
164 // Returns commu handle
165 cm_mod::MpiCommWorldType com() const;
166 //decltype(MPI_COMM_WORLD) com() const;
167
168 int idcm() const { return taskId; };
169 int id() { return taskId; };
170 bool mas(const CmMod& cm_mod) const { return (taskId == cm_mod.master); };
171
172 // Create a new Communicator
173 void new_cm(decltype(MPI_COMM_WORLD) comHandle);
174
175 int np() const { return nProcs; };
176
177 int nT() { return nThreads; };
178
179
180 //--------
181 // reduce
182 //--------
183 // For an int or double scalar.
184 //
185 template<typename T>
186 T reduce(const CmMod& cm_mod, T u, MPI_Op op = MPI_SUM) const
187 {
188 T gU{};
189
190 MPI_Datatype data_type;
191 if (typeid(T) == typeid(double)) {
192 data_type = MPI_DOUBLE_PRECISION;
193 } else if (typeid(T) == typeid(int)) {
194 data_type = MPI_INTEGER;
195 } else {
196 throw std::runtime_error("[cm_mod::reduce called with unknown data type.");
197 }
198
199 if (seq()) {
200 gU = u;
201 } else {
202 MPI_Allreduce(&u, &gU, 1, data_type, op, com());
203 }
204
205 return gU;
206 }
207
208 //--------
209 // reduce
210 //--------
211 // For an int or double Vector.
212 //
213 template<typename T>
214 Vector<T> reduce(const CmMod& cm_mod, Vector<T>& u, MPI_Op op = MPI_SUM) const
215 {
216 int size = u.size();
217 Vector<T> gU(size);
218
219 MPI_Datatype data_type;
220 if (typeid(T) == typeid(double)) {
221 data_type = MPI_DOUBLE_PRECISION;
222 } if (typeid(T) == typeid(int)) {
223 data_type = MPI_INTEGER;
224 }
225
226 if (seq()) {
227 gU = u;
228 } else {
229 MPI_Allreduce(u.data(), gU.data(), size, data_type, op, com());
230 }
231
232 return gU;
233 }
234
235 bool seq() const { return (nProcs == 1); };
236
237 bool slv(const CmMod& cm_mod) const { return (taskId != cm_mod.master); };
238
239 // Returns processor ID in fortran indexing
240 int tF(const CmMod& cm_mod) const { return taskId + 1; };
241
242};
243
244
245#endif
246
The Array3 template class implements a simple interface to 3D arrays.
Definition Array3.h:25
The CmMod class duplicates the data structures in the Fortran CMMOD module defined in COMU....
Definition CmMod.h:37
The Vector template class is used for storing int and double data.
Definition Vector.h:26
void gatherv(const CmMod &cm_mod, const Vector< int > &send_data, Vector< int > &recv_data, const Vector< int > &recv_counts, const Vector< int > &displs, int root) const
gatherv int Vector
Definition CmMod.cpp:205
void scatterv(const CmMod &cm_mod, const Vector< int > &send_data, const Vector< int > &send_counts, const Vector< int > &displs, Vector< int > &recv_data, int root) const
scatterv int Vector
Definition CmMod.cpp:219
void bcast(const CmMod &cm_mod, bool *data) const
bcast bool.
Definition CmMod.cpp:53
void gather(const CmMod &cm_mod, const int *send_data, int send_count, int *recv_data, int recv_count, int root) const
gather int array
Definition CmMod.cpp:153
cm_mod::MpiCommWorldType com() const
Returns commu handle.
Definition CmMod.cpp:32
void scatter(const CmMod &cm_mod, const int *send_data, int send_count, int *recv_data, int recv_count, int root) const
scatter int array
Definition CmMod.cpp:179