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-2024 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
45  );
46  }
47  else if (UPstream::is_parallel(comm))
48  {
49  if (UPstream::master(comm))
50  {
51  OPBstream os(comm);
52  os << value;
53  }
54  else // UPstream::is_subrank(comm)
55  {
56  IPBstream is(comm);
57  is >> value;
58  }
59  }
60 }
61 
62 
63 template<class Type, class... Args>
64 void Foam::Pstream::broadcasts(const label comm, Type& arg1, Args&&... args)
65 {
66  if (UPstream::is_parallel(comm))
67  {
68  if (UPstream::master(comm))
69  {
70  OPBstream os(comm);
71  Detail::outputLoop(os, arg1, std::forward<Args>(args)...);
72  }
73  else // UPstream::is_subrank(comm)
74  {
75  IPBstream is(comm);
76  Detail::inputLoop(is, arg1, std::forward<Args>(args)...);
77  }
78  }
79 }
80 
81 
82 template<class ListType>
83 void Foam::Pstream::broadcastList(ListType& list, const label comm)
84 {
85  if (is_contiguous<typename ListType::value_type>::value)
86  {
87  // List data are contiguous
88  // 1. broadcast the size
89  // 2. resize for receiver list
90  // 3. broadcast contiguous contents
91 
92  if (UPstream::is_parallel(comm))
93  {
94  label len(list.size());
95 
97  (
98  reinterpret_cast<char*>(&len),
99  sizeof(label),
100  comm
101  );
102 
103  if (UPstream::is_subrank(comm))
104  {
105  list.resize_nocopy(len);
106  }
107 
108  if (len)
109  {
111  (
112  list.data_bytes(),
113  list.size_bytes(),
114  comm
115  );
116  }
117  }
118  }
119  else if (UPstream::is_parallel(comm))
120  {
121  // List data are non-contiguous - serialize/de-serialize
122 
123  if (UPstream::master(comm))
124  {
125  OPBstream os(comm);
126  os << list;
127  }
128  else // UPstream::is_subrank(comm)
129  {
130  IPBstream is(comm);
131  is >> list;
132  }
133  }
134 }
135 
136 
137 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
138 
139 // Convenience wrappers - defined after all specialisations are known
140 
141 namespace Foam
142 {
143 
144 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
145 
146 //- Return a broadcasted value (uses a copy internally)
147 template<class Type>
148 Type returnBroadcast
149 (
150  const Type& value,
151  const label comm = UPstream::worldComm
152 )
153 {
154  Type work(value);
155  Pstream::broadcast(work, comm);
156  return work;
157 }
158 
159 
160 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
161 
162 } // End namespace Foam
163 
164 
165 // ************************************************************************* //
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 label worldComm
Communicator for all ranks. May differ from commGlobal() if local worlds are in use.
Definition: UPstream.H:421
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...
void inputLoop(IS &)
Termination for input looping (no-op)
Definition: IOstream.H:634
static bool is_parallel(const label communicator=worldComm)
True if parallel algorithm or exchange is required.
Definition: UPstream.H:1123
static bool is_subrank(const label communicator=worldComm)
True if process corresponds to a sub-rank in the given communicator.
Definition: UPstream.H:1111
OBJstream os(runTime.globalPath()/outputName)
Type returnBroadcast(const Type &value, const label comm=UPstream::worldComm)
Return a broadcasted value (uses a copy internally)
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:111
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:1094
Output inter-processor communications stream using MPI broadcast.
Definition: OPstream.H:130
Foam::argList args(argc, argv)
void outputLoop(OS &)
Termination for output looping (no-op)
Definition: IOstream.H:639
Namespace for OpenFOAM.