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 {
37  List<T> allValues;
38 
39  if (UPstream::is_parallel(comm))
40  {
41  allValues.resize(UPstream::nProcs(comm));
42  allValues[UPstream::myProcNo(comm)] = localValue;
43 
45  {
46  UPstream::mpiAllGather(allValues.data_bytes(), sizeof(T), comm);
47  }
48  else
49  {
51  << "Cannot all-gather values for non-contiguous types"
52  " - consider Pstream variant instead" << endl
54  }
55  }
56  else
57  {
58  // non-parallel: return own value
59  // TBD: only when UPstream::is_rank(comm) as well?
60  allValues.resize(1);
61  allValues[0] = localValue;
62  }
63 
64  return allValues;
65 }
66 
67 
68 template<class T>
70 (
71  const T& localValue,
72  const label comm
73 )
74 {
75  List<T> allValues;
76 
77  if (UPstream::is_parallel(comm))
78  {
79  if (UPstream::master(comm))
80  {
81  allValues.resize(UPstream::nProcs(comm));
82  }
83 
85  {
87  (
88  reinterpret_cast<const char*>(&localValue),
89  allValues.data_bytes(),
90  sizeof(T), // The send/recv size per rank
91  comm
92  );
93  }
94  else
95  {
97  << "Cannot gather values for non-contiguous types"
98  " - consider Pstream variant instead" << endl
100  }
101  }
102  else
103  {
104  // non-parallel: return own value
105  // TBD: only when UPstream::is_rank(comm) as well?
106  allValues.resize(1);
107  allValues[0] = localValue;
108  }
110  return allValues;
111 }
112 
113 
114 template<class T>
116 (
117  const UList<T>& allValues,
118  const label comm
119 )
120 {
121  T localValue{};
122 
123  if (UPstream::is_parallel(comm))
124  {
125  const label numProc = UPstream::nProcs(comm);
126 
127  if (UPstream::master(comm) && allValues.size() < numProc)
128  {
130  << "Attempting to send " << allValues.size()
131  << " values to " << numProc << " processors" << endl
133  }
134 
135  if (is_contiguous<T>::value)
136  {
138  (
139  allValues.cdata_bytes(),
140  reinterpret_cast<char*>(&localValue),
141  sizeof(T), // The send/recv size per rank
142  comm
143  );
144  }
145  else
146  {
148  << "Cannot scatter values for non-contiguous types"
149  " - consider Pstream variant instead" << endl
151  }
152  }
153  else
154  {
155  // non-parallel: return first value
156  // TBD: only when UPstream::is_rank(comm) as well?
157 
158  if (!allValues.empty())
159  {
160  return allValues[0];
161  }
162  }
163 
164  return localValue;
165 }
166 
167 
168 // ************************************************************************* //
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:153
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:608
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:675
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:1086
char * data_bytes() noexcept
Return pointer to the underlying array serving as data storage,.
Definition: UListI.H:279
const char * cdata_bytes() const noexcept
Return pointer to the underlying array serving as data storage,.
Definition: UListI.H:272
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:1077
static bool is_parallel(const label communicator=worldComm)
True if parallel algorithm or exchange is required.
Definition: UPstream.H:1123
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:1094
static T listScatterValues(const UList< T > &allValues, const label communicator=worldComm)
Scatter individual values from list locations.