Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' into disk
[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) if(fh==MPI_FILE_NULL) return MPI_ERR_FILE;
42 #define CHECK_BUFFER(buf, count)  if (buf==nullptr && count > 0) return MPI_ERR_BUFFER;
43 #define CHECK_COUNT(count)  if (count < 0) return MPI_ERR_COUNT;
44 #define CHECK_OFFSET(offset)  if (offset < 0) return MPI_ERR_DISP;
45 #define CHECK_DATATYPE(datatype, count) if (datatype == MPI_DATATYPE_NULL && count > 0) return MPI_ERR_TYPE;
46 #define CHECK_STATUS(status) if (status == nullptr) return MPI_ERR_ARG;
47 #define CHECK_FLAGS(fh) if (fh->flags() & MPI_MODE_SEQUENTIAL) return MPI_ERR_AMODE;
48 #define CHECK_RDONLY(fh) if (fh->flags() & MPI_MODE_RDONLY ) return MPI_ERR_AMODE;
49
50 #define PASS_ZEROCOUNT(count) if (count == 0) {\
51 status->count=0;\
52 return MPI_SUCCESS;\
53 }
54
55 int PMPI_File_seek(MPI_File fh, MPI_Offset offset, int whence){
56   CHECK_FILE(fh);
57   smpi_bench_end();
58   int ret = fh->seek(offset,whence);
59   smpi_bench_begin();
60   return ret;
61
62 }
63
64 int PMPI_File_seek_shared(MPI_File fh, MPI_Offset offset, int whence){
65   CHECK_FILE(fh)
66   smpi_bench_end();
67   int ret = fh->seek_shared(offset,whence);
68   smpi_bench_begin();
69   return ret;
70
71 }
72
73 int PMPI_File_get_position(MPI_File fh, MPI_Offset* offset){
74   if (offset==nullptr)
75     return MPI_ERR_DISP;
76   smpi_bench_end();
77   int ret = fh->get_position(offset);
78   smpi_bench_begin();
79   return ret;
80 }
81
82 int PMPI_File_get_position_shared(MPI_File fh, MPI_Offset* offset){
83   CHECK_FILE(fh)
84   if (offset==nullptr)
85     return MPI_ERR_DISP;
86   smpi_bench_end();
87   int ret = fh->get_position_shared(offset);
88   smpi_bench_begin();
89   return ret;
90 }
91
92 int PMPI_File_read(MPI_File fh, void *buf, int count,MPI_Datatype datatype, MPI_Status *status){
93   CHECK_FILE(fh)
94   CHECK_BUFFER(buf, count)
95   CHECK_COUNT(count)
96   CHECK_DATATYPE(datatype, count)
97   CHECK_STATUS(status)
98   CHECK_FLAGS(fh)
99   PASS_ZEROCOUNT(count)
100   smpi_bench_end();
101   int rank_traced = simgrid::s4u::this_actor::get_pid();
102   TRACE_smpi_comm_in(rank_traced, __func__, new simgrid::instr::CpuTIData("IO - read", count * datatype->size()));
103   int ret = simgrid::smpi::File::read(fh, buf, count, datatype, status);
104   TRACE_smpi_comm_out(rank_traced);
105   smpi_bench_begin();
106   return ret;
107 }
108
109 int PMPI_File_read_shared(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__,
120                      new simgrid::instr::CpuTIData("IO - read_shared", count * datatype->size()));
121   int ret = simgrid::smpi::File::read_shared(fh, buf, count, datatype, status);
122   TRACE_smpi_comm_out(rank_traced);
123   smpi_bench_begin();
124   return ret;
125 }
126
127 int PMPI_File_write(MPI_File fh, const void *buf, int count,MPI_Datatype datatype, MPI_Status *status){
128   CHECK_FILE(fh)
129   CHECK_BUFFER(buf, count)
130   CHECK_COUNT(count)
131   CHECK_DATATYPE(datatype, count)
132   CHECK_STATUS(status)
133   CHECK_FLAGS(fh)
134   CHECK_RDONLY(fh)
135   PASS_ZEROCOUNT(count)
136   smpi_bench_end();
137   int rank_traced = simgrid::s4u::this_actor::get_pid();
138   TRACE_smpi_comm_in(rank_traced, __func__, new simgrid::instr::CpuTIData("IO - write", count * datatype->size()));
139   int ret = simgrid::smpi::File::write(fh, const_cast<void*>(buf), count, datatype, status);
140   TRACE_smpi_comm_out(rank_traced);
141   smpi_bench_begin();
142   return ret;
143 }
144
145 int PMPI_File_write_shared(MPI_File fh, const void *buf, int count,MPI_Datatype datatype, MPI_Status *status){
146   CHECK_FILE(fh)
147   CHECK_BUFFER(buf, count)
148   CHECK_COUNT(count)
149   CHECK_DATATYPE(datatype, count)
150   CHECK_STATUS(status)
151   CHECK_FLAGS(fh)
152   CHECK_RDONLY(fh)
153   PASS_ZEROCOUNT(count)
154   smpi_bench_end();
155   int rank_traced = simgrid::s4u::this_actor::get_pid();
156   TRACE_smpi_comm_in(rank_traced, __func__,
157                      new simgrid::instr::CpuTIData("IO - write_shared", count * datatype->size()));
158   int ret = simgrid::smpi::File::write_shared(fh, buf, count, datatype, status);
159   TRACE_smpi_comm_out(rank_traced);
160   smpi_bench_begin();
161   return ret;
162 }
163
164 int PMPI_File_read_all(MPI_File fh, void *buf, int count,MPI_Datatype datatype, MPI_Status *status){
165   CHECK_FILE(fh)
166   CHECK_BUFFER(buf, count)
167   CHECK_COUNT(count)
168   CHECK_DATATYPE(datatype, count)
169   CHECK_STATUS(status)
170   CHECK_FLAGS(fh)
171   smpi_bench_end();
172   int rank_traced = simgrid::s4u::this_actor::get_pid();
173   TRACE_smpi_comm_in(rank_traced, __func__, new simgrid::instr::CpuTIData("IO - read_all", count * datatype->size()));
174   int ret = fh->op_all<simgrid::smpi::File::read>(buf, count, datatype, status);
175   TRACE_smpi_comm_out(rank_traced);
176   smpi_bench_begin();
177   return ret;
178 }
179
180 int PMPI_File_read_ordered(MPI_File fh, void *buf, int count,MPI_Datatype datatype, MPI_Status *status){
181   CHECK_FILE(fh)
182   CHECK_BUFFER(buf, count)
183   CHECK_COUNT(count)
184   CHECK_DATATYPE(datatype, count)
185   CHECK_STATUS(status)
186   CHECK_FLAGS(fh)
187   smpi_bench_end();
188   int rank_traced = simgrid::s4u::this_actor::get_pid();
189   TRACE_smpi_comm_in(rank_traced, __func__,
190                      new simgrid::instr::CpuTIData("IO - read_ordered", count * datatype->size()));
191   int ret = simgrid::smpi::File::read_ordered(fh, buf, count, datatype, status);
192   TRACE_smpi_comm_out(rank_traced);
193   smpi_bench_begin();
194   return ret;
195 }
196
197 int PMPI_File_write_all(MPI_File fh, const 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   CHECK_RDONLY(fh)
205   smpi_bench_end();
206   int rank_traced = simgrid::s4u::this_actor::get_pid();
207   TRACE_smpi_comm_in(rank_traced, __func__, new simgrid::instr::CpuTIData("IO - write_all", count * datatype->size()));
208   int ret = fh->op_all<simgrid::smpi::File::write>(const_cast<void*>(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_ordered(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__,
225                      new simgrid::instr::CpuTIData("IO - write_ordered", count * datatype->size()));
226   int ret = simgrid::smpi::File::write_ordered(fh, buf, count, datatype, status);
227   TRACE_smpi_comm_out(rank_traced);
228   smpi_bench_begin();
229   return ret;
230 }
231
232 int PMPI_File_read_at(MPI_File fh, MPI_Offset offset, void *buf, int count,MPI_Datatype datatype, MPI_Status *status){
233   CHECK_FILE(fh)
234   CHECK_BUFFER(buf, count)
235   CHECK_OFFSET(offset)
236   CHECK_COUNT(count)
237   CHECK_DATATYPE(datatype, count)
238   CHECK_STATUS(status)
239   CHECK_FLAGS(fh)
240   PASS_ZEROCOUNT(count);
241   smpi_bench_end();
242   int rank_traced = simgrid::s4u::this_actor::get_pid();
243   TRACE_smpi_comm_in(rank_traced, __func__, new simgrid::instr::CpuTIData("IO - read", count * datatype->size()));
244   int ret = fh->seek(offset,MPI_SEEK_SET);
245   if(ret!=MPI_SUCCESS)
246     return ret;
247   ret = simgrid::smpi::File::read(fh, buf, count, datatype, status);
248   TRACE_smpi_comm_out(rank_traced);
249   smpi_bench_begin();
250   return ret;
251 }
252
253 int PMPI_File_read_at_all(MPI_File fh, MPI_Offset offset, void *buf, int count,MPI_Datatype datatype, MPI_Status *status){
254   CHECK_FILE(fh)
255   CHECK_BUFFER(buf, count)
256   CHECK_OFFSET(offset)
257   CHECK_COUNT(count)
258   CHECK_DATATYPE(datatype, count)
259   CHECK_STATUS(status)
260   CHECK_FLAGS(fh)
261   smpi_bench_end();
262   int rank_traced = simgrid::s4u::this_actor::get_pid();
263   TRACE_smpi_comm_in(rank_traced, __func__,
264                      new simgrid::instr::CpuTIData("IO - read_at_all", count * datatype->size()));
265   int ret = fh->seek(offset,MPI_SEEK_SET);
266   if(ret!=MPI_SUCCESS)
267     return ret;
268   ret = fh->op_all<simgrid::smpi::File::read>(buf, count, datatype, status);
269   TRACE_smpi_comm_out(rank_traced);
270   smpi_bench_begin();
271   return ret;
272 }
273
274 int PMPI_File_write_at(MPI_File fh, MPI_Offset offset, const void *buf, int count,MPI_Datatype datatype, MPI_Status *status){
275   CHECK_FILE(fh)
276   CHECK_BUFFER(buf, count)
277   CHECK_OFFSET(offset)
278   CHECK_COUNT(count)
279   CHECK_DATATYPE(datatype, count)
280   CHECK_STATUS(status)
281   CHECK_FLAGS(fh)
282   CHECK_RDONLY(fh)
283   PASS_ZEROCOUNT(count);
284   smpi_bench_end();
285   int rank_traced = simgrid::s4u::this_actor::get_pid();
286   TRACE_smpi_comm_in(rank_traced, __func__, new simgrid::instr::CpuTIData("IO - write", count * datatype->size()));
287   int ret = fh->seek(offset,MPI_SEEK_SET);
288   if(ret!=MPI_SUCCESS)
289     return ret;
290   ret = simgrid::smpi::File::write(fh, const_cast<void*>(buf), count, datatype, status);
291   TRACE_smpi_comm_out(rank_traced);
292   smpi_bench_begin();
293   return ret;
294 }
295
296 int PMPI_File_write_at_all(MPI_File fh, MPI_Offset offset, const void *buf, int count,MPI_Datatype datatype, MPI_Status *status){
297   CHECK_FILE(fh)
298   CHECK_BUFFER(buf, count)
299   CHECK_OFFSET(offset)
300   CHECK_COUNT(count)
301   CHECK_DATATYPE(datatype, count)
302   CHECK_STATUS(status)
303   CHECK_FLAGS(fh)
304   CHECK_RDONLY(fh)
305   smpi_bench_end();
306   int rank_traced = simgrid::s4u::this_actor::get_pid();
307   TRACE_smpi_comm_in(rank_traced, __func__,
308                      new simgrid::instr::CpuTIData("IO - write_at_all", count * datatype->size()));
309   int ret = fh->seek(offset,MPI_SEEK_SET);
310   if(ret!=MPI_SUCCESS)
311     return ret;
312   ret = fh->op_all<simgrid::smpi::File::write>(const_cast<void*>(buf), count, datatype, status);
313   TRACE_smpi_comm_out(rank_traced);
314   smpi_bench_begin();
315   return ret;
316 }
317
318 int PMPI_File_delete(const char *filename, MPI_Info info){
319   if (filename == nullptr)
320     return MPI_ERR_FILE;
321   smpi_bench_end();
322   int ret = simgrid::smpi::File::del(filename, info);
323   smpi_bench_begin();
324   return ret;
325 }
326
327 int PMPI_File_get_info(MPI_File  fh, MPI_Info* info)
328 {
329   CHECK_FILE(fh)
330   *info = fh->info();
331   return MPI_SUCCESS;
332 }
333
334 int PMPI_File_set_info(MPI_File  fh, MPI_Info info)
335 {
336   CHECK_FILE(fh)
337   fh->set_info(info);
338   return MPI_SUCCESS;
339 }
340
341 int PMPI_File_get_size(MPI_File  fh, MPI_Offset* size)
342 {
343   CHECK_FILE(fh)
344   *size = fh->size();
345   return MPI_SUCCESS;
346 }
347
348 int PMPI_File_get_amode(MPI_File  fh, int* amode)
349 {
350   CHECK_FILE(fh)
351   *amode = fh->flags();
352   return MPI_SUCCESS;
353 }
354
355 int PMPI_File_get_group(MPI_File  fh, MPI_Group* group)
356 {
357   CHECK_FILE(fh)
358   *group = fh->comm()->group();
359   return MPI_SUCCESS;
360 }
361
362 int PMPI_File_sync(MPI_File  fh)
363 {
364   CHECK_FILE(fh)
365   fh->sync();
366   return MPI_SUCCESS;
367 }
368
369 int PMPI_File_create_errhandler(MPI_File_errhandler_function* function, MPI_Errhandler* errhandler){
370   *errhandler=new simgrid::smpi::Errhandler(function);
371   return MPI_SUCCESS;
372 }
373
374 int PMPI_File_get_errhandler(MPI_File file, MPI_Errhandler* errhandler){
375   if (errhandler==nullptr){
376     return MPI_ERR_ARG;
377   } else if (file == MPI_FILE_NULL) {
378     *errhandler = SMPI_default_File_Errhandler;
379     return MPI_SUCCESS;
380   }
381   *errhandler=file->errhandler();
382   return MPI_SUCCESS;
383 }
384
385 int PMPI_File_set_errhandler(MPI_File file, MPI_Errhandler errhandler){
386   if (errhandler==nullptr){
387     return MPI_ERR_ARG;
388   } else if (file == MPI_FILE_NULL) {
389     SMPI_default_File_Errhandler = errhandler;
390     return MPI_SUCCESS;
391   }
392   file->set_errhandler(errhandler);
393   return MPI_SUCCESS;
394 }
395
396 int PMPI_File_call_errhandler(MPI_File file,int errorcode){
397   if (file == nullptr) {
398     return MPI_ERR_WIN;
399   }
400   file->errhandler()->call(file, errorcode);
401   return MPI_SUCCESS;
402 }