forked from Kimahriman/mpihdfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMPIFile.c
328 lines (267 loc) · 7.99 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
/*
* Helper functions that aren't direct I/O.
*/
#include "MPIHook.h"
int MPI_File_open(MPI_Comm comm, char *filename, int amode,
MPI_Info info, MPI_File *fh)
{
char fsname[BUF_SIZE];
char hdfs_filename[BUF_SIZE];
hdfsFile_wrapper *fh_w;
hdfsFile file = NULL;
struct hdfsBuilder *builder;
hdfsFS fs;
int mode;
status("File_open called.\n");
#ifndef DEFAULT
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 -1;
}
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 -1;
}
fh_w = malloc(sizeof(hdfsFile_wrapper));
if (!fh) {
fprintf(stderr, "Malloc failed.\n");
return -1;
}
fh_w->magic = HDFSFILEMAGIC;
builder = hdfsNewBuilder();
if (!builder) {
fprintf(stderr, "Failed to make new hdfsBuilder.\n");
free(fh_w);
return -1;
}
#ifndef DEFAULT
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 -1;
}
// 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 -1;
}
}
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 -1;
}
strcpy(fh_w->filename, hdfs_filename);
fh_w->fs = fs;
fh_w->file = file;
fh_w->mode = mode;
fh_w->amode = amode;
*fh = (MPI_File)fh_w;
status("File_open returning successfully.\n");
return MPI_SUCCESS;
}
int MPI_File_close(MPI_File *fh)
{
hdfsFile_wrapper *fh_w;
int ret;
status("File_close called.\n");
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 -1;
}
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 -1;
}
}
if (fh_w) {
if (fh_w->filename)
free(fh_w->filename);
free(fh_w);
}
status("File_close returning successfully.\n");
return MPI_SUCCESS;
}
int MPI_File_delete(char *filename, MPI_Info info)
{
char fsname[BUF_SIZE];
char hdfs_filename[BUF_SIZE];
hdfsFile file;
struct hdfsBuilder *builder;
hdfsFS fs;
status("File_delete called.\n");
if (hdfs_url_parse(filename, fsname, BUF_SIZE, hdfs_filename, BUF_SIZE)) {
int (*real_MPI_File_delete)(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 -1;
}
status("Passing File_delete call to actual MPI function.\n");
return real_MPI_File_delete(filename, info);
}
builder = hdfsNewBuilder();
if (!builder) {
fprintf(stderr, "Failed to make new hdfsBuilder.\n");
return -1;
}
hdfsBuilderSetNameNode(builder, fsname);
fs = hdfsBuilderConnect(builder);
if (!fs) {
fprintf(stderr, "Failed to connect to hdfs.\n");
return -1;
}
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; }
int MPI_File_get_size(MPI_File fh, MPI_Offset *size)
{
hdfsFile_wrapper *fh_w;
status("File_get_size called.\n");
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; }
int MPI_File_get_amode(MPI_File fh, int *amode)
{
hdfsFile_wrapper *fh_w;
status("File_get_amode called.\n");
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) { NOT_IMPLEMENTED; }
int MPI_File_get_info(MPI_File fh, MPI_Info *info) { NOT_IMPLEMENTED; }
/* Section 9.3 */
int MPI_File_set_view(MPI_File fh, MPI_Offset disp, MPI_Datatype etype,
MPI_Datatype filetype, char *datarep, MPI_Info info) { NOT_IMPLEMENTED; }
int MPI_File_get_view(MPI_File fh, MPI_Offset *disp,
MPI_Datatype *etype, MPI_Datatype *filetype, char *datarep) { NOT_IMPLEMENTED; }
int MPI_File_seek(MPI_File fh, MPI_Offset offset, int whence)
{
hdfsFile_wrapper *fh_w;
status("File_seek called.\n");
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 -1;
}
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 -1;
}
}
else if (whence == MPI_SEEK_CUR) {
tOffset cur_off = hdfsTell(fh_w->fs, fh_w->file);
if (offset == -1) {
fprintf(stderr, "Failed to tell for seek in hdfs file.\n");
return -1;
}
if (hdfsSeek(fh_w->fs, fh_w->file, cur_off + offset)) {
fprintf(stderr, "Failed to seek in hdfs file.\n");
return -1;
}
}
else {
fprintf(stderr, "Operation not supported.\n");
return -1;
}
return MPI_SUCCESS;
}
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");
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 -1;
}
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 -1;
}
*offset = cur_off;
return MPI_SUCCESS;
}
int MPI_File_get_byte_offset(MPI_File fh, MPI_Offset offset, MPI_Offset *disp) { NOT_IMPLEMENTED; }