-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathfilewriter.h
587 lines (498 loc) · 18.3 KB
/
filewriter.h
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
//
// (c) 2016 by University of Delaware, Argonne National Laboratory, San Diego
// Supercomputer Center, National University of Defense Technology,
// National Supercomputer Center in Guangzhou, and Sun Yat-sen University.
//
// See COPYRIGHT in top-level directory.
//
#ifndef MIMIR_FILE_WRITER_H
#define MIMIR_FILE_WRITER_H
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string>
#include <sstream>
#include "log.h"
#include "stat.h"
#include "config.h"
#include "interface.h"
#include "memory.h"
#include "globals.h"
#include "baseshuffler.h"
#include "serializer.h"
#ifndef HAVE_FTRUNCATE64
#define ftruncate64 ftruncate
#endif
namespace MIMIR_NS {
enum OUTPUT_FORMAT { BINARY_FORMAT, TEXT_FORMAT };
template <typename KeyType, typename ValType>
class FileWriter : public Writable<KeyType, ValType>
{
public:
static FileWriter *getWriter(MPI_Comm comm, const char *filename,
int keycount = 1, int valcount = 1);
static FileWriter *writer;
public:
FileWriter(MPI_Comm comm, const char *filename, bool singlefile = false,
int keycount = 1, int valcount = 1)
{
this->writer_comm = comm;
this->filename = filename;
this->singlefile = singlefile;
this->keycount = keycount;
this->valcount = valcount;
MPI_Comm_rank(writer_comm, &(this->writer_rank));
MPI_Comm_size(writer_comm, &(this->writer_size));
if (!singlefile) {
std::ostringstream oss;
oss << this->writer_size << "." << this->writer_rank;
this->filename += oss.str();
}
shuffler = NULL;
ser = new Serializer<KeyType, ValType>(keycount, valcount);
output_format = BINARY_FORMAT;
}
virtual ~FileWriter() { delete ser; }
std::string get_object_name() { return "FileWriter"; }
std::string &get_file_name() { return filename; }
void set_file_format(const char *format = "binary")
{
if (strcmp(format, "binary") == 0) {
output_format = BINARY_FORMAT;
}
else if (strcmp(format, "text") == 0) {
output_format = TEXT_FORMAT;
}
else {
LOG_ERROR("Wrong output format (%s)!\n", format);
}
}
void set_shuffler(BaseShuffler<KeyType, ValType> *shuffler)
{
this->shuffler = shuffler;
}
virtual bool is_single_file() { return singlefile; }
virtual int open()
{
bufsize = INPUT_BUF_SIZE;
this->datasize = 0;
buffer = (char *) mem_aligned_malloc(MEMPAGE_SIZE, bufsize,
MCDRAM_ALLOCATE);
record_count = 0;
this->done_flag = 0;
return file_open();
}
virtual void close()
{
this->done_flag = 1;
if (this->datasize > 0) file_write();
file_close();
mem_aligned_free(buffer);
}
virtual int seek(DB_POS pos)
{
LOG_WARNING("FileWritter doesnot support seek methods!\n");
return 0;
}
virtual int write(KeyType *key, ValType *val)
{
int kvsize = 0;
if (output_format == BINARY_FORMAT) {
//kvsize = this->ser->get_kv_bytes(key, val);
//if (kvsize > bufsize) {
// LOG_ERROR("The write record length is larger than the buffer size!\n");
//}
kvsize = this->ser->kv_to_bytes(key, val, buffer + datasize,
(int) (bufsize - datasize));
if (kvsize == -1) {
file_write();
kvsize = this->ser->kv_to_bytes(key, val, buffer + datasize,
(int) (bufsize - datasize));
if (kvsize == -1)
LOG_ERROR(
"The write record length is larger than the buffer "
"size!\n");
}
}
else if (output_format == TEXT_FORMAT) {
//kvsize = this->ser->get_kv_txt_len(key, val);
//kvsize += 1; // add \n
//if (kvsize > bufsize) {
// LOG_ERROR("The write record length is larger than the buffer size!\n");
//}
kvsize = this->ser->kv_to_txt(key, val, buffer + datasize,
(int) (bufsize - datasize));
if (kvsize == -1) {
file_write();
kvsize = this->ser->kv_to_txt(key, val, buffer + datasize,
(int) (bufsize - datasize));
if (kvsize == -1)
LOG_ERROR(
"The write record length is larger than the buffer "
"size!\n");
}
//this->ser->kv_to_txt(key, val, buffer + datasize, bufsize - datasize);
//*(buffer + datasize + kvsize - 1) = '\n';
}
datasize += kvsize;
record_count++;
return true;
}
virtual uint64_t get_record_count() { return record_count; }
virtual int file_open()
{
TRACKER_RECORD_EVENT(EVENT_COMPUTE_APP);
//std::ostringstream oss;
//oss << mimir_world_size << "." << mimir_world_rank;
//filename += oss.str();
PROFILER_RECORD_TIME_START;
this->union_fp.c_fp = fopen(this->filename.c_str(), "w+");
if (!this->union_fp.c_fp) {
LOG_ERROR("Open file %s error!\n", this->filename.c_str());
}
PROFILER_RECORD_TIME_END(TIMER_PFS_OUTPUT);
TRACKER_RECORD_EVENT(EVENT_DISK_FOPEN);
LOG_PRINT(DBG_IO, "Open output file %s.\n", this->filename.c_str());
return true;
}
virtual void file_write()
{
TRACKER_RECORD_EVENT(EVENT_COMPUTE_APP);
LOG_PRINT(DBG_IO, "Write output file %s:%d\n", this->filename.c_str(),
(int) (this->datasize));
PROFILER_RECORD_TIME_START;
fwrite(this->buffer, this->datasize, 1, this->union_fp.c_fp);
PROFILER_RECORD_COUNT(COUNTER_OUTPUT_SIZE, this->datasize, OPSUM);
PROFILER_RECORD_TIME_END(TIMER_PFS_OUTPUT);
this->datasize = 0;
TRACKER_RECORD_EVENT(EVENT_DISK_FWRITE);
}
virtual void file_close()
{
if (this->union_fp.c_fp) {
TRACKER_RECORD_EVENT(EVENT_COMPUTE_APP);
PROFILER_RECORD_TIME_START;
fclose(this->union_fp.c_fp);
PROFILER_RECORD_TIME_END(TIMER_PFS_OUTPUT);
this->union_fp.c_fp = NULL;
TRACKER_RECORD_EVENT(EVENT_DISK_FCLOSE);
LOG_PRINT(DBG_IO, "Close output file %s.\n",
this->filename.c_str());
}
}
protected:
std::string filename;
uint64_t record_count;
BaseShuffler<KeyType, ValType> *shuffler;
union FilePtr {
FILE *c_fp;
MPI_File mpi_fp;
int posix_fd;
} union_fp;
char *buffer;
uint64_t datasize;
uint64_t bufsize;
bool singlefile;
int done_flag;
MPI_Comm writer_comm;
int writer_rank;
int writer_size;
int keycount, valcount;
Serializer<KeyType, ValType> *ser;
OUTPUT_FORMAT output_format;
};
template <typename KeyType, typename ValType>
class DirectFileWriter : public FileWriter<KeyType, ValType>
{
public:
DirectFileWriter(MPI_Comm comm, const char *filename, int keycount = 1,
int valcount = 1)
: FileWriter<KeyType, ValType>(comm, filename, false, keycount,
valcount)
{
}
virtual int file_open()
{
TRACKER_RECORD_EVENT(EVENT_COMPUTE_APP);
PROFILER_RECORD_TIME_START;
#ifdef O_DIRECT
this->union_fp.posix_fd = ::open(
this->filename.c_str(), O_CREAT | O_WRONLY | O_DIRECT | O_LARGEFILE,
S_IRUSR | S_IWUSR);
if (this->union_fp.posix_fd == -1) {
LOG_ERROR("Open file %s error %d!\n", this->filename.c_str(),
errno);
}
#else
#ifdef F_NOCACHE
this->union_fp.posix_fd = ::open(
this->filename.c_str(), O_CREAT | O_WRONLY,
S_IRUSR | S_IWUSR);
::fcntl(this->union_fp.posix_fd, F_NOCACHE, 1);
if (this->union_fp.posix_fd == -1) {
LOG_ERROR("Open file %s error %d!\n", this->filename.c_str(),
errno);
}
#else
#warning "No direct IO support"
this->union_fp.posix_fd = ::open(
this->filename.c_str(), O_CREAT | O_WRONLY | O_LARGEFILE,
S_IRUSR | S_IWUSR);
if (this->union_fp.posix_fd == -1) {
LOG_ERROR("Open file %s error %d!\n", this->filename.c_str(),
errno);
}
#endif
#endif
filesize = 0;
PROFILER_RECORD_TIME_END(TIMER_PFS_OUTPUT);
TRACKER_RECORD_EVENT(EVENT_DISK_FOPEN);
LOG_PRINT(DBG_IO, "Open (POSIX) output file %s.\n",
this->filename.c_str());
return true;
}
virtual void file_write()
{
TRACKER_RECORD_EVENT(EVENT_COMPUTE_APP);
LOG_PRINT(DBG_IO, "Write (POSIX) output file %s:%d\n",
this->filename.c_str(), (int) (this->datasize));
PROFILER_RECORD_TIME_START;
//::lseek64(union_fp.posix_fd, 0, SEEK_END);
uint64_t total_bytes = 0;
if (this->done_flag) {
total_bytes
= ROUNDUP((this->datasize), DISKPAGE_SIZE) * DISKPAGE_SIZE;
}
else {
total_bytes
= ROUNDDOWN((this->datasize), DISKPAGE_SIZE) * DISKPAGE_SIZE;
}
uint64_t remain_bytes = total_bytes;
char *remain_buffer = this->buffer;
do {
ssize_t write_bytes
= ::write(this->union_fp.posix_fd, remain_buffer, remain_bytes);
if (write_bytes == -1) {
LOG_ERROR("Write error, %d\n", errno);
}
PROFILER_RECORD_COUNT(COUNTER_OUTPUT_SIZE, (uint64_t) write_bytes,
OPSUM);
remain_bytes -= write_bytes;
remain_buffer += write_bytes;
} while (remain_bytes > 0);
PROFILER_RECORD_TIME_END(TIMER_PFS_OUTPUT);
if (total_bytes < this->datasize) {
filesize += total_bytes;
this->datasize = this->datasize - total_bytes;
for (size_t i = 0; i < this->datasize; i++) {
this->buffer[i] = this->buffer[total_bytes + i];
}
}
else if (total_bytes > this->datasize) {
filesize += this->datasize;
this->datasize = 0;
LOG_PRINT(DBG_IO, "Set (POSIX) output file %s:%ld\n",
this->filename.c_str(), filesize);
::ftruncate64(this->union_fp.posix_fd, filesize);
}
else {
filesize += this->datasize;
this->datasize = 0;
}
TRACKER_RECORD_EVENT(EVENT_DISK_FWRITE);
}
virtual void file_close()
{
if (this->union_fp.posix_fd != -1) {
TRACKER_RECORD_EVENT(EVENT_COMPUTE_APP);
PROFILER_RECORD_TIME_START;
::close(this->union_fp.posix_fd);
PROFILER_RECORD_TIME_END(TIMER_PFS_OUTPUT);
this->union_fp.posix_fd = -1;
TRACKER_RECORD_EVENT(EVENT_DISK_FCLOSE);
LOG_PRINT(DBG_IO, "Close (POSIX) output file %s.\n",
this->filename.c_str());
}
}
private:
off64_t filesize;
};
template <typename KeyType, typename ValType>
class MPIFileWriter : public FileWriter<KeyType, ValType>
{
public:
MPIFileWriter(MPI_Comm comm, const char *filename, int keycount = 1,
int valcount = 1)
: FileWriter<KeyType, ValType>(comm, filename, true, keycount, valcount)
{
}
virtual int file_open()
{
TRACKER_RECORD_EVENT(EVENT_COMPUTE_APP);
MPI_Request req;
MPI_Status st;
if (this->shuffler) {
//PROFILER_RECORD_TIME_START;
MPI_Ibarrier(this->writer_comm, &req);
//PROFILER_RECORD_TIME_END(TIMER_COMM_IBARRIER);
int flag = 0;
while (!flag) {
//PROFILER_RECORD_TIME_START;
MPI_Test(&req, &flag, &st);
//PROFILER_RECORD_TIME_END(TIMER_COMM_TEST);
this->shuffler->make_progress();
}
TRACKER_RECORD_EVENT(EVENT_SYN_COMM);
}
LOG_PRINT(DBG_IO, "Collective open output file %s.\n",
this->filename.c_str());
PROFILER_RECORD_TIME_START;
MPI_Info file_info;
MPI_Info_create(&file_info);
if (DIRECT_WRITE) MPI_Info_set(file_info, "direct_write", "true");
MPI_CHECK(MPI_File_open(this->writer_comm, this->filename.c_str(),
MPI_MODE_WRONLY | MPI_MODE_CREATE, file_info,
&(this->union_fp.mpi_fp)));
MPI_Info_free(&file_info);
if (this->union_fp.mpi_fp == MPI_FILE_NULL) {
LOG_ERROR("Open file %s error!\n", this->filename.c_str());
}
PROFILER_RECORD_TIME_END(TIMER_PFS_OUTPUT);
TRACKER_RECORD_EVENT(EVENT_DISK_MPIOPEN);
done_count = 0;
filesize = 0;
return true;
}
virtual void file_write()
{
//MPI_Request done_req, req;
MPI_Request req;
MPI_Status st;
MPI_Offset fileoff = 0;
int sendcounts[this->writer_size];
TRACKER_RECORD_EVENT(EVENT_COMPUTE_APP);
if (this->shuffler) {
//PROFILER_RECORD_TIME_START;
MPI_Ibarrier(this->writer_comm, &req);
//PROFILER_RECORD_TIME_END(TIMER_COMM_IBARRIER);
int flag = 0;
while (!flag) {
//PROFILER_RECORD_TIME_START;
MPI_Test(&req, &flag, &st);
//PROFILER_RECORD_TIME_END(TIMER_COMM_TEST);
this->shuffler->make_progress();
}
TRACKER_RECORD_EVENT(EVENT_SYN_COMM);
}
//MPI_File_get_size(union_fp.mpi_fp, &filesize);
PROFILER_RECORD_TIME_START;
MPI_Allgather(&(this->datasize), 1, MPI_INT, sendcounts, 1, MPI_INT,
this->writer_comm);
PROFILER_RECORD_TIME_END(TIMER_COMM_ALLGATHER);
TRACKER_RECORD_EVENT(EVENT_COMM_ALLGATHER);
fileoff = filesize;
for (int i = 0; i < this->writer_rank; i++) fileoff += sendcounts[i];
for (int i = 0; i < this->writer_size; i++) filesize += sendcounts[i];
//if (mimir_world_rank == 0)
//LOG_PRINT(DBG_IO, "Collective set output file %s:%lld\n",
// filename.c_str(), filesize);
//MPI_File_set_size(union_fp.mpi_fp, filesize);
//if (this->shuffler) {
// MPI_Ibarrier(mimir_world_comm, &req);
// int flag = 0;
// while (!flag) {
// MPI_Test(&req, &flag, &st);
// this->shuffler->make_progress();
// }
// TRACKER_RECORD_EVENT(EVENT_SYN_COMM);
//}
LOG_PRINT(DBG_IO, "Collective write output file %s:%lld+%d\n",
this->filename.c_str(), fileoff, (int) (this->datasize));
PROFILER_RECORD_TIME_START;
MPI_CHECK(MPI_File_write_at_all(this->union_fp.mpi_fp, fileoff,
this->buffer, (int) (this->datasize),
MPI_BYTE, &st));
PROFILER_RECORD_COUNT(COUNTER_OUTPUT_SIZE, this->datasize, OPSUM);
this->datasize = 0;
PROFILER_RECORD_TIME_END(TIMER_PFS_OUTPUT);
TRACKER_RECORD_EVENT(EVENT_DISK_MPIWRITEATALL);
//int flag = 0;
//while (!flag) {
// PROFILER_RECORD_TIME_START;
// MPI_Test(&done_req, &flag, &st);
// PROFILER_RECORD_TIME_END(TIMER_COMM_TEST);
// if (this->shuffler) this->shuffler->make_progress();
//}
PROFILER_RECORD_TIME_START;
MPI_Allreduce(&this->done_flag, &done_count, 1, MPI_INT, MPI_SUM,
this->writer_comm);
PROFILER_RECORD_TIME_END(TIMER_COMM_RDC);
TRACKER_RECORD_EVENT(EVENT_SYN_COMM);
}
virtual void file_close()
{
if (this->union_fp.mpi_fp) {
while (done_count < this->writer_size) {
file_write();
}
TRACKER_RECORD_EVENT(EVENT_COMPUTE_APP);
MPI_Request req;
MPI_Status st;
if (this->shuffler) {
//PROFILER_RECORD_TIME_START;
MPI_Ibarrier(this->writer_comm, &req);
//PROFILER_RECORD_TIME_END(TIMER_COMM_IBARRIER);
int flag = 0;
while (!flag) {
//PROFILER_RECORD_TIME_START;
MPI_Test(&req, &flag, &st);
//PROFILER_RECORD_TIME_END(TIMER_COMM_TEST);
this->shuffler->make_progress();
}
TRACKER_RECORD_EVENT(EVENT_SYN_COMM);
}
PROFILER_RECORD_TIME_START;
MPI_CHECK(MPI_File_close(&(this->union_fp.mpi_fp)));
this->union_fp.mpi_fp = MPI_FILE_NULL;
PROFILER_RECORD_TIME_END(TIMER_PFS_OUTPUT);
TRACKER_RECORD_EVENT(EVENT_DISK_MPICLOSE);
LOG_PRINT(DBG_IO, "Collective close output file %s.\n",
this->filename.c_str());
}
}
private:
int done_count;
MPI_Offset filesize;
};
template <typename KeyType, typename ValType>
FileWriter<KeyType, ValType> *FileWriter<KeyType, ValType>::writer = NULL;
template <typename KeyType, typename ValType>
FileWriter<KeyType, ValType> *FileWriter<KeyType, ValType>::getWriter(
MPI_Comm comm, const char *filename, int keycount, int valcount)
{
//if (writer != NULL) delete writer;
if (WRITE_TYPE == 0) {
if (DIRECT_WRITE) {
writer = new DirectFileWriter<KeyType, ValType>(comm, filename,
keycount, valcount);
}
else {
writer = new FileWriter<KeyType, ValType>(comm, filename, false,
keycount, valcount);
}
}
else if (WRITE_TYPE == 1) {
writer = new MPIFileWriter<KeyType, ValType>(comm, filename, keycount,
valcount);
}
else {
LOG_ERROR("Error writer type %d\n", WRITE_TYPE);
}
return writer;
}
} // namespace MIMIR_NS
#endif