From b0ef650a12be6f6af290964e888525cb70d0252a Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Mon, 14 Feb 2022 12:12:32 +0100 Subject: [PATCH] ENH: Pstream specialization for float/scalar, FixedList (#2351) - native MPI min/max/sum reductions for float/double irrespective of WM_PRECISION_OPTION - native MPI min/max/sum reductions for (u)int32_t/(u)int64_t types, irrespective of WM_LABEL_SIZE - replace rarely used vector2D sum reduction with FixedList as a indicator of its intent and also generalizes to different lengths. OLD: vector2D values; values.x() = ...; values.y() = ...; reduce(values, sumOp()); NEW: FixedList values; values[0] = ...; values[1] = ...; reduce(values, sumOp()); - allow returnReduce() to use native reductions. Previous code (with linear/tree selector) would have bypassed them inadvertently. ENH: added support for MPI broadcast (for a memory span) ENH: select communication schedule as a static method - UPstream::whichCommunication(comm) to select linear/tree communication instead of ternary or if (Pstream::nProcs() < Pstream::nProcsSimpleSum) ... STYLE: align nProcsSimpleSum static value with etc/controlDict override --- .../Test-parallel-communicators.C | 10 - .../Pstreams/PstreamCombineReduceOps.H | 45 +-- .../db/IOstreams/Pstreams/PstreamReduceOps.H | 311 ++++++++++-------- src/OpenFOAM/db/IOstreams/Pstreams/UPstream.C | 4 +- src/OpenFOAM/db/IOstreams/Pstreams/UPstream.H | 44 ++- .../IOstreams/Pstreams/combineGatherScatter.C | 155 +++------ .../db/IOstreams/Pstreams/gatherScatter.C | 20 +- .../db/IOstreams/Pstreams/gatherScatterList.C | 20 +- .../masterUncollatedFileOperation.C | 18 +- .../uncollatedFileOperation.C | 9 +- .../lduMatrix/solvers/GAMG/GAMGSolverScale.C | 20 +- src/Pstream/dummy/Make/files | 2 + src/Pstream/dummy/UPstream.C | 97 +----- src/Pstream/dummy/UPstreamBroadcast.C | 45 +++ src/Pstream/dummy/UPstreamReduce.C | 153 +++++++++ src/Pstream/mpi/Make/files | 2 + src/Pstream/mpi/PstreamGlobals.H | 8 +- src/Pstream/mpi/UPstream.C | 297 +---------------- src/Pstream/mpi/UPstreamBroadcast.C | 93 ++++++ src/Pstream/mpi/UPstreamReduce.C | 211 ++++++++++++ src/Pstream/mpi/allReduce.H | 45 ++- src/Pstream/mpi/allReduceTemplates.C | 214 +++++------- .../patchMeanVelocityForce.C | 39 +-- .../state/lumpedPointState.C | 10 +- 24 files changed, 932 insertions(+), 940 deletions(-) create mode 100644 src/Pstream/dummy/UPstreamBroadcast.C create mode 100644 src/Pstream/dummy/UPstreamReduce.C create mode 100644 src/Pstream/mpi/UPstreamBroadcast.C create mode 100644 src/Pstream/mpi/UPstreamReduce.C diff --git a/applications/test/parallel-communicators/Test-parallel-communicators.C b/applications/test/parallel-communicators/Test-parallel-communicators.C index 99a3158d06..6c4662d011 100644 --- a/applications/test/parallel-communicators/Test-parallel-communicators.C +++ b/applications/test/parallel-communicators/Test-parallel-communicators.C @@ -163,16 +163,6 @@ int main(int argc, char *argv[]) if (Pstream::myProcNo(comm) != -1) { - //scalar sum = sumReduce(comm, localValue); - //scalar sum = localValue; - //reduce - //( - // UPstream::treeCommunication(comm), - // sum, - // sumOp(), - // Pstream::msgType(), - // comm - //); scalar sum = returnReduce ( localValue, diff --git a/src/OpenFOAM/db/IOstreams/Pstreams/PstreamCombineReduceOps.H b/src/OpenFOAM/db/IOstreams/Pstreams/PstreamCombineReduceOps.H index 23dd721a11..7ab8c9dbd9 100644 --- a/src/OpenFOAM/db/IOstreams/Pstreams/PstreamCombineReduceOps.H +++ b/src/OpenFOAM/db/IOstreams/Pstreams/PstreamCombineReduceOps.H @@ -35,8 +35,8 @@ Description \*---------------------------------------------------------------------------*/ -#ifndef PstreamCombineReduceOps_H -#define PstreamCombineReduceOps_H +#ifndef Foam_PstreamCombineReduceOps_H +#define Foam_PstreamCombineReduceOps_H #include "UPstream.H" #include "Pstream.H" @@ -73,42 +73,11 @@ void combineReduce const label comm = Pstream::worldComm ) { - if (UPstream::nProcs(comm) < UPstream::nProcsSimpleSum) - { - Pstream::combineGather - ( - UPstream::linearCommunication(comm), - Value, - cop, - tag, - comm - ); - Pstream::combineScatter - ( - UPstream::linearCommunication(comm), - Value, - tag, - comm - ); - } - else - { - Pstream::combineGather - ( - UPstream::treeCommunication(comm), - Value, - cop, - tag, - comm - ); - Pstream::combineScatter - ( - UPstream::treeCommunication(comm), - Value, - tag, - comm - ); - } + const List& comms = + UPstream::whichCommunication(comm); + + Pstream::combineGather(comms, Value, cop, tag, comm); + Pstream::combineScatter(comms, Value, tag, comm); } diff --git a/src/OpenFOAM/db/IOstreams/Pstreams/PstreamReduceOps.H b/src/OpenFOAM/db/IOstreams/Pstreams/PstreamReduceOps.H index 2741d562bd..669a37d248 100644 --- a/src/OpenFOAM/db/IOstreams/Pstreams/PstreamReduceOps.H +++ b/src/OpenFOAM/db/IOstreams/Pstreams/PstreamReduceOps.H @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation - Copyright (C) 2016 OpenCFD Ltd. + Copyright (C) 2016-2022 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -32,11 +32,11 @@ Description \*---------------------------------------------------------------------------*/ -#ifndef PstreamReduceOps_H -#define PstreamReduceOps_H +#ifndef Foam_PstreamReduceOps_H +#define Foam_PstreamReduceOps_H #include "ops.H" -#include "vector2D.H" +#include "FixedList.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -45,12 +45,12 @@ namespace Foam // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // -// Reduce operation with user specified communication schedule +//- Reduce operation with user specified communication schedule template void reduce ( const List& comms, - T& Value, + T& value, const BinaryOp& bop, const int tag, const label comm @@ -58,96 +58,86 @@ void reduce { if (UPstream::warnComm != -1 && comm != UPstream::warnComm) { - Pout<< "** reducing:" << Value << " with comm:" << comm + Pout<< "** reducing:" << value << " with comm:" << comm << endl; error::printStack(Pout); } - Pstream::gather(comms, Value, bop, tag, comm); - Pstream::scatter(comms, Value, tag, comm); + Pstream::gather(comms, value, bop, tag, comm); + Pstream::scatter(comms, value, tag, comm); } -// Reduce using either linear or tree communication schedule +//- Reduce (inplace) using either linear or tree communication schedule template void reduce ( - T& Value, + T& value, const BinaryOp& bop, - const int tag = Pstream::msgType(), + const int tag = UPstream::msgType(), const label comm = UPstream::worldComm ) { - if (UPstream::nProcs(comm) < UPstream::nProcsSimpleSum) + if (UPstream::parRun()) { - reduce(UPstream::linearCommunication(comm), Value, bop, tag, comm); - } - else - { - reduce(UPstream::treeCommunication(comm), Value, bop, tag, comm); + reduce(UPstream::whichCommunication(comm), value, bop, tag, comm); } } -// Reduce using either linear or tree communication schedule +//- Reduce (copy) and return value template T returnReduce ( - const T& Value, + const T& value, const BinaryOp& bop, - const int tag = Pstream::msgType(), + const int tag = UPstream::msgType(), const label comm = UPstream::worldComm ) { - T WorkValue(Value); - - if (UPstream::nProcs(comm) < UPstream::nProcsSimpleSum) - { - reduce - ( - UPstream::linearCommunication(comm), - WorkValue, - bop, - tag, - comm - ); - } - else - { - reduce - ( - UPstream::treeCommunication(comm), - WorkValue, - bop, - tag, - comm - ); - } - - return WorkValue; + T work(value); + reduce(work, bop, tag, comm); + return work; } -// Reduce with sum of both value and count (for averaging) +//- Reduce with sum of both value and count (for averaging) template void sumReduce ( - T& Value, - label& Count, - const int tag = Pstream::msgType(), + T& value, + label& count, + const int tag = UPstream::msgType(), const label comm = UPstream::worldComm ) { - reduce(Value, sumOp(), tag, comm); - reduce(Count, sumOp