Block-Structured AMR Software Framework
Loading...
Searching...
No Matches
AMReX_NFiles.H
Go to the documentation of this file.
1
2#ifndef BL_NFILES_H
3#define BL_NFILES_H
4#include <AMReX_Config.H>
5
7#include <AMReX_Utility.H>
8#include <AMReX_VisMFBuffer.H>
9
10#include <fstream>
11#include <string>
12
13namespace amrex {
14
15
27{
28 public:
29
30
43 NFilesIter(int noutfiles, std::string fileprefix,
44 bool groupsets, bool setBuf);
45
46
53 void SetDynamic(int deciderproc = -1);
54 [[nodiscard]] bool GetDynamic() const { return ( ! useStaticSetSelection); }
55
56
64 void SetSparseFPP(const Vector<int> &ranksToWrite);
65 [[nodiscard]] bool GetSparseFPP() const { return useSparseFPP; }
66
67
81 NFilesIter(std::string fileName,
82 Vector<int> readRanks,
83 bool setBuf = false,
84 int mpi_tag = -1);
85
87
88 NFilesIter (NFilesIter const&) = delete;
89 NFilesIter (NFilesIter &&) = delete;
90 NFilesIter& operator= (NFilesIter const&) = delete;
92
99 bool ReadyToWrite(bool appendFirst = false);
100
101 bool ReadyToRead();
102
104
105 std::fstream &Stream() { return fileStream; }
106
107
111 std::streampos SeekPos();
112
113 static int LengthOfSet(int nProcs, int nOutFiles) {
114 int anf(ActualNFiles(nOutFiles));
115 return ((nProcs + (anf - 1)) / anf);
116 }
117
118 static int WhichSetPosition(int whichproc, int nprocs,
119 int noutfiles, bool groupsets)
120 {
121 int whichset;
122 if(groupsets) {
123 whichset = whichproc / noutfiles;
124 } else {
125 whichset = whichproc % LengthOfSet(nprocs, noutfiles);
126 }
127 return whichset;
128 }
129
130
137 static int ActualNFiles(int nOutFiles)
138 {
139 return( std::max(1, std::min(ParallelDescriptor::NProcs(), nOutFiles)) );
140 }
141
142
151 static bool CheckNFiles(int nProcs, int nOutFiles, bool groupSets);
152
153 static int FileNumber(int nOutFiles, int whichProc, bool groupSets)
154 {
155 BL_ASSERT(whichProc >= 0 && whichProc < ParallelDescriptor::NProcs());
156
157 int anf(ActualNFiles(nOutFiles));
158 if(groupSets) {
159 return(whichProc % anf);
160 } else {
161
162 int nProcs(ParallelDescriptor::NProcs());
163 return(whichProc / LengthOfSet(nProcs, anf));
164 }
165 }
166
167 [[nodiscard]] const std::string &FileName() const { return fullFileName; }
168 [[nodiscard]] int FileNumber() const { return fileNumber; }
169
170 static std::string FileName(int nOutFiles,
171 const std::string &filePrefix,
172 int whichProc, bool groupSets)
173 {
174 BL_ASSERT(whichProc >= 0 && whichProc < ParallelDescriptor::NProcs());
175
176 return ( amrex::Concatenate(filePrefix,
177 FileNumber(ActualNFiles(nOutFiles),
178 whichProc, groupSets), minDigits ) );
179 }
180
181 static std::string FileName(int fileNumber,
182 const std::string &filePrefix)
183 {
184 return ( amrex::Concatenate(filePrefix, fileNumber, minDigits ) );
185 }
186
187
191 [[nodiscard]] int CoordinatorProc() const { return coordinatorProc; }
192
193
199
200
205 [[nodiscard]] const Vector< Vector<int> > &FileNumbersWriteOrder() const { return fileNumbersWriteOrder; }
206
207 void CleanUpMessages();
208
209 static int GetMinDigits() { return minDigits; }
210
211 static void SetMinDigits(int md) { minDigits = md; }
212
213 private:
214
215 int myProc = -1;
216 int nProcs = -1;
217 int nOutFiles = -1;
218 int nSets = -1;
219 bool groupSets = false;
220 int mySetPosition = -1;
221 int fileNumber = -1;
222 std::string filePrefix;
223 std::string fullFileName;
224 VisMFBuffer::IO_Buffer io_buffer;
225 std::fstream fileStream;
226 bool finishedWriting = false;
227 bool isReading = false;
228 bool finishedReading = false;
229 Vector<int> readRanks;
230 Vector< Vector<int> > fileNumbersWriteOrder;
231 int myReadIndex = -1;
232 bool useStaticSetSelection = true;
233 int remainingWriters = -1, deciderProc=-1, coordinatorProc = -1;
234 int deciderTag = -1, coordinatorTag = -1, doneTag = -1, writeTag=-1;
235 int stWriteTag = -1;
236 int stReadTag = -1;
237 Vector<int> availableDeciders;
238 Vector<int> setZeroProcs;
239 bool useSparseFPP = false;
240 Vector<int> sparseWritingRanks;
241 int mySparseFileNumber = -1;
242
243 static int currentDeciderIndex;
244
246 Vector<std::pair<int, int> > unreadMessages;
247
248 static const int indexUndefined = -1;
249
250 static AMREX_EXPORT int minDigits;
251};
252
253}
254
255#endif /* BL_NFILES_H */
#define BL_ASSERT(EX)
Definition AMReX_BLassert.H:39
#define AMREX_EXPORT
Definition AMReX_Extension.H:223
This class encapsulates writing to nfiles.
Definition AMReX_NFiles.H:27
Vector< int > FileNumbersWritten()
these are the file numbers to which each rank wrote [rank] a rank only writes to one file
Definition AMReX_NFiles.cpp:528
static bool CheckNFiles(int nProcs, int nOutFiles, bool groupSets)
this checks if nOutFiles equals the calculated number of files returns false if they do not match
Definition AMReX_NFiles.cpp:509
static int ActualNFiles(int nOutFiles)
this returns the actual number of files used the range [1, nProcs] is enforced
Definition AMReX_NFiles.H:137
bool GetDynamic() const
Definition AMReX_NFiles.H:54
static void SetMinDigits(int md)
Definition AMReX_NFiles.H:211
bool GetSparseFPP() const
Definition AMReX_NFiles.H:65
static int GetMinDigits()
Definition AMReX_NFiles.H:209
std::fstream & Stream()
Definition AMReX_NFiles.H:105
void SetSparseFPP(const Vector< int > &ranksToWrite)
call this to use a file per process for a sparse set of writers. ranksToWrite.size() will set noutfil...
Definition AMReX_NFiles.cpp:115
NFilesIter & operator++()
Definition AMReX_NFiles.cpp:356
static std::string FileName(int nOutFiles, const std::string &filePrefix, int whichProc, bool groupSets)
Definition AMReX_NFiles.H:170
~NFilesIter()
Definition AMReX_NFiles.cpp:204
static std::string FileName(int fileNumber, const std::string &filePrefix)
Definition AMReX_NFiles.H:181
std::streampos SeekPos()
get the current Stream()'s seek position
Definition AMReX_NFiles.cpp:504
bool ReadyToWrite(bool appendFirst=false)
if appendFirst is true, the first set for this iterator will open the files in append mode
Definition AMReX_NFiles.cpp:211
NFilesIter(NFilesIter const &)=delete
NFilesIter & operator=(NFilesIter const &)=delete
static int LengthOfSet(int nProcs, int nOutFiles)
Definition AMReX_NFiles.H:113
const Vector< Vector< int > > & FileNumbersWriteOrder() const
these are the order of ranks which wrote to each file [filenumber][ranks in order they wrote to filen...
Definition AMReX_NFiles.H:205
int FileNumber() const
Definition AMReX_NFiles.H:168
NFilesIter(NFilesIter &&)=delete
void CleanUpMessages()
Definition AMReX_NFiles.cpp:563
const std::string & FileName() const
Definition AMReX_NFiles.H:167
int CoordinatorProc() const
this is the processor coordinating dynamic set selection
Definition AMReX_NFiles.H:191
static int FileNumber(int nOutFiles, int whichProc, bool groupSets)
Definition AMReX_NFiles.H:153
bool ReadyToRead()
Definition AMReX_NFiles.cpp:336
static int WhichSetPosition(int whichproc, int nprocs, int noutfiles, bool groupsets)
Definition AMReX_NFiles.H:118
void SetDynamic(int deciderproc=-1)
call this to use dynamic set selection deciderproc defaults to nprocs - 1 if < 0
Definition AMReX_NFiles.cpp:70
This class is a thin wrapper around std::vector. Unlike vector, Vector::operator[] provides bound che...
Definition AMReX_Vector.H:29
int NProcs() noexcept
Definition AMReX_ParallelDescriptor.H:255
Definition AMReX_Amr.cpp:50
std::string Concatenate(const std::string &root, int num, int mindigits)
Returns rootNNNN where NNNN == num.
Definition AMReX_String.cpp:35