-
Notifications
You must be signed in to change notification settings - Fork 1
/
MPISync.c
304 lines (232 loc) · 7.8 KB
/
MPISync.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
/*
* Synchronous I/O functions
*/
#include "MPIHook.h"
/*
* Performs a read at a specific offset in the file
*/
int MPI_File_read_at(MPI_File fh, MPI_Offset offset, void *buf,
int count, MPI_Datatype datatype, MPI_Status *status)
{
hdfsFile_wrapper *fh_w;
int ret, size;
if (!fh)
return MPI_ERR_ARG;
fh_w = (hdfsFile_wrapper*)fh;
if (fh_w->magic != HDFSFILEMAGIC)
{
int (*real_MPI_File_read_at)(MPI_File, MPI_Offset, void*, int, MPI_Datatype, MPI_Status*) = NULL;
real_MPI_File_read_at = dlsym(RTLD_NEXT, "MPI_File_read_at");
if (!real_MPI_File_read_at) {
fprintf(stderr, "Failed to load actual MPI_File_close location.\n");
return MPI_ERR_OTHER;
}
status("Passing File_read_at to actual MPI function.\n");
return real_MPI_File_read_at(fh, offset, buf, count, datatype, status);
}
status("HDFS file found in File_read_at.\n");
#ifdef NO_MPI
size = 1;
#else
MPI_Type_size(datatype, &size);
#endif
status("Got type size: %d.\n", size);
status("Offset: %d\n", offset);
ret = hdfsPread(fh_w->fs, fh_w->file, offset, buf, size * count);
if (ret == -1)
return MPI_ERR_IO;
return MPI_SUCCESS;
}
int MPI_File_read_at_all(MPI_File fh, MPI_Offset offset, void *buf,
int count, MPI_Datatype datatype, MPI_Status *status)
{
hdfsFile_wrapper *fh_w;
if (!fh)
return MPI_ERR_ARG;
fh_w = (hdfsFile_wrapper*)fh;
if (fh_w->magic != HDFSFILEMAGIC)
{
int (*real_MPI_File_read_at_all)(MPI_File, MPI_Offset, void *, int, MPI_Datatype, MPI_Status*) = NULL;
real_MPI_File_read_at_all = dlsym(RTLD_NEXT, "MPI_File_read_at_all");
if (!real_MPI_File_read_at_all) {
fprintf(stderr, "Failed to load actual MPI_File_read_at_all location.\n");
return MPI_ERR_OTHER;
}
status("Passing File_readat_all to actual MPI function.\n");
return real_MPI_File_read_at_all(fh, offset, buf, count, datatype, status);
}
NOT_IMPLEMENTED;
}
int MPI_File_write_at(MPI_File fh, MPI_Offset offset, MPIHDFS_CONST void *buf,
int count, MPI_Datatype datatype, MPI_Status *status)
{
hdfsFile_wrapper *fh_w;
if (!fh)
return MPI_ERR_ARG;
fh_w = (hdfsFile_wrapper*)fh;
if (fh_w->magic != HDFSFILEMAGIC)
{
int (*real_MPI_File_write_at)(MPI_File, MPI_Offset, MPIHDFS_CONST void *, int, MPI_Datatype, MPI_Status*) = NULL;
real_MPI_File_write_at = dlsym(RTLD_NEXT, "MPI_File_write_at");
if (!real_MPI_File_write_at) {
fprintf(stderr, "Failed to load actual MPI_File_write_at location.\n");
return MPI_ERR_OTHER;
}
status("Passing File_write_at to actual MPI function.\n");
return real_MPI_File_write_at(fh, offset, buf, count, datatype, status);
}
NOT_IMPLEMENTED;
}
int MPI_File_write_at_all(MPI_File fh, MPI_Offset offset, MPIHDFS_CONST void *buf,
int count, MPI_Datatype datatype, MPI_Status *status)
{
hdfsFile_wrapper *fh_w;
if (!fh)
return MPI_ERR_ARG;
fh_w = (hdfsFile_wrapper*)fh;
if (fh_w->magic != HDFSFILEMAGIC)
{
int (*real_MPI_File_write_at_all)(MPI_File, MPI_Offset, MPIHDFS_CONST void *, int, MPI_Datatype, MPI_Status*) = NULL;
real_MPI_File_write_at_all = dlsym(RTLD_NEXT, "MPI_File_write_at_all");
if (!real_MPI_File_write_at_all) {
fprintf(stderr, "Failed to load actual MPI_File_write_at_all location.\n");
return MPI_ERR_OTHER;
}
status("Passing File_write_at_all to actual MPI function.\n");
return real_MPI_File_write_at_all(fh, offset, buf, count, datatype, status);
}
NOT_IMPLEMENTED;
}
int MPI_File_iread_at(MPI_File fh, MPI_Offset offset, void *buf,
int count, MPI_Datatype datatype, MPIO_Request *request) { NOT_IMPLEMENTED; }
int MPI_File_iwrite_at(MPI_File fh, MPI_Offset offset, MPIHDFS_CONST void *buf,
int count, MPI_Datatype datatype, MPIO_Request *request) { NOT_IMPLEMENTED; }
/*
* Performs a read based on the current position in the file
*/
int MPI_File_read(MPI_File fh, void *buf, int count, MPI_Datatype datatype, MPI_Status *status)
{
hdfsFile_wrapper *fh_w;
int ret, size;
if (!fh)
return MPI_ERR_ARG;
fh_w = (hdfsFile_wrapper*)fh;
if (fh_w->magic != HDFSFILEMAGIC)
{
int (*real_MPI_File_read)(MPI_File, void*, int, MPI_Datatype, MPI_Status*) = NULL;
real_MPI_File_read = dlsym(RTLD_NEXT, "MPI_File_read");
if (!real_MPI_File_read) {
fprintf(stderr, "Failed to load actual MPI_File_read location.\n");
return MPI_ERR_OTHER;
}
status("Passing File_read to actual MPI function.\n");
return real_MPI_File_read(fh, buf, count, datatype, status);
}
status("HDFS file found in File_read_at.\n");
#ifdef NO_MPI
size = 1;
#else
MPI_Type_size(datatype, &size);
#endif
status("Got type size: %d.\n", size);
ret = hdfsRead(fh_w->fs, fh_w->file, buf, size * count);
if (ret == -1)
return MPI_ERR_IO;
return MPI_SUCCESS;
}
int MPI_File_read_all(MPI_File fh, void *buf, int count, MPI_Datatype datatype, MPI_Status *status)
{
hdfsFile_wrapper *fh_w;
if (!fh)
return MPI_ERR_ARG;
fh_w = (hdfsFile_wrapper*)fh;
if (fh_w->magic != HDFSFILEMAGIC)
{
int (*real_MPI_File_read_all)(MPI_File, void *, int, MPI_Datatype, MPI_Status*) = NULL;
real_MPI_File_read_all = dlsym(RTLD_NEXT, "MPI_File_read_all");
if (!real_MPI_File_read_all) {
fprintf(stderr, "Failed to load actual MPI_File_read_all location.\n");
return MPI_ERR_OTHER;
}
status("Passing File_read_all to actual MPI function.\n");
return real_MPI_File_read_all(fh, buf, count, datatype, status);
}
NOT_IMPLEMENTED;
}
/*
* Performs a write based on the current position in the file
*/
int MPI_File_write(MPI_File fh, MPIHDFS_CONST void *buf, int count, MPI_Datatype datatype, MPI_Status *status)
{
hdfsFile_wrapper *fh_w;
int ret, size, mode;
if (!fh)
return MPI_ERR_ARG;
fh_w = (hdfsFile_wrapper*)fh;
if (fh_w->magic != HDFSFILEMAGIC)
{
int (*real_MPI_File_write)(MPI_File, MPIHDFS_CONST void*, int, MPI_Datatype, MPI_Status*) = NULL;
real_MPI_File_write = dlsym(RTLD_NEXT, "MPI_File_write");
if (!real_MPI_File_write) {
fprintf(stderr, "Failed to load actual MPI_File_close location.\n");
return MPI_ERR_OTHER;
}
status("Passing File_write to actual MPI function.\n");
return real_MPI_File_write(fh, buf, count, datatype, status);
}
status("HDFS file found in File_write.\n");
if (fh_w->file != NULL || !(fh_w->amode & MPI_MODE_WRONLY)) {
fprintf(stderr, "Write called on a file not open for writing.\n");
return MPI_ERR_AMODE;
}
if (!hdfsExists(fh_w->fs, fh_w->filename)) {
status("Opening file in append mode.\n");
mode = O_WRONLY | O_APPEND;
}
else {
status("Opening file in create mode.\n");
mode = O_WRONLY;
}
fh_w->file = hdfsOpenFile(fh_w->fs, fh_w->filename, mode, 0, 0, 0);
if (!fh_w->file) {
fprintf(stderr, "Failed to open hdfs file for writing.\n");
return -1;
}
status("Successfully opened hdfs file for writing.\n");
#ifdef NO_MPI
size = 1;
#else
MPI_Type_size(datatype, &size);
#endif
ret = hdfsWrite(fh_w->fs, fh_w->file, buf, size * count);
if (ret == -1)
fprintf(stderr, "Failed to write to hdfs file.\n");
if (hdfsCloseFile(fh_w->fs, fh_w->file)) {
fh_w->file = NULL;
fprintf(stderr, "Failed to close file after hdfs write. errno = %d\n", errno);
return MPI_ERR_FILE;
}
fh_w->file = NULL;
if (ret == -1)
return MPI_ERR_IO;
return MPI_SUCCESS;
}
int MPI_File_write_all(MPI_File fh, MPIHDFS_CONST void *buf, int count, MPI_Datatype datatype, MPI_Status *status)
{
hdfsFile_wrapper *fh_w;
if (!fh)
return MPI_ERR_ARG;
fh_w = (hdfsFile_wrapper*)fh;
if (fh_w->magic != HDFSFILEMAGIC)
{
int (*real_MPI_File_write_all)(MPI_File, MPIHDFS_CONST void *, int, MPI_Datatype, MPI_Status*) = NULL;
real_MPI_File_write_all = dlsym(RTLD_NEXT, "MPI_File_write_all");
if (!real_MPI_File_write_all) {
fprintf(stderr, "Failed to load actual MPI_File_write_all location.\n");
return MPI_ERR_OTHER;
}
status("Passing File_write_all to actual MPI function.\n");
return real_MPI_File_write_all(fh, buf, count, datatype, status);
}
NOT_IMPLEMENTED;
}