-
Notifications
You must be signed in to change notification settings - Fork 17
/
file.hpp
298 lines (247 loc) · 7.66 KB
/
file.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
* Copyright 2015 Georgia Institute of Technology
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http:https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @file file.hpp
* @author Patrick Flick <[email protected]>
* @brief Block decompose and distribute file as string on MPI communicator.
*
* This is not a substitute for MPI_File functionality.
* TODO:
* - [ ] Implement proper MPI_File functions for parallel reading of files
*/
#ifndef MXX_FILE_HPP
#define MXX_FILE_HPP
#include <mpi.h>
// C++ includes
#include <string>
#include <fstream>
#include <streambuf>
#include <sstream>
#include <iostream>
#include <iomanip>
// mxx includes
#include "partition.hpp"
namespace mxx {
std::ifstream::pos_type get_filesize(const char* filename)
{
std::ifstream in(filename, std::ifstream::ate | std::ifstream::binary);
return in.tellg();
}
class rangebuf: public std::streambuf {
public:
rangebuf(std::streampos start,
size_t size,
std::streambuf* sbuf):
size_(size), sbuf_(sbuf), buf_(new char[64])
{
sbuf->pubseekpos(start, std::ios_base::in);
}
int underflow() {
size_t r(this->sbuf_->sgetn(this->buf_,
std::min<size_t>(sizeof(this->buf_), this->size_)));
this->size_ -= r;
this->setg(this->buf_, this->buf_, this->buf_ + r);
return this->gptr() == this->egptr()
? traits_type::eof()
: traits_type::to_int_type(*this->gptr());
}
~rangebuf()
{
delete [] this->buf_;
}
protected:
size_t size_;
std::streambuf* sbuf_;
char* buf_;
};
struct file {
protected:
MPI_File handle;
std::string filename;
bool isopen;
void clear() {
handle = MPI_FILE_NULL;
filename = "";
isopen = false;
}
public:
file() : handle(MPI_FILE_NULL), filename(), isopen(false) {}
file(file&& o) : handle(o.handle), filename(o.filename), isopen(o.isopen) {
o.clear();
}
file(const std::string& filename) : handle(MPI_FILE_NULL), filename(filename), isopen(false) {}
file(const std::string& filename, int mode) : file(filename) {
open(mode);
}
void open(const std::string& filename, int mode) {
this->filename = filename;
MPI_File_open(MPI_COMM_SELF, &filename[0], mode, MPI_INFO_NULL, &handle);
this->isopen = true;
}
void open(int mode) {
open(this->filename, mode);
}
void close() {
if (isopen) {
MPI_File_close(&handle);
isopen = false;
handle = MPI_FILE_NULL;
}
}
static void delete_file(const std::string& filename) {
MPI_File_delete(&filename[0], MPI_INFO_NULL);
}
size_t get_size() {
MPI_Offset s;
MPI_File_get_size(handle, &s);
return s;
}
void set_size(size_t size) {
MPI_File_set_size(handle, size);
}
template <typename T>
void read_at(MPI_Offset offset, T* out, size_t count) {
mxx::datatype dt = mxx::get_datatype<T>().contiguous(count);
MPI_File_read_at(handle, offset, out, 1, dt.type(), MPI_STATUS_IGNORE);
}
template <typename T>
void write_at(MPI_Offset offset, T* data, size_t count) {
mxx::datatype dt = mxx::get_datatype<T>().contiguous(count);
MPI_File_write_at(handle, offset, data, 1, dt.type(), MPI_STATUS_IGNORE);
}
// TODO:
// iread_at
// iwrite_at
//
// read_at_all
// write_at_all
//
// iread_at_all
// iwrite_at_all
//
// read
// write
//
// read_all
// write_all
//
// iread
// iwrite
//
// iread_all
// iwrite_all
//
//
//
// seek_set
// seek_cur
// seek_end
//
// get_pos // `etype` units
// get_byte_offset
//
//
//
virtual ~file() {
close();
}
};
struct coll_file : public file {
const mxx::comm& comm;
coll_file(const std::string& filename, const mxx::comm& comm)
: file(filename), comm(comm) {}
template <typename T>
void read_ordered(size_t count, T* out) {
mxx::datatype dt = mxx::get_datatype<T>().contiguous(count);
MPI_File_read_ordered(handle, out, 1, dt.type(), MPI_STATUS_IGNORE);
}
// collective, blocking
// simple ordered write without a need for a file view
template <typename T>
void write_ordered(const T* buf, size_t count) {
mxx::datatype dt = mxx::get_datatype<T>().contiguous(count);
MPI_File_write_ordered(handle, const_cast<T*>(buf), 1, dt.type(), MPI_STATUS_IGNORE);
}
void open(int mode) {
MPI_File_open(comm, &filename[0], mode, MPI_INFO_NULL, &handle);
this->isopen = true;
}
};
std::string file_block_decompose(const char* filename, MPI_Comm comm = MPI_COMM_WORLD, std::size_t max_local_size = 0)
{
// TODO: handle error if file doesn't exist
// get size of input file
std::size_t file_size = get_filesize(filename);
// get communication parameters
int p, rank;
MPI_Comm_size(comm, &p);
MPI_Comm_rank(comm, &rank);
// restrict max local size (assuming that it is the same parameter on each
// processor)
if (max_local_size > 0 && file_size / p > max_local_size)
file_size = p*max_local_size;
blk_dist part(file_size, p, rank);
// block decompose
std::size_t local_size = part.local_size();
std::size_t offset = part.eprefix_size();
// open file
std::ifstream t(filename);
// wrap in our custom range buffer (of type std::streambuf)
rangebuf rb(offset, local_size, t.rdbuf());
// read file (range) buffer into string stream
std::stringstream ss;
ss << &rb;
std::string local_str(ss.str());
return local_str;
}
template <typename T>
void write_ordered(const std::string& filename, const T* buf, size_t count, const mxx::comm& comm) {
MPI_File handle;
MPI_File_open(comm, const_cast<char*>(&filename[0]), MPI_MODE_CREATE | MPI_MODE_WRONLY, MPI_INFO_NULL, &handle);
mxx::datatype dt = mxx::get_datatype<T>();
MPI_File_write_ordered(handle, const_cast<T*>(buf), count, dt.type(), MPI_STATUS_IGNORE);
MPI_File_close(&handle);
}
template <typename T>
void write_ordered(const std::string& filename, const std::vector<T>& data, const mxx::comm& comm) {
write_ordered(filename, data.data(), data.size(), comm);
}
template <typename _Iterator>
void write_files(const std::string& filename, _Iterator begin, _Iterator end, MPI_Comm comm = MPI_COMM_WORLD)
{
// get MPI Communicator properties
int rank, p;
MPI_Comm_size(comm, &p);
MPI_Comm_rank(comm, &rank);
// get max rank string length:
std::stringstream sslen;
sslen << p;
int rank_slen = sslen.str().size();
// concat rank at end of filename
std::stringstream ss;
ss << filename << "." << std::setfill('0') << std::setw(rank_slen) << p << "." << std::setfill('0') << std::setw(rank_slen) << rank;
// open file with stream
//std::cerr << "writing to file " << ss.str() << std::endl;
std::ofstream outfs(ss.str());
// write the content into the file, sep by newline
while (begin != end)
{
outfs << *(begin++) << std::endl;
}
outfs.close();
}
} // namespace mxx
#endif // MXX_FILE_HPP