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-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 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
29 
30 template<class T>
32 (
33  const T& localValue,
34  const label comm
35 )
36 {
38  {
40  << "Cannot all-gather values for non-contiguous types" << endl
42  }
43 
44 
45  List<T> allValues;
46 
47  if (UPstream::is_parallel(comm))
48  {
49  allValues.resize(UPstream::nProcs(comm));
50  allValues[UPstream::myProcNo(comm)] = localValue;
51 
52  UPstream::mpiAllGather(allValues.data_bytes(), sizeof(T), comm);
53  }
54  else
55  {
56  // non-parallel: return own value
57  // TBD: only when UPstream::is_rank(comm) as well?
58  allValues.resize(1);
59  allValues[0] = localValue;
60  }
61 
62  return allValues;
63 }
64 
65 
66 template<class T>
68 (
69  const T& localValue,
70  const label comm
71 )
72 {
74  {
76  << "Cannot gather values for non-contiguous types" << endl
78  }
79 
80 
81  List<T> allValues;
82 
83  if (UPstream::is_parallel(comm))
84  {
85  if (UPstream::master(comm))
86  {
87  allValues.resize(UPstream::nProcs(comm));
88  }
89 
91  (
92  reinterpret_cast<const char*>(&localValue),
93  allValues.data_bytes(),
94  sizeof(T), // The send/recv size per rank
95  comm
96  );
97  }
98  else
99  {
100  // non-parallel: return own value
101  // TBD: only when UPstream::is_rank(comm) as well?
102  allValues.resize(1);
103  allValues[0] = localValue;
104  }
106  return allValues;
107 }
108 
109 
110 template<class T>
112 (
113  const UList<T>& allValues,
114  const label comm
115 )
116 {
118  {
120  << "Cannot scatter values for non-contiguous types" << endl
122  }
123 
124 
125  T localValue;
126 
127  if (UPstream::is_parallel(comm))
128  {
129  const label nproc = UPstream::nProcs(comm);
130 
131  if (UPstream::master(comm) && allValues.size() < nproc)
132  {
134  << "Attempting to send " << allValues.size()
135  << " values to " << nproc << " processors" << endl
137  }
138 
140  (
141  allValues.cdata_bytes(),
142  reinterpret_cast<char*>(&localValue),
143  sizeof(T), // The send/recv size per rank
144  comm
145  );
146  }
147  else
148  {
149  // non-parallel: return local value
150 
151  if (UPstream::is_rank(comm) && !allValues.empty())
152  {
153  localValue = allValues[0];
154  }
155  else
156  {
157  localValue = Zero;
158  }
159  }
160 
161  return localValue;
162 }
163 
164 
165 // ************************************************************************* //
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:116
void resize(const label len)
Adjust allocated size of list.
Definition: ListI.H:160
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:598
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:56
bool empty() const noexcept
True if List is empty (ie, size() is zero)
Definition: UList.H:666
static List< T > listGatherValues(const T &localValue, const label communicator=worldComm)
Gather individual values into list locations.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
static void mpiAllGather(char *allData, int count, const label communicator=worldComm)
Gather/scatter identically-sized char data.
static List< T > allGatherValues(const T &localValue, const label communicator=worldComm)
Allgather individual values into list locations.
static int myProcNo(const label communicator=worldComm)
Rank of this process in the communicator (starting from masterNo()). Can be negative if the process i...
Definition: UPstream.H:1074
char * data_bytes() noexcept
Return pointer to the underlying array serving as data storage,.
Definition: UListI.H:286
const char * cdata_bytes() const noexcept
Return pointer to the underlying array serving as data storage,.
Definition: UListI.H:279
static label nProcs(const label communicator=worldComm)
Number of ranks in parallel run (for given communicator). It is 1 for serial run. ...
Definition: UPstream.H:1065
static bool is_rank(const label communicator=worldComm)
True if process corresponds to any rank (master or sub-rank) in the given communicator.
Definition: UPstream.H:1091
static bool is_parallel(const label communicator=worldComm)
True if parallel algorithm or exchange is required.
Definition: UPstream.H:1111
static void mpiGather(const char *sendData, char *recvData, int count, const label communicator=worldComm)
Receive identically-sized char data from all ranks.
errorManip< error > abort(error &err)
Definition: errorManip.H:139
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:105
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
const volScalarField & T
static void mpiScatter(const char *sendData, char *recvData, int count, const label communicator=worldComm)
Send identically-sized char data to all ranks.
A template class to specify that a data type can be considered as being contiguous in memory...
Definition: contiguous.H:70
static bool master(const label communicator=worldComm)
True if process corresponds to the master rank in the communicator.
Definition: UPstream.H:1082
static T listScatterValues(const UList< T > &allValues, const label communicator=worldComm)
Scatter individual values from list locations.
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:127