Logo AND Algorithmique Numérique Distribuée

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