Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use C++17's std::scoped_lock where appropriate.
[simgrid.git] / src / smpi / mpi / smpi_file.cpp
1 /* Copyright (c) 2007-2023. 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 #include "private.hpp"
6
7 #include "smpi_comm.hpp"
8 #include "smpi_coll.hpp"
9 #include "smpi_datatype.hpp"
10 #include "smpi_info.hpp"
11 #include "smpi_win.hpp"
12 #include "smpi_request.hpp"
13
14 #include "smpi_file.hpp"
15 #include "smpi_status.hpp"
16 #include "simgrid/s4u/Disk.hpp"
17 #include "simgrid/s4u/Host.hpp"
18 #include "simgrid/plugins/file_system.h"
19
20 #include <mutex> // std::scoped_lock
21
22 #define FP_SIZE sizeof(MPI_Offset)
23
24 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_io, smpi, "Logging specific to SMPI (RMA operations)");
25
26 MPI_Errhandler SMPI_default_File_Errhandler =  _smpi_cfg_default_errhandler_is_error ? MPI_ERRORS_ARE_FATAL : MPI_ERRORS_RETURN;;
27
28 namespace simgrid::smpi {
29
30 File::File(MPI_Comm comm, const char* filename, int amode, MPI_Info info) : comm_(comm), flags_(amode), info_(info)
31 {
32   if (info_ != MPI_INFO_NULL)
33     info_->ref();
34   std::string fullname = filename;
35   xbt_assert(not simgrid::s4u::Host::current()->get_disks().empty(),
36              "SMPI/IO : Trying to open file on a diskless host ! Add one to your platform file");
37
38   // in case no fullpath is provided ... just pick the first mountpoint.
39   if (size_t found = fullname.find('/'); found == std::string::npos || fullname.rfind("./", 1) != std::string::npos) {
40     const auto* disk = simgrid::s4u::Host::current()->get_disks().front();
41     std::string mount;
42     if (disk->get_host() != simgrid::s4u::Host::current())
43       mount = disk->extension<simgrid::s4u::FileSystemDiskExt>()->get_mount_point(disk->get_host());
44     else
45       mount = disk->extension<simgrid::s4u::FileSystemDiskExt>()->get_mount_point();
46     XBT_DEBUG("No absolute path given for file opening, use '%s'", mount.c_str());
47     if (fullname.rfind("./", 1) != std::string::npos)
48       fullname.replace(fullname.begin(), fullname.begin() + 1, mount);
49     else {
50       mount.append("/");
51       fullname.insert(0, mount);
52     }
53   }
54   XBT_DEBUG("Opening %s", fullname.c_str());
55   file_ = simgrid::s4u::File::open(fullname, nullptr);
56   list_ = nullptr;
57   disp_ = 0;
58   etype_ = MPI_BYTE;
59   atomicity_ = true;
60   if (comm_->rank() == 0) {
61     int size    = comm_->size() + FP_SIZE;
62     list_       = new char[size];
63     errhandler_ = SMPI_default_File_Errhandler;
64     errhandler_->ref();
65     memset(list_, 0, size);
66     shared_file_pointer_  = new MPI_Offset();
67     shared_mutex_         = s4u::Mutex::create();
68     *shared_file_pointer_ = 0;
69     win_                  = new Win(list_, size, 1, MPI_INFO_NULL, comm_);
70   } else {
71     errhandler_ = MPI_ERRHANDLER_NULL;
72     win_        = new Win(list_, 0, 1, MPI_INFO_NULL, comm_);
73   }
74   simgrid::smpi::colls::bcast(&shared_file_pointer_, 1, MPI_AINT, 0, comm);
75   simgrid::smpi::colls::bcast(&shared_mutex_, 1, MPI_AINT, 0, comm);
76   if (comm_->rank() != 0)
77     intrusive_ptr_add_ref(&*shared_mutex_);
78   this->add_f();
79 }
80
81 File::~File()
82 {
83   if (comm_->rank() == 0) {
84     delete shared_file_pointer_;
85     delete[] list_;
86   }
87   simgrid::smpi::Win::del(win_);
88   file_->close();
89   F2C::free_f(this->f2c_id());
90   if (info_ != MPI_INFO_NULL)
91     simgrid::smpi::Info::unref(info_);
92   if (errhandler_ != MPI_ERRHANDLER_NULL)
93     simgrid::smpi::Errhandler::unref(errhandler_);
94 }
95
96 int File::close(MPI_File* fh)
97 {
98   XBT_DEBUG("Closing MPI_File %s", (*fh)->file_->get_path());
99   (*fh)->sync();
100   if ((*fh)->flags() & MPI_MODE_DELETE_ON_CLOSE)
101     (*fh)->file_->unlink();
102   delete (*fh);
103   return MPI_SUCCESS;
104 }
105
106 int File::del(const char* filename, const Info*)
107 {
108   // get the file with MPI_MODE_DELETE_ON_CLOSE and then close it
109   auto* f = new File(MPI_COMM_SELF, filename, MPI_MODE_DELETE_ON_CLOSE | MPI_MODE_RDWR, nullptr);
110   close(&f);
111   return MPI_SUCCESS;
112 }
113
114 int File::get_position(MPI_Offset* offset) const
115 {
116   *offset = file_->tell()/etype_->get_extent();
117   return MPI_SUCCESS;
118 }
119
120 int File::get_position_shared(MPI_Offset* offset) const
121 {
122   const std::scoped_lock lock(*shared_mutex_);
123   *offset = *shared_file_pointer_/etype_->get_extent();
124   return MPI_SUCCESS;
125 }
126
127 int File::seek(MPI_Offset offset, int whence)
128 {
129   switch (whence) {
130     case MPI_SEEK_SET:
131       XBT_VERB("Seeking in MPI_File %s, setting offset %lld", file_->get_path(), offset);
132       file_->seek(offset, SEEK_SET);
133       break;
134     case MPI_SEEK_CUR:
135       XBT_VERB("Seeking in MPI_File %s, current offset + %lld", file_->get_path(), offset);
136       file_->seek(offset, SEEK_CUR);
137       break;
138     case MPI_SEEK_END:
139       XBT_VERB("Seeking in MPI_File %s, end offset + %lld", file_->get_path(), offset);
140       file_->seek(offset, SEEK_END);
141       break;
142     default:
143       return MPI_ERR_FILE;
144   }
145   return MPI_SUCCESS;
146 }
147
148 int File::seek_shared(MPI_Offset offset, int whence)
149 {
150   const std::scoped_lock lock(*shared_mutex_);
151   seek(offset, whence);
152   *shared_file_pointer_ = file_->tell();
153   return MPI_SUCCESS;
154 }
155
156 int File::read(MPI_File fh, void* /*buf*/, int count, const Datatype* datatype, MPI_Status* status)
157 {
158   // get position first as we may be doing non contiguous reads and it will probably be updated badly
159   MPI_Offset position = fh->file_->tell();
160   MPI_Offset movesize = datatype->get_extent() * count;
161   MPI_Offset readsize = datatype->size() * count;
162   XBT_DEBUG("Position before read in MPI_File %s : %llu, size %llu", fh->file_->get_path(), fh->file_->tell(), fh->file_->size());
163   MPI_Offset read = fh->file_->read(readsize);
164   XBT_VERB("Read in MPI_File %s, %lld bytes read, count %d, readsize %lld bytes, movesize %lld", fh->file_->get_path(), read, count,
165            readsize, movesize);
166   if (readsize != movesize) {
167     fh->file_->seek(position + movesize, SEEK_SET);
168   }
169   XBT_VERB("Position after read in MPI_File %s : %llu", fh->file_->get_path(), fh->file_->tell());
170   if (status != MPI_STATUS_IGNORE)
171     status->count = count * datatype->size();
172   return MPI_SUCCESS;
173 }
174
175 /*Ordered and Shared Versions, with RMA-based locks : Based on the model described in :*/
176 /* @InProceedings{10.1007/11557265_15,*/
177 /* author="Latham, Robert and Ross, Robert and Thakur, Rajeev and Toonen, Brian",*/
178 /* title="Implementing MPI-IO Shared File Pointers Without File System Support",*/
179 /* booktitle="Recent Advances in Parallel Virtual Machine and Message Passing Interface",*/
180 /* year="2005",*/
181 /* publisher="Springer Berlin Heidelberg",*/
182 /* address="Berlin, Heidelberg",*/
183 /* pages="84--93"*/
184 /* }*/
185 int File::read_shared(MPI_File fh, void* buf, int count, const Datatype* datatype, MPI_Status* status)
186 {
187   if (const std::scoped_lock lock(*fh->shared_mutex_); true) {
188     fh->seek(*(fh->shared_file_pointer_), MPI_SEEK_SET);
189     read(fh, buf, count, datatype, status);
190     *(fh->shared_file_pointer_) = fh->file_->tell();
191   }
192   fh->seek(*(fh->shared_file_pointer_), MPI_SEEK_SET);
193   return MPI_SUCCESS;
194 }
195
196 int File::read_ordered(MPI_File fh, void* buf, int count, const Datatype* datatype, MPI_Status* status)
197 {
198   // 0 needs to get the shared pointer value
199   MPI_Offset val;
200   if (fh->comm_->rank() == 0) {
201     val = *(fh->shared_file_pointer_);
202   } else {
203     val = count * datatype->size();
204   }
205
206   MPI_Offset result;
207   simgrid::smpi::colls::scan(&val, &result, 1, MPI_OFFSET, MPI_SUM, fh->comm_);
208   MPI_Offset prev;
209   fh->get_position(&prev);
210   fh->seek(result, MPI_SEEK_SET);
211   int ret = fh->op_all<simgrid::smpi::File::read>(buf, count, datatype, status);
212   if (fh->comm_->rank() == fh->comm_->size() - 1) {
213     const std::scoped_lock lock(*fh->shared_mutex_);
214     *(fh->shared_file_pointer_)=fh->file_->tell();
215   }
216   char c;
217   simgrid::smpi::colls::bcast(&c, 1, MPI_BYTE, fh->comm_->size() - 1, fh->comm_);
218   fh->seek(prev, MPI_SEEK_SET);
219   return ret;
220 }
221
222 int File::write(MPI_File fh, void* /*buf*/, int count, const Datatype* datatype, MPI_Status* status)
223 {
224   // get position first as we may be doing non contiguous reads and it will probably be updated badly
225   MPI_Offset position  = fh->file_->tell();
226   MPI_Offset movesize  = datatype->get_extent() * count;
227   MPI_Offset writesize = datatype->size() * count;
228   XBT_DEBUG("Position before write in MPI_File %s : %llu, size %llu", fh->file_->get_path(), fh->file_->tell(), fh->file_->size());
229   MPI_Offset write = fh->file_->write(writesize, true);
230   XBT_VERB("Write in MPI_File %s, %lld bytes written, count %d, writesize %lld bytes, movesize %lld", fh->file_->get_path(), write,
231            count, writesize, movesize);
232   if (writesize != movesize) {
233     fh->file_->seek(position + movesize, SEEK_SET);
234   }
235   XBT_VERB("Position after write in MPI_File %s : %llu", fh->file_->get_path(), fh->file_->tell());
236   if (status != MPI_STATUS_IGNORE)
237     status->count = count * datatype->size();
238   return MPI_SUCCESS;
239 }
240
241 int File::write_shared(MPI_File fh, const void* buf, int count, const Datatype* datatype, MPI_Status* status)
242 {
243   const std::scoped_lock lock(*fh->shared_mutex_);
244   XBT_DEBUG("Write shared on %s - Shared ptr before : %lld", fh->file_->get_path(), *(fh->shared_file_pointer_));
245   fh->seek(*(fh->shared_file_pointer_), MPI_SEEK_SET);
246   write(fh, const_cast<void*>(buf), count, datatype, status);
247   *(fh->shared_file_pointer_) = fh->file_->tell();
248   XBT_DEBUG("Write shared on %s - Shared ptr after : %lld", fh->file_->get_path(), *(fh->shared_file_pointer_));
249   fh->seek(*(fh->shared_file_pointer_), MPI_SEEK_SET);
250   return MPI_SUCCESS;
251 }
252
253 int File::write_ordered(MPI_File fh, const void* buf, int count, const Datatype* datatype, MPI_Status* status)
254 {
255   // 0 needs to get the shared pointer value
256   MPI_Offset val;
257   if (fh->comm_->rank() == 0) {
258     val = *(fh->shared_file_pointer_);
259   } else {
260     val = count * datatype->size();
261   }
262   MPI_Offset result;
263   simgrid::smpi::colls::scan(&val, &result, 1, MPI_OFFSET, MPI_SUM, fh->comm_);
264   MPI_Offset prev;
265   fh->get_position(&prev);
266   fh->seek(result, MPI_SEEK_SET);
267   int ret = fh->op_all<simgrid::smpi::File::write>(const_cast<void*>(buf), count, datatype, status);
268   if (fh->comm_->rank() == fh->comm_->size() - 1) {
269     const std::scoped_lock lock(*fh->shared_mutex_);
270     *(fh->shared_file_pointer_)=fh->file_->tell();
271   }
272   char c;
273   simgrid::smpi::colls::bcast(&c, 1, MPI_BYTE, fh->comm_->size() - 1, fh->comm_);
274   fh->seek(prev, MPI_SEEK_SET);
275   return ret;
276 }
277
278 int File::set_view(MPI_Offset disp, MPI_Datatype etype, MPI_Datatype filetype, const char* datarep, const Info*)
279 {
280   etype_    = etype;
281   filetype_ = filetype;
282   datarep_  = datarep;
283   disp_     = disp;
284   if (comm_->rank() == 0){
285     if(disp != MPI_DISPLACEMENT_CURRENT)
286       seek_shared(disp, MPI_SEEK_SET);
287     else
288       seek_shared(0, MPI_SEEK_CUR);
289   }
290   sync();
291   return MPI_SUCCESS;
292 }
293
294 int File::get_view(MPI_Offset* disp, MPI_Datatype* etype, MPI_Datatype* filetype, char* datarep) const
295 {
296   *disp     = disp_;
297   *etype    = etype_;
298   *filetype = filetype_;
299   snprintf(datarep, MPI_MAX_NAME_STRING + 1, "%s", datarep_.c_str());
300   return MPI_SUCCESS;
301 }
302
303 int File::size() const
304 {
305   return file_->size();
306 }
307
308 void File::set_size(int size)
309 {
310   file_->write(size, true);
311 }
312
313 int File::flags() const
314 {
315   return flags_;
316 }
317
318 MPI_Datatype File::etype() const
319 {
320   return etype_;
321 }
322
323 int File::sync()
324 {
325   // no idea
326   return simgrid::smpi::colls::barrier(comm_);
327 }
328
329 MPI_Info File::info()
330 {
331   return info_;
332 }
333
334 void File::set_info(MPI_Info info)
335 {
336   if (info_ != MPI_INFO_NULL)
337     simgrid::smpi::Info::unref(info_);
338   info_ = info;
339   if (info_ != MPI_INFO_NULL)
340     info_->ref();
341 }
342
343 MPI_Comm File::comm() const
344 {
345   return comm_;
346 }
347
348 MPI_Errhandler File::errhandler()
349 {
350   if (errhandler_ != MPI_ERRHANDLER_NULL)
351     errhandler_->ref();
352   return errhandler_;
353 }
354
355 void File::set_errhandler(MPI_Errhandler errhandler)
356 {
357   if (errhandler_ != MPI_ERRHANDLER_NULL)
358     simgrid::smpi::Errhandler::unref(errhandler_);
359   errhandler_ = errhandler;
360   if (errhandler_ != MPI_ERRHANDLER_NULL)
361     errhandler_->ref();
362 }
363
364 File* File::f2c(int id)
365 {
366   return static_cast<File*>(F2C::f2c(id));
367 }
368
369 void File::set_atomicity(bool a){
370   atomicity_ = a;
371 }
372
373 bool File::get_atomicity() const
374 {
375   return atomicity_;
376 }
377
378 } // namespace simgrid::smpi