openfoam/applications/test/parallel-nonBlocking/Test-parallel-nonBlocking.C
Mark Olesen 473e14418a ENH: more consistent use of broadcast, combineReduce etc.
- broadcast           : (replaces scatter)
  - combineReduce       == combineGather + broadcast
  - listCombineReduce   == listCombineGather + broadcast
  - mapCombineReduce    == mapCombineGather + broadcast
  - allGatherList       == gatherList + scatterList

  Before settling on a more consistent naming convention,
  some intermediate namings were used in OpenFOAM-v2206:

    - combineReduce       (2206: combineAllGather)
    - listCombineReduce   (2206: listCombineAllGather)
    - mapCombineReduce    (2206: mapCombineAllGather)
2022-11-08 16:48:08 +00:00

196 lines
5.1 KiB
C

/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2012-2017 OpenFOAM Foundation
Copyright (C) 2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
Application
Test-parallel-nonBlocking
Description
Test for various non-blocking parallel routines.
\*---------------------------------------------------------------------------*/
#include "List.H"
#include "mapDistribute.H"
#include "argList.H"
#include "Time.H"
#include "IPstream.H"
#include "OPstream.H"
#include "vector.H"
#include "IOstreams.H"
#include "Random.H"
#include "Tuple2.H"
#include "PstreamBuffers.H"
using namespace Foam;
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
int main(int argc, char *argv[])
{
#include "setRootCase.H"
#include "createTime.H"
// Test PstreamBuffers
// ~~~~~~~~~~~~~~~~~~~
if (false)
{
Perr<< "\nStarting transfers\n" << endl;
vector data
(
Pstream::myProcNo(),
Pstream::myProcNo(),
Pstream::myProcNo()
);
PstreamBuffers pBufs(Pstream::commsTypes::nonBlocking);
if (!Pstream::master())
{
Perr<< "sending to master" << endl;
UOPstream toMaster(Pstream::masterNo(), pBufs);
toMaster << data;
}
pBufs.finishedGathers();
// Consume
DynamicList<vector> allData;
if (Pstream::master())
{
// Collect my own data
allData.append(data);
for (const int proci : Pstream::subProcs())
{
Perr << "master receiving from " << proci << endl;
UIPstream fromProc(proci, pBufs);
allData.append(vector(fromProc));
}
}
// Send allData back
pBufs.clear();
if (Pstream::master())
{
for (const int proci : Pstream::subProcs())
{
Perr << "master sending to " << proci << endl;
UOPstream toProc(proci, pBufs);
toSlave << allData;
}
}
pBufs.finishedScatters();
// Consume
if (!Pstream::master())
{
Perr<< "receive from master" << endl;
UIPstream fromMaster(Pstream::masterNo(), pBufs);
fromMaster >> allData;
Perr<< allData << endl;
}
}
// Test non-blocking reductions
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
scalar data1 = 1.0;
label request1 = -1;
{
Foam::reduce(data1, sumOp<scalar>(), UPstream::msgType(), request1);
}
scalar data2 = 0.1;
label request2 = -1;
{
Foam::reduce(data2, sumOp<scalar>(), UPstream::msgType(), request2);
}
// Do a non-blocking send inbetween
{
PstreamBuffers pBufs(Pstream::commsTypes::nonBlocking);
for (const int proci : Pstream::allProcs())
{
UOPstream toProc(proci, pBufs);
toProc << Pstream::myProcNo();
}
// Start sending and receiving and block
pBufs.finishedSends();
// Consume
for (const int proci : Pstream::allProcs())
{
UIPstream fromProc(proci, pBufs);
label data;
fromProc >> data;
if (data != proci)
{
FatalErrorInFunction
<< "From processor " << proci << " received " << data
<< " but expected " << proci
<< exit(FatalError);
}
}
}
if (request1 != -1)
{
Pout<< "Waiting for non-blocking reduce with request " << request1
<< endl;
Pstream::waitRequest(request1);
}
Info<< "Reduced data1:" << data1 << endl;
if (request2 != -1)
{
Pout<< "Waiting for non-blocking reduce with request " << request1
<< endl;
Pstream::waitRequest(request2);
}
Info<< "Reduced data2:" << data2 << endl;
// Clear any outstanding requests
Pstream::resetRequests(0);
Info<< "End\n" << endl;
return 0;
}
// ************************************************************************* //