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-2023 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 {
38  {
39  // Note: contains parallel guard internally
41  (
42  reinterpret_cast<char*>(&value),
43  sizeof(Type),
44  comm,
46  );
47  }
48  else if (UPstream::is_parallel(comm))
49  {
50  if (UPstream::master(comm))
51  {
53  os << value;
54  }
55  else // UPstream::is_subrank(comm)
56  {
57  IPBstream is(UPstream::masterNo(), comm);
58  is >> value;
59  }
60  }
61 }
62 
63 
64 template<class Type, class... Args>
65 void Foam::Pstream::broadcasts(const label comm, Type& arg1, Args&&... args)
66 {
67  if (UPstream::is_parallel(comm))
68  {
69  if (UPstream::master(comm))
70  {
71  OPBstream os(UPstream::masterNo(), comm);
72  Detail::outputLoop(os, arg1, std::forward<Args>(args)...);
73  }
74  else // UPstream::is_subrank(comm)
75  {
76  IPBstream is(UPstream::masterNo(), comm);
77  Detail::inputLoop(is, arg1, std::forward<Args>(args)...);
78  }
79  }
80 }
81 
82 
83 template<class ListType>
84 void Foam::Pstream::broadcastList(ListType& list, const label comm)
85 {
86  if (is_contiguous<typename ListType::value_type>::value)
87  {
88  // List data are contiguous
89  // 1. broadcast the size
90  // 2. resize for receiver list
91  // 3. broadcast contiguous contents
92 
93  if (UPstream::is_parallel(comm))
94  {
95  label len(list.size());
96 
98  (
99  reinterpret_cast<char*>(&len),
100  sizeof(label),
101  comm,
103  );
104 
105  if (UPstream::is_subrank(comm))
106  {
107  list.resize_nocopy(len);
108  }
109 
110  if (len)
111  {
113  (
114  list.data_bytes(),
115  list.size_bytes(),
116  comm,
118  );
119  }
120  }
121  }
122  else if (UPstream::is_parallel(comm))
123  {
124  // List data are non-contiguous - serialize/de-serialize
125 
126  if (UPstream::master(comm))
127  {
128  OPBstream os(UPstream::masterNo(), comm);
129  os << list;
130  }
131  else // UPstream::is_subrank(comm)
132  {
133  IPBstream is(UPstream::masterNo(), comm);
134  is >> list;
135  }
136  }
137 }
138 
139 
140 // ************************************************************************* //
static void broadcastList(ListType &list, const label comm=UPstream::worldComm)
Broadcast list content (contiguous or non-contiguous) to all communicator ranks. Does nothing in non-...
static void broadcast(Type &value, const label comm=UPstream::worldComm)
Broadcast content (contiguous or non-contiguous) to all communicator ranks. Does nothing in non-paral...
static constexpr int masterNo() noexcept
Relative rank for the master process - is always 0.
Definition: UPstream.H:1059
void inputLoop(IS &)
Termination for input looping (no-op)
Definition: IOstream.H:578
static bool is_parallel(const label communicator=worldComm)
True if parallel algorithm or exchange is required.
Definition: UPstream.H:1111
static bool is_subrank(const label communicator=worldComm)
True if process corresponds to a sub-rank in the given communicator.
Definition: UPstream.H:1099
OBJstream os(runTime.globalPath()/outputName)
static void broadcasts(const label comm, Type &arg1, Args &&... args)
Broadcast multiple items to all communicator ranks. Does nothing in non-parallel. ...
Input inter-processor communications stream using MPI broadcast.
Definition: IPstream.H:82
A template class to specify that a data type can be considered as being contiguous in memory...
Definition: contiguous.H:70
static bool broadcast(char *buf, const std::streamsize bufSize, const label communicator, const int rootProcNo=masterNo())
Broadcast buffer contents to all processes in given communicator. The sizes must match on all process...
static bool master(const label communicator=worldComm)
True if process corresponds to the master rank in the communicator.
Definition: UPstream.H:1082
Output inter-processor communications stream using MPI broadcast.
Definition: OPstream.H:82
Foam::argList args(argc, argv)
void outputLoop(OS &)
Termination for output looping (no-op)
Definition: IOstream.H:583