13#ifndef CD_PARALLELOPSIMPLEM_H
14#define CD_PARALLELOPSIMPLEM_H
23#include <CD_NamespaceHeader.H>
28 CH_TIME(
"ParallelOps::barrier");
31 MPI_Barrier(Chombo_MPI::comm);
35inline std::pair<int, int>
38 CH_TIME(
"ParallelOps::partition");
41 const int equalChunk = a_N / numProc();
42 const int remainder = a_N % numProc();
44 int begin = procID() * equalChunk;
45 int end = begin + equalChunk - 1;
48 if (procID() == numProc() - 1) {
52 return std::make_pair(begin, end);
54 return std::make_pair(0, a_N - 1);
61 CH_TIME(
"ParallelOps::min(Real)");
66 const int result = MPI_Allreduce(MPI_IN_PLACE, &ret, 1, MPI_CH_REAL, MPI_MIN, Chombo_MPI::comm);
68 if (result != MPI_SUCCESS) {
69 MayDay::Error(
"In file ParallelOps::min -- MPI communication error");
81 CH_TIME(
"ParallelOps::min(int)");
86 const int result = MPI_Allreduce(MPI_IN_PLACE, &ret, 1, MPI_INT, MPI_MIN, Chombo_MPI::comm);
88 if (result != MPI_SUCCESS) {
89 MayDay::Error(
"In file ParallelOps::min -- MPI communication error");
100 CH_TIME(
"ParallelOps::min(long long int)");
103 long long int ret = a_input;
105 const int result = MPI_Allreduce(MPI_IN_PLACE, &ret, 1, MPI_LONG_LONG, MPI_MIN, Chombo_MPI::comm);
107 if (result != MPI_SUCCESS) {
108 MayDay::Error(
"In file ParallelOps::min -- MPI communication error");
116inline std::pair<Real, RealVect>
119 CH_TIME(
"ParallelOps::min(Real, RealVect)");
122 const int nProc = numProc();
125 Real* D =
new Real[nProc];
128 Real* X =
new Real[nProc];
131 Real* Y =
new Real[nProc];
135 Real* Z =
new Real[nProc];
138 MPI_Allgather(&d, 1, MPI_CH_REAL, D, 1, MPI_CH_REAL, Chombo_MPI::comm);
139 MPI_Allgather(&x, 1, MPI_CH_REAL, X, 1, MPI_CH_REAL, Chombo_MPI::comm);
140 MPI_Allgather(&y, 1, MPI_CH_REAL, Y, 1, MPI_CH_REAL, Chombo_MPI::comm);
142 MPI_Allgather(&z, 1, MPI_CH_REAL, Z, 1, MPI_CH_REAL, Chombo_MPI::comm);
152 for (
int i = 1; i < nProc; i++) {
170 return std::make_pair(dRet, RealVect(D_DECL(xRet, yRet, zRet)));
172 return std::make_pair(a_val, a_pos);
179 CH_TIME(
"ParallelOps::max(Real)");
184 const int result = MPI_Allreduce(MPI_IN_PLACE, &ret, 1, MPI_CH_REAL, MPI_MAX, Chombo_MPI::comm);
185 if (result != MPI_SUCCESS) {
186 MayDay::Error(
"In file ParallelOps::max -- MPI communication error");
198 CH_TIME(
"ParallelOps::max(int)");
203 const int result = MPI_Allreduce(MPI_IN_PLACE, &ret, 1, MPI_INT, MPI_MAX, Chombo_MPI::comm);
205 if (result != MPI_SUCCESS) {
206 MayDay::Error(
"In file ParallelOps::max -- MPI communication error");
217 CH_TIME(
"ParallelOps::max(long long)");
220 long long ret = a_input;
222 const int result = MPI_Allreduce(MPI_IN_PLACE, &ret, 1, MPI_LONG_LONG, MPI_MAX, Chombo_MPI::comm);
224 if (result != MPI_SUCCESS) {
225 MayDay::Error(
"In file ParallelOps::max -- MPI communication error");
233inline std::pair<Real, RealVect>
236 CH_TIME(
"ParallelOps::max(Real, RealVect)");
239 const int nProc = numProc();
242 Real* D =
new Real[nProc];
245 Real* X =
new Real[nProc];
248 Real* Y =
new Real[nProc];
252 Real* Z =
new Real[nProc];
255 MPI_Allgather(&d, 1, MPI_CH_REAL, D, 1, MPI_CH_REAL, Chombo_MPI::comm);
256 MPI_Allgather(&x, 1, MPI_CH_REAL, X, 1, MPI_CH_REAL, Chombo_MPI::comm);
257 MPI_Allgather(&y, 1, MPI_CH_REAL, Y, 1, MPI_CH_REAL, Chombo_MPI::comm);
259 MPI_Allgather(&z, 1, MPI_CH_REAL, Z, 1, MPI_CH_REAL, Chombo_MPI::comm);
269 for (
int i = 1; i < nProc; i++) {
287 return std::make_pair(dRet, RealVect(D_DECL(xRet, yRet, zRet)));
289 return std::make_pair(a_val, a_pos);
293inline std::pair<Real, int>
296 CH_TIME(
"ParallelOps::maxRank(Real)");
299 const int nProc = numProc();
301 Real* values =
new Real[nProc];
303 MPI_Allgather(&a_val, 1, MPI_CH_REAL, values, 1, MPI_CH_REAL, Chombo_MPI::comm);
306 Real maxVal = -std::numeric_limits<Real>::max();
308 for (
int i = 0; i < nProc; i++) {
309 if (values[i] > maxVal) {
317 return std::make_pair(maxVal, irank);
319 return std::make_pair(a_val, 0);
323inline std::pair<Real, int>
326 CH_TIME(
"ParallelOps::minRank(Real)");
329 const int nProc = numProc();
331 Real* values =
new Real[nProc];
333 MPI_Allgather(&a_val, 1, MPI_CH_REAL, values, 1, MPI_CH_REAL, Chombo_MPI::comm);
336 Real maxVal = std::numeric_limits<Real>::max();
338 for (
int i = 0; i < nProc; i++) {
339 if (values[i] < maxVal) {
347 return std::make_pair(maxVal, irank);
349 return std::make_pair(a_val, 0);
356 CH_TIME(
"ParallelOps::sum(Real)");
361 const int result = MPI_Allreduce(&a_value, &ret, 1, MPI_CH_REAL, MPI_SUM, Chombo_MPI::comm);
362 if (result != MPI_SUCCESS) {
363 MayDay::Error(
"In file ParallelOps::sum -- MPI communication error");
375 CH_TIME(
"ParallelOps::sum(Real)");
380 const int result = MPI_Allreduce(&a_value, &ret, 1, MPI_INT, MPI_SUM, Chombo_MPI::comm);
381 if (result != MPI_SUCCESS) {
382 MayDay::Error(
"In file ParallelOps::sum -- MPI communication error");
394 CH_TIME(
"ParallelOps::sum(long)");
399 const int result = MPI_Allreduce(&a_value, &ret, 1, MPI_LONG, MPI_SUM, Chombo_MPI::comm);
400 if (result != MPI_SUCCESS) {
401 MayDay::Error(
"In file ParallelOps::sum -- MPI communication error");
413 CH_TIME(
"ParallelOps::sum(long long)");
418 const int result = MPI_Allreduce(&a_value, &ret, 1, MPI_LONG_LONG, MPI_SUM, Chombo_MPI::comm);
419 if (result != MPI_SUCCESS) {
420 MayDay::Error(
"In file ParallelOps::sum -- MPI communication error");
429inline unsigned long long
432 CH_TIME(
"ParallelOps::sum(unsigned long long)");
435 unsigned long long ret = 0;
437 const int result = MPI_Allreduce(&a_value, &ret, 1, MPI_UNSIGNED_LONG_LONG, MPI_SUM, Chombo_MPI::comm);
438 if (result != MPI_SUCCESS) {
439 MayDay::Error(
"In file ParallelOps::sum -- MPI communication error");
451 CH_TIME(
"ParallelOps::sum(Real)");
454 const int result = MPI_Allreduce(MPI_IN_PLACE, &(a_data[0]), a_data.size(), MPI_CH_REAL, MPI_SUM, Chombo_MPI::comm);
455 if (result != MPI_SUCCESS) {
456 MayDay::Error(
"In file ParallelOps::sum -- MPI communication error");
464 CH_TIME(
"ParallelOps::sum(int)");
467 const int result = MPI_Allreduce(MPI_IN_PLACE, &(a_data[0]), a_data.size(), MPI_INT, MPI_SUM, Chombo_MPI::comm);
468 if (result != MPI_SUCCESS) {
469 MayDay::Error(
"In file ParallelOps::sum -- MPI communication error");
477 CH_TIME(
"ParallelOps::sum(long int)");
480 const int result = MPI_Allreduce(MPI_IN_PLACE, &(a_data[0]), a_data.size(), MPI_LONG, MPI_SUM, Chombo_MPI::comm);
481 if (result != MPI_SUCCESS) {
482 MayDay::Error(
"In file ParallelOps::sum -- MPI communication error");
490 CH_TIME(
"ParallelOps::sum(long long int)");
493 const int result = MPI_Allreduce(MPI_IN_PLACE, &(a_data[0]), a_data.size(), MPI_LONG_LONG, MPI_SUM, Chombo_MPI::comm);
494 if (result != MPI_SUCCESS) {
495 MayDay::Error(
"In file ParallelOps::sum -- MPI communication error");
503 CH_TIME(
"ParallelOps::average(Real)");
515 CH_TIME(
"ParallelOps::standardDeviation(Real)");
518 const int nProc = numProc();
522 Real* values =
new Real[nProc];
524 MPI_Allgather(&a_value, 1, MPI_CH_REAL, values, 1, MPI_CH_REAL, Chombo_MPI::comm);
527 for (
int i = 0; i < nProc; i++) {
528 sigma += std::pow(values[i] -
average, 2);
533 return sqrt(sigma / nProc);
542 CH_TIME(
"ParallelOps::gather(int)");
545 Vector<int> ret(numProc());
547 MPI_Allgather(&a_localValue, 1, MPI_INT, ret.stdVector().data(), 1, MPI_INT, Chombo_MPI::comm);
551 return Vector<int>(1, a_localValue);
558 CH_TIME(
"ParallelOps::gather(Real)");
561 Vector<Real> ret(numProc());
563 MPI_Allgather(&a_localValue, 1, MPI_CH_REAL, ret.stdVector().data(), 1, MPI_CH_REAL, Chombo_MPI::comm);
567 return Vector<Real>(1, a_localValue);
571inline Vector<long long>
574 CH_TIME(
"ParallelOps::gather(long long)");
577 Vector<long long> ret(numProc());
579 MPI_Allgather(&a_localValue, 1, MPI_LONG_LONG, ret.stdVector().data(), 1, MPI_LONG_LONG, Chombo_MPI::comm);
583 return Vector<long long>(1, a_localValue);
587#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:177
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:294
Real average(const Real &a_val) noexcept
Compute the average (across MPI ranks) of the input value.
Definition CD_ParallelOpsImplem.H:501
Real standardDeviation(const Real &a_value) noexcept
Compute the standard deviation of the input value.
Definition CD_ParallelOpsImplem.H:513
Vector< int > gather(const int &a_localValue) noexcept
Gather local values – return a vector of local values.
Definition CD_ParallelOpsImplem.H:540
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:324
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:59
Real sum(const Real &a_value) noexcept
Compute the sum across all MPI ranks.
Definition CD_ParallelOpsImplem.H:354
std::pair< int, int > partition(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:36
void barrier() noexcept
MPI barrier.
Definition CD_ParallelOpsImplem.H:26