Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
d8a1ceb03c54beb8c220079dc2919018e58285cb
[simgrid.git] / src / smpi / bindings / smpi_pmpi_file.cpp
1 /* Copyright (c) 2007-2019. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "private.hpp"
7
8 #include "smpi_file.hpp"
9 #include "smpi_datatype.hpp"
10
11 extern MPI_Errhandler SMPI_default_File_Errhandler;
12
13 int PMPI_File_open(MPI_Comm comm, const char *filename, int amode, MPI_Info info, MPI_File *fh){
14   if (comm == MPI_COMM_NULL)
15     return MPI_ERR_COMM;
16   if (filename == nullptr)
17     return MPI_ERR_FILE;
18   if (amode < 0)
19     return MPI_ERR_AMODE;
20   smpi_bench_end();
21   *fh =  new simgrid::smpi::File(comm, filename, amode, info);
22   smpi_bench_begin();
23   if ((*fh)->size() != 0 && (amode & MPI_MODE_EXCL)){
24     delete fh;
25     return MPI_ERR_AMODE;
26   }
27   if(amode & MPI_MODE_APPEND)
28     (*fh)->seek(0,MPI_SEEK_END);
29   return MPI_SUCCESS;
30 }
31
32 int PMPI_File_close(MPI_File *fh){
33   if (fh==nullptr)
34     return MPI_ERR_ARG;
35   smpi_bench_end();
36   int ret = simgrid::smpi::File::close(fh);
37   *fh = MPI_FILE_NULL;
38   smpi_bench_begin();
39   return ret;
40 }
41 #define CHECK_FILE(fh)                                                                                                 \
42   if ((fh) == MPI_FILE_NULL)                                                                                           \
43     return MPI_ERR_FILE;
44 #define CHECK_BUFFER(buf, count)                                                                                       \
45   if ((buf) == nullptr && (count) > 0)                                                                                 \
46     return MPI_ERR_BUFFER;
47 #define CHECK_COUNT(count)                                                                                             \
48   if ((count) < 0)                                                                                                     \
49     return MPI_ERR_COUNT;
50 #define CHECK_OFFSET(offset)                                                                                           \
51   if ((offset) < 0)                                                                                                    \
52     return MPI_ERR_DISP;
53 #define CHECK_DATATYPE(datatype, count)                                                                                \
54   if ((datatype) == MPI_DATATYPE_NULL && (count) > 0)                                                                  \
55     return MPI_ERR_TYPE;
56 #define CHECK_STATUS(status)                                                                                           \
57   if ((status) == nullptr)                                                                                             \
58     return MPI_ERR_ARG;
59 #define CHECK_FLAGS(fh)                                                                                                \
60   if ((fh)->flags() & MPI_MODE_SEQUENTIAL)                                                                             \
61     return MPI_ERR_AMODE;
62 #define CHECK_RDONLY(fh)                                                                                               \
63   if ((fh)->flags() & MPI_MODE_RDONLY)                                                                                 \
64     return MPI_ERR_AMODE;
65
66 #define PASS_ZEROCOUNT(count)                                                                                          \
67   if ((count) == 0) {                                                                                                  \
68     status->count = 0;                                                                                                 \
69     return MPI_SUCCESS;                                                                                                \
70   }
71
72 int PMPI_File_seek(MPI_File fh, MPI_Offset offset, int whence){
73   CHECK_FILE(fh)
74   smpi_bench_end();
75   int ret = fh->seek(offset,whence);
76   smpi_bench_begin();
77   return ret;
78 }
79
80 int PMPI_File_seek_shared(MPI_File fh, MPI_Offset offset, int whence){
81   CHECK_FILE(fh)
82   smpi_bench_end();
83   int ret = fh->seek_shared(offset,whence);
84   smpi_bench_begin();
85   return ret;
86 }
87
88 int PMPI_File_get_position(MPI_File fh, MPI_Offset* offset){
89   if (offset==nullptr)
90     return MPI_ERR_DISP;
91   smpi_bench_end();
92   int ret = fh->get_position(offset);
93   smpi_bench_begin();
94   return ret;
95 }
96
97 int PMPI_File_get_position_shared(MPI_File fh, MPI_Offset* offset){
98   CHECK_FILE(fh)
99   if (offset==nullptr)
100     return MPI_ERR_DISP;
101   smpi_bench_end();
102   int ret = fh->get_position_shared(offset);
103   smpi_bench_begin();
104   return ret;
105 }
106
107 int PMPI_File_read(MPI_File fh, void *buf, int count,MPI_Datatype datatype, MPI_Status *status){
108   CHECK_FILE(fh)
109   CHECK_BUFFER(buf, count)
110   CHECK_COUNT(count)
111   CHECK_DATATYPE(datatype, count)
112   CHECK_STATUS(status)
113   CHECK_FLAGS(fh)
114   PASS_ZEROCOUNT(count)
115   smpi_bench_end();
116   int rank_traced = simgrid::s4u::this_actor::get_pid();
117   TRACE_smpi_comm_in(rank_traced, __func__, new simgrid::instr::CpuTIData("IO - read", count * datatype->size()));
118   int ret = simgrid::smpi::File::read(fh, buf, count, datatype, status);
119   TRACE_smpi_comm_out(rank_traced);
120   smpi_bench_begin();
121   return ret;
122 }
123
124 int PMPI_File_read_shared(MPI_File fh, void *buf, int count,MPI_Datatype datatype, MPI_Status *status){
125   CHECK_FILE(fh)
126   CHECK_BUFFER(buf, count)
127   CHECK_COUNT(count)
128   CHECK_DATATYPE(datatype, count)
129   CHECK_STATUS(status)
130   CHECK_FLAGS(fh)
131   PASS_ZEROCOUNT(count)
132   smpi_bench_end();
133   int rank_traced = simgrid::s4u::this_actor::get_pid();
134   TRACE_smpi_comm_in(rank_traced, __func__,
135                      new simgrid::instr::CpuTIData("IO - read_shared", count * datatype->size()));
136   int ret = simgrid::smpi::File::read_shared(fh, buf, count, datatype, status);
137   TRACE_smpi_comm_out(rank_traced);
138   smpi_bench_begin();
139   return ret;
140 }
141
142 int PMPI_File_write(MPI_File fh, const void *buf, int count,MPI_Datatype datatype, MPI_Status *status){
143   CHECK_FILE(fh)
144   CHECK_BUFFER(buf, count)
145   CHECK_COUNT(count)
146   CHECK_DATATYPE(datatype, count)
147   CHECK_STATUS(status)
148   CHECK_FLAGS(fh)
149   CHECK_RDONLY(fh)
150   PASS_ZEROCOUNT(count)
151   smpi_bench_end();
152   int rank_traced = simgrid::s4u::this_actor::get_pid();
153   TRACE_smpi_comm_in(rank_traced, __func__, new simgrid::instr::CpuTIData("IO - write", count * datatype->size()));
154   int ret = simgrid::smpi::File::write(fh, const_cast<void*>(buf), count, datatype, status);
155   TRACE_smpi_comm_out(rank_traced);
156   smpi_bench_begin();
157   return ret;
158 }
159
160 int PMPI_File_write_shared(MPI_File fh, const void *buf, int count,MPI_Datatype datatype, MPI_Status *status){
161   CHECK_FILE(fh)
162   CHECK_BUFFER(buf, count)
163   CHECK_COUNT(count)
164   CHECK_DATATYPE(datatype, count)
165   CHECK_STATUS(status)
166   CHECK_FLAGS(fh)
167   CHECK_RDONLY(fh)
168   PASS_ZEROCOUNT(count)
169   smpi_bench_end();
170   int rank_traced = simgrid::s4u::this_actor::get_pid();
171   TRACE_smpi_comm_in(rank_traced, __func__,
172                      new simgrid::instr::CpuTIData("IO - write_shared", count * datatype->size()));
173   int ret = simgrid::smpi::File::write_shared(fh, buf, count, datatype, status);
174   TRACE_smpi_comm_out(rank_traced);
175   smpi_bench_begin();
176   return ret;
177 }
178
179 int PMPI_File_read_all(MPI_File fh, void *buf, int count,MPI_Datatype datatype, MPI_Status *status){
180   CHECK_FILE(fh)
181   CHECK_BUFFER(buf, count)
182   CHECK_COUNT(count)
183   CHECK_DATATYPE(datatype, count)
184   CHECK_STATUS(status)
185   CHECK_FLAGS(fh)
186   smpi_bench_end();
187   int rank_traced = simgrid::s4u::this_actor::get_pid();
188   TRACE_smpi_comm_in(rank_traced, __func__, new simgrid::instr::CpuTIData("IO - read_all", count * datatype->size()));
189   int ret = fh->op_all<simgrid::smpi::File::read>(buf, count, datatype, status);
190   TRACE_smpi_comm_out(rank_traced);
191   smpi_bench_begin();
192   return ret;
193 }
194
195 int PMPI_File_read_ordered(MPI_File fh, void *buf, int count,MPI_Datatype datatype, MPI_Status *status){
196   CHECK_FILE(fh)
197   CHECK_BUFFER(buf, count)
198   CHECK_COUNT(count)
199   CHECK_DATATYPE(datatype, count)
200   CHECK_STATUS(status)
201   CHECK_FLAGS(fh)
202   smpi_bench_end();
203   int rank_traced = simgrid::s4u::this_actor::get_pid();
204   TRACE_smpi_comm_in(rank_traced, __func__,
205                      new simgrid::instr::CpuTIData("IO - read_ordered", count * datatype->size()));
206   int ret = simgrid::smpi::File::read_ordered(fh, buf, count, datatype, status);
207   TRACE_smpi_comm_out(rank_traced);
208   smpi_bench_begin();
209   return ret;
210 }
211
212 int PMPI_File_write_all(MPI_File fh, const void *buf, int count,MPI_Datatype datatype, MPI_Status *status){
213   CHECK_FILE(fh)
214   CHECK_BUFFER(buf, count)
215   CHECK_COUNT(count)
216   CHECK_DATATYPE(datatype, count)
217   CHECK_STATUS(status)
218   CHECK_FLAGS(fh)
219   CHECK_RDONLY(fh)
220   smpi_bench_end();
221   int rank_traced = simgrid::s4u::this_actor::get_pid();
222   TRACE_smpi_comm_in(rank_traced, __func__, new simgrid::instr::CpuTIData("IO - write_all", count * datatype->size()));
223   int ret = fh->op_all<simgrid::smpi::File::write>(const_cast<void*>(buf), count, datatype, status);
224   TRACE_smpi_comm_out(rank_traced);
225   smpi_bench_begin();
226   return ret;
227 }
228
229 int PMPI_File_write_ordered(MPI_File fh, const void *buf, int count,MPI_Datatype datatype, MPI_Status *status){
230   CHECK_FILE(fh)
231   CHECK_BUFFER(buf, count)
232   CHECK_COUNT(count)
233   CHECK_DATATYPE(datatype, count)
234   CHECK_STATUS(status)
235   CHECK_FLAGS(fh)
236   CHECK_RDONLY(fh)
237   smpi_bench_end();
238   int rank_traced = simgrid::s4u::this_actor::get_pid();
239   TRACE_smpi_comm_in(rank_traced, __func__,
240                      new simgrid::instr::CpuTIData("IO - write_ordered", count * datatype->size()));
241   int ret = simgrid::smpi::File::write_ordered(fh, buf, count, datatype, status);
242   TRACE_smpi_comm_out(rank_traced);
243   smpi_bench_begin();
244   return ret;
245 }
246
247 int PMPI_File_read_at(MPI_File fh, MPI_Offset offset, void *buf, int count,MPI_Datatype datatype, MPI_Status *status){
248   CHECK_FILE(fh)
249   CHECK_BUFFER(buf, count)
250   CHECK_OFFSET(offset)
251   CHECK_COUNT(count)
252   CHECK_DATATYPE(datatype, count)
253   CHECK_STATUS(status)
254   CHECK_FLAGS(fh)
255   PASS_ZEROCOUNT(count);
256   smpi_bench_end();
257   int rank_traced = simgrid::s4u::this_actor::get_pid();
258   TRACE_smpi_comm_in(rank_traced, __func__, new simgrid::instr::CpuTIData("IO - read", count * datatype->size()));
259   int ret = fh->seek(offset,MPI_SEEK_SET);
260   if(ret!=MPI_SUCCESS)
261     return ret;
262   ret = simgrid::smpi::File::read(fh, buf, count, datatype, status);
263   TRACE_smpi_comm_out(rank_traced);
264   smpi_bench_begin();
265   return ret;
266 }
267
268 int PMPI_File_read_at_all(MPI_File fh, MPI_Offset offset, void *buf, int count,MPI_Datatype datatype, MPI_Status *status){
269   CHECK_FILE(fh)
270   CHECK_BUFFER(buf, count)
271   CHECK_OFFSET(offset)
272   CHECK_COUNT(count)
273   CHECK_DATATYPE(datatype, count)
274   CHECK_STATUS(status)
275   CHECK_FLAGS(fh)
276   smpi_bench_end();
277   int rank_traced = simgrid::s4u::this_actor::get_pid();
278   TRACE_smpi_comm_in(rank_traced, __func__,
279                      new simgrid::instr::CpuTIData("IO - read_at_all", count * datatype->size()));
280   int ret = fh->seek(offset,MPI_SEEK_SET);
281   if(ret!=MPI_SUCCESS)
282     return ret;
283   ret = fh->op_all<simgrid::smpi::File::read>(buf, count, datatype, status);
284   TRACE_smpi_comm_out(rank_traced);
285   smpi_bench_begin();
286   return ret;
287 }
288
289 int PMPI_File_write_at(MPI_File fh, MPI_Offset offset, const void *buf, int count,MPI_Datatype datatype, MPI_Status *status){
290   CHECK_FILE(fh)
291   CHECK_BUFFER(buf, count)
292   CHECK_OFFSET(offset)
293   CHECK_COUNT(count)
294   CHECK_DATATYPE(datatype, count)
295   CHECK_STATUS(status)
296   CHECK_FLAGS(fh)
297   CHECK_RDONLY(fh)
298   PASS_ZEROCOUNT(count);
299   smpi_bench_end();
300   int rank_traced = simgrid::s4u::this_actor::get_pid();
301   TRACE_smpi_comm_in(rank_traced, __func__, new simgrid::instr::CpuTIData("IO - write", count * datatype->size()));
302   int ret = fh->seek(offset,MPI_SEEK_SET);
303   if(ret!=MPI_SUCCESS)
304     return ret;
305   ret = simgrid::smpi::File::write(fh, const_cast<void*>(buf), count, datatype, status);
306   TRACE_smpi_comm_out(rank_traced);
307   smpi_bench_begin();
308   return ret;
309 }
310
311 int PMPI_File_write_at_all(MPI_File fh, MPI_Offset offset, const void *buf, int count,MPI_Datatype datatype, MPI_Status *status){
312   CHECK_FILE(fh)
313   CHECK_BUFFER(buf, count)
314   CHECK_OFFSET(offset)
315   CHECK_COUNT(count)
316   CHECK_DATATYPE(datatype, count)
317   CHECK_STATUS(status)
318   CHECK_FLAGS(fh)
319   CHECK_RDONLY(fh)
320   smpi_bench_end();
321   int rank_traced = simgrid::s4u::this_actor::get_pid();
322   TRACE_smpi_comm_in(rank_traced, __func__,
323                      new simgrid::instr::CpuTIData("IO - write_at_all", count * datatype->size()));
324   int ret = fh->seek(offset,MPI_SEEK_SET);
325   if(ret!=MPI_SUCCESS)
326     return ret;
327   ret = fh->op_all<simgrid::smpi::File::write>(const_cast<void*>(buf), count, datatype, status);
328   TRACE_smpi_comm_out(rank_traced);
329   smpi_bench_begin();
330   return ret;
331 }
332
333 int PMPI_File_delete(const char *filename, MPI_Info info){
334   if (filename == nullptr)
335     return MPI_ERR_FILE;
336   smpi_bench_end();
337   int ret = simgrid::smpi::File::del(filename, info);
338   smpi_bench_begin();
339   return ret;
340 }
341
342 int PMPI_File_get_info(MPI_File  fh, MPI_Info* info)
343 {
344   CHECK_FILE(fh)
345   *info = fh->info();
346   return MPI_SUCCESS;
347 }
348
349 int PMPI_File_set_info(MPI_File  fh, MPI_Info info)
350 {
351   CHECK_FILE(fh)
352   fh->set_info(info);
353   return MPI_SUCCESS;
354 }
355
356 int PMPI_File_get_size(MPI_File  fh, MPI_Offset* size)
357 {
358   CHECK_FILE(fh)
359   *size = fh->size();
360   return MPI_SUCCESS;
361 }
362
363 int PMPI_File_get_amode(MPI_File  fh, int* amode)
364 {
365   CHECK_FILE(fh)
366   *amode = fh->flags();
367   return MPI_SUCCESS;
368 }
369
370 int PMPI_File_get_group(MPI_File  fh, MPI_Group* group)
371 {
372   CHECK_FILE(fh)
373   *group = fh->comm()->group();
374   return MPI_SUCCESS;
375 }
376
377 int PMPI_File_sync(MPI_File  fh)
378 {
379   CHECK_FILE(fh)
380   fh->sync();
381   return MPI_SUCCESS;
382 }
383
384 int PMPI_File_create_errhandler(MPI_File_errhandler_function* function, MPI_Errhandler* errhandler){
385   *errhandler=new simgrid::smpi::Errhandler(function);
386   return MPI_SUCCESS;
387 }
388
389 int PMPI_File_get_errhandler(MPI_File file, MPI_Errhandler* errhandler){
390   if (errhandler==nullptr){
391     return MPI_ERR_ARG;
392   } else if (file == MPI_FILE_NULL) {
393     *errhandler = SMPI_default_File_Errhandler;
394     return MPI_SUCCESS;
395   }
396   *errhandler=file->errhandler();
397   return MPI_SUCCESS;
398 }
399
400 int PMPI_File_set_errhandler(MPI_File file, MPI_Errhandler errhandler){
401   if (errhandler==nullptr){
402     return MPI_ERR_ARG;
403   } else if (file == MPI_FILE_NULL) {
404     SMPI_default_File_Errhandler = errhandler;
405     return MPI_SUCCESS;
406   }
407   file->set_errhandler(errhandler);
408   return MPI_SUCCESS;
409 }
410
411 int PMPI_File_call_errhandler(MPI_File file,int errorcode){
412   if (file == nullptr) {
413     return MPI_ERR_WIN;
414   }
415   file->errhandler()->call(file, errorcode);
416   return MPI_SUCCESS;
417 }