Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
constify MPI_File*
[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", static_cast<double>(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__, new simgrid::instr::CpuTIData("IO - read_shared", static_cast<double>(count*datatype->size())));
118   int ret = simgrid::smpi::File::read_shared(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_write(MPI_File fh, const 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   CHECK_RDONLY(fh)
132   PASS_ZEROCOUNT(count)
133   smpi_bench_end();
134   int rank_traced = simgrid::s4u::this_actor::get_pid();
135   TRACE_smpi_comm_in(rank_traced, __func__, new simgrid::instr::CpuTIData("IO - write", static_cast<double>(count*datatype->size())));
136   int ret = simgrid::smpi::File::write(fh, const_cast<void*>(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_shared(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_shared", static_cast<double>(count*datatype->size())));
154   int ret = simgrid::smpi::File::write_shared(fh, buf, count, datatype, status);
155   TRACE_smpi_comm_out(rank_traced);
156   smpi_bench_begin();
157   return ret;
158 }
159
160 int PMPI_File_read_all(MPI_File fh, 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   smpi_bench_end();
168   int rank_traced = simgrid::s4u::this_actor::get_pid();
169   TRACE_smpi_comm_in(rank_traced, __func__, new simgrid::instr::CpuTIData("IO - read_all", static_cast<double>(count*datatype->size())));
170   int ret = fh->op_all<simgrid::smpi::File::read>(buf, count, datatype, status);
171   TRACE_smpi_comm_out(rank_traced);
172   smpi_bench_begin();
173   return ret;
174 }
175
176 int PMPI_File_read_ordered(MPI_File fh, void *buf, int count,MPI_Datatype datatype, MPI_Status *status){
177   CHECK_FILE(fh)
178   CHECK_BUFFER(buf, count)
179   CHECK_COUNT(count)
180   CHECK_DATATYPE(datatype, count)
181   CHECK_STATUS(status)
182   CHECK_FLAGS(fh)
183   smpi_bench_end();
184   int rank_traced = simgrid::s4u::this_actor::get_pid();
185   TRACE_smpi_comm_in(rank_traced, __func__, new simgrid::instr::CpuTIData("IO - read_ordered", static_cast<double>(count*datatype->size())));
186   int ret = simgrid::smpi::File::read_ordered(fh, buf, count, datatype, status);
187   TRACE_smpi_comm_out(rank_traced);
188   smpi_bench_begin();
189   return ret;
190 }
191
192 int PMPI_File_write_all(MPI_File fh, const void *buf, int count,MPI_Datatype datatype, MPI_Status *status){
193   CHECK_FILE(fh)
194   CHECK_BUFFER(buf, count)
195   CHECK_COUNT(count)
196   CHECK_DATATYPE(datatype, count)
197   CHECK_STATUS(status)
198   CHECK_FLAGS(fh)
199   CHECK_RDONLY(fh)
200   smpi_bench_end();
201   int rank_traced = simgrid::s4u::this_actor::get_pid();
202   TRACE_smpi_comm_in(rank_traced, __func__, new simgrid::instr::CpuTIData("IO - write_all", static_cast<double>(count*datatype->size())));
203   int ret = fh->op_all<simgrid::smpi::File::write>(const_cast<void*>(buf), count, datatype, status);
204   TRACE_smpi_comm_out(rank_traced);
205   smpi_bench_begin();
206   return ret;
207 }
208
209 int PMPI_File_write_ordered(MPI_File fh, const void *buf, int count,MPI_Datatype datatype, MPI_Status *status){
210   CHECK_FILE(fh)
211   CHECK_BUFFER(buf, count)
212   CHECK_COUNT(count)
213   CHECK_DATATYPE(datatype, count)
214   CHECK_STATUS(status)
215   CHECK_FLAGS(fh)
216   CHECK_RDONLY(fh)
217   smpi_bench_end();
218   int rank_traced = simgrid::s4u::this_actor::get_pid();
219   TRACE_smpi_comm_in(rank_traced, __func__, new simgrid::instr::CpuTIData("IO - write_ordered", static_cast<double>(count*datatype->size())));
220   int ret = simgrid::smpi::File::write_ordered(fh, buf, count, datatype, status);
221   TRACE_smpi_comm_out(rank_traced);
222   smpi_bench_begin();
223   return ret;
224 }
225
226 int PMPI_File_read_at(MPI_File fh, MPI_Offset offset, void *buf, int count,MPI_Datatype datatype, MPI_Status *status){
227   CHECK_FILE(fh)
228   CHECK_BUFFER(buf, count)
229   CHECK_OFFSET(offset)
230   CHECK_COUNT(count)
231   CHECK_DATATYPE(datatype, count)
232   CHECK_STATUS(status)
233   CHECK_FLAGS(fh)
234   PASS_ZEROCOUNT(count);
235   smpi_bench_end();
236   int rank_traced = simgrid::s4u::this_actor::get_pid();
237   TRACE_smpi_comm_in(rank_traced, __func__, new simgrid::instr::CpuTIData("IO - read", static_cast<double>(count*datatype->size())));
238   int ret = fh->seek(offset,MPI_SEEK_SET);
239   if(ret!=MPI_SUCCESS)
240     return ret;
241   ret = simgrid::smpi::File::read(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_all(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   smpi_bench_end();
256   int rank_traced = simgrid::s4u::this_actor::get_pid();
257   TRACE_smpi_comm_in(rank_traced, __func__, new simgrid::instr::CpuTIData("IO - read_at_all", static_cast<double>(count*datatype->size())));
258   int ret = fh->seek(offset,MPI_SEEK_SET);
259   if(ret!=MPI_SUCCESS)
260     return ret;
261   ret = fh->op_all<simgrid::smpi::File::read>(buf, count, datatype, status);
262   TRACE_smpi_comm_out(rank_traced);
263   smpi_bench_begin();
264   return ret;
265 }
266
267 int PMPI_File_write_at(MPI_File fh, MPI_Offset offset, const void *buf, int count,MPI_Datatype datatype, MPI_Status *status){
268   CHECK_FILE(fh)
269   CHECK_BUFFER(buf, count)
270   CHECK_OFFSET(offset)
271   CHECK_COUNT(count)
272   CHECK_DATATYPE(datatype, count)
273   CHECK_STATUS(status)
274   CHECK_FLAGS(fh)
275   CHECK_RDONLY(fh)
276   PASS_ZEROCOUNT(count);
277   smpi_bench_end();
278   int rank_traced = simgrid::s4u::this_actor::get_pid();
279   TRACE_smpi_comm_in(rank_traced, __func__, new simgrid::instr::CpuTIData("IO - write", static_cast<double>(count*datatype->size())));
280   int ret = fh->seek(offset,MPI_SEEK_SET);
281   if(ret!=MPI_SUCCESS)
282     return ret;
283   ret = simgrid::smpi::File::write(fh, const_cast<void*>(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_all(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   smpi_bench_end();
299   int rank_traced = simgrid::s4u::this_actor::get_pid();
300   TRACE_smpi_comm_in(rank_traced, __func__, new simgrid::instr::CpuTIData("IO - write_at_all", static_cast<double>(count*datatype->size())));
301   int ret = fh->seek(offset,MPI_SEEK_SET);
302   if(ret!=MPI_SUCCESS)
303     return ret;
304   ret = fh->op_all<simgrid::smpi::File::write>(const_cast<void*>(buf), count, datatype, status);
305   TRACE_smpi_comm_out(rank_traced);
306   smpi_bench_begin();
307   return ret;
308 }
309
310 int PMPI_File_delete(const char *filename, MPI_Info info){
311   if (filename == nullptr)
312     return MPI_ERR_FILE;
313   smpi_bench_end();
314   int ret = simgrid::smpi::File::del(filename, info);
315   smpi_bench_begin();
316   return ret;
317 }
318
319 int PMPI_File_get_info(MPI_File  fh, MPI_Info* info)
320 {
321   CHECK_FILE(fh)
322   *info = fh->info();
323   return MPI_SUCCESS;
324 }
325
326 int PMPI_File_set_info(MPI_File  fh, MPI_Info info)
327 {
328   CHECK_FILE(fh)
329   fh->set_info(info);
330   return MPI_SUCCESS;
331 }
332
333 int PMPI_File_get_size(MPI_File  fh, MPI_Offset* size)
334 {
335   CHECK_FILE(fh)
336   *size = fh->size();
337   return MPI_SUCCESS;
338 }
339
340 int PMPI_File_get_amode(MPI_File  fh, int* amode)
341 {
342   CHECK_FILE(fh)
343   *amode = fh->flags();
344   return MPI_SUCCESS;
345 }
346
347 int PMPI_File_get_group(MPI_File  fh, MPI_Group* group)
348 {
349   CHECK_FILE(fh)
350   *group = fh->comm()->group();
351   return MPI_SUCCESS;
352 }
353
354 int PMPI_File_sync(MPI_File  fh)
355 {
356   CHECK_FILE(fh)
357   fh->sync();
358   return MPI_SUCCESS;
359 }