UPstreamTemplates.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) 2021 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// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
29
30template<class T>
32(
33 const T& localValue,
34 const label comm
35)
36{
38 {
40 << "Cannot gather values for non-contiguous types" << endl
42 }
43
44
45 List<T> allValues;
46
47 const label nproc = (UPstream::parRun() ? UPstream::nProcs(comm) : 1);
48
49 if (nproc > 1)
50 {
51 if (UPstream::master(comm))
52 {
53 allValues.resize(nproc);
54 }
55
57 (
58 reinterpret_cast<const char*>(&localValue),
59 sizeof(T),
60 allValues.data_bytes(),
61 sizeof(T),
62 comm
63 );
64 }
65 else
66 {
67 // non-parallel: return own value
68 allValues.resize(1);
69 allValues[0] = localValue;
70 }
71
72 return allValues;
73}
74
75
76template<class T>
78(
79 const UList<T>& allValues,
80 const label comm
81)
82{
84 {
86 << "Cannot scatter values for non-contiguous types" << endl
88 }
89
90
91 const label nproc = (UPstream::parRun() ? UPstream::nProcs(comm) : 1);
92
93 T localValue;
94
95 if (nproc > 1)
96 {
97 if (UPstream::master(comm) && allValues.size() < nproc)
98 {
100 << "Attempting to send " << allValues.size()
101 << " values to " << nproc << " processors" << endl
103 }
104
106 (
107 allValues.cdata_bytes(),
108 sizeof(T),
109 reinterpret_cast<char*>(&localValue),
110 sizeof(T),
111 comm
112 );
113 }
114 else
115 {
116 // non-parallel: return local value
117
118 if (allValues.empty()) // Extra safety
119 {
120 localValue = Zero;
121 }
122 else
123 {
124 localValue = allValues[0];
125 }
126 }
127
128 return localValue;
129}
130
131
132// ************************************************************************* //
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: List.H:77
void resize(const label len)
Adjust allocated size of list.
Definition: ListI.H:139
label nProcs() const noexcept
Number of ranks associated with PstreamBuffers.
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: UList.H:94
char * data_bytes() noexcept
Return pointer to the underlying array serving as data storage,.
Definition: UListI.H:251
bool empty() const noexcept
True if the UList is empty (ie, size() is zero)
Definition: UListI.H:427
const char * cdata_bytes() const noexcept
Return pointer to the underlying array serving as data storage,.
Definition: UListI.H:244
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
static void mpiGather(const char *sendData, int sendCount, char *recvData, int recvCount, const label communicator=worldComm)
Receive identically-sized char data from all ranks.
static List< T > listGatherValues(const T &localValue, const label communicator=worldComm)
Gather individual values into list locations.
static void mpiScatter(const char *sendData, int sendCount, char *recvData, int recvCount, const label communicator=worldComm)
Send identically-sized char data to all ranks.
static T listScatterValues(const UList< T > &allValues, const label communicator=worldComm)
Scatter individual values from list locations.
static bool & parRun() noexcept
Test if this a parallel run.
Definition: UPstream.H:433
splitCell * master() const
Definition: splitCell.H:113
const volScalarField & T
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
errorManip< error > abort(error &err)
Definition: errorManip.H:144
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
error FatalError
A template class to specify that a data type can be considered as being contiguous in memory.
Definition: contiguous.H:78