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-------------------------------------------------------------------------------
10License
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
34template<class Type>
35void 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
67template<class Type, class... Args>
68void 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 {
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// ************************************************************************* //
label nProcs() const noexcept
Number of ranks associated with PstreamBuffers.
static void broadcast(Type &value, const label comm=UPstream::worldComm)
static void broadcasts(const label comm, Type &arg1, Args &&... args)
Broadcast multiple items to all processes in communicator.
static constexpr int masterNo() noexcept
Process index of the master (always 0)
Definition: UPstream.H:451
static bool broadcast(char *buf, const std::streamsize bufSize, const label communicator=worldComm, const int rootProcNo=masterNo())
static bool & parRun() noexcept
Test if this a parallel run.
Definition: UPstream.H:433
splitCell * master() const
Definition: splitCell.H:113
OBJstream os(runTime.globalPath()/outputName)
void inputLoop(IS &)
Termination for input looping (no-op)
Definition: IOstream.H:477
void outputLoop(OS &)
Termination for output looping (no-op)
Definition: IOstream.H:480
Foam::argList args(argc, argv)
A template class to specify that a data type can be considered as being contiguous in memory.
Definition: contiguous.H:78