Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Read_at, Write, Write_at
[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 \r
83 int PMPI_File_write(MPI_File fh, void *buf, int count,MPI_Datatype datatype, MPI_Status *status){\r
84   CHECK_FILE(fh);
85   CHECK_BUFFER(buf, count);\r
86   CHECK_COUNT(count);\r
87   PASS_ZEROCOUNT(count);\r
88   CHECK_DATATYPE(datatype, count);\r
89   CHECK_STATUS(status);\r
90   CHECK_FLAGS(fh);\r
91   else {\r
92     smpi_bench_end();\r
93     int rank_traced = simgrid::s4u::this_actor::get_pid();\r
94     TRACE_smpi_comm_in(rank_traced, __func__, new simgrid::instr::CpuTIData("IO - write", static_cast<double>(count*datatype->size())));\r
95     int ret = simgrid::smpi::File::write(fh, buf, count, datatype, status);\r
96     TRACE_smpi_comm_out(rank_traced);\r
97     smpi_bench_begin();\r
98     return ret;\r
99   }\r
100 }\r
101 \r
102 int PMPI_File_read_at(MPI_File fh, MPI_Offset offset, void *buf, int count,MPI_Datatype datatype, MPI_Status *status){\r
103   CHECK_FILE(fh);
104   CHECK_BUFFER(buf, count);\r
105   CHECK_OFFSET(offset);\r
106   CHECK_COUNT(count);\r
107   PASS_ZEROCOUNT(count);\r
108   CHECK_DATATYPE(datatype, count);\r
109   CHECK_STATUS(status);\r
110   CHECK_FLAGS(fh);\r
111   else {\r
112     smpi_bench_end();\r
113     int rank_traced = simgrid::s4u::this_actor::get_pid();\r
114     TRACE_smpi_comm_in(rank_traced, __func__, new simgrid::instr::CpuTIData("IO - read", static_cast<double>(count*datatype->size())));\r
115     int ret = fh->seek(offset,SEEK_SET);\r
116     if(ret!=MPI_SUCCESS)\r
117       return ret;\r
118     ret = simgrid::smpi::File::read(fh, buf, count, datatype, status);\r
119     TRACE_smpi_comm_out(rank_traced);\r
120     smpi_bench_begin();\r
121     return ret;\r
122   }\r
123 }\r
124 \r
125 \r
126 int PMPI_File_write_at(MPI_File fh, MPI_Offset offset, void *buf, int count,MPI_Datatype datatype, MPI_Status *status){\r
127   CHECK_FILE(fh);
128   CHECK_BUFFER(buf, count);\r
129   CHECK_OFFSET(offset);\r
130   CHECK_COUNT(count);\r
131   PASS_ZEROCOUNT(count);\r
132   CHECK_DATATYPE(datatype, count);\r
133   CHECK_STATUS(status);\r
134   CHECK_FLAGS(fh);\r
135   else {\r
136     smpi_bench_end();\r
137     int rank_traced = simgrid::s4u::this_actor::get_pid();\r
138     TRACE_smpi_comm_in(rank_traced, __func__, new simgrid::instr::CpuTIData("IO - write", static_cast<double>(count*datatype->size())));\r
139     int ret = fh->seek(offset,SEEK_SET);\r
140     if(ret!=MPI_SUCCESS)\r
141       return ret;\r
142     ret = simgrid::smpi::File::write(fh, buf, count, datatype, status);\r
143     TRACE_smpi_comm_out(rank_traced);\r
144     smpi_bench_begin();\r
145     return ret;\r
146   }\r
147 }\r
148 \r
149 int PMPI_File_delete(char *filename, MPI_Info info){\r
150   if (filename == nullptr) {\r
151     return MPI_ERR_FILE;\r
152   } else {\r
153     smpi_bench_end();\r
154     int ret = simgrid::smpi::File::del(filename, info);\r
155     smpi_bench_begin();\r
156     return ret;\r
157   }\r
158 }\r
159 \r