PstreamBroadcast.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2022 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "OPstream.H"
29 #include "IPstream.H"
30 #include "contiguous.H"
31 
32 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
33 
34 template<class Type>
35 void Foam::Pstream::broadcast(Type& value, const label comm)
36 {
37  if (UPstream::parRun() && UPstream::nProcs(comm) > 1)
38  {
40  {
41  // Note: contains parallel guard internally as well
43  (
44  reinterpret_cast<char*>(&value),
45  sizeof(Type),
46  comm,
48  );
49  }
50  else
51  {
52  if (UPstream::master(comm))
53  {
55  os << value;
56  }
57  else
58  {
59  IPBstream is(UPstream::masterNo(), comm);
60  is >> value;
61  }
62  }
63  }
64 }
65 
66 
67 template<class Type, class... Args>
68 void Foam::Pstream::broadcasts(const label comm, Type& arg1, Args&&... args)
69 {
70  if (UPstream::parRun() && UPstream::nProcs(comm) > 1)
71  {
72  if (UPstream::master(comm))
73  {
74  OPBstream os(UPstream::masterNo(), comm);
75  Detail::outputLoop(os, arg1, std::forward<Args>(args)...);
76  }
77  else
78  {
79  IPBstream is(UPstream::masterNo(), comm);
80  Detail::inputLoop(is, arg1, std::forward<Args>(args)...);
81  }
82  }
83 }
84 
85 
86 // ************************************************************************* //
static bool & parRun() noexcept
Test if this a parallel run.
Definition: UPstream.H:639
static void broadcast(Type &value, const label comm=UPstream::worldComm)
Broadcast content (contiguous or non-contiguous) to all processes in communicator.
static label nProcs(const label communicator=worldComm)
Number of ranks in parallel run (for given communicator) is 1 for serial run.
Definition: UPstream.H:656
static constexpr int masterNo() noexcept
Process index of the master (always 0)
Definition: UPstream.H:664
void inputLoop(IS &)
Termination for input looping (no-op)
Definition: IOstream.H:581
OBJstream os(runTime.globalPath()/outputName)
static void broadcasts(const label comm, Type &arg1, Args &&... args)
Broadcast multiple items to all processes in communicator.
Input inter-processor communications stream using MPI broadcast.
Definition: IPstream.H:89
A template class to specify that a data type can be considered as being contiguous in memory...
Definition: contiguous.H:71
static bool master(const label communicator=worldComm)
Am I the master rank.
Definition: UPstream.H:672
Output inter-processor communications stream using MPI broadcast.
Definition: OPstream.H:82
static bool broadcast(char *buf, const std::streamsize bufSize, const label communicator=worldComm, const int rootProcNo=masterNo())
Broadcast buffer contents to all processes in communicator. The sizes must match on all processes...
Foam::argList args(argc, argv)
void outputLoop(OS &)
Termination for output looping (no-op)
Definition: IOstream.H:586