Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Test using macros for arguments checking, saves some duplicated lines.
[simgrid.git] / src / smpi / bindings / smpi_pmpi_file.cpp
1 /* Copyright (c) 2007-2019. The SimGrid Team. All rights reserved.          */\r
2 \r
3 /* This program is free software; you can redistribute it and/or modify it\r
4  * under the terms of the license (GNU LGPL) which comes with this package. */\r
5 \r
6 #include "private.hpp"\r
7 #include "smpi_file.hpp"\r
8 #include "smpi_datatype.hpp"\r
9 \r
10 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(smpi_pmpi);\r
11 \r
12 int PMPI_File_open(MPI_Comm comm, char *filename, int amode, MPI_Info info, MPI_File *fh){\r
13   if (comm == MPI_COMM_NULL) {\r
14     return MPI_ERR_COMM;\r
15   } else if (filename == nullptr) {\r
16     return MPI_ERR_FILE;\r
17   } else if (amode < 0) {\r
18     return MPI_ERR_AMODE;\r
19   } else {\r
20     smpi_bench_end();\r
21     *fh =  new simgrid::smpi::File(comm, filename, amode, info);\r
22     smpi_bench_begin();\r
23     if (((*fh)->size() == 0 && (not amode & MPI_MODE_CREATE)) ||\r
24        ((*fh)->size() != 0 && (amode & MPI_MODE_EXCL))){\r
25       delete fh;\r
26       return MPI_ERR_AMODE;\r
27     }\r
28     if(amode & MPI_MODE_APPEND)\r
29       (*fh)->seek(0,MPI_SEEK_END);\r
30     return MPI_SUCCESS;\r
31   }\r
32 }\r
33 \r
34 int PMPI_File_close(MPI_File *fh){\r
35   if (fh==nullptr){\r
36     return MPI_ERR_ARG;\r
37   } else {\r
38     smpi_bench_end();\r
39     int ret = simgrid::smpi::File::close(fh);\r
40     *fh = MPI_FILE_NULL;\r
41     smpi_bench_begin();\r
42     return ret;\r
43   }\r
44 }\r
45 #define CHECK_FILE(fh) if(fh==MPI_FILE_NULL) return MPI_ERR_FILE\r
46 #define CHECK_BUFFER(buf, count)  else if (buf==nullptr && count > 0) return MPI_ERR_BUFFER\r
47 #define CHECK_COUNT(count)  else if ( count < 0) return MPI_ERR_COUNT\r
48 #define CHECK_OFFSET(offset)  else if ( offset < 0) return MPI_ERR_DISP\r
49 #define PASS_ZEROCOUNT(count)  else if ( count == 0) return MPI_SUCCESS\r
50 #define CHECK_DATATYPE(datatype, count) else if ( datatype == MPI_DATATYPE_NULL && count > 0) return MPI_ERR_TYPE\r
51 #define CHECK_STATUS(status) else if (status == nullptr) return MPI_ERR_ARG\r
52 #define CHECK_FLAGS(fh) else if (fh->flags() & MPI_MODE_SEQUENTIAL) return MPI_ERR_AMODE\r
53 \r
54 int PMPI_File_seek(MPI_File fh, MPI_Offset offset, int whence){\r
55   CHECK_FILE(fh);
56   else {\r
57     smpi_bench_end();\r
58     int ret = fh->seek(offset,whence);\r
59     smpi_bench_begin();\r
60     return ret;\r
61   }\r
62 }\r
63 \r
64 int PMPI_File_read(MPI_File fh, void *buf, int count,MPI_Datatype datatype, MPI_Status *status){\r
65   CHECK_FILE(fh);
66   CHECK_BUFFER(buf, count);\r
67   CHECK_COUNT(count);\r
68   PASS_ZEROCOUNT(count);\r
69   CHECK_DATATYPE(datatype, count);\r
70   CHECK_STATUS(status);\r
71   CHECK_FLAGS(fh);\r
72   else {\r
73     smpi_bench_end();\r
74     int rank_traced = simgrid::s4u::this_actor::get_pid();\r
75     TRACE_smpi_comm_in(rank_traced, __func__, new simgrid::instr::CpuTIData("IO - read", static_cast<double>(count*datatype->size())));\r
76     int ret = simgrid::smpi::File::read(fh, buf, count, datatype, status);\r
77     TRACE_smpi_comm_out(rank_traced);\r
78     smpi_bench_begin();\r
79     return ret;\r
80   }\r
81 }\r
82     smpi_bench_end();\r
83     int rank_traced = simgrid::s4u::this_actor::get_pid();\r
84     TRACE_smpi_comm_in(rank_traced, __func__, new simgrid::instr::CpuTIData("IO - read", static_cast<double>(count*datatype->size())));\r
85     int ret = fh->read(buf, count, datatype, status);\r
86     TRACE_smpi_comm_out(rank_traced);\r
87     smpi_bench_begin();\r
88     return ret;\r
89   }\r
90 }\r
91 \r
92 \r
93 int PMPI_File_delete(char *filename, MPI_Info info){\r
94   if (filename == nullptr) {\r
95     return MPI_ERR_FILE;\r
96   } else {\r
97     smpi_bench_end();\r
98     int ret = simgrid::smpi::File::del(filename, info);\r
99     smpi_bench_begin();\r
100     return ret;\r
101   }\r
102 }\r
103 \r