masterUncollatedFileOperationTemplates.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2017-2018 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "Pstream.H"
27 #include "IFstream.H"
28 
29 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
30 
31 template<class Type>
33 (
34  const UList<Type>& masterLst,
35  const int tag,
36  const label comm
37 ) const
38 {
39  // TBD: more efficient scatter
40  PstreamBuffers pBufs(UPstream::commsTypes::nonBlocking, tag, comm);
41  if (Pstream::master(comm))
42  {
43  for (label proci = 1; proci < Pstream::nProcs(comm); proci++)
44  {
45  UOPstream os(proci, pBufs);
46  os << masterLst[proci];
47  }
48  }
49  pBufs.finishedSends();
50 
51  Type myResult;
52 
53  if (Pstream::master(comm))
54  {
55  myResult = masterLst[Pstream::myProcNo(comm)];
56  }
57  else
58  {
59  UIPstream is(Pstream::masterNo(), pBufs);
60  is >> myResult;
61  }
62  return myResult;
63 }
64 
65 
66 template<class Type, class fileOp>
68 (
69  const fileName& fName,
70  const fileOp& fop,
71  const int tag,
72  const label comm
73 ) const
74 {
75  if (IFstream::debug)
76  {
77  Pout<< "masterUncollatedFileOperation::masterOp : Operation "
78  << typeid(fileOp).name()
79  << " on " << fName << endl;
80  }
81  if (Pstream::parRun())
82  {
83  List<fileName> filePaths(Pstream::nProcs(comm));
84  filePaths[Pstream::myProcNo(comm)] = fName;
85  Pstream::gatherList(filePaths, tag, comm);
86 
87  List<Type> result(filePaths.size());
88  if (Pstream::master(comm))
89  {
90  result = fop(filePaths[0]);
91  for (label i = 1; i < filePaths.size(); i++)
92  {
93  if (filePaths[i] != filePaths[0])
94  {
95  result[i] = fop(filePaths[i]);
96  }
97  }
98  }
99 
100  return scatterList(result, tag, comm);
101  }
102  else
103  {
104  return fop(fName);
105  }
106 }
107 
108 
109 template<class Type, class fileOp>
111 (
112  const fileName& src,
113  const fileName& dest,
114  const fileOp& fop,
115  const int tag,
116  const label comm
117 ) const
118 {
119  if (IFstream::debug)
120  {
121  Pout<< "masterUncollatedFileOperation : Operation on src:" << src
122  << " dest:" << dest << endl;
123  }
124  if (Pstream::parRun())
125  {
126  List<fileName> srcs(Pstream::nProcs(comm));
127  srcs[Pstream::myProcNo(comm)] = src;
128  Pstream::gatherList(srcs, tag, comm);
129 
130  List<fileName> dests(srcs.size());
131  dests[Pstream::myProcNo(comm)] = dest;
132  Pstream::gatherList(dests, tag, comm);
133 
134  List<Type> result(Pstream::nProcs(comm));
135  if (Pstream::master(comm))
136  {
137  result = fop(srcs[0], dests[0]);
138  for (label i = 1; i < srcs.size(); i++)
139  {
140  if (srcs[i] != srcs[0])
141  {
142  result[i] = fop(srcs[i], dests[i]);
143  }
144  }
145  }
146 
147  return scatterList(result, tag, comm);
148  }
149  else
150  {
151  return fop(src, dest);
152  }
153 }
154 
155 
156 // ************************************************************************* //
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
A class for handling file names.
Definition: fileName.H:79
void finishedSends(const bool block=true)
Mark all sends as having been done. This will start receives.
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:164
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Input inter-processor communications stream operating on external buffer.
Definition: UIPstream.H:53
Type scatterList(const UList< Type > &, const int, const label comm) const
Type masterOp(const fileName &, const fileOp &fop, const int tag, const label comm) const
Output inter-processor communications stream operating on external buffer.
Definition: UOPstream.H:54
Buffers for inter-processor communications streams (UOPstream, UIPstream).
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
prefixOSstream Pout(cout, "Pout")
Definition: IOstreams.H:53