12 #ifndef CD_ParallelOpsImplem_H
13 #define CD_ParallelOpsImplem_H
22 #include <CD_NamespaceHeader.H>
27 CH_TIME(
"ParallelOps::barrier");
30 MPI_Barrier(Chombo_MPI::comm);
34 inline std::pair<int, int>
37 CH_TIME(
"ParallelOps::partition");
40 const int equalChunk = a_N / numProc();
41 const int remainder = a_N % numProc();
43 int begin = procID() * equalChunk;
44 int end = begin + equalChunk - 1;
47 if (procID() == numProc() - 1) {
51 return std::make_pair(begin, end);
53 return std::make_pair(0, a_N - 1);
60 CH_TIME(
"ParallelOps::min(Real)");
65 const int result = MPI_Allreduce(MPI_IN_PLACE, &ret, 1, MPI_CH_REAL, MPI_MIN, Chombo_MPI::comm);
67 if (result != MPI_SUCCESS) {
68 MayDay::Error(
"In file ParallelOps::min -- MPI communication error");
80 CH_TIME(
"ParallelOps::min(int)");
85 const int result = MPI_Allreduce(MPI_IN_PLACE, &ret, 1, MPI_INT, MPI_MIN, Chombo_MPI::comm);
87 if (result != MPI_SUCCESS) {
88 MayDay::Error(
"In file ParallelOps::min -- MPI communication error");
99 CH_TIME(
"ParallelOps::min(long long int)");
102 long long int ret = a_input;
104 const int result = MPI_Allreduce(MPI_IN_PLACE, &ret, 1, MPI_LONG_LONG, MPI_MIN, Chombo_MPI::comm);
106 if (result != MPI_SUCCESS) {
107 MayDay::Error(
"In file ParallelOps::min -- MPI communication error");
115 inline std::pair<Real, RealVect>
118 CH_TIME(
"ParallelOps::min(Real, RealVect)");
121 const int nProc = numProc();
124 Real* D =
new Real[nProc];
127 Real* X =
new Real[nProc];
130 Real* Y =
new Real[nProc];
134 Real* Z =
new Real[nProc];
137 MPI_Allgather(&d, 1, MPI_CH_REAL, D, 1, MPI_CH_REAL, Chombo_MPI::comm);
138 MPI_Allgather(&x, 1, MPI_CH_REAL, X, 1, MPI_CH_REAL, Chombo_MPI::comm);
139 MPI_Allgather(&y, 1, MPI_CH_REAL, Y, 1, MPI_CH_REAL, Chombo_MPI::comm);
141 MPI_Allgather(&z, 1, MPI_CH_REAL, Z, 1, MPI_CH_REAL, Chombo_MPI::comm);
151 for (
int i = 1; i < nProc; i++) {
169 return std::make_pair(dRet, RealVect(D_DECL(xRet, yRet, zRet)));
171 return std::make_pair(a_val, a_pos);
178 CH_TIME(
"ParallelOps::max(Real)");
183 const int result = MPI_Allreduce(MPI_IN_PLACE, &ret, 1, MPI_CH_REAL, MPI_MAX, Chombo_MPI::comm);
184 if (result != MPI_SUCCESS) {
185 MayDay::Error(
"In file ParallelOps::max -- MPI communication error");
197 CH_TIME(
"ParallelOps::max(int)");
202 const int result = MPI_Allreduce(MPI_IN_PLACE, &ret, 1, MPI_INT, MPI_MAX, Chombo_MPI::comm);
204 if (result != MPI_SUCCESS) {
205 MayDay::Error(
"In file ParallelOps::max -- MPI communication error");
216 CH_TIME(
"ParallelOps::max(long long)");
219 long long ret = a_input;
221 const int result = MPI_Allreduce(MPI_IN_PLACE, &ret, 1, MPI_LONG_LONG, MPI_MAX, Chombo_MPI::comm);
223 if (result != MPI_SUCCESS) {
224 MayDay::Error(
"In file ParallelOps::max -- MPI communication error");
232 inline std::pair<Real, RealVect>
235 CH_TIME(
"ParallelOps::max(Real, RealVect)");
238 const int nProc = numProc();
241 Real* D =
new Real[nProc];
244 Real* X =
new Real[nProc];
247 Real* Y =
new Real[nProc];
251 Real* Z =
new Real[nProc];
254 MPI_Allgather(&d, 1, MPI_CH_REAL, D, 1, MPI_CH_REAL, Chombo_MPI::comm);
255 MPI_Allgather(&x, 1, MPI_CH_REAL, X, 1, MPI_CH_REAL, Chombo_MPI::comm);
256 MPI_Allgather(&y, 1, MPI_CH_REAL, Y, 1, MPI_CH_REAL, Chombo_MPI::comm);
258 MPI_Allgather(&z, 1, MPI_CH_REAL, Z, 1, MPI_CH_REAL, Chombo_MPI::comm);
268 for (
int i = 1; i < nProc; i++) {
286 return std::make_pair(dRet, RealVect(D_DECL(xRet, yRet, zRet)));
288 return std::make_pair(a_val, a_pos);
292 inline std::pair<Real, int>
295 CH_TIME(
"ParallelOps::maxRank(Real)");
298 const int nProc = numProc();
300 Real* values =
new Real[nProc];
302 MPI_Allgather(&a_val, 1, MPI_CH_REAL, values, 1, MPI_CH_REAL, Chombo_MPI::comm);
307 for (
int i = 0; i < nProc; i++) {
308 if (values[i] > maxVal) {
316 return std::make_pair(maxVal, irank);
318 return std::make_pair(a_val, 0);
322 inline std::pair<Real, int>
325 CH_TIME(
"ParallelOps::minRank(Real)");
328 const int nProc = numProc();
330 Real* values =
new Real[nProc];
332 MPI_Allgather(&a_val, 1, MPI_CH_REAL, values, 1, MPI_CH_REAL, Chombo_MPI::comm);
337 for (
int i = 0; i < nProc; i++) {
338 if (values[i] < maxVal) {
346 return std::make_pair(maxVal, irank);
348 return std::make_pair(a_val, 0);
355 CH_TIME(
"ParallelOps::sum(Real)");
360 const int result = MPI_Allreduce(&a_value, &ret, 1, MPI_CH_REAL, MPI_SUM, Chombo_MPI::comm);
361 if (result != MPI_SUCCESS) {
362 MayDay::Error(
"In file ParallelOps::sum -- MPI communication error");
374 CH_TIME(
"ParallelOps::sum(Real)");
379 const int result = MPI_Allreduce(&a_value, &ret, 1, MPI_INT, MPI_SUM, Chombo_MPI::comm);
380 if (result != MPI_SUCCESS) {
381 MayDay::Error(
"In file ParallelOps::sum -- MPI communication error");
393 CH_TIME(
"ParallelOps::sum(long)");
398 const int result = MPI_Allreduce(&a_value, &ret, 1, MPI_LONG, MPI_SUM, Chombo_MPI::comm);
399 if (result != MPI_SUCCESS) {
400 MayDay::Error(
"In file ParallelOps::sum -- MPI communication error");
412 CH_TIME(
"ParallelOps::sum(long long)");
417 const int result = MPI_Allreduce(&a_value, &ret, 1, MPI_LONG_LONG, MPI_SUM, Chombo_MPI::comm);
418 if (result != MPI_SUCCESS) {
419 MayDay::Error(
"In file ParallelOps::sum -- MPI communication error");
428 inline unsigned long long
431 CH_TIME(
"ParallelOps::sum(unsigned long long)");
434 unsigned long long ret = 0;
436 const int result = MPI_Allreduce(&a_value, &ret, 1, MPI_UNSIGNED_LONG_LONG, MPI_SUM, Chombo_MPI::comm);
437 if (result != MPI_SUCCESS) {
438 MayDay::Error(
"In file ParallelOps::sum -- MPI communication error");
450 CH_TIME(
"ParallelOps::vectorSum(Real)");
453 const int result = MPI_Allreduce(MPI_IN_PLACE, &(a_data[0]), a_data.size(), MPI_CH_REAL, MPI_SUM, Chombo_MPI::comm);
454 if (result != MPI_SUCCESS) {
455 MayDay::Error(
"In file ParallelOps::vectorSum -- MPI communication error");
463 CH_TIME(
"ParallelOps::vectorSum(int)");
466 const int result = MPI_Allreduce(MPI_IN_PLACE, &(a_data[0]), a_data.size(), MPI_INT, MPI_SUM, Chombo_MPI::comm);
467 if (result != MPI_SUCCESS) {
468 MayDay::Error(
"In file ParallelOps::vectorSum -- MPI communication error");
476 CH_TIME(
"ParallelOps::vectorSum(long int)");
479 const int result = MPI_Allreduce(MPI_IN_PLACE, &(a_data[0]), a_data.size(), MPI_LONG, MPI_SUM, Chombo_MPI::comm);
480 if (result != MPI_SUCCESS) {
481 MayDay::Error(
"In file ParallelOps::vectorSum -- MPI communication error");
489 CH_TIME(
"ParallelOps::vectorSum(long long int)");
492 const int result = MPI_Allreduce(MPI_IN_PLACE, &(a_data[0]), a_data.size(), MPI_LONG_LONG, MPI_SUM, Chombo_MPI::comm);
493 if (result != MPI_SUCCESS) {
494 MayDay::Error(
"In file ParallelOps::vectorSum -- MPI communication error");
502 CH_TIME(
"ParallelOps::average(Real)");
514 CH_TIME(
"ParallelOps::standardDeviation(Real)");
517 const int nProc = numProc();
521 Real* values =
new Real[nProc];
523 MPI_Allgather(&a_value, 1, MPI_CH_REAL, values, 1, MPI_CH_REAL, Chombo_MPI::comm);
526 for (
int i = 0; i < nProc; i++) {
527 sigma += std::pow(values[i] -
average, 2);
532 return sqrt(sigma / nProc);
538 #include <CD_NamespaceFooter.H>
Real max(const Real &a_input) noexcept
Get the maximum of the input, reduced over MPI ranks (in the Chombo communicator)
Definition: CD_ParallelOpsImplem.H:176
std::pair< Real, int > maxRank(const Real &a_val) noexcept
Get the maximum value and the rank having the maximum value.
Definition: CD_ParallelOpsImplem.H:293
Real average(const Real &a_val) noexcept
Compute the average (across MPI ranks) of the input value.
Definition: CD_ParallelOpsImplem.H:500
Real standardDeviation(const Real &a_value) noexcept
Compute the standard deviation of the input value.
Definition: CD_ParallelOpsImplem.H:512
std::pair< Real, int > minRank(const Real &a_val) noexcept
Get the minimum value and the rank having the minimum value.
Definition: CD_ParallelOpsImplem.H:323
void vectorSum(Vector< Real > &a_data) noexcept
Perform a summation of all the MPI ranks's input data.
Definition: CD_ParallelOpsImplem.H:448
Real min(const Real &a_input) noexcept
Get the minimum of the input, reduced over MPI ranks (in the Chombo communicator)
Definition: CD_ParallelOpsImplem.H:58
Real sum(const Real &a_value) noexcept
Compute the sum across all MPI ranks.
Definition: CD_ParallelOpsImplem.H:353
void barrier() noexcept
MPI barrier.
Definition: CD_ParallelOpsImplem.H:25
std::pair< int, int > partition(const int a_N) noexcept
Partition the input number such that each MPI rank gets an equal chunk. We assume that N is the lengt...
Definition: CD_ParallelOpsImplem.H:35