Block-Structured AMR Software Framework
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 
13 namespace 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 
75  NFilesIter(std::string fileName,
77  bool setBuf = false);
78 
79  ~NFilesIter();
80 
81  NFilesIter (NFilesIter const&) = delete;
82  NFilesIter (NFilesIter &&) = delete;
83  NFilesIter& operator= (NFilesIter const&) = delete;
85 
92  bool ReadyToWrite(bool appendFirst = false);
93 
94  bool ReadyToRead();
95 
97 
98  std::fstream &Stream() { return fileStream; }
99 
100 
104  std::streampos SeekPos();
105 
106  static int LengthOfSet(int nProcs, int nOutFiles) {
107  int anf(ActualNFiles(nOutFiles));
108  return ((nProcs + (anf - 1)) / anf);
109  }
110 
111  static int WhichSetPosition(int whichproc, int nprocs,
112  int noutfiles, bool groupsets)
113  {
114  int whichset;
115  if(groupsets) {
116  whichset = whichproc / noutfiles;
117  } else {
118  whichset = whichproc % LengthOfSet(nprocs, noutfiles);
119  }
120  return whichset;
121  }
122 
123 
130  static int ActualNFiles(int nOutFiles)
131  {
133  }
134 
135 
144  static bool CheckNFiles(int nProcs, int nOutFiles, bool groupSets);
145 
146  static int FileNumber(int nOutFiles, int whichProc, bool groupSets)
147  {
148  BL_ASSERT(whichProc >= 0 && whichProc < ParallelDescriptor::NProcs());
149 
150  int anf(ActualNFiles(nOutFiles));
151  if(groupSets) {
152  return(whichProc % anf);
153  } else {
154 
156  return(whichProc / LengthOfSet(nProcs, anf));
157  }
158  }
159 
160  [[nodiscard]] const std::string &FileName() const { return fullFileName; }
161  [[nodiscard]] int FileNumber() const { return fileNumber; }
162 
163  static std::string FileName(int nOutFiles,
164  const std::string &filePrefix,
165  int whichProc, bool groupSets)
166  {
167  BL_ASSERT(whichProc >= 0 && whichProc < ParallelDescriptor::NProcs());
168 
171  whichProc, groupSets), minDigits ) );
172  }
173 
174  static std::string FileName(int fileNumber,
175  const std::string &filePrefix)
176  {
178  }
179 
180 
184  [[nodiscard]] int CoordinatorProc() const { return coordinatorProc; }
185 
186 
192 
193 
198  [[nodiscard]] const Vector< Vector<int> > &FileNumbersWriteOrder() const { return fileNumbersWriteOrder; }
199 
200  void CleanUpMessages();
201 
202  static int GetMinDigits() { return minDigits; }
203 
204  static void SetMinDigits(int md) { minDigits = md; }
205 
206  private:
207 
208  int myProc = -1;
209  int nProcs = -1;
210  int nOutFiles = -1;
211  int nSets = -1;
212  bool groupSets = false;
213  int mySetPosition = -1;
214  int fileNumber = -1;
215  std::string filePrefix;
216  std::string fullFileName;
218  std::fstream fileStream;
219  bool finishedWriting = false;
220  bool isReading = false;
221  bool finishedReading = false;
224  int myReadIndex = -1;
227  int deciderTag = -1, coordinatorTag = -1, doneTag = -1, writeTag=-1;
228  int stWriteTag = -1;
229  int stReadTag = -1;
232  bool useSparseFPP = false;
235 
237 
240 
241  static const int indexUndefined = -1;
242 
243  static AMREX_EXPORT int minDigits;
244 };
245 
246 }
247 
248 #endif /* BL_NFILES_H */
#define BL_ASSERT(EX)
Definition: AMReX_BLassert.H:39
#define AMREX_EXPORT
Definition: AMReX_Extension.H:191
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:517
static AMREX_EXPORT int minDigits
for Concatenate
Definition: AMReX_NFiles.H:243
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:498
int myProc
Definition: AMReX_NFiles.H:208
static int ActualNFiles(int nOutFiles)
this returns the actual number of files used the range [1, nProcs] is enforced
Definition: AMReX_NFiles.H:130
std::string fullFileName
Definition: AMReX_NFiles.H:216
bool GetDynamic() const
Definition: AMReX_NFiles.H:54
int nOutFiles
Definition: AMReX_NFiles.H:210
static int currentDeciderIndex
Definition: AMReX_NFiles.H:236
static void SetMinDigits(int md)
Definition: AMReX_NFiles.H:204
bool GetSparseFPP() const
Definition: AMReX_NFiles.H:65
int coordinatorTag
Definition: AMReX_NFiles.H:227
Vector< int > setZeroProcs
Definition: AMReX_NFiles.H:231
NFilesIter & operator=(NFilesIter const &)=delete
static int GetMinDigits()
Definition: AMReX_NFiles.H:202
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:114
NFilesIter & operator++()
Definition: AMReX_NFiles.cpp:345
static std::string FileName(int nOutFiles, const std::string &filePrefix, int whichProc, bool groupSets)
Definition: AMReX_NFiles.H:163
std::fstream & Stream()
Definition: AMReX_NFiles.H:98
~NFilesIter()
Definition: AMReX_NFiles.cpp:197
NFilesIter(int noutfiles, std::string fileprefix, bool groupsets, bool setBuf)
the nfiles will be named "filePrefix" + "00000" through "filePrefix" + "noutfiles-1" the range [1,...
Definition: AMReX_NFiles.cpp:13
static std::string FileName(int fileNumber, const std::string &filePrefix)
Definition: AMReX_NFiles.H:174
Vector< int > readRanks
Definition: AMReX_NFiles.H:222
std::fstream fileStream
Definition: AMReX_NFiles.H:218
int deciderTag
Definition: AMReX_NFiles.H:227
int deciderProc
Definition: AMReX_NFiles.H:226
std::streampos SeekPos()
get the current Stream()'s seek position
Definition: AMReX_NFiles.cpp:493
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:204
NFilesIter(NFilesIter const &)=delete
VisMFBuffer::IO_Buffer io_buffer
Definition: AMReX_NFiles.H:217
std::string filePrefix
Definition: AMReX_NFiles.H:215
int nProcs
Definition: AMReX_NFiles.H:209
Vector< int > sparseWritingRanks
Definition: AMReX_NFiles.H:233
bool finishedWriting
Definition: AMReX_NFiles.H:219
bool finishedReading
Definition: AMReX_NFiles.H:221
int writeTag
Definition: AMReX_NFiles.H:227
int coordinatorProc
Definition: AMReX_NFiles.H:226
Vector< std::pair< int, int > > unreadMessages
these were ignored by the decider procs and need to be cleaned up
Definition: AMReX_NFiles.H:239
static int LengthOfSet(int nProcs, int nOutFiles)
Definition: AMReX_NFiles.H:106
int FileNumber() const
Definition: AMReX_NFiles.H:161
bool useSparseFPP
Definition: AMReX_NFiles.H:232
Vector< int > availableDeciders
Definition: AMReX_NFiles.H:230
NFilesIter(NFilesIter &&)=delete
const std::string & FileName() const
Definition: AMReX_NFiles.H:160
void CleanUpMessages()
Definition: AMReX_NFiles.cpp:552
int stReadTag
Definition: AMReX_NFiles.H:229
int fileNumber
Definition: AMReX_NFiles.H:214
bool useStaticSetSelection
Definition: AMReX_NFiles.H:225
static const int indexUndefined
Definition: AMReX_NFiles.H:241
int doneTag
Definition: AMReX_NFiles.H:227
int mySetPosition
Definition: AMReX_NFiles.H:213
int CoordinatorProc() const
this is the processor coordinating dynamic set selection
Definition: AMReX_NFiles.H:184
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:198
int mySparseFileNumber
Definition: AMReX_NFiles.H:234
int remainingWriters
Definition: AMReX_NFiles.H:226
int stWriteTag
Definition: AMReX_NFiles.H:228
bool groupSets
Definition: AMReX_NFiles.H:212
int myReadIndex
Definition: AMReX_NFiles.H:224
static int FileNumber(int nOutFiles, int whichProc, bool groupSets)
Definition: AMReX_NFiles.H:146
int nSets
Definition: AMReX_NFiles.H:211
bool ReadyToRead()
Definition: AMReX_NFiles.cpp:325
Vector< Vector< int > > fileNumbersWriteOrder
[filenumber][ranks in order they wrote to filenumber]
Definition: AMReX_NFiles.H:223
static int WhichSetPosition(int whichproc, int nprocs, int noutfiles, bool groupsets)
Definition: AMReX_NFiles.H:111
void SetDynamic(int deciderproc=-1)
call this to use dynamic set selection deciderproc defaults to nprocs - 1 if < 0
Definition: AMReX_NFiles.cpp:69
bool isReading
Definition: AMReX_NFiles.H:220
int NProcs() noexcept
return the number of MPI ranks local to the current Parallel Context
Definition: AMReX_ParallelDescriptor.H:243
@ min
Definition: AMReX_ParallelReduce.H:18
@ max
Definition: AMReX_ParallelReduce.H:17
Definition: AMReX_Amr.cpp:49
std::string Concatenate(const std::string &root, int num, int mindigits)
Returns rootNNNN where NNNN == num.
Definition: AMReX_String.cpp:34