Loading...
Searching...
No Matches
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-------------------------------------------------------------------------------
11License
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\*---------------------------------------------------------------------------*/
29#include "Pstream.H"
30#include "IFstream.H"
31
32// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
33
34template<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
82template<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// ************************************************************************* //
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition List.H:72
void resize(const label len)
Adjust allocated size of list.
Definition ListI.H:153
static T listScatterValues(const UList< T > &allValues, const int communicator=UPstream::worldComm, const int tag=UPstream::msgType())
Scatter individual values from list locations.
T & first()
Access first element of the list, position [0].
Definition UList.H:957
static int myProcNo(const label communicator=worldComm)
Rank of this process in the communicator (starting from masterNo()). Negative if the process is not a...
Definition UPstream.H:1706
static bool master(const label communicator=worldComm)
True if process corresponds to the master rank in the communicator.
Definition UPstream.H:1714
static bool is_parallel(const label communicator=worldComm)
True if parallel algorithm or exchange is required.
Definition UPstream.H:1743
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:1697
@ gatherList
gatherList [manual algorithm]
Definition UPstream.H:194
A class for handling file names.
Definition fileName.H:75
label comm() const noexcept
Communicator to use.
Type masterOp(const fileName &fName, const FileOp &fop, const int tag, const label comm) const
auto & name
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition Ostream.H:519
prefixOSstream Pout
OSstream wrapped stdout (std::cout) with parallel prefix.