-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmpistream.hpp
281 lines (227 loc) · 7.44 KB
/
mpistream.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// -*- C++ -*-
#ifndef _MPISTREAM_HPP_
#define _MPISTREAM_HPP_
#include "debug.hpp"
#include "tinyformat.hpp"
#include <filesystem>
#include <fstream>
#include <iostream>
#include <mpi.h>
#include <ostream>
#include <sstream>
// format for temporary stdout and stderr files
static constexpr char filename_format[] = "%06d";
static constexpr char dev_null[] = "/dev/null";
///
/// @brief singleton class
/// @tparam T typename
///
template <typename T>
class Singleton
{
private:
Singleton(const Singleton&);
Singleton& operator=(const Singleton&);
protected:
Singleton(){};
virtual ~Singleton(){};
public:
static T* getInstance()
{
static T instance;
return &instance;
}
};
///
/// @brief stream buffer mimicking "tee" command
///
class teebuf : public std::streambuf
{
private:
std::streambuf* m_sb1;
std::streambuf* m_sb2;
virtual int overflow(int c) override
{
if (c == EOF) {
return !EOF;
} else {
int const r1 = m_sb1->sputc(c);
int const r2 = m_sb2->sputc(c);
return r1 == EOF || r2 == EOF ? EOF : c;
}
}
virtual int sync() override
{
int const r1 = m_sb1->pubsync();
int const r2 = m_sb2->pubsync();
return r1 == 0 && r2 == 0 ? 0 : -1;
}
public:
teebuf(std::streambuf* sb1, std::streambuf* sb2) : m_sb1(sb1), m_sb2(sb2)
{
}
};
///
/// @brief MPI stream class
///
class MpiStream : public Singleton<MpiStream>
{
friend class Singleton<MpiStream>;
protected:
// for stdout/stderr
std::string m_outf; ///< dummy standard output file
std::string m_errf; ///< dummy standard error file
std::unique_ptr<std::ofstream> m_out; ///< dummy standard output
std::unique_ptr<std::ofstream> m_err; ///< dummy standard error
std::unique_ptr<teebuf> m_outtee; ///< buffer for replicating cout and file
std::unique_ptr<teebuf> m_errtee; ///< buffer for replicating cerr and file
std::streambuf* m_errbuf; ///< buffer of original cerr
std::streambuf* m_outbuf; ///< buffer of original cout
MpiStream(){};
~MpiStream(){};
// remain undefined
MpiStream(const MpiStream&);
MpiStream& operator=(const MpiStream&);
public:
static void initialize(std::string dirname, int max_file_per_dir = -1)
{
int thisrank = 0;
int nprocess = 0;
MPI_Comm_rank(MPI_COMM_WORLD, &thisrank);
MPI_Comm_size(MPI_COMM_WORLD, &nprocess);
MpiStream* instance = getInstance();
// create directories
create_directory_tree(dirname, thisrank, nprocess, max_file_per_dir);
// open dummy standard output stream
instance->m_outf = get_stdout_filename(dirname, thisrank, nprocess, max_file_per_dir);
instance->m_out = std::make_unique<std::ofstream>(instance->m_outf.c_str());
instance->m_outtee = std::make_unique<teebuf>(std::cout.rdbuf(), instance->m_out->rdbuf());
// open dummy standard error stream
instance->m_errf = get_stderr_filename(dirname, thisrank, nprocess, max_file_per_dir);
instance->m_err = std::make_unique<std::ofstream>(instance->m_errf.c_str());
instance->m_errtee = std::make_unique<teebuf>(std::cerr.rdbuf(), instance->m_err->rdbuf());
if (thisrank == 0) {
// stdout/stderr are replicated for rank==0
instance->m_outbuf = std::cout.rdbuf(instance->m_outtee.get());
instance->m_errbuf = std::cerr.rdbuf(instance->m_errtee.get());
} else {
instance->m_outbuf = std::cout.rdbuf(instance->m_out->rdbuf());
instance->m_errbuf = std::cerr.rdbuf(instance->m_err->rdbuf());
}
}
static void finalize()
{
MpiStream* instance = getInstance();
// close dummy standard output
if (instance->m_out != nullptr) {
instance->m_out->flush();
instance->m_out->close();
std::cout.rdbuf(instance->m_outbuf);
}
// close dummy standard error
if (instance->m_err != nullptr) {
instance->m_err->flush();
instance->m_err->close();
std::cerr.rdbuf(instance->m_errbuf);
}
}
static void flush()
{
MpiStream* instance = getInstance();
instance->m_out->flush();
instance->m_err->flush();
}
static int get_directory_level(int nprocess, int max_file_per_dir)
{
if (max_file_per_dir == -1) {
max_file_per_dir = nprocess;
}
int directory_level = 0;
while (nprocess > max_file_per_dir) {
nprocess /= max_file_per_dir;
directory_level++;
}
return directory_level;
}
static bool create_directory_tree(std::string dirname, int thisrank, int nprocess,
int max_file_per_dir = -1)
{
namespace fs = std::filesystem;
bool use_null_stream = std::string("") == dirname;
bool status = true;
int directory_level = get_directory_level(nprocess, max_file_per_dir);
if (use_null_stream) {
return status;
}
// base directory
{
if (thisrank == 0 && fs::exists(dirname) == false) {
status = status & fs::create_directory(dirname);
nix::sync_directory(dirname);
}
// synchronize
MPI_Barrier(MPI_COMM_WORLD);
}
// recursive directory creation
{
fs::path path(dirname);
for (int level = 0; level < directory_level; level++) {
int max_file_level = std::pow(max_file_per_dir, directory_level - level);
path = path / tfm::format(filename_format, (thisrank / max_file_level) * max_file_level);
if (thisrank % max_file_level == 0 && fs::exists(path) == false) {
status = status & fs::create_directory(path.string());
nix::sync_directory(path.string());
}
// synchronize
MPI_Barrier(MPI_COMM_WORLD);
}
}
return status;
}
static std::string get_filename_pattern(int thisrank, int nprocess, int max_file_per_dir = -1)
{
namespace fs = std::filesystem;
int directory_level = get_directory_level(nprocess, max_file_per_dir);
// make directory name
fs::path path;
for (int level = 0; level < directory_level; level++) {
int max_file_level = std::pow(max_file_per_dir, directory_level - level);
path = path / tfm::format(filename_format, (thisrank / max_file_level) * max_file_level);
}
path = path / tfm::format(filename_format, thisrank);
return path.string();
}
static std::string get_filename(std::string dirname, std::string extension, int thisrank,
int nprocess, int max_file_per_dir = -1)
{
namespace fs = std::filesystem;
fs::path path(dirname);
path = path / get_filename_pattern(thisrank, nprocess, max_file_per_dir);
return path.string() + extension;
}
static std::string get_stdout_filename(std::string dirname, int thisrank, int nprocess,
int max_file_per_dir = -1)
{
bool use_null_stream = std::string("") == dirname;
if (use_null_stream) {
return dev_null;
} else {
return get_filename(dirname, ".stdout", thisrank, nprocess, max_file_per_dir);
}
}
static std::string get_stderr_filename(std::string dirname, int thisrank, int nprocess,
int max_file_per_dir = -1)
{
bool use_null_stream = std::string("") == dirname;
if (use_null_stream) {
return dev_null;
} else {
return get_filename(dirname, ".stderr", thisrank, nprocess, max_file_per_dir);
}
}
};
// Local Variables:
// c-file-style : "gnu"
// c-file-offsets : ((innamespace . 0) (inline-open . 0))
// End:
#endif