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-2024 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, class FileOp>
36 (
37  const fileName& fName,
38  const FileOp& fop,
39  const int tag,
40  const label comm
41 ) const
42 {
43  if (IFstream::debug)
44  {
45  Pout<< "masterUncollatedFileOperation::masterOp : Operation "
46  << typeid(FileOp).name()
47  << " on " << fName << endl;
48  }
49 
51  {
52  const label myProci = UPstream::myProcNo(comm);
53  const label numProc = UPstream::nProcs(comm);
54 
55  List<fileName> filePaths(numProc);
56  filePaths[myProci] = fName;
57  Pstream::gatherList(filePaths, tag, comm);
58  // OR filePaths = Pstream::listGatherValues(fName, comm, tag)
59 
60  List<Type> result;
62  {
63  result.resize(numProc);
64  result = fop(filePaths[0]);
65 
66  for (label i = 1; i < numProc; ++i)
67  {
68  if (filePaths[i] != filePaths[0])
69  {
70  result[i] = fop(filePaths[i]);
71  }
72  }
73  }
74 
75  return Pstream::listScatterValues(result, comm, tag);
76  }
77 
78  return fop(fName);
79 }
80 
81 
82 template<class Type, class FileOp>
84 (
85  const fileName& src,
86  const fileName& dest,
87  const FileOp& fop,
88  const int tag,
89  const label comm
90 ) const
91 {
92  if (IFstream::debug)
93  {
94  Pout<< "masterUncollatedFileOperation : Operation on src:" << src
95  << " dest:" << dest << endl;
96  }
97 
98  if (UPstream::is_parallel(comm))
99  {
100  const label myProci = UPstream::myProcNo(comm);
101  const label numProc = UPstream::nProcs(comm);
102 
103  List<Pair<fileName>> filePaths(numProc);
104  filePaths[myProci].first() = src;
105  filePaths[myProci].second() = dest;
106  Pstream::gatherList(filePaths, tag, comm);
107  // OR
108  // Pair<fileName> tup(src, dest);
109  // filePaths = Pstream::listGatherValues(tup, comm, tag)
110 
111  List<Type> result;
112  if (UPstream::master(comm))
113  {
114  result.resize(numProc);
115  result = fop(filePaths[0].first(), filePaths[0].second());
116 
117  for (label i = 1; i < numProc; ++i)
118  {
119  // TBD: also check second() ?
120  if (filePaths[i].first() != filePaths[0].first())
121  {
122  result[i] =
123  fop(filePaths[i].first(), filePaths[i].second());
124  }
125  }
126  }
127 
128  return Pstream::listScatterValues(result, comm, tag);
129  }
130 
131  return fop(src, dest);
132 }
133 
134 
135 // ************************************************************************* //
static T listScatterValues(const UList< T > &allValues, const label comm=UPstream::worldComm, const int tag=UPstream::msgType())
Scatter individual values from list locations.
A class for handling file names.
Definition: fileName.H:72
void resize(const label len)
Adjust allocated size of list.
Definition: ListI.H:153
Type masterOp(const fileName &fName, const FileOp &fop, const int tag, const label comm) const
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
static void gatherList(const UList< commsStruct > &comms, UList< T > &values, const int tag, const label comm)
Gather data, but keep individual values separate. Uses the specified communication schedule...
static int myProcNo(const label communicator=worldComm)
Rank of this process in the communicator (starting from masterNo()). Can be negative if the process i...
Definition: UPstream.H:1086
static label nProcs(const label communicator=worldComm)
Number of ranks in parallel run (for given communicator). It is 1 for serial run. ...
Definition: UPstream.H:1077
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
static bool is_parallel(const label communicator=worldComm)
True if parallel algorithm or exchange is required.
Definition: UPstream.H:1123
int debug
Static debugging option.
label comm() const noexcept
Communicator to use.
static bool master(const label communicator=worldComm)
True if process corresponds to the master rank in the communicator.
Definition: UPstream.H:1094
prefixOSstream Pout
OSstream wrapped stdout (std::cout) with parallel prefix.