Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
ee7493d9748e8491d7fd41acdd4303dd518b1f1d
[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, 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 && not (amode & MPI_MODE_CREATE)) ||
22      ((*fh)->size() != 0 && (amode & MPI_MODE_EXCL))){
23     delete fh;
24     return MPI_ERR_AMODE;
25   }
26   if(amode & MPI_MODE_APPEND)
27     (*fh)->seek(0,MPI_SEEK_END);
28   return MPI_SUCCESS;
29 }
30
31 int PMPI_File_close(MPI_File *fh){
32   if (fh==nullptr)
33     return MPI_ERR_ARG;
34   smpi_bench_end();
35   int ret = simgrid::smpi::File::close(fh);
36   *fh = MPI_FILE_NULL;
37   smpi_bench_begin();
38   return ret;
39 }
40 #define CHECK_FILE(fh) if(fh==MPI_FILE_NULL) return MPI_ERR_FILE;
41 #define CHECK_BUFFER(buf, count)  if (buf==nullptr && count > 0) return MPI_ERR_BUFFER;
42 #define CHECK_COUNT(count)  if (count < 0) return MPI_ERR_COUNT;
43 #define CHECK_OFFSET(offset)  if (offset < 0) return MPI_ERR_DISP;
44 #define CHECK_DATATYPE(datatype, count) if (datatype == MPI_DATATYPE_NULL && count > 0) return MPI_ERR_TYPE;
45 #define CHECK_STATUS(status) if (status == nullptr) return MPI_ERR_ARG;
46 #define CHECK_FLAGS(fh) if (fh->flags() & MPI_MODE_SEQUENTIAL) 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, 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__, new simgrid::instr::CpuTIData("IO - write", static_cast<double>(count*datatype->size())));
135   int ret = simgrid::smpi::File::write(fh, buf, count, datatype, status);
136   TRACE_smpi_comm_out(rank_traced);
137   smpi_bench_begin();
138   return ret;
139 }
140
141 int PMPI_File_write_shared(MPI_File fh, void *buf, int count,MPI_Datatype datatype, MPI_Status *status){
142   CHECK_FILE(fh)
143   CHECK_BUFFER(buf, count)
144   CHECK_COUNT(count)
145   CHECK_DATATYPE(datatype, count)
146   CHECK_STATUS(status)
147   CHECK_FLAGS(fh)
148   PASS_ZEROCOUNT(count)
149   smpi_bench_end();
150   int rank_traced = simgrid::s4u::this_actor::get_pid();
151   TRACE_smpi_comm_in(rank_traced, __func__, new simgrid::instr::CpuTIData("IO - write_shared", static_cast<double>(count*datatype->size())));
152   int ret = simgrid::smpi::File::write_shared(fh, buf, count, datatype, status);
153   TRACE_smpi_comm_out(rank_traced);
154   smpi_bench_begin();
155   return ret;
156 }
157
158 int PMPI_File_read_all(MPI_File fh, void *buf, int count,MPI_Datatype datatype, MPI_Status *status){
159   CHECK_FILE(fh)
160   CHECK_BUFFER(buf, count)
161   CHECK_COUNT(count)
162   CHECK_DATATYPE(datatype, count)
163   CHECK_STATUS(status)
164   CHECK_FLAGS(fh)
165   smpi_bench_end();
166   int rank_traced = simgrid::s4u::this_actor::get_pid();
167   TRACE_smpi_comm_in(rank_traced, __func__, new simgrid::instr::CpuTIData("IO - read_all", static_cast<double>(count*datatype->size())));
168   int ret = fh->op_all<simgrid::smpi::File::read>(buf, count, datatype, status);
169   TRACE_smpi_comm_out(rank_traced);
170   smpi_bench_begin();
171   return ret;
172 }
173
174 int PMPI_File_read_ordered(MPI_File fh, void *buf, int count,MPI_Datatype datatype, MPI_Status *status){
175   CHECK_FILE(fh)
176   CHECK_BUFFER(buf, count)
177   CHECK_COUNT(count)
178   CHECK_DATATYPE(datatype, count)
179   CHECK_STATUS(status)
180   CHECK_FLAGS(fh)
181   smpi_bench_end();
182   int rank_traced = simgrid::s4u::this_actor::get_pid();
183   TRACE_smpi_comm_in(rank_traced, __func__, new simgrid::instr::CpuTIData("IO - read_ordered", static_cast<double>(count*datatype->size())));
184   int ret = simgrid::smpi::File::read_ordered(fh, buf, count, datatype, status);
185   TRACE_smpi_comm_out(rank_traced);
186   smpi_bench_begin();
187   return ret;
188 }
189
190 int PMPI_File_write_all(MPI_File fh, void *buf, int count,MPI_Datatype datatype, MPI_Status *status){
191   CHECK_FILE(fh)
192   CHECK_BUFFER(buf, count)
193   CHECK_COUNT(count)
194   CHECK_DATATYPE(datatype, count)
195   CHECK_STATUS(status)
196   CHECK_FLAGS(fh)
197   smpi_bench_end();
198   int rank_traced = simgrid::s4u::this_actor::get_pid();
199   TRACE_smpi_comm_in(rank_traced, __func__, new simgrid::instr::CpuTIData("IO - write_all", static_cast<double>(count*datatype->size())));
200   int ret = fh->op_all<simgrid::smpi::File::write>(buf, count, datatype, status);
201   TRACE_smpi_comm_out(rank_traced);
202   smpi_bench_begin();
203   return ret;
204 }
205
206 int PMPI_File_write_ordered(MPI_File fh, void *buf, int count,MPI_Datatype datatype, MPI_Status *status){
207   CHECK_FILE(fh)
208   CHECK_BUFFER(buf, count)
209   CHECK_COUNT(count)
210   CHECK_DATATYPE(datatype, count)
211   CHECK_STATUS(status)
212   CHECK_FLAGS(fh)
213   smpi_bench_end();
214   int rank_traced = simgrid::s4u::this_actor::get_pid();
215   TRACE_smpi_comm_in(rank_traced, __func__, new simgrid::instr::CpuTIData("IO - write_ordered", static_cast<double>(count*datatype->size())));
216   int ret = simgrid::smpi::File::write_ordered(fh, buf, count, datatype, status);
217   TRACE_smpi_comm_out(rank_traced);
218   smpi_bench_begin();
219   return ret;
220 }
221
222 int PMPI_File_read_at(MPI_File fh, MPI_Offset offset, void *buf, int count,MPI_Datatype datatype, MPI_Status *status){
223   CHECK_FILE(fh)
224   CHECK_BUFFER(buf, count)
225   CHECK_OFFSET(offset)
226   CHECK_COUNT(count)
227   CHECK_DATATYPE(datatype, count)
228   CHECK_STATUS(status)
229   CHECK_FLAGS(fh)
230   PASS_ZEROCOUNT(count);
231   smpi_bench_end();
232   int rank_traced = simgrid::s4u::this_actor::get_pid();
233   TRACE_smpi_comm_in(rank_traced, __func__, new simgrid::instr::CpuTIData("IO - read", static_cast<double>(count*datatype->size())));
234   int ret = fh->seek(offset,MPI_SEEK_SET);
235   if(ret!=MPI_SUCCESS)
236     return ret;
237   ret = simgrid::smpi::File::read(fh, buf, count, datatype, status);
238   TRACE_smpi_comm_out(rank_traced);
239   smpi_bench_begin();
240   return ret;
241 }
242
243 int PMPI_File_read_at_all(MPI_File fh, MPI_Offset offset, void *buf, int count,MPI_Datatype datatype, MPI_Status *status){
244   CHECK_FILE(fh)
245   CHECK_BUFFER(buf, count)
246   CHECK_OFFSET(offset)
247   CHECK_COUNT(count)
248   CHECK_DATATYPE(datatype, count)
249   CHECK_STATUS(status)
250   CHECK_FLAGS(fh)
251   smpi_bench_end();
252   int rank_traced = simgrid::s4u::this_actor::get_pid();
253   TRACE_smpi_comm_in(rank_traced, __func__, new simgrid::instr::CpuTIData("IO - read_at_all", static_cast<double>(count*datatype->size())));
254   int ret = fh->seek(offset,MPI_SEEK_SET);
255   if(ret!=MPI_SUCCESS)
256     return ret;
257   ret = fh->op_all<simgrid::smpi::File::read>(buf, count, datatype, status);
258   TRACE_smpi_comm_out(rank_traced);
259   smpi_bench_begin();
260   return ret;
261 }
262
263 int PMPI_File_write_at(MPI_File fh, MPI_Offset offset, void *buf, int count,MPI_Datatype datatype, MPI_Status *status){
264   CHECK_FILE(fh)
265   CHECK_BUFFER(buf, count)
266   CHECK_OFFSET(offset)
267   CHECK_COUNT(count)
268   CHECK_DATATYPE(datatype, count)
269   CHECK_STATUS(status)
270   CHECK_FLAGS(fh)
271   PASS_ZEROCOUNT(count);
272   smpi_bench_end();
273   int rank_traced = simgrid::s4u::this_actor::get_pid();
274   TRACE_smpi_comm_in(rank_traced, __func__, new simgrid::instr::CpuTIData("IO - write", static_cast<double>(count*datatype->size())));
275   int ret = fh->seek(offset,MPI_SEEK_SET);
276   if(ret!=MPI_SUCCESS)
277     return ret;
278   ret = simgrid::smpi::File::write(fh, buf, count, datatype, status);
279   TRACE_smpi_comm_out(rank_traced);
280   smpi_bench_begin();
281   return ret;
282 }
283
284 int PMPI_File_write_at_all(MPI_File fh, MPI_Offset offset, void *buf, int count,MPI_Datatype datatype, MPI_Status *status){
285   CHECK_FILE(fh)
286   CHECK_BUFFER(buf, count)
287   CHECK_OFFSET(offset)
288   CHECK_COUNT(count)
289   CHECK_DATATYPE(datatype, count)
290   CHECK_STATUS(status)
291   CHECK_FLAGS(fh)
292   smpi_bench_end();
293   int rank_traced = simgrid::s4u::this_actor::get_pid();
294   TRACE_smpi_comm_in(rank_traced, __func__, new simgrid::instr::CpuTIData("IO - write_at_all", static_cast<double>(count*datatype->size())));
295   int ret = fh->seek(offset,MPI_SEEK_SET);
296   if(ret!=MPI_SUCCESS)
297     return ret;
298   ret = fh->op_all<simgrid::smpi::File::write>(buf, count, datatype, status);
299   TRACE_smpi_comm_out(rank_traced);
300   smpi_bench_begin();
301   return ret;
302 }
303
304 int PMPI_File_delete(char *filename, MPI_Info info){
305   if (filename == nullptr)
306     return MPI_ERR_FILE;
307   smpi_bench_end();
308   int ret = simgrid::smpi::File::del(filename, info);
309   smpi_bench_begin();
310   return ret;
311 }
312
313 int PMPI_File_get_info(MPI_File  fh, MPI_Info* info)
314 {
315   CHECK_FILE(fh)
316   *info = fh->info();
317   return MPI_SUCCESS;
318 }
319
320 int PMPI_File_set_info(MPI_File  fh, MPI_Info info)
321 {
322   CHECK_FILE(fh)
323   fh->set_info(info);
324   return MPI_SUCCESS;
325 }