Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
3680ca94171c09a31a55bdcb396bacfca1d35682
[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
81 int PMPI_File_seek_shared(MPI_File fh, MPI_Offset offset, int whence){
82   CHECK_FILE(fh)
83   smpi_bench_end();
84   int ret = fh->seek_shared(offset,whence);
85   smpi_bench_begin();
86   return ret;
87
88 }
89
90 int PMPI_File_get_position(MPI_File fh, MPI_Offset* offset){
91   if (offset==nullptr)
92     return MPI_ERR_DISP;
93   smpi_bench_end();
94   int ret = fh->get_position(offset);
95   smpi_bench_begin();
96   return ret;
97 }
98
99 int PMPI_File_get_position_shared(MPI_File fh, MPI_Offset* offset){
100   CHECK_FILE(fh)
101   if (offset==nullptr)
102     return MPI_ERR_DISP;
103   smpi_bench_end();
104   int ret = fh->get_position_shared(offset);
105   smpi_bench_begin();
106   return ret;
107 }
108
109 int PMPI_File_read(MPI_File fh, void *buf, int count,MPI_Datatype datatype, MPI_Status *status){
110   CHECK_FILE(fh)
111   CHECK_BUFFER(buf, count)
112   CHECK_COUNT(count)
113   CHECK_DATATYPE(datatype, count)
114   CHECK_STATUS(status)
115   CHECK_FLAGS(fh)
116   PASS_ZEROCOUNT(count)
117   smpi_bench_end();
118   int rank_traced = simgrid::s4u::this_actor::get_pid();
119   TRACE_smpi_comm_in(rank_traced, __func__, new simgrid::instr::CpuTIData("IO - read", count * datatype->size()));
120   int ret = simgrid::smpi::File::read(fh, buf, count, datatype, status);
121   TRACE_smpi_comm_out(rank_traced);
122   smpi_bench_begin();
123   return ret;
124 }
125
126 int PMPI_File_read_shared(MPI_File fh, void *buf, int count,MPI_Datatype datatype, MPI_Status *status){
127   CHECK_FILE(fh)
128   CHECK_BUFFER(buf, count)
129   CHECK_COUNT(count)
130   CHECK_DATATYPE(datatype, count)
131   CHECK_STATUS(status)
132   CHECK_FLAGS(fh)
133   PASS_ZEROCOUNT(count)
134   smpi_bench_end();
135   int rank_traced = simgrid::s4u::this_actor::get_pid();
136   TRACE_smpi_comm_in(rank_traced, __func__,
137                      new simgrid::instr::CpuTIData("IO - read_shared", count * datatype->size()));
138   int ret = simgrid::smpi::File::read_shared(fh, buf, count, datatype, status);
139   TRACE_smpi_comm_out(rank_traced);
140   smpi_bench_begin();
141   return ret;
142 }
143
144 int PMPI_File_write(MPI_File fh, const void *buf, int count,MPI_Datatype datatype, MPI_Status *status){
145   CHECK_FILE(fh)
146   CHECK_BUFFER(buf, count)
147   CHECK_COUNT(count)
148   CHECK_DATATYPE(datatype, count)
149   CHECK_STATUS(status)
150   CHECK_FLAGS(fh)
151   CHECK_RDONLY(fh)
152   PASS_ZEROCOUNT(count)
153   smpi_bench_end();
154   int rank_traced = simgrid::s4u::this_actor::get_pid();
155   TRACE_smpi_comm_in(rank_traced, __func__, new simgrid::instr::CpuTIData("IO - write", count * datatype->size()));
156   int ret = simgrid::smpi::File::write(fh, const_cast<void*>(buf), count, datatype, status);
157   TRACE_smpi_comm_out(rank_traced);
158   smpi_bench_begin();
159   return ret;
160 }
161
162 int PMPI_File_write_shared(MPI_File fh, const void *buf, int count,MPI_Datatype datatype, MPI_Status *status){
163   CHECK_FILE(fh)
164   CHECK_BUFFER(buf, count)
165   CHECK_COUNT(count)
166   CHECK_DATATYPE(datatype, count)
167   CHECK_STATUS(status)
168   CHECK_FLAGS(fh)
169   CHECK_RDONLY(fh)
170   PASS_ZEROCOUNT(count)
171   smpi_bench_end();
172   int rank_traced = simgrid::s4u::this_actor::get_pid();
173   TRACE_smpi_comm_in(rank_traced, __func__,
174                      new simgrid::instr::CpuTIData("IO - write_shared", count * datatype->size()));
175   int ret = simgrid::smpi::File::write_shared(fh, buf, count, datatype, status);
176   TRACE_smpi_comm_out(rank_traced);
177   smpi_bench_begin();
178   return ret;
179 }
180
181 int PMPI_File_read_all(MPI_File fh, void *buf, int count,MPI_Datatype datatype, MPI_Status *status){
182   CHECK_FILE(fh)
183   CHECK_BUFFER(buf, count)
184   CHECK_COUNT(count)
185   CHECK_DATATYPE(datatype, count)
186   CHECK_STATUS(status)
187   CHECK_FLAGS(fh)
188   smpi_bench_end();
189   int rank_traced = simgrid::s4u::this_actor::get_pid();
190   TRACE_smpi_comm_in(rank_traced, __func__, new simgrid::instr::CpuTIData("IO - read_all", count * datatype->size()));
191   int ret = fh->op_all<simgrid::smpi::File::read>(buf, count, datatype, status);
192   TRACE_smpi_comm_out(rank_traced);
193   smpi_bench_begin();
194   return ret;
195 }
196
197 int PMPI_File_read_ordered(MPI_File fh, void *buf, int count,MPI_Datatype datatype, MPI_Status *status){
198   CHECK_FILE(fh)
199   CHECK_BUFFER(buf, count)
200   CHECK_COUNT(count)
201   CHECK_DATATYPE(datatype, count)
202   CHECK_STATUS(status)
203   CHECK_FLAGS(fh)
204   smpi_bench_end();
205   int rank_traced = simgrid::s4u::this_actor::get_pid();
206   TRACE_smpi_comm_in(rank_traced, __func__,
207                      new simgrid::instr::CpuTIData("IO - read_ordered", count * datatype->size()));
208   int ret = simgrid::smpi::File::read_ordered(fh, buf, count, datatype, status);
209   TRACE_smpi_comm_out(rank_traced);
210   smpi_bench_begin();
211   return ret;
212 }
213
214 int PMPI_File_write_all(MPI_File fh, const void *buf, int count,MPI_Datatype datatype, MPI_Status *status){
215   CHECK_FILE(fh)
216   CHECK_BUFFER(buf, count)
217   CHECK_COUNT(count)
218   CHECK_DATATYPE(datatype, count)
219   CHECK_STATUS(status)
220   CHECK_FLAGS(fh)
221   CHECK_RDONLY(fh)
222   smpi_bench_end();
223   int rank_traced = simgrid::s4u::this_actor::get_pid();
224   TRACE_smpi_comm_in(rank_traced, __func__, new simgrid::instr::CpuTIData("IO - write_all", count * datatype->size()));
225   int ret = fh->op_all<simgrid::smpi::File::write>(const_cast<void*>(buf), count, datatype, status);
226   TRACE_smpi_comm_out(rank_traced);
227   smpi_bench_begin();
228   return ret;
229 }
230
231 int PMPI_File_write_ordered(MPI_File fh, const void *buf, int count,MPI_Datatype datatype, MPI_Status *status){
232   CHECK_FILE(fh)
233   CHECK_BUFFER(buf, count)
234   CHECK_COUNT(count)
235   CHECK_DATATYPE(datatype, count)
236   CHECK_STATUS(status)
237   CHECK_FLAGS(fh)
238   CHECK_RDONLY(fh)
239   smpi_bench_end();
240   int rank_traced = simgrid::s4u::this_actor::get_pid();
241   TRACE_smpi_comm_in(rank_traced, __func__,
242                      new simgrid::instr::CpuTIData("IO - write_ordered", count * datatype->size()));
243   int ret = simgrid::smpi::File::write_ordered(fh, buf, count, datatype, status);
244   TRACE_smpi_comm_out(rank_traced);
245   smpi_bench_begin();
246   return ret;
247 }
248
249 int PMPI_File_read_at(MPI_File fh, MPI_Offset offset, void *buf, int count,MPI_Datatype datatype, MPI_Status *status){
250   CHECK_FILE(fh)
251   CHECK_BUFFER(buf, count)
252   CHECK_OFFSET(offset)
253   CHECK_COUNT(count)
254   CHECK_DATATYPE(datatype, count)
255   CHECK_STATUS(status)
256   CHECK_FLAGS(fh)
257   PASS_ZEROCOUNT(count);
258   smpi_bench_end();
259   int rank_traced = simgrid::s4u::this_actor::get_pid();
260   TRACE_smpi_comm_in(rank_traced, __func__, new simgrid::instr::CpuTIData("IO - read", count * datatype->size()));
261   int ret = fh->seek(offset,MPI_SEEK_SET);
262   if(ret!=MPI_SUCCESS)
263     return ret;
264   ret = simgrid::smpi::File::read(fh, buf, count, datatype, status);
265   TRACE_smpi_comm_out(rank_traced);
266   smpi_bench_begin();
267   return ret;
268 }
269
270 int PMPI_File_read_at_all(MPI_File fh, MPI_Offset offset, void *buf, int count,MPI_Datatype datatype, MPI_Status *status){
271   CHECK_FILE(fh)
272   CHECK_BUFFER(buf, count)
273   CHECK_OFFSET(offset)
274   CHECK_COUNT(count)
275   CHECK_DATATYPE(datatype, count)
276   CHECK_STATUS(status)
277   CHECK_FLAGS(fh)
278   smpi_bench_end();
279   int rank_traced = simgrid::s4u::this_actor::get_pid();
280   TRACE_smpi_comm_in(rank_traced, __func__,
281                      new simgrid::instr::CpuTIData("IO - read_at_all", count * datatype->size()));
282   int ret = fh->seek(offset,MPI_SEEK_SET);
283   if(ret!=MPI_SUCCESS)
284     return ret;
285   ret = fh->op_all<simgrid::smpi::File::read>(buf, count, datatype, status);
286   TRACE_smpi_comm_out(rank_traced);
287   smpi_bench_begin();
288   return ret;
289 }
290
291 int PMPI_File_write_at(MPI_File fh, MPI_Offset offset, const void *buf, int count,MPI_Datatype datatype, MPI_Status *status){
292   CHECK_FILE(fh)
293   CHECK_BUFFER(buf, count)
294   CHECK_OFFSET(offset)
295   CHECK_COUNT(count)
296   CHECK_DATATYPE(datatype, count)
297   CHECK_STATUS(status)
298   CHECK_FLAGS(fh)
299   CHECK_RDONLY(fh)
300   PASS_ZEROCOUNT(count);
301   smpi_bench_end();
302   int rank_traced = simgrid::s4u::this_actor::get_pid();
303   TRACE_smpi_comm_in(rank_traced, __func__, new simgrid::instr::CpuTIData("IO - write", count * datatype->size()));
304   int ret = fh->seek(offset,MPI_SEEK_SET);
305   if(ret!=MPI_SUCCESS)
306     return ret;
307   ret = simgrid::smpi::File::write(fh, const_cast<void*>(buf), count, datatype, status);
308   TRACE_smpi_comm_out(rank_traced);
309   smpi_bench_begin();
310   return ret;
311 }
312
313 int PMPI_File_write_at_all(MPI_File fh, MPI_Offset offset, const void *buf, int count,MPI_Datatype datatype, MPI_Status *status){
314   CHECK_FILE(fh)
315   CHECK_BUFFER(buf, count)
316   CHECK_OFFSET(offset)
317   CHECK_COUNT(count)
318   CHECK_DATATYPE(datatype, count)
319   CHECK_STATUS(status)
320   CHECK_FLAGS(fh)
321   CHECK_RDONLY(fh)
322   smpi_bench_end();
323   int rank_traced = simgrid::s4u::this_actor::get_pid();
324   TRACE_smpi_comm_in(rank_traced, __func__,
325                      new simgrid::instr::CpuTIData("IO - write_at_all", count * datatype->size()));
326   int ret = fh->seek(offset,MPI_SEEK_SET);
327   if(ret!=MPI_SUCCESS)
328     return ret;
329   ret = fh->op_all<simgrid::smpi::File::write>(const_cast<void*>(buf), count, datatype, status);
330   TRACE_smpi_comm_out(rank_traced);
331   smpi_bench_begin();
332   return ret;
333 }
334
335 int PMPI_File_delete(const char *filename, MPI_Info info){
336   if (filename == nullptr)
337     return MPI_ERR_FILE;
338   smpi_bench_end();
339   int ret = simgrid::smpi::File::del(filename, info);
340   smpi_bench_begin();
341   return ret;
342 }
343
344 int PMPI_File_get_info(MPI_File  fh, MPI_Info* info)
345 {
346   CHECK_FILE(fh)
347   *info = fh->info();
348   return MPI_SUCCESS;
349 }
350
351 int PMPI_File_set_info(MPI_File  fh, MPI_Info info)
352 {
353   CHECK_FILE(fh)
354   fh->set_info(info);
355   return MPI_SUCCESS;
356 }
357
358 int PMPI_File_get_size(MPI_File  fh, MPI_Offset* size)
359 {
360   CHECK_FILE(fh)
361   *size = fh->size();
362   return MPI_SUCCESS;
363 }
364
365 int PMPI_File_get_amode(MPI_File  fh, int* amode)
366 {
367   CHECK_FILE(fh)
368   *amode = fh->flags();
369   return MPI_SUCCESS;
370 }
371
372 int PMPI_File_get_group(MPI_File  fh, MPI_Group* group)
373 {
374   CHECK_FILE(fh)
375   *group = fh->comm()->group();
376   return MPI_SUCCESS;
377 }
378
379 int PMPI_File_sync(MPI_File  fh)
380 {
381   CHECK_FILE(fh)
382   fh->sync();
383   return MPI_SUCCESS;
384 }
385
386 int PMPI_File_create_errhandler(MPI_File_errhandler_function* function, MPI_Errhandler* errhandler){
387   *errhandler=new simgrid::smpi::Errhandler(function);
388   return MPI_SUCCESS;
389 }
390
391 int PMPI_File_get_errhandler(MPI_File file, MPI_Errhandler* errhandler){
392   if (errhandler==nullptr){
393     return MPI_ERR_ARG;
394   } else if (file == MPI_FILE_NULL) {
395     *errhandler = SMPI_default_File_Errhandler;
396     return MPI_SUCCESS;
397   }
398   *errhandler=file->errhandler();
399   return MPI_SUCCESS;
400 }
401
402 int PMPI_File_set_errhandler(MPI_File file, MPI_Errhandler errhandler){
403   if (errhandler==nullptr){
404     return MPI_ERR_ARG;
405   } else if (file == MPI_FILE_NULL) {
406     SMPI_default_File_Errhandler = errhandler;
407     return MPI_SUCCESS;
408   }
409   file->set_errhandler(errhandler);
410   return MPI_SUCCESS;
411 }
412
413 int PMPI_File_call_errhandler(MPI_File file,int errorcode){
414   if (file == nullptr) {
415     return MPI_ERR_WIN;
416   }
417   file->errhandler()->call(file, errorcode);
418   return MPI_SUCCESS;
419 }