Documentation 6b54b78464
Loading...
Searching...
No Matches
CFileWriter.hpp
Go to the documentation of this file.
1/*!
2 * \file CFileWriter.hpp
3 * \brief Headers fo the file writer class.
4 * \author T. Albring
5 * \version 8.5.0 "Harrier"
6 *
7 * SU2 Project Website: https://su2code.github.io
8 *
9 * The SU2 Project is maintained by the SU2 Foundation
10 * (http://su2foundation.org)
11 *
12 * Copyright 2012-2026, SU2 Contributors (cf. AUTHORS.md)
13 *
14 * SU2 is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or (at your option) any later version.
18 *
19 * SU2 is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with SU2. If not, see <http://www.gnu.org/licenses/>.
26 */
27
28#pragma once
29#include "../../../../Common/include/parallelization/mpi_structure.hpp"
30#include "../../../../Common/include/option_structure.hpp"
31#include <sys/stat.h>
32#include <vector>
33#include <string>
34#include <cstring>
35#include <fstream>
36#include <utility>
37
38#include "../../output/filewriter/CParallelDataSorter.hpp"
39
40using namespace std;
41
43protected:
44
45 /*!
46 * \brief The MPI rank
47 */
48 int rank;
49
50 /*!
51 * \brief The MPI size, aka the number of processors.
52 */
53 int size;
54
55 /*!
56 * \brief The file extension to be attached to the filename.
57 */
58 std::string fileExt;
59
60 su2double startTime, /*!< \brief Start time for time measurement */
61 stopTime , /*!< \brief Stop time for time measurement */
62 usedTime , /*!< \brief Used time for time measurement */
63 bandwidth, /*!< \brief Used bandwidth */
64 fileSize; /*!< \brief Size of the last written file */
65
66 /*!
67 * \brief Determine the file size
68 * \param[in] filename - Name of the file.
69 * \return
70 */
71 inline unsigned long DetermineFilesize(std::string filename){
72 struct stat stat_buf;
73 int rc = stat(filename.c_str(), &stat_buf);
74 return rc == 0 ? stat_buf.st_size : -1;
75 }
76
77 /*!
78 * \brief The parallel data sorter
79 */
81
82#ifdef HAVE_MPI
83 /*!
84 * \brief The displacement that every process has in the current file view
85 */
86 MPI_Offset disp;
87
88 /*!
89 * \brief The file handle for writing
90 */
91 MPI_File fhw;
92#else
93
94 /*!
95 * \brief The file handle for writing
96 */
97 FILE* fhw;
98#endif
99
100public:
101 /*!
102 * \brief Construct a file writer using field names, the data sorter and the file extension.
103 * \param[in] valDataSorter - The parallel sorted data to write
104 * \param[in] valFileExt - The file extension.
105 */
106 CFileWriter(CParallelDataSorter* valDataSorter, string valFileExt);
107
108 /*!
109 * \brief Construct a file writer using field names, file extension.
110 * \param[in] valFileExt - The file extension to be attached to the filename
111 */
112 CFileWriter(string valFileExt);
113
114 /*!
115 * \brief Destructor
116 */
117 virtual ~CFileWriter();
118
119 /*!
120 * \brief Write sorted data to file
121 * \param[in] val_filename - The name of the file
122 */
123 virtual void WriteData(string val_filename){}
124
125 /*!
126 * \brief Get the bandwith used for the last writing
127 */
129
130 /*!
131 * \brief Get the filesize of the last written file.
132 */
134
135 /*!
136 * \brief Get the used time of the last file writing.
137 * \return The time used to write to file.
138 */
140
141protected:
142
143 /*!
144 * \brief Collectively write a binary data array distributed over all processors to file using MPI I/O.
145 * \param[in] data - Pointer to the data to write.
146 * \param sizeInBytes - The size of the data in bytes on this processor.
147 * \param totalSizeInBytes - The total size of the array accumulated over all processors.
148 * \param offset - The offset in bytes of the chunk of data the current processor owns within the global array.
149 * \return Boolean indicating whether the writing was successful.
150 */
151 bool WriteMPIBinaryDataAll(const void *data, unsigned long sizeInBytes, unsigned long totalSizeInBytes, unsigned long offset);
152
153 /*!
154 * \brief Write a binary data array to a currently opened file using MPI I/O. Note: routine must be called collectively,
155 * although only one processor writes its data.
156 * \param[in] data - Pointer to the beginning of the data. Can be NULL for all processors that do not write.
157 * \param[in] sizeInBytes - The size of the data in bytes.
158 * \param[in] processor - Rank of the processor that should write its data.
159 * \return Boolean indicating whether the writing was successful.
160 */
161 bool WriteMPIBinaryData(const void *data, unsigned long sizeInBytes, unsigned short processor);
162
163 /*!
164 * \brief Write a string to a currently opened file using MPI I/O. Note: routine must be called collectively,
165 * although only one processor writes the string.
166 * \param[in] str - The string to write to file.
167 * \param[in] processor - Rank of the processor that should the string.
168 * \return
169 */
170 bool WriteMPIString(const std::string& str, unsigned short processor);
171
172 /*!
173 * \brief Get the position of the data of this rank in an array that holds the data of all ranks in rank order.
174 * \note Collective call, all ranks must call it.
175 * \param[in] localCount - The size of the data of this rank.
176 * \return The offset of this rank (the size of the data of the ranks before it) and the total size over all ranks.
177 */
178 std::pair<unsigned long, unsigned long> GetRankOffset(unsigned long localCount) const;
179
180 /*!
181 * \brief Write a string of each rank to the file, one after the other in rank order.
182 * \param[in] str - The string of this rank.
183 * \return <code>TRUE</code> if the writing was successful.
184 */
185 bool WriteMPIStringAll(const std::string& str);
186
187 /*!
188 * \brief Open a file to write using MPI I/O. Already existing file is deleted.
189 * \param[in] val_filename - The name of the file
190 * \return Boolean indicating whether the opening was successful.
191 */
192 bool OpenMPIFile(string val_filename, bool append = false);
193
194 /*!
195 * \brief Close a file using MPI I/O.
196 * \return Boolean indicating whether the closing was successful.
197 */
198 bool CloseMPIFile();
199
200};
201
Definition CFileWriter.hpp:42
unsigned long DetermineFilesize(std::string filename)
Determine the file size.
Definition CFileWriter.hpp:71
virtual void WriteData(string val_filename)
Write sorted data to file.
Definition CFileWriter.hpp:123
std::pair< unsigned long, unsigned long > GetRankOffset(unsigned long localCount) const
Get the position of the data of this rank in an array that holds the data of all ranks in rank order.
Definition CParallelFileWriter.cpp:279
bool CloseMPIFile()
Close a file using MPI I/O.
Definition CParallelFileWriter.cpp:298
su2double fileSize
Size of the last written file
Definition CFileWriter.hpp:64
su2double startTime
Start time for time measurement
Definition CFileWriter.hpp:60
su2double GetBandwidth() const
Get the bandwith used for the last writing.
Definition CFileWriter.hpp:128
FILE * fhw
The file handle for writing.
Definition CFileWriter.hpp:97
CParallelDataSorter * dataSorter
The parallel data sorter.
Definition CFileWriter.hpp:80
int rank
The MPI rank.
Definition CFileWriter.hpp:48
bool WriteMPIBinaryDataAll(const void *data, unsigned long sizeInBytes, unsigned long totalSizeInBytes, unsigned long offset)
Collectively write a binary data array distributed over all processors to file using MPI I/O.
Definition CParallelFileWriter.cpp:59
su2double usedTime
Used time for time measurement
Definition CFileWriter.hpp:62
int size
The MPI size, aka the number of processors.
Definition CFileWriter.hpp:53
su2double GetFilesize() const
Get the filesize of the last written file.
Definition CFileWriter.hpp:133
su2double stopTime
Stop time for time measurement
Definition CFileWriter.hpp:61
bool WriteMPIStringAll(const std::string &str)
Write a string of each rank to the file, one after the other in rank order.
Definition CParallelFileWriter.cpp:288
su2double GetUsedTime() const
Get the used time of the last file writing.
Definition CFileWriter.hpp:139
bool WriteMPIBinaryData(const void *data, unsigned long sizeInBytes, unsigned short processor)
Write a binary data array to a currently opened file using MPI I/O. Note: routine must be called coll...
Definition CParallelFileWriter.cpp:122
std::string fileExt
The file extension to be attached to the filename.
Definition CFileWriter.hpp:58
bool WriteMPIString(const std::string &str, unsigned short processor)
Write a string to a currently opened file using MPI I/O. Note: routine must be called collectively,...
Definition CParallelFileWriter.cpp:166
bool OpenMPIFile(string val_filename, bool append=false)
Open a file to write using MPI I/O. Already existing file is deleted.
Definition CParallelFileWriter.cpp:211
su2double bandwidth
Used bandwidth
Definition CFileWriter.hpp:63
virtual ~CFileWriter()
Destructor.
Definition CParallelDataSorter.hpp:39
passivedouble su2double
Definition code_config.hpp:181