-
Notifications
You must be signed in to change notification settings - Fork 1
/
MPIFile.c
508 lines (410 loc) · 12.4 KB
/
MPIFile.c
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
/*
* Helper functions that aren't direct I/O.
*/
#include "MPIHook.h"
#define BUF_SIZE 4096
/*
* Opens a file for reading or writing. If the ALL_HDFS option is used, all
* files are assumed to be part of the default HDFS. Otherwise, file paths are
* parsed to look for the hdfs:// prefix, and all other files simply interact
* with the native MPI functions. Files to be opened for writing are not
* actually opened until the write calls, so that a single file can be opened
* for writing multiple times, as long as only one process is writing to it at
* a time.
*/
int MPI_File_open(MPI_Comm comm, MPIHDFS_CONST char *filename, int amode,
MPI_Info info, MPI_File *fh)
{
#ifndef ALL_HDFS
char fsname[BUF_SIZE];
#endif
char hdfs_filename[BUF_SIZE];
hdfsFile_wrapper *fh_w;
hdfsFile file = NULL;
struct hdfsBuilder *builder;
hdfsFS fs;
int mode;
status("File_open called.\n");
if (!filename || !fh)
return MPI_ERR_ARG;
#ifndef ALL_HDFS
if (hdfs_url_parse(filename, fsname, BUF_SIZE, hdfs_filename, BUF_SIZE)) {
int (*real_MPI_File_open)(MPI_Comm, const char*, int, MPI_Info, MPI_File *) = NULL;
real_MPI_File_open = dlsym(RTLD_NEXT, "MPI_File_open");
if (!real_MPI_File_open) {
fprintf(stderr, "Failed to load actual MPI_File_open location.\n");
return MPI_ERR_OTHER;
}
status("Passing File_open call to actual MPI function.\n");
return real_MPI_File_open(comm, filename, amode, info, fh);
}
#endif
if (amode & MPI_MODE_RDONLY) {
mode = O_RDONLY;
}
else if (amode & MPI_MODE_WRONLY) {
mode = O_WRONLY;
}
else {
fprintf(stderr, "Only reading or writing to HDFS is supported currently.\n");
return MPI_ERR_AMODE;
}
fh_w = malloc(sizeof(hdfsFile_wrapper));
if (!fh) {
fprintf(stderr, "Malloc failed.\n");
return MPI_ERR_OTHER;
}
fh_w->magic = HDFSFILEMAGIC;
builder = hdfsNewBuilder();
if (!builder) {
fprintf(stderr, "Failed to make new hdfsBuilder.\n");
free(fh_w);
return MPI_ERR_OTHER;
}
#ifndef ALL_HDFS
hdfsBuilderSetNameNode(builder, fsname);
#else
hdfsBuilderSetNameNode(builder, "default");
strcpy(hdfs_filename, filename);
#endif
fs = hdfsBuilderConnect(builder);
if (!fs) {
fprintf(stderr, "Failed to connect to hdfs.\n");
free(fh_w);
return MPI_ERR_FILE;
}
// Only open files for writing in the write functions
if (mode == O_RDONLY) {
file = hdfsOpenFile(fs, hdfs_filename, mode, 0, 0, 0);
if (!file) {
fprintf(stderr, "Failed to open hdfs file.\n");
free(fh_w);
return MPI_ERR_FILE;
}
}
fh_w->filename = malloc(strlen(hdfs_filename) + 1);
if (!fh_w->filename) {
fprintf(stderr, "Failed to allocate space for filename.\n");
free(fh_w);
return MPI_ERR_OTHER;
}
strcpy(fh_w->filename, hdfs_filename);
fh_w->fs = fs;
fh_w->file = file;
fh_w->amode = amode;
*fh = (MPI_File)fh_w;
status("File_open returning successfully.\n");
return MPI_SUCCESS;
}
/*
* Closes a file.
*/
int MPI_File_close(MPI_File *fh)
{
hdfsFile_wrapper *fh_w;
int ret;
status("File_close called.\n");
if (!fh)
return MPI_ERR_ARG;
fh_w = (hdfsFile_wrapper*)*fh;
if (fh_w->magic != HDFSFILEMAGIC) {
int (*real_MPI_File_close)(MPI_File *) = NULL;
real_MPI_File_close = dlsym(RTLD_NEXT, "MPI_File_close");
if (!real_MPI_File_close) {
fprintf(stderr, "Failed to load actual MPI_File_close location.\n");
return MPI_ERR_OTHER;
}
status("Passing File_close to actual MPI function.\n");
return real_MPI_File_close(fh);
}
status("HDFS file found in File_close.\n");
if (fh_w->file) {
ret = hdfsCloseFile(fh_w->fs, fh_w->file);
if (ret) {
fprintf(stderr, "Failed to close hdfs file.\n");
return MPI_ERR_FILE;
}
}
if (fh_w->filename)
free(fh_w->filename);
free(fh_w);
status("File_close returning successfully.\n");
return MPI_SUCCESS;
}
/*
* Deletes a file.
*/
int MPI_File_delete(MPIHDFS_CONST char *filename, MPI_Info info)
{
#ifndef ALL_HDFS
char fsname[BUF_SIZE];
#endif
char hdfs_filename[BUF_SIZE];
struct hdfsBuilder *builder;
hdfsFS fs;
status("File_delete called.\n");
if (!filename)
return MPI_ERR_ARG;
#ifndef ALL_HDFS
if (hdfs_url_parse(filename, fsname, BUF_SIZE, hdfs_filename, BUF_SIZE)) {
int (*real_MPI_File_delete)(const char *, MPI_Info) = NULL;
real_MPI_File_delete = dlsym(RTLD_NEXT, "MPI_File_delete");
if (!real_MPI_File_delete) {
fprintf(stderr, "Failed to load actual MPI_File_open location.\n");
return MPI_ERR_OTHER;
}
status("Passing File_delete call to actual MPI function.\n");
return real_MPI_File_delete(filename, info);
}
#endif
builder = hdfsNewBuilder();
if (!builder) {
fprintf(stderr, "Failed to make new hdfsBuilder.\n");
return MPI_ERR_OTHER;
}
#ifndef ALL_HDFS
hdfsBuilderSetNameNode(builder, fsname);
#else
hdfsBuilderSetNameNode(builder, "default");
strcpy(hdfs_filename, filename);
#endif
fs = hdfsBuilderConnect(builder);
if (!fs) {
fprintf(stderr, "Failed to connect to hdfs.\n");
return MPI_ERR_FILE;
}
return hdfsDelete(fs, hdfs_filename, 1);
}
int MPI_File_set_size(MPI_File fh, MPI_Offset size) { NOT_IMPLEMENTED; }
int MPI_File_preallocate(MPI_File fh, MPI_Offset size) { NOT_IMPLEMENTED; }
/*
* Returns the size of a file. In HDFS, you need the path to the file to get
* the size, so we must store the file path in the file wrapper.
*/
int MPI_File_get_size(MPI_File fh, MPI_Offset *size)
{
hdfsFile_wrapper *fh_w;
status("File_get_size called.\n");
if (!fh || !size)
return MPI_ERR_OTHER;
fh_w = (hdfsFile_wrapper*)fh;
if (fh_w->magic != HDFSFILEMAGIC)
{
int (*real_MPI_File_get_size)(MPI_File, MPI_Offset*) = NULL;
real_MPI_File_get_size = dlsym(RTLD_NEXT, "MPI_File_get_size");
if (!real_MPI_File_get_size) {
fprintf(stderr, "Failed to load actual MPI_File_get_size location.\n");
return -1;
}
status("Passing File_get_size to actual MPI function.\n");
return real_MPI_File_get_size(fh, size);
}
hdfsFileInfo *fileInfo = hdfsGetPathInfo(fh_w->fs, fh_w->filename);
if (!fileInfo) {
fprintf(stderr, "Failed to get path info for file size.\n");
return -1;
}
*size = fileInfo->mSize;
hdfsFreeFileInfo(fileInfo, 1);
return MPI_SUCCESS;
}
int MPI_File_get_group(MPI_File fh, MPI_Group *group) { NOT_IMPLEMENTED; }
/*
* Returns the mode this file was opened in. This is stored in the file wrapper
* object in the initial File_open call.
*/
int MPI_File_get_amode(MPI_File fh, int *amode)
{
hdfsFile_wrapper *fh_w;
status("File_get_amode called.\n");
if (!fh || !amode)
return MPI_ERR_OTHER;
fh_w = (hdfsFile_wrapper*)fh;
if (fh_w->magic != HDFSFILEMAGIC)
{
int (*real_MPI_File_get_amode)(MPI_File, int *amode) = NULL;
real_MPI_File_get_amode = dlsym(RTLD_NEXT, "MPI_File_get_amode");
if (!real_MPI_File_get_amode) {
fprintf(stderr, "Failed to load actual MPI_File_get_size location.\n");
return -1;
}
status("Passing File_get_size to actual MPI function.\n");
return real_MPI_File_get_amode(fh, amode);
}
*amode = fh_w->amode;
return MPI_SUCCESS;
}
int MPI_File_set_info(MPI_File fh, MPI_Info info)
{
hdfsFile_wrapper *fh_w;
if (!fh)
return MPI_ERR_ARG;
fh_w = (hdfsFile_wrapper*)fh;
if (fh_w->magic != HDFSFILEMAGIC)
{
int (*real_MPI_File_set_info)(MPI_File, MPI_Info) = NULL;
real_MPI_File_set_info = dlsym(RTLD_NEXT, "MPI_File_set_info");
if (!real_MPI_File_set_info) {
fprintf(stderr, "Failed to load actual MPI_File_set_info location.\n");
return MPI_ERR_OTHER;
}
status("Passing File_set_info to actual MPI function.\n");
return real_MPI_File_set_info(fh, info);
}
NOT_IMPLEMENTED;
}
int MPI_File_get_info(MPI_File fh, MPI_Info *info)
{
hdfsFile_wrapper *fh_w;
if (!fh)
return MPI_ERR_ARG;
fh_w = (hdfsFile_wrapper*)fh;
if (fh_w->magic != HDFSFILEMAGIC)
{
int (*real_MPI_File_get_info)(MPI_File, MPI_Info *) = NULL;
real_MPI_File_get_info = dlsym(RTLD_NEXT, "MPI_File_get_info");
if (!real_MPI_File_get_info) {
fprintf(stderr, "Failed to load actual MPI_File_get_info location.\n");
return MPI_ERR_OTHER;
}
status("Passing File_get_info to actual MPI function.\n");
return real_MPI_File_get_info(fh, info);
}
NOT_IMPLEMENTED;
}
/* Section 9.3 */
int MPI_File_set_view(MPI_File fh, MPI_Offset disp, MPI_Datatype etype,
MPI_Datatype filetype, MPIHDFS_CONST char *datarep, MPI_Info info)
{
hdfsFile_wrapper *fh_w;
if (!fh)
return MPI_ERR_ARG;
fh_w = (hdfsFile_wrapper*)fh;
if (fh_w->magic != HDFSFILEMAGIC)
{
int (*real_MPI_File_set_view)(MPI_File, MPI_Offset, MPI_Datatype, MPI_Datatype, MPIHDFS_CONST char *, MPI_Info) = NULL;
real_MPI_File_set_view = dlsym(RTLD_NEXT, "MPI_File_set_view");
if (!real_MPI_File_set_view) {
fprintf(stderr, "Failed to load actual MPI_File_set_view location.\n");
return MPI_ERR_OTHER;
}
status("Passing File_set_view to actual MPI function.\n");
return real_MPI_File_set_view(fh, disp, etype, filetype, datarep, info);
}
NOT_IMPLEMENTED;
}
int MPI_File_get_view(MPI_File fh, MPI_Offset *disp,
MPI_Datatype *etype, MPI_Datatype *filetype, char *datarep)
{
hdfsFile_wrapper *fh_w;
if (!fh)
return MPI_ERR_ARG;
fh_w = (hdfsFile_wrapper*)fh;
if (fh_w->magic != HDFSFILEMAGIC)
{
int (*real_MPI_File_get_view)(MPI_File, MPI_Offset *, MPI_Datatype *, MPI_Datatype *, char *) = NULL;
real_MPI_File_get_view = dlsym(RTLD_NEXT, "MPI_File_get_view");
if (!real_MPI_File_get_view) {
fprintf(stderr, "Failed to load actual MPI_File_get_view location.\n");
return MPI_ERR_OTHER;
}
status("Passing File_get_view to actual MPI function.\n");
return real_MPI_File_get_view(fh, disp, etype, filetype, datarep);
}
NOT_IMPLEMENTED;
}
/*
* Moves to a certain position in the file. Since views are not currently
* supported, the offset is the byte offset.
*/
int MPI_File_seek(MPI_File fh, MPI_Offset offset, int whence)
{
hdfsFile_wrapper *fh_w;
status("File_seek called.\n");
if (!fh)
return MPI_ERR_OTHER;
fh_w = (hdfsFile_wrapper*)fh;
if (fh_w->magic != HDFSFILEMAGIC)
{
int (*real_MPI_File_seek)(MPI_File, MPI_Offset, int) = NULL;
real_MPI_File_seek = dlsym(RTLD_NEXT, "MPI_File_seek");
if (!real_MPI_File_seek) {
fprintf(stderr, "Failed to load actual MPI_File_seek location.\n");
return MPI_ERR_OTHER;
}
status("Passing File_seek to actual MPI function.\n");
return real_MPI_File_seek(fh, offset, whence);
}
if (whence == MPI_SEEK_SET) {
if (hdfsSeek(fh_w->fs, fh_w->file, offset)) {
fprintf(stderr, "Failed to seek in hdfs file.\n");
return MPI_ERR_FILE;
}
}
else if (whence == MPI_SEEK_CUR) {
tOffset cur_off = hdfsTell(fh_w->fs, fh_w->file);
if (cur_off == -1) {
fprintf(stderr, "Failed to tell for seek in hdfs file.\n");
return MPI_ERR_FILE;
}
if (hdfsSeek(fh_w->fs, fh_w->file, cur_off + offset)) {
fprintf(stderr, "Failed to seek in hdfs file.\n");
return MPI_ERR_FILE;
}
}
else {
fprintf(stderr, "Operation not supported.\n");
return MPI_ERR_ARG;
}
return MPI_SUCCESS;
}
/*
* Gets the current offset into the file. Since views are not currently
* supported, this is the byte offset into the file.
*/
int MPI_File_get_position(MPI_File fh, MPI_Offset *offset)
{
hdfsFile_wrapper *fh_w;
tOffset cur_off;
status("File_get_byte_offset called.\n");
if (!fh || !offset)
return MPI_ERR_ARG;
fh_w = (hdfsFile_wrapper*)fh;
if (fh_w->magic != HDFSFILEMAGIC)
{
int (*real_MPI_File_get_position)(MPI_File, MPI_Offset *) = NULL;
real_MPI_File_get_position = dlsym(RTLD_NEXT, "MPI_File_get_position");
if (!real_MPI_File_get_position) {
fprintf(stderr, "Failed to load actual MPI_File_get_position location.\n");
return MPI_ERR_OTHER;
}
status("Passing File_get_position to actual MPI function.\n");
return real_MPI_File_get_position(fh, offset);
}
cur_off = hdfsTell(fh_w->fs, fh_w->file);
if (cur_off == -1) {
fprintf(stderr, "Failed to get position in hdfs file.\n");
return MPI_ERR_FILE;
}
*offset = cur_off;
return MPI_SUCCESS;
}
int MPI_File_get_byte_offset(MPI_File fh, MPI_Offset offset, MPI_Offset *disp)
{
hdfsFile_wrapper *fh_w;
if (!fh)
return MPI_ERR_ARG;
fh_w = (hdfsFile_wrapper*)fh;
if (fh_w->magic != HDFSFILEMAGIC)
{
int (*real_MPI_File_get_byte_offset)(MPI_File, MPI_Offset, MPI_Offset *) = NULL;
real_MPI_File_get_byte_offset = dlsym(RTLD_NEXT, "MPI_File_get_byte_offset");
if (!real_MPI_File_get_byte_offset) {
fprintf(stderr, "Failed to load actual MPI_File_get_byte_offset location.\n");
return MPI_ERR_OTHER;
}
status("Passing File_get_byte_offset to actual MPI function.\n");
return real_MPI_File_get_byte_offset(fh, offset, disp);
}
NOT_IMPLEMENTED;
}