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 -------------------------------------------------------------------------------
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 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 
56  UPstream::mpiGather
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 
76 template<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 
105  UPstream::mpiScatter
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 // ************************************************************************* //
Foam::UList::cdata_bytes
const char * cdata_bytes() const noexcept
Return pointer to the underlying array serving as data storage,.
Definition: UListI.H:244
Foam::List::resize
void resize(const label len)
Adjust allocated size of list.
Definition: ListI.H:139
Foam::Zero
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::UPstream::listScatterValues
static T listScatterValues(const UList< T > &allValues, const label communicator=worldComm)
Individual values into list locations.
Definition: UPstreamTemplates.C:78
Foam::UList::empty
bool empty() const noexcept
True if the UList is empty (ie, size() is zero)
Definition: UListI.H:427
Foam::FatalError
error FatalError
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:144
T
const volScalarField & T
Definition: createFieldRefs.H:2
Foam::UPstream::listGatherValues
static List< T > listGatherValues(const T &localValue, const label communicator=worldComm)
Individual values into list locations.
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:63
Foam::UList
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:103
Foam::UList::size
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
Foam::is_contiguous
A template class to specify that a data type can be considered as being contiguous in memory.
Definition: contiguous.H:75