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 -------------------------------------------------------------------------------
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 #include "Pstream.H"
29 #include "IFstream.H"
30 
31 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
32 
33 template<class Type>
35 (
36  const UList<Type>& masterLst,
37  const int tag,
38  const label comm
39 ) const
40 {
41  // TBD: more efficient scatter
42  PstreamBuffers pBufs(UPstream::commsTypes::nonBlocking, tag, comm);
43  if (Pstream::master(comm))
44  {
45  for (label proci = 1; proci < Pstream::nProcs(comm); proci++)
46  {
47  UOPstream os(proci, pBufs);
48  os << masterLst[proci];
49  }
50  }
51  pBufs.finishedSends();
52 
53  Type myResult;
54 
55  if (Pstream::master(comm))
56  {
57  myResult = masterLst[Pstream::myProcNo(comm)];
58  }
59  else
60  {
61  UIPstream is(Pstream::masterNo(), pBufs);
62  is >> myResult;
63  }
64  return myResult;
65 }
66 
67 
68 template<class Type, class fileOp>
70 (
71  const fileName& fName,
72  const fileOp& fop,
73  const int tag,
74  const label comm
75 ) const
76 {
77  if (IFstream::debug)
78  {
79  Pout<< "masterUncollatedFileOperation::masterOp : Operation "
80  << typeid(fileOp).name()
81  << " on " << fName << endl;
82  }
83  if (Pstream::parRun())
84  {
85  List<fileName> filePaths(Pstream::nProcs(comm));
86  filePaths[Pstream::myProcNo(comm)] = fName;
87  Pstream::gatherList(filePaths, tag, comm);
88 
89  List<Type> result(filePaths.size());
90  if (Pstream::master(comm))
91  {
92  result = fop(filePaths[0]);
93  for (label i = 1; i < filePaths.size(); i++)
94  {
95  if (filePaths[i] != filePaths[0])
96  {
97  result[i] = fop(filePaths[i]);
98  }
99  }
100  }
101 
102  return scatterList(result, tag, comm);
103  }
104  else
105  {
106  return fop(fName);
107  }
108 }
109 
110 
111 template<class Type, class fileOp>
113 (
114  const fileName& src,
115  const fileName& dest,
116  const fileOp& fop,
117  const int tag,
118  const label comm
119 ) const
120 {
121  if (IFstream::debug)
122  {
123  Pout<< "masterUncollatedFileOperation : Operation on src:" << src
124  << " dest:" << dest << endl;
125  }
126  if (Pstream::parRun())
127  {
128  List<fileName> srcs(Pstream::nProcs(comm));
129  srcs[Pstream::myProcNo(comm)] = src;
130  Pstream::gatherList(srcs, tag, comm);
131 
132  List<fileName> dests(srcs.size());
133  dests[Pstream::myProcNo(comm)] = dest;
134  Pstream::gatherList(dests, tag, comm);
135 
136  List<Type> result(Pstream::nProcs(comm));
137  if (Pstream::master(comm))
138  {
139  result = fop(srcs[0], dests[0]);
140  for (label i = 1; i < srcs.size(); i++)
141  {
142  if (srcs[i] != srcs[0])
143  {
144  result[i] = fop(srcs[i], dests[i]);
145  }
146  }
147  }
148 
149  return scatterList(result, tag, comm);
150  }
151  else
152  {
153  return fop(src, dest);
154  }
155 }
156 
157 
158 // ************************************************************************* //
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:69
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:337
Foam::Pout
prefixOSstream Pout
An Ostream wrapper for parallel output to std::cout.
Foam::label
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:62
Foam::name
word name(const complex &c)
Return string representation of complex.
Definition: complex.C:76
IFstream.H
Foam::PstreamBuffers::finishedSends
void finishedSends(const bool block=true)
Mark all sends as having been done. This will start receives.
Definition: PstreamBuffers.C:80
Pstream.H
Foam::fileOperations::masterUncollatedFileOperation::masterOp
Type masterOp(const fileName &, const fileOp &fop, const int tag, const label comm) const
Definition: masterUncollatedFileOperationTemplates.C:70
Foam::fileOperations::masterUncollatedFileOperation::scatterList
Type scatterList(const UList< Type > &, const int, const label comm) const
Definition: masterUncollatedFileOperationTemplates.C:35
Foam::List< fileName >
Foam::UList< Type >
Foam::UIPstream
Input inter-processor communications stream operating on external buffer.
Definition: UIPstream.H:56