Loading...
Searching...
No Matches
UPstreamBroadcast.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-2025 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 "UPstream.H"
29#include "PstreamGlobals.H"
30#include "profilingPstream.H"
31
32// * * * * * * * * * * Protected Static Member Functions * * * * * * * * * * //
33
35(
36 void* buf, // Type checking done by caller
37 std::streamsize count,
38 const UPstream::dataTypes dataTypeId, // Proper type passed by caller
39 const int communicator, // Index into MPICommunicators_
40 const int root // The broadcast root (usually 0)
41)
42{
43 MPI_Datatype datatype = PstreamGlobals::getDataType(dataTypeId);
44
45 if (!count || !UPstream::is_parallel(communicator))
46 {
47 // Nothing to do - ignore
48 return true;
49 }
50
51 //Needed? PstreamGlobals::checkCommunicator(communicator, 0);
52
53 // Without MPI_Bcast_c()
54 if (FOAM_UNLIKELY(count > std::streamsize(INT_MAX)))
55 {
57 << "Broadcast size " << label(count)
58 << " exceeds INT_MAX bytes" << Foam::endl
60 return false;
61 }
62
63 const bool withTopo =
64 (
65 (root == 0) // Can only use node topology if root == master
68 );
69
70 if (FOAM_UNLIKELY(UPstream::debug))
71 {
72 Perr<< "[mpi_broadcast] :"
73 << " type:" << int(dataTypeId)
74 << " count:" << label(count)
75 << " comm:" << communicator
76 << " root:" << root
77 << " topo:" << withTopo << Foam::endl;
78 }
79
80 int returnCode = MPI_SUCCESS;
81
83
84 if (withTopo)
85 {
86 // Topological broadcast
87
88 for
89 (
90 const int subComm :
91 // std::initializer_list<int>
92 {
93 UPstream::commInterNode_, // Stage 1: between nodes
94 UPstream::commLocalNode_ // Stage 2: within a node
95 }
96 )
97 {
98 if (UPstream::is_parallel(subComm))
99 {
100 if (FOAM_UNLIKELY(UPstream::debug))
101 {
102 Perr<< "[mpi_broadcast] :"
103 << " type:" << int(dataTypeId)
104 << " count:" << label(count)
105 << " comm:" << subComm
106 << " substage" << Foam::endl;
107 }
108
109 returnCode = MPI_Bcast
110 (
111 buf,
112 count,
113 datatype,
114 0, // (root rank) == UPstream::masterNo()
116 );
117 }
118 }
119 }
120 else
121 {
122 // Regular broadcast
123 // PstreamDetail::broadcast(buf, count, datatype, communicator, root);
124
125 returnCode = MPI_Bcast
126 (
127 buf,
128 count,
129 datatype,
130 root, // The broadcast root (usually 0 == UPstream::masterNo())
132 );
133 }
134
136
137 return (returnCode == MPI_SUCCESS);
138}
139
140
141// ************************************************************************* //
Wrapper for internally indexed communicator label. Always invokes UPstream::allocateCommunicatorCompo...
Definition UPstream.H:2546
static bool mpi_broadcast(void *buf, std::streamsize count, const UPstream::dataTypes dataTypeId, const int communicator, const int root=0)
Broadcast buffer contents to all ranks (default: from rank=0). The sizes must match on all processes.
static bool usingTopoControl(UPstream::topoControls ctrl) noexcept
Test for selection of given topology-aware routine.
Definition UPstream.H:1014
static bool is_parallel(const label communicator=worldComm)
True if parallel algorithm or exchange is required.
Definition UPstream.H:1743
static bool usingNodeComms(const int communicator)
True if node topology-aware routines have been enabled, it is running in parallel,...
Definition UPstream.C:751
@ broadcast
broadcast [MPI]
Definition UPstream.H:189
static void addBroadcastTime()
Add time increment to broadcast time.
static void beginTiming()
Update timer prior to measurement.
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition error.H:600
MPI_Datatype getDataType(UPstream::dataTypes id)
Lookup of dataTypes enumeration as an MPI_Datatype.
DynamicList< MPI_Comm > MPICommunicators_
prefixOSstream Perr
OSstream wrapped stderr (std::cerr) with parallel prefix.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition Ostream.H:519
errorManip< error > abort(error &err)
Definition errorManip.H:139
error FatalError
Error stream (stdout output on all processes), with additional 'FOAM FATAL ERROR' header text and sta...
#define FOAM_UNLIKELY(cond)
Definition stdFoam.H:64