Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
2336b8dd41b2a0b468dd663ffc50fd2b947406b8
[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 #define CHECK_RDONLY(fh) if (fh->flags() & MPI_MODE_RDONLY ) return MPI_ERR_AMODE;
48
49 #define PASS_ZEROCOUNT(count) if (count == 0) {\
50 status->count=0;\
51 return MPI_SUCCESS;\
52 }
53
54 int PMPI_File_seek(MPI_File fh, MPI_Offset offset, int whence){
55   CHECK_FILE(fh);
56   smpi_bench_end();
57   int ret = fh->seek(offset,whence);
58   smpi_bench_begin();
59   return ret;
60
61 }
62
63 int PMPI_File_seek_shared(MPI_File fh, MPI_Offset offset, int whence){
64   CHECK_FILE(fh)
65   smpi_bench_end();
66   int ret = fh->seek_shared(offset,whence);
67   smpi_bench_begin();
68   return ret;
69
70 }
71
72 int PMPI_File_get_position(MPI_File fh, MPI_Offset* offset){
73   if (offset==nullptr)
74     return MPI_ERR_DISP;
75   smpi_bench_end();
76   int ret = fh->get_position(offset);
77   smpi_bench_begin();
78   return ret;
79 }
80
81 int PMPI_File_get_position_shared(MPI_File fh, MPI_Offset* offset){
82   CHECK_FILE(fh)
83   if (offset==nullptr)
84     return MPI_ERR_DISP;
85   smpi_bench_end();
86   int ret = fh->get_position_shared(offset);
87   smpi_bench_begin();
88   return ret;
89 }
90
91 int PMPI_File_read(MPI_File fh, void *buf, int count,MPI_Datatype datatype, MPI_Status *status){
92   CHECK_FILE(fh)
93   CHECK_BUFFER(buf, count)
94   CHECK_COUNT(count)
95   CHECK_DATATYPE(datatype, count)
96   CHECK_STATUS(status)
97   CHECK_FLAGS(fh)
98   PASS_ZEROCOUNT(count)
99   smpi_bench_end();
100   int rank_traced = simgrid::s4u::this_actor::get_pid();
101   TRACE_smpi_comm_in(rank_traced, __func__, new simgrid::instr::CpuTIData("IO - read", static_cast<double>(count*datatype->size())));
102   int ret = simgrid::smpi::File::read(fh, buf, count, datatype, status);
103   TRACE_smpi_comm_out(rank_traced);
104   smpi_bench_begin();
105   return ret;
106 }
107
108 int PMPI_File_read_shared(MPI_File fh, void *buf, int count,MPI_Datatype datatype, MPI_Status *status){
109   CHECK_FILE(fh)
110   CHECK_BUFFER(buf, count)
111   CHECK_COUNT(count)
112   CHECK_DATATYPE(datatype, count)
113   CHECK_STATUS(status)
114   CHECK_FLAGS(fh)
115   PASS_ZEROCOUNT(count)
116   smpi_bench_end();
117   int rank_traced = simgrid::s4u::this_actor::get_pid();
118   TRACE_smpi_comm_in(rank_traced, __func__, new simgrid::instr::CpuTIData("IO - read_shared", static_cast<double>(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, 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", static_cast<double>(count*datatype->size())));
137   int ret = simgrid::smpi::File::write(fh, 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, 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__, new simgrid::instr::CpuTIData("IO - write_shared", static_cast<double>(count*datatype->size())));
155   int ret = simgrid::smpi::File::write_shared(fh, buf, count, datatype, status);
156   TRACE_smpi_comm_out(rank_traced);
157   smpi_bench_begin();
158   return ret;
159 }
160
161 int PMPI_File_read_all(MPI_File fh, void *buf, int count,MPI_Datatype datatype, MPI_Status *status){
162   CHECK_FILE(fh)
163   CHECK_BUFFER(buf, count)
164   CHECK_COUNT(count)
165   CHECK_DATATYPE(datatype, count)
166   CHECK_STATUS(status)
167   CHECK_FLAGS(fh)
168   smpi_bench_end();
169   int rank_traced = simgrid::s4u::this_actor::get_pid();
170   TRACE_smpi_comm_in(rank_traced, __func__, new simgrid::instr::CpuTIData("IO - read_all", static_cast<double>(count*datatype->size())));
171   int ret = fh->op_all<simgrid::smpi::File::read>(buf, count, datatype, status);
172   TRACE_smpi_comm_out(rank_traced);
173   smpi_bench_begin();
174   return ret;
175 }
176
177 int PMPI_File_read_ordered(MPI_File fh, void *buf, int count,MPI_Datatype datatype, MPI_Status *status){
178   CHECK_FILE(fh)
179   CHECK_BUFFER(buf, count)
180   CHECK_COUNT(count)
181   CHECK_DATATYPE(datatype, count)
182   CHECK_STATUS(status)
183   CHECK_FLAGS(fh)
184   smpi_bench_end();
185   int rank_traced = simgrid::s4u::this_actor::get_pid();
186   TRACE_smpi_comm_in(rank_traced, __func__, new simgrid::instr::CpuTIData("IO - read_ordered", static_cast<double>(count*datatype->size())));
187   int ret = simgrid::smpi::File::read_ordered(fh, buf, count, datatype, status);
188   TRACE_smpi_comm_out(rank_traced);
189   smpi_bench_begin();
190   return ret;
191 }
192
193 int PMPI_File_write_all(MPI_File fh, void *buf, int count,MPI_Datatype datatype, MPI_Status *status){
194   CHECK_FILE(fh)
195   CHECK_BUFFER(buf, count)
196   CHECK_COUNT(count)
197   CHECK_DATATYPE(datatype, count)
198   CHECK_STATUS(status)
199   CHECK_FLAGS(fh)
200   CHECK_RDONLY(fh)
201   smpi_bench_end();
202   int rank_traced = simgrid::s4u::this_actor::get_pid();
203   TRACE_smpi_comm_in(rank_traced, __func__, new simgrid::instr::CpuTIData("IO - write_all", static_cast<double>(count*datatype->size())));
204   int ret = fh->op_all<simgrid::smpi::File::write>(buf, count, datatype, status);
205   TRACE_smpi_comm_out(rank_traced);
206   smpi_bench_begin();
207   return ret;
208 }
209
210 int PMPI_File_write_ordered(MPI_File fh, void *buf, int count,MPI_Datatype datatype, MPI_Status *status){
211   CHECK_FILE(fh)
212   CHECK_BUFFER(buf, count)
213   CHECK_COUNT(count)
214   CHECK_DATATYPE(datatype, count)
215   CHECK_STATUS(status)
216   CHECK_FLAGS(fh)
217   CHECK_RDONLY(fh)
218   smpi_bench_end();
219   int rank_traced = simgrid::s4u::this_actor::get_pid();
220   TRACE_smpi_comm_in(rank_traced, __func__, new simgrid::instr::CpuTIData("IO - write_ordered", static_cast<double>(count*datatype->size())));
221   int ret = simgrid::smpi::File::write_ordered(fh, buf, count, datatype, status);
222   TRACE_smpi_comm_out(rank_traced);
223   smpi_bench_begin();
224   return ret;
225 }
226
227 int PMPI_File_read_at(MPI_File fh, MPI_Offset offset, void *buf, int count,MPI_Datatype datatype, MPI_Status *status){
228   CHECK_FILE(fh)
229   CHECK_BUFFER(buf, count)
230   CHECK_OFFSET(offset)
231   CHECK_COUNT(count)
232   CHECK_DATATYPE(datatype, count)
233   CHECK_STATUS(status)
234   CHECK_FLAGS(fh)
235   PASS_ZEROCOUNT(count);
236   smpi_bench_end();
237   int rank_traced = simgrid::s4u::this_actor::get_pid();
238   TRACE_smpi_comm_in(rank_traced, __func__, new simgrid::instr::CpuTIData("IO - read", static_cast<double>(count*datatype->size())));
239   int ret = fh->seek(offset,MPI_SEEK_SET);
240   if(ret!=MPI_SUCCESS)
241     return ret;
242   ret = simgrid::smpi::File::read(fh, buf, count, datatype, status);
243   TRACE_smpi_comm_out(rank_traced);
244   smpi_bench_begin();
245   return ret;
246 }
247
248 int PMPI_File_read_at_all(MPI_File fh, MPI_Offset offset, void *buf, int count,MPI_Datatype datatype, MPI_Status *status){
249   CHECK_FILE(fh)
250   CHECK_BUFFER(buf, count)
251   CHECK_OFFSET(offset)
252   CHECK_COUNT(count)
253   CHECK_DATATYPE(datatype, count)
254   CHECK_STATUS(status)
255   CHECK_FLAGS(fh)
256   smpi_bench_end();
257   int rank_traced = simgrid::s4u::this_actor::get_pid();
258   TRACE_smpi_comm_in(rank_traced, __func__, new simgrid::instr::CpuTIData("IO - read_at_all", static_cast<double>(count*datatype->size())));
259   int ret = fh->seek(offset,MPI_SEEK_SET);
260   if(ret!=MPI_SUCCESS)
261     return ret;
262   ret = fh->op_all<simgrid::smpi::File::read>(buf, count, datatype, status);
263   TRACE_smpi_comm_out(rank_traced);
264   smpi_bench_begin();
265   return ret;
266 }
267
268 int PMPI_File_write_at(MPI_File fh, MPI_Offset offset, void *buf, int count,MPI_Datatype datatype, MPI_Status *status){
269   CHECK_FILE(fh)
270   CHECK_BUFFER(buf, count)
271   CHECK_OFFSET(offset)
272   CHECK_COUNT(count)
273   CHECK_DATATYPE(datatype, count)
274   CHECK_STATUS(status)
275   CHECK_FLAGS(fh)
276   CHECK_RDONLY(fh)
277   PASS_ZEROCOUNT(count);
278   smpi_bench_end();
279   int rank_traced = simgrid::s4u::this_actor::get_pid();
280   TRACE_smpi_comm_in(rank_traced, __func__, new simgrid::instr::CpuTIData("IO - write", static_cast<double>(count*datatype->size())));
281   int ret = fh->seek(offset,MPI_SEEK_SET);
282   if(ret!=MPI_SUCCESS)
283     return ret;
284   ret = simgrid::smpi::File::write(fh, buf, count, datatype, status);
285   TRACE_smpi_comm_out(rank_traced);
286   smpi_bench_begin();
287   return ret;
288 }
289
290 int PMPI_File_write_at_all(MPI_File fh, MPI_Offset offset, void *buf, int count,MPI_Datatype datatype, MPI_Status *status){
291   CHECK_FILE(fh)
292   CHECK_BUFFER(buf, count)
293   CHECK_OFFSET(offset)
294   CHECK_COUNT(count)
295   CHECK_DATATYPE(datatype, count)
296   CHECK_STATUS(status)
297   CHECK_FLAGS(fh)
298   CHECK_RDONLY(fh)
299   smpi_bench_end();
300   int rank_traced = simgrid::s4u::this_actor::get_pid();
301   TRACE_smpi_comm_in(rank_traced, __func__, new simgrid::instr::CpuTIData("IO - write_at_all", static_cast<double>(count*datatype->size())));
302   int ret = fh->seek(offset,MPI_SEEK_SET);
303   if(ret!=MPI_SUCCESS)
304     return ret;
305   ret = fh->op_all<simgrid::smpi::File::write>(buf, count, datatype, status);
306   TRACE_smpi_comm_out(rank_traced);
307   smpi_bench_begin();
308   return ret;
309 }
310
311 int PMPI_File_delete(char *filename, MPI_Info info){
312   if (filename == nullptr)
313     return MPI_ERR_FILE;
314   smpi_bench_end();
315   int ret = simgrid::smpi::File::del(filename, info);
316   smpi_bench_begin();
317   return ret;
318 }
319
320 int PMPI_File_get_info(MPI_File  fh, MPI_Info* info)
321 {
322   CHECK_FILE(fh)
323   *info = fh->info();
324   return MPI_SUCCESS;
325 }
326
327 int PMPI_File_set_info(MPI_File  fh, MPI_Info info)
328 {
329   CHECK_FILE(fh)
330   fh->set_info(info);
331   return MPI_SUCCESS;
332 }