masterUncollatedFileOperationTemplates.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) 2017-2018 OpenFOAM Foundation
9 Copyright (C) 2020-2022 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
12 This file is part of OpenFOAM.
13
14 OpenFOAM is free software: you can redistribute it and/or modify it
15 under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
18
19 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26
27\*---------------------------------------------------------------------------*/
28
29#include "Pstream.H"
30#include "IFstream.H"
31
32// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
33
34template<class Type>
36(
37 const UList<Type>& allValues,
38 const int tag,
39 const label comm
40) const
41{
42 // TBD: more efficient scatter
44 if (Pstream::master(comm))
45 {
46 for (const int proci : Pstream::subProcs(comm))
47 {
48 UOPstream os(proci, pBufs);
49 os << allValues[proci];
50 }
51 }
52 pBufs.finishedScatters();
53
54 Type value;
55
56 if (Pstream::master(comm))
57 {
58 value = allValues[0];
59 }
60 else
61 {
62 UIPstream is(Pstream::masterNo(), pBufs);
63 is >> value;
64 }
65 return value;
66}
67
68
69template<class Type, class FileOp>
71(
72 const fileName& fName,
73 const FileOp& fop,
74 const int tag,
75 const label comm
76) const
77{
78 if (IFstream::debug)
79 {
80 Pout<< "masterUncollatedFileOperation::masterOp : Operation "
81 << typeid(FileOp).name()
82 << " on " << fName << endl;
83 }
84 if (Pstream::parRun())
85 {
86 List<fileName> filePaths(Pstream::nProcs(comm));
87 filePaths[Pstream::myProcNo(comm)] = fName;
88 Pstream::gatherList(filePaths, tag, comm);
89
90 List<Type> result(filePaths.size());
91 if (Pstream::master(comm))
92 {
93 result = fop(filePaths[0]);
94 for (label i = 1; i < filePaths.size(); i++)
95 {
96 if (filePaths[i] != filePaths[0])
97 {
98 result[i] = fop(filePaths[i]);
99 }
100 }
101 }
102
103 return scatterList(result, tag, comm);
104 }
105 else
106 {
107 return fop(fName);
108 }
109}
110
111
112template<class Type, class FileOp>
114(
115 const fileName& src,
116 const fileName& dest,
117 const FileOp& fop,
118 const int tag,
119 const label comm
120) const
121{
122 if (IFstream::debug)
123 {
124 Pout<< "masterUncollatedFileOperation : Operation on src:" << src
125 << " dest:" << dest << endl;
126 }
127 if (Pstream::parRun())
128 {
130 srcs[Pstream::myProcNo(comm)] = src;
131 Pstream::gatherList(srcs, tag, comm);
132
133 List<fileName> dests(srcs.size());
134 dests[Pstream::myProcNo(comm)] = dest;
135 Pstream::gatherList(dests, tag, comm);
136
137 List<Type> result(Pstream::nProcs(comm));
138 if (Pstream::master(comm))
139 {
140 result = fop(srcs[0], dests[0]);
141 for (label i = 1; i < srcs.size(); i++)
142 {
143 if (srcs[i] != srcs[0])
144 {
145 result[i] = fop(srcs[i], dests[i]);
146 }
147 }
148 }
149
150 return scatterList(result, tag, comm);
151 }
152 else
153 {
154 return fop(src, dest);
155 }
156}
157
158
159// ************************************************************************* //
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
Buffers for inter-processor communications streams (UOPstream, UIPstream).
label nProcs() const noexcept
Number of ranks associated with PstreamBuffers.
void finishedScatters(const bool wait=true)
Mark all sends to sub-procs as done.
UPstream::rangeType subProcs() const noexcept
Range of sub-processes indices associated with PstreamBuffers.
static void gatherList(const List< commsStruct > &comms, List< T > &values, const int tag, const label comm)
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
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
@ nonBlocking
"nonBlocking"
static constexpr int masterNo() noexcept
Process index of the master (always 0)
Definition: UPstream.H:451
static bool & parRun() noexcept
Test if this a parallel run.
Definition: UPstream.H:433
A class for handling file names.
Definition: fileName.H:76
Type masterOp(const fileName &fName, const FileOp &fop, const int tag, const label comm) const
Type scatterList(const UList< Type > &, const int, const label comm) const
int myProcNo() const noexcept
Return processor number.
splitCell * master() const
Definition: splitCell.H:113
OBJstream os(runTime.globalPath()/outputName)
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
prefixOSstream Pout
OSstream wrapped stdout (std::cout) with parallel prefix.