Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Define class SmpiBenchGuard, and use RAII to handle smpi_bench_end()/smpi_bench_begin().
[simgrid.git] / src / smpi / bindings / smpi_pmpi_file.cpp
1 /* Copyright (c) 2007-2021. 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 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(smpi_pmpi);
12 #define CHECK_RDONLY(fh)                                                                                               \
13   if ((fh)->flags() & MPI_MODE_RDONLY)                                                                                 \
14     return MPI_ERR_AMODE;
15 #define CHECK_WRONLY(fh)                                                                                               \
16   if ((fh)->flags() & MPI_MODE_WRONLY)                                                                                 \
17     return MPI_ERR_AMODE;
18 #define PASS_ZEROCOUNT(count)                                                                                          \
19   if ((count) == 0) {                                                                                                  \
20     status->count = 0;                                                                                                 \
21     return MPI_SUCCESS;                                                                                                \
22   }
23
24 #define CHECK_FILE_INPUTS                                                                                              \
25   CHECK_FILE(1, fh)                                                                                                    \
26   CHECK_COUNT(3, count)                                                                                                \
27   CHECK_TYPE(4, datatype)                                                                                              \
28   CHECK_BUFFER(2, buf, count, datatype)                                                                                          \
29
30 #define CHECK_FILE_INPUT_OFFSET                                                                                        \
31   CHECK_FILE(1, fh)                                                                                                    \
32   CHECK_OFFSET(3, offset)                                                                                                 \
33   CHECK_COUNT(4, count)                                                                                                \
34   CHECK_TYPE(5, datatype)                                                                                              \
35   CHECK_BUFFER(2, buf, count, datatype)                                                                                          \
36
37 extern MPI_Errhandler SMPI_default_File_Errhandler;
38
39 int PMPI_File_open(MPI_Comm comm, const char *filename, int amode, MPI_Info info, MPI_File *fh){
40   CHECK_COMM(1)
41   CHECK_NULL(2, MPI_ERR_FILE, filename)
42   if (amode < 0)
43     return MPI_ERR_AMODE;
44   const SmpiBenchGuard suspend_bench;
45   *fh =  new simgrid::smpi::File(comm, filename, amode, info);
46   if ((*fh)->size() != 0 && (amode & MPI_MODE_EXCL)){
47     delete fh;
48     return MPI_ERR_AMODE;
49   }
50   if(amode & MPI_MODE_APPEND)
51     (*fh)->seek(0,MPI_SEEK_END);
52   return MPI_SUCCESS;
53 }
54
55 int PMPI_File_close(MPI_File *fh){
56   CHECK_NULL(2, MPI_ERR_ARG, fh)
57   const SmpiBenchGuard suspend_bench;
58   int ret = simgrid::smpi::File::close(fh);
59   *fh = MPI_FILE_NULL;
60   return ret;
61 }
62
63 int PMPI_File_seek(MPI_File fh, MPI_Offset offset, int whence){
64   CHECK_FILE(1, fh)
65   const SmpiBenchGuard suspend_bench;
66   int ret = fh->seek(offset,whence);
67   return ret;
68 }
69
70 int PMPI_File_seek_shared(MPI_File fh, MPI_Offset offset, int whence){
71   CHECK_FILE(1, fh)
72   const SmpiBenchGuard suspend_bench;
73   int ret = fh->seek_shared(offset,whence);
74   return ret;
75 }
76
77 int PMPI_File_get_position(MPI_File fh, MPI_Offset* offset){
78   CHECK_FILE(1, fh)
79   CHECK_NULL(2, MPI_ERR_DISP, offset)
80   const SmpiBenchGuard suspend_bench;
81   int ret = fh->get_position(offset);
82   return ret;
83 }
84
85 int PMPI_File_get_position_shared(MPI_File fh, MPI_Offset* offset){
86   CHECK_FILE(1, fh)
87   CHECK_NULL(2, MPI_ERR_DISP, offset)
88   const SmpiBenchGuard suspend_bench;
89   int ret = fh->get_position_shared(offset);
90   return ret;
91 }
92
93 int PMPI_File_read(MPI_File fh, void *buf, int count,MPI_Datatype datatype, MPI_Status *status){
94   CHECK_FILE_INPUTS
95   CHECK_WRONLY(fh)
96   PASS_ZEROCOUNT(count)
97   const SmpiBenchGuard suspend_bench;
98   aid_t rank_traced = simgrid::s4u::this_actor::get_pid();
99   TRACE_smpi_comm_in(rank_traced, __func__, new simgrid::instr::CpuTIData("IO - read", count * datatype->size()));
100   int ret = simgrid::smpi::File::read(fh, buf, count, datatype, status);
101   TRACE_smpi_comm_out(rank_traced);
102   return ret;
103 }
104
105 int PMPI_File_read_shared(MPI_File fh, void *buf, int count,MPI_Datatype datatype, MPI_Status *status){
106   CHECK_FILE_INPUTS
107   CHECK_WRONLY(fh)
108   PASS_ZEROCOUNT(count)
109   const SmpiBenchGuard suspend_bench;
110   aid_t rank_traced = simgrid::s4u::this_actor::get_pid();
111   TRACE_smpi_comm_in(rank_traced, __func__,
112                      new simgrid::instr::CpuTIData("IO - read_shared", count * datatype->size()));
113   int ret = simgrid::smpi::File::read_shared(fh, buf, count, datatype, status);
114   TRACE_smpi_comm_out(rank_traced);
115   return ret;
116 }
117
118 int PMPI_File_write(MPI_File fh, const void *buf, int count,MPI_Datatype datatype, MPI_Status *status){
119   CHECK_FILE_INPUTS
120   CHECK_RDONLY(fh)
121   PASS_ZEROCOUNT(count)
122   const SmpiBenchGuard suspend_bench;
123   aid_t rank_traced = simgrid::s4u::this_actor::get_pid();
124   TRACE_smpi_comm_in(rank_traced, __func__, new simgrid::instr::CpuTIData("IO - write", count * datatype->size()));
125   int ret = simgrid::smpi::File::write(fh, const_cast<void*>(buf), count, datatype, status);
126   TRACE_smpi_comm_out(rank_traced);
127   return ret;
128 }
129
130 int PMPI_File_write_shared(MPI_File fh, const void *buf, int count,MPI_Datatype datatype, MPI_Status *status){
131   CHECK_FILE_INPUTS
132   CHECK_RDONLY(fh)
133   PASS_ZEROCOUNT(count)
134   const SmpiBenchGuard suspend_bench;
135   aid_t rank_traced = simgrid::s4u::this_actor::get_pid();
136   TRACE_smpi_comm_in(rank_traced, __func__,
137                      new simgrid::instr::CpuTIData("IO - write_shared", count * datatype->size()));
138   int ret = simgrid::smpi::File::write_shared(fh, buf, count, datatype, status);
139   TRACE_smpi_comm_out(rank_traced);
140   return ret;
141 }
142
143 int PMPI_File_read_all(MPI_File fh, void *buf, int count,MPI_Datatype datatype, MPI_Status *status){
144   CHECK_FILE_INPUTS
145   CHECK_WRONLY(fh)
146   const SmpiBenchGuard suspend_bench;
147   aid_t rank_traced = simgrid::s4u::this_actor::get_pid();
148   TRACE_smpi_comm_in(rank_traced, __func__, new simgrid::instr::CpuTIData("IO - read_all", count * datatype->size()));
149   int ret = fh->op_all<simgrid::smpi::File::read>(buf, count, datatype, status);
150   TRACE_smpi_comm_out(rank_traced);
151   return ret;
152 }
153
154 int PMPI_File_read_ordered(MPI_File fh, void *buf, int count,MPI_Datatype datatype, MPI_Status *status){
155   CHECK_FILE_INPUTS
156   CHECK_WRONLY(fh)
157   const SmpiBenchGuard suspend_bench;
158   aid_t rank_traced = simgrid::s4u::this_actor::get_pid();
159   TRACE_smpi_comm_in(rank_traced, __func__,
160                      new simgrid::instr::CpuTIData("IO - read_ordered", count * datatype->size()));
161   int ret = simgrid::smpi::File::read_ordered(fh, buf, count, datatype, status);
162   TRACE_smpi_comm_out(rank_traced);
163   return ret;
164 }
165
166 int PMPI_File_write_all(MPI_File fh, const void *buf, int count,MPI_Datatype datatype, MPI_Status *status){
167   CHECK_FILE_INPUTS
168   CHECK_RDONLY(fh)
169   const SmpiBenchGuard suspend_bench;
170   aid_t rank_traced = simgrid::s4u::this_actor::get_pid();
171   TRACE_smpi_comm_in(rank_traced, __func__, new simgrid::instr::CpuTIData("IO - write_all", count * datatype->size()));
172   int ret = fh->op_all<simgrid::smpi::File::write>(const_cast<void*>(buf), count, datatype, status);
173   TRACE_smpi_comm_out(rank_traced);
174   return ret;
175 }
176
177 int PMPI_File_write_ordered(MPI_File fh, const void *buf, int count,MPI_Datatype datatype, MPI_Status *status){
178   CHECK_FILE_INPUTS
179   CHECK_RDONLY(fh)
180   const SmpiBenchGuard suspend_bench;
181   aid_t rank_traced = simgrid::s4u::this_actor::get_pid();
182   TRACE_smpi_comm_in(rank_traced, __func__,
183                      new simgrid::instr::CpuTIData("IO - write_ordered", count * datatype->size()));
184   int ret = simgrid::smpi::File::write_ordered(fh, buf, count, datatype, status);
185   TRACE_smpi_comm_out(rank_traced);
186   return ret;
187 }
188
189 int PMPI_File_read_at(MPI_File fh, MPI_Offset offset, void *buf, int count,MPI_Datatype datatype, MPI_Status *status){
190   CHECK_FILE_INPUTS
191   CHECK_WRONLY(fh)
192   PASS_ZEROCOUNT(count)
193   const SmpiBenchGuard suspend_bench;
194   aid_t rank_traced = simgrid::s4u::this_actor::get_pid();
195   TRACE_smpi_comm_in(rank_traced, __func__, new simgrid::instr::CpuTIData("IO - read", count * datatype->size()));
196   int ret = fh->seek(offset,MPI_SEEK_SET);
197   if (ret == MPI_SUCCESS)
198     ret = simgrid::smpi::File::read(fh, buf, count, datatype, status);
199   TRACE_smpi_comm_out(rank_traced);
200   return ret;
201 }
202
203 int PMPI_File_read_at_all(MPI_File fh, MPI_Offset offset, void *buf, int count,MPI_Datatype datatype, MPI_Status *status){
204   CHECK_FILE_INPUT_OFFSET
205   CHECK_WRONLY(fh)
206   const SmpiBenchGuard suspend_bench;
207   aid_t rank_traced = simgrid::s4u::this_actor::get_pid();
208   TRACE_smpi_comm_in(rank_traced, __func__,
209                      new simgrid::instr::CpuTIData("IO - read_at_all", count * datatype->size()));
210   int ret = fh->seek(offset,MPI_SEEK_SET);
211   if (ret == MPI_SUCCESS)
212     ret = fh->op_all<simgrid::smpi::File::read>(buf, count, datatype, status);
213   TRACE_smpi_comm_out(rank_traced);
214   return ret;
215 }
216
217 int PMPI_File_write_at(MPI_File fh, MPI_Offset offset, const void *buf, int count,MPI_Datatype datatype, MPI_Status *status){
218   CHECK_FILE_INPUT_OFFSET
219   CHECK_RDONLY(fh)
220   PASS_ZEROCOUNT(count)
221   const SmpiBenchGuard suspend_bench;
222   aid_t rank_traced = simgrid::s4u::this_actor::get_pid();
223   TRACE_smpi_comm_in(rank_traced, __func__, new simgrid::instr::CpuTIData("IO - write", count * datatype->size()));
224   int ret = fh->seek(offset,MPI_SEEK_SET);
225   if (ret == MPI_SUCCESS)
226     ret = simgrid::smpi::File::write(fh, const_cast<void*>(buf), count, datatype, status);
227   TRACE_smpi_comm_out(rank_traced);
228   return ret;
229 }
230
231 int PMPI_File_write_at_all(MPI_File fh, MPI_Offset offset, const void *buf, int count,MPI_Datatype datatype, MPI_Status *status){
232   CHECK_FILE_INPUT_OFFSET
233   CHECK_RDONLY(fh)
234   const SmpiBenchGuard suspend_bench;
235   aid_t rank_traced = simgrid::s4u::this_actor::get_pid();
236   TRACE_smpi_comm_in(rank_traced, __func__,
237                      new simgrid::instr::CpuTIData("IO - write_at_all", count * datatype->size()));
238   int ret = fh->seek(offset,MPI_SEEK_SET);
239   if (ret == MPI_SUCCESS)
240     ret = fh->op_all<simgrid::smpi::File::write>(const_cast<void*>(buf), count, datatype, status);
241   TRACE_smpi_comm_out(rank_traced);
242   return ret;
243 }
244
245 int PMPI_File_delete(const char *filename, MPI_Info info){
246   if (filename == nullptr)
247     return MPI_ERR_FILE;
248   const SmpiBenchGuard suspend_bench;
249   int ret = simgrid::smpi::File::del(filename, info);
250   return ret;
251 }
252
253 int PMPI_File_set_view(MPI_File fh, MPI_Offset disp, MPI_Datatype etype, MPI_Datatype filetype, const char *datarep, MPI_Info info){
254   CHECK_FILE(1, fh)
255   if(not ((fh->flags() & MPI_MODE_SEQUENTIAL) && (disp == MPI_DISPLACEMENT_CURRENT)))
256     CHECK_OFFSET(2, disp)
257   CHECK_TYPE(3, etype)
258   CHECK_TYPE(4, filetype)
259   const SmpiBenchGuard suspend_bench;
260   int ret = fh->set_view(disp, etype, filetype, datarep, info);
261   return ret;
262 }
263
264 int PMPI_File_get_view(MPI_File fh, MPI_Offset *disp, MPI_Datatype *etype, MPI_Datatype *filetype, char *datarep){
265   CHECK_FILE(1, fh)
266   CHECK_NULL(2, MPI_ERR_ARG, disp)
267   CHECK_NULL(3, MPI_ERR_ARG, etype)
268   CHECK_NULL(4, MPI_ERR_ARG, filetype)
269   const SmpiBenchGuard suspend_bench;
270   int ret = fh->get_view(disp, etype, filetype, datarep);
271   return ret;
272 }
273
274 int PMPI_File_get_info(MPI_File  fh, MPI_Info* info)
275 {
276   CHECK_FILE(1, fh)
277   *info = new simgrid::smpi::Info(fh->info());
278   return MPI_SUCCESS;
279 }
280
281 int PMPI_File_set_info(MPI_File  fh, MPI_Info info)
282 {
283   CHECK_FILE(1, fh)
284   fh->set_info(info);
285   return MPI_SUCCESS;
286 }
287
288 int PMPI_File_get_size(MPI_File  fh, MPI_Offset* size)
289 {
290   CHECK_FILE(1, fh)
291   *size = fh->size();
292   return MPI_SUCCESS;
293 }
294
295 int PMPI_File_set_size(MPI_File  fh, MPI_Offset size)
296 {
297   CHECK_FILE(1, fh)
298   fh->set_size(size);
299   return MPI_SUCCESS;
300 }
301
302 int PMPI_File_get_amode(MPI_File  fh, int* amode)
303 {
304   CHECK_FILE(1, fh)
305   *amode = fh->flags();
306   return MPI_SUCCESS;
307 }
308
309 int PMPI_File_get_group(MPI_File  fh, MPI_Group* group)
310 {
311   CHECK_FILE(1, fh)
312   *group = fh->comm()->group();
313   return MPI_SUCCESS;
314 }
315
316 int PMPI_File_sync(MPI_File  fh)
317 {
318   CHECK_FILE(1, fh)
319   fh->sync();
320   return MPI_SUCCESS;
321 }
322
323 int PMPI_File_create_errhandler(MPI_File_errhandler_function* function, MPI_Errhandler* errhandler){
324   *errhandler=new simgrid::smpi::Errhandler(function);
325   return MPI_SUCCESS;
326 }
327
328 int PMPI_File_get_errhandler(MPI_File file, MPI_Errhandler* errhandler){
329   if (errhandler==nullptr){
330     return MPI_ERR_ARG;
331   } else if (file == MPI_FILE_NULL) {
332     *errhandler = SMPI_default_File_Errhandler;
333     return MPI_SUCCESS;
334   }
335   *errhandler=file->errhandler();
336   return MPI_SUCCESS;
337 }
338
339 int PMPI_File_set_errhandler(MPI_File file, MPI_Errhandler errhandler){
340   if (errhandler==nullptr){
341     return MPI_ERR_ARG;
342   } else if (file == MPI_FILE_NULL) {
343     SMPI_default_File_Errhandler = errhandler;
344     return MPI_SUCCESS;
345   }
346   file->set_errhandler(errhandler);
347   return MPI_SUCCESS;
348 }
349
350 int PMPI_File_call_errhandler(MPI_File file,int errorcode){
351   if (file == nullptr) {
352     return MPI_ERR_WIN;
353   }
354   MPI_Errhandler err = file->errhandler();
355   err->call(file, errorcode);
356   simgrid::smpi::Errhandler::unref(err);
357   return MPI_SUCCESS;
358 }