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 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
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 
34 template<class Type>
36 (
37  const UList<Type>& masterLst,
38  const int tag,
39  const label comm
40 ) const
41 {
42  // TBD: more efficient scatter
43  PstreamBuffers pBufs(UPstream::commsTypes::nonBlocking, tag, comm);
44  if (Pstream::master(comm))
45  {
46  for (const int proci : Pstream::subProcs(comm))
47  {
48  UOPstream os(proci, pBufs);
49  os << masterLst[proci];
50  }
51  }
52  pBufs.finishedSends();
53 
54  Type myResult;
55 
56  if (Pstream::master(comm))
57  {
58  myResult = masterLst[Pstream::myProcNo(comm)];
59  }
60  else
61  {
62  UIPstream is(Pstream::masterNo(), pBufs);
63  is >> myResult;
64  }
65  return myResult;
66 }
67 
68 
69 template<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 
112 template<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  {
129  List<fileName> srcs(Pstream::nProcs(comm));
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 // ************************************************************************* //
Foam::expressions::patchExpr::debug
int debug
Static debugging option.
Foam::UOPstream
Output inter-processor communications stream operating on external buffer.
Definition: UOPstream.H:57
Foam::fileName
A class for handling file names.
Definition: fileName.H:73
Foam::PstreamBuffers
Buffers for inter-processor communications streams (UOPstream, UIPstream).
Definition: PstreamBuffers.H:88
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::Pout
prefixOSstream Pout
OSstream wrapped stdout (std::cout) with parallel prefix.
IFstream.H
Foam::PstreamBuffers::finishedSends
void finishedSends(const bool block=true)
Mark all sends as having been done.
Definition: PstreamBuffers.C:73
os
OBJstream os(runTime.globalPath()/outputName)
Pstream.H
Foam::fileOperations::masterUncollatedFileOperation::masterOp
Type masterOp(const fileName &, const fileOp &fop, const int tag, const label comm) const
Definition: masterUncollatedFileOperationTemplates.C:71
Foam::fileOperations::masterUncollatedFileOperation::scatterList
Type scatterList(const UList< Type > &, const int, const label comm) const
Definition: masterUncollatedFileOperationTemplates.C:36
Foam::List< fileName >
Foam::UList< Type >
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
Foam::UIPstream
Input inter-processor communications stream operating on external buffer.
Definition: UIPstream.H:56