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 | Copyright (C) 2017 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
36 {
37  // TBD: more efficient scatter
38  PstreamBuffers pBufs(UPstream::commsTypes::nonBlocking);
39  if (Pstream::master())
40  {
41  for (label proci = 1; proci < Pstream::nProcs(); proci++)
42  {
43  UOPstream os(proci, pBufs);
44  os << masterLst[proci];
45  }
46  }
47  pBufs.finishedSends();
48 
49  Type myResult;
50 
51  if (Pstream::master())
52  {
53  myResult = masterLst[Pstream::myProcNo()];
54  }
55  else
56  {
57  UIPstream is(Pstream::masterNo(), pBufs);
58  is >> myResult;
59  }
60  return myResult;
61 }
62 
63 
64 template<class Type, class fileOp>
66 (
67  const fileName& fName,
68  const fileOp& fop
69 ) const
70 {
71  if (IFstream::debug)
72  {
73  Pout<< "masterUncollatedFileOperation : Operation on " << fName << endl;
74  }
75  if (Pstream::parRun())
76  {
77  List<fileName> filePaths(Pstream::nProcs());
78  filePaths[Pstream::myProcNo()] = fName;
79  Pstream::gatherList(filePaths);
80 
81  List<Type> result(Pstream::nProcs());
82  if (Pstream::master())
83  {
84  result = fop(filePaths[0]);
85  for (label i = 1; i < filePaths.size(); i++)
86  {
87  if (filePaths[i] != filePaths[0])
88  {
89  result[i] = fop(filePaths[i]);
90  }
91  }
92  }
93 
94  return scatterList(result);
95  }
96  else
97  {
98  return fop(fName);
99  }
100 }
101 
102 
103 template<class Type, class fileOp>
105 (
106  const fileName& src,
107  const fileName& dest,
108  const fileOp& fop
109 ) const
110 {
111  if (IFstream::debug)
112  {
113  Pout<< "masterUncollatedFileOperation : Operation on src:" << src
114  << " dest:" << dest << endl;
115  }
116  if (Pstream::parRun())
117  {
118  List<fileName> srcs(Pstream::nProcs());
119  srcs[Pstream::myProcNo()] = src;
120  Pstream::gatherList(srcs);
121 
122  List<fileName> dests(Pstream::nProcs());
123  dests[Pstream::myProcNo()] = dest;
124  Pstream::gatherList(dests);
125 
126  List<Type> result(Pstream::nProcs());
127  if (Pstream::master())
128  {
129  result = fop(srcs[0], dests[0]);
130  for (label i = 1; i < srcs.size(); i++)
131  {
132  if (srcs[i] != srcs[0])
133  {
134  result[i] = fop(srcs[i], dests[i]);
135  }
136  }
137  }
138 
139  return scatterList(result);
140  }
141  else
142  {
143  return fop(src, dest);
144  }
145 }
146 
147 
148 // ************************************************************************* //
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:69
void finishedSends(const bool block=true)
Mark all sends as having been done. This will start receives.
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:163
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:253
Input inter-processor communications stream operating on external buffer.
Definition: UIPstream.H:53
Output inter-processor communications stream operating on external buffer.
Definition: UOPstream.H:54
Buffers for inter-processor communications streams (UOPstream, UIPstream).
prefixOSstream Pout(cout, "Pout")
Definition: IOstreams.H:53