diff --git a/applications/test/parallel-comm2/Test-parallel-comm2.C b/applications/test/parallel-comm2/Test-parallel-comm2.C index 53b8fc3e5e..7f5a4afa5a 100644 --- a/applications/test/parallel-comm2/Test-parallel-comm2.C +++ b/applications/test/parallel-comm2/Test-parallel-comm2.C @@ -52,8 +52,8 @@ void rankInfo(const label comm) << "(parent:" << UPstream::parent(comm) << ')' << " rank:" << ranki << "(sub:" << UPstream::is_subrank(comm) - << ") nProcs:" << UPstream::nProcs(comm) - << " baseProcNo:" << UPstream::baseProcNo(comm, ranki); + << ") nProcs:" << UPstream::nProcs(comm); + // << " baseProcNo:" << UPstream::baseProcNo(comm, ranki); } @@ -82,8 +82,10 @@ int main(int argc, char *argv[]) #include "setRootCase.H" Info<< nl + << "parallel:" << UPstream::parRun() << "nProcs = " << UPstream::nProcs() - << " with " << UPstream::nComms() << " predefined comm(s)" << nl; + << " with " << UPstream::nComms() << " predefined comm(s)." + << " proc:" << UPstream::myProcNo() << nl; Info<< nl; diff --git a/src/OpenFOAM/db/IOstreams/Pstreams/PstreamBroadcast.C b/src/OpenFOAM/db/IOstreams/Pstreams/PstreamBroadcast.C index 5532260412..98c2486337 100644 --- a/src/OpenFOAM/db/IOstreams/Pstreams/PstreamBroadcast.C +++ b/src/OpenFOAM/db/IOstreams/Pstreams/PstreamBroadcast.C @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2022 OpenCFD Ltd. + Copyright (C) 2022-2023 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -34,31 +34,28 @@ License template void Foam::Pstream::broadcast(Type& value, const label comm) { - if (UPstream::parRun() && UPstream::nProcs(comm) > 1) + if (is_contiguous::value) { - if (is_contiguous::value) + // Note: contains parallel guard internally + UPstream::broadcast + ( + reinterpret_cast(&value), + sizeof(Type), + comm, + UPstream::masterNo() + ); + } + else if (UPstream::is_parallel(comm)) + { + if (UPstream::master(comm)) { - // Note: contains parallel guard internally as well - UPstream::broadcast - ( - reinterpret_cast(&value), - sizeof(Type), - comm, - UPstream::masterNo() - ); + OPBstream os(UPstream::masterNo(), comm); + os << value; } - else + else // UPstream::is_subrank(comm) { - if (UPstream::master(comm)) - { - OPBstream os(UPstream::masterNo(), comm); - os << value; - } - else - { - IPBstream is(UPstream::masterNo(), comm); - is >> value; - } + IPBstream is(UPstream::masterNo(), comm); + is >> value; } } } @@ -67,14 +64,14 @@ void Foam::Pstream::broadcast(Type& value, const label comm) template void Foam::Pstream::broadcasts(const label comm, Type& arg1, Args&&... args) { - if (UPstream::parRun() && UPstream::nProcs(comm) > 1) + if (UPstream::is_parallel(comm)) { if (UPstream::master(comm)) { OPBstream os(UPstream::masterNo(), comm); Detail::outputLoop(os, arg1, std::forward(args)...); } - else + else // UPstream::is_subrank(comm) { IPBstream is(UPstream::masterNo(), comm); Detail::inputLoop(is, arg1, std::forward(args)...); diff --git a/src/OpenFOAM/db/IOstreams/Pstreams/PstreamCombineGather.C b/src/OpenFOAM/db/IOstreams/Pstreams/PstreamCombineGather.C index 8460e33a73..41d427635b 100644 --- a/src/OpenFOAM/db/IOstreams/Pstreams/PstreamCombineGather.C +++ b/src/OpenFOAM/db/IOstreams/Pstreams/PstreamCombineGather.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2017 OpenFOAM Foundation - Copyright (C) 2019-2022 OpenCFD Ltd. + Copyright (C) 2019-2023 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -53,7 +53,7 @@ void Foam::Pstream::combineGather const label comm ) { - if (UPstream::parRun() && UPstream::nProcs(comm) > 1) + if (UPstream::is_parallel(comm)) { // My communication order const commsStruct& myComm = comms[UPstream::myProcNo(comm)]; @@ -155,7 +155,7 @@ void Foam::Pstream::combineScatter #ifndef Foam_Pstream_scatter_nobroadcast Pstream::broadcast(value, comm); #else - if (UPstream::parRun() && UPstream::nProcs(comm) > 1) + if (UPstream::is_parallel(comm)) { // My communication order const UPstream::commsStruct& myComm = comms[UPstream::myProcNo(comm)]; @@ -290,7 +290,7 @@ void Foam::Pstream::combineReduce const label comm ) { - if (UPstream::parRun() && UPstream::nProcs(comm) > 1) + if (UPstream::is_parallel(comm)) { const auto& comms = UPstream::whichCommunication(comm); @@ -312,7 +312,7 @@ void Foam::Pstream::listCombineGather const label comm ) { - if (UPstream::parRun() && UPstream::nProcs(comm) > 1) + if (UPstream::is_parallel(comm)) { // My communication order const commsStruct& myComm = comms[UPstream::myProcNo(comm)]; @@ -420,7 +420,7 @@ void Foam::Pstream::listCombineScatter #ifndef Foam_Pstream_scatter_nobroadcast Pstream::broadcast(values, comm); #else - if (UPstream::parRun() && UPstream::nProcs(comm) > 1) + if (UPstream::is_parallel(comm)) { // My communication order const UPstream::commsStruct& myComm = comms[UPstream::myProcNo(comm)]; @@ -540,7 +540,7 @@ void Foam::Pstream::listCombineReduce const label comm ) { - if (UPstream::parRun() && UPstream::nProcs(comm) > 1) + if (UPstream::is_parallel(comm)) { const auto& comms = UPstream::whichCommunication(comm); @@ -562,7 +562,7 @@ void Foam::Pstream::mapCombineGather const label comm ) { - if (UPstream::parRun() && UPstream::nProcs(comm) > 1) + if (UPstream::is_parallel(comm)) { // My communication order const commsStruct& myComm = comms[UPstream::myProcNo(comm)]; @@ -645,7 +645,7 @@ void Foam::Pstream::mapCombineScatter #ifndef Foam_Pstream_scatter_nobroadcast Pstream::broadcast(values, comm); #else - if (UPstream::parRun() && UPstream::nProcs(comm) > 1) + if (UPstream::is_parallel(comm)) { // My communication order const UPstream::commsStruct& myComm = comms[UPstream::myProcNo(comm)]; @@ -746,7 +746,7 @@ void Foam::Pstream::mapCombineReduce const label comm ) { - if (UPstream::parRun() && UPstream::nProcs(comm) > 1) + if (UPstream::is_parallel(comm)) { const auto& comms = UPstream::whichCommunication(comm); diff --git a/src/OpenFOAM/db/IOstreams/Pstreams/PstreamExchange.C b/src/OpenFOAM/db/IOstreams/Pstreams/PstreamExchange.C index 6c590d76d8..ff6eeab4d4 100644 --- a/src/OpenFOAM/db/IOstreams/Pstreams/PstreamExchange.C +++ b/src/OpenFOAM/db/IOstreams/Pstreams/PstreamExchange.C @@ -469,6 +469,11 @@ void Foam::Pstream::exchange { static_assert(is_contiguous::value, "Contiguous data only!"); + if (!UPstream::is_rank(comm)) + { + return; // Process not in communicator + } + const label myProci = UPstream::myProcNo(comm); const label numProcs = UPstream::nProcs(comm); @@ -482,7 +487,7 @@ void Foam::Pstream::exchange recvBufs.resize_nocopy(numProcs); - if (UPstream::parRun() && numProcs > 1) + if (UPstream::is_parallel(comm)) { // Presize all receive buffers forAll(recvSizes, proci) @@ -567,7 +572,7 @@ void Foam::Pstream::exchange } } - // Do myself + // Do myself. Already checked if in communicator recvBufs[myProci] = sendBufs[myProci]; } @@ -586,7 +591,6 @@ void Foam::Pstream::exchange static_assert(is_contiguous::value, "Contiguous data only!"); const int myProci = UPstream::myProcNo(comm); - const label numProcs = UPstream::nProcs(comm); // Initial: clear out receive 'slots' // Preferrable to clear out the map entries instead of the map itself @@ -598,7 +602,7 @@ void Foam::Pstream::exchange iter.val().clear(); } - if (UPstream::parRun() && numProcs > 1) + if (UPstream::is_parallel(comm)) { // Presize all receive buffers forAllIters(recvSizes, iter) @@ -708,8 +712,8 @@ void Foam::Pstream::exchange } } - - // Do myself + // Do myself (if actually in the communicator) + if (UPstream::is_rank(comm)) { const auto iter = sendBufs.find(myProci); @@ -746,6 +750,12 @@ void Foam::Pstream::exchangeSizes const label comm ) { + if (!UPstream::is_rank(comm)) + { + recvSizes.clear(); + return; // Process not in communicator + } + const label myProci = UPstream::myProcNo(comm); const label numProcs = UPstream::nProcs(comm); @@ -817,6 +827,12 @@ void Foam::Pstream::exchangeSizes const label comm ) { + if (!UPstream::is_rank(comm)) + { + recvSizes.clear(); + return; // Process not in communicator + } + Pstream::exchangeSizes ( neighProcs, // send @@ -842,6 +858,11 @@ void Foam::Pstream::exchangeSizes Map