Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Declare functions "const" (sonar).
[simgrid.git] / src / smpi / mpi / smpi_file.cpp
1 /* Copyright (c) 2007-2022. 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 #define FP_SIZE sizeof(MPI_Offset)
21
22 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_io, smpi, "Logging specific to SMPI (RMA operations)");
23
24 MPI_Errhandler SMPI_default_File_Errhandler =  _smpi_cfg_default_errhandler_is_error ? MPI_ERRORS_ARE_FATAL : MPI_ERRORS_RETURN;;
25
26 namespace simgrid::smpi {
27
28 File::File(MPI_Comm comm, const char* filename, int amode, MPI_Info info) : comm_(comm), flags_(amode), info_(info)
29 {
30   if (info_ != MPI_INFO_NULL)
31     info_->ref();
32   std::string fullname = filename;
33   xbt_assert(not simgrid::s4u::Host::current()->get_disks().empty(),
34              "SMPI/IO : Trying to open file on a diskless host ! Add one to your platform file");
35
36   // in case no fullpath is provided ... just pick the first mountpoint.
37   if (size_t found = fullname.find('/'); found == std::string::npos || fullname.rfind("./", 1) != std::string::npos) {
38     const auto* disk = simgrid::s4u::Host::current()->get_disks().front();
39     std::string mount;
40     if (disk->get_host() != simgrid::s4u::Host::current())
41       mount = disk->extension<simgrid::s4u::FileSystemDiskExt>()->get_mount_point(disk->get_host());
42     else
43       mount = disk->extension<simgrid::s4u::FileSystemDiskExt>()->get_mount_point();
44     XBT_DEBUG("No absolute path given for file opening, use '%s'", mount.c_str());
45     if (fullname.rfind("./", 1) != std::string::npos)
46       fullname.replace(fullname.begin(), fullname.begin() + 1, mount);
47     else {
48       mount.append("/");
49       fullname.insert(0, mount);
50     }
51   }
52   XBT_DEBUG("Opening %s", fullname.c_str());
53   file_ = simgrid::s4u::File::open(fullname, nullptr);
54   list_ = nullptr;
55   disp_ = 0;
56   etype_ = MPI_BYTE;
57   atomicity_ = true;
58   if (comm_->rank() == 0) {
59     int size    = comm_->size() + FP_SIZE;
60     list_       = new char[size];
61     errhandler_ = SMPI_default_File_Errhandler;
62     errhandler_->ref();
63     memset(list_, 0, size);
64     shared_file_pointer_  = new MPI_Offset();
65     shared_mutex_         = s4u::Mutex::create();
66     *shared_file_pointer_ = 0;
67     win_                  = new Win(list_, size, 1, MPI_INFO_NULL, comm_);
68   } else {
69     errhandler_ = MPI_ERRHANDLER_NULL;
70     win_        = new Win(list_, 0, 1, MPI_INFO_NULL, comm_);
71   }
72   simgrid::smpi::colls::bcast(&shared_file_pointer_, 1, MPI_AINT, 0, comm);
73   simgrid::smpi::colls::bcast(&shared_mutex_, 1, MPI_AINT, 0, comm);
74   if (comm_->rank() != 0)
75     intrusive_ptr_add_ref(&*shared_mutex_);
76   this->add_f();
77 }
78
79 File::~File()
80 {
81   if (comm_->rank() == 0) {
82     delete shared_file_pointer_;
83     delete[] list_;
84   }
85   simgrid::smpi::Win::del(win_);
86   file_->close();
87   F2C::free_f(this->f2c_id());
88   if (info_ != MPI_INFO_NULL)
89     simgrid::smpi::Info::unref(info_);
90   if (errhandler_ != MPI_ERRHANDLER_NULL)
91     simgrid::smpi::Errhandler::unref(errhandler_);
92 }
93
94 int File::close(MPI_File* fh)
95 {
96   XBT_DEBUG("Closing MPI_File %s", (*fh)->file_->get_path());
97   (*fh)->sync();
98   if ((*fh)->flags() & MPI_MODE_DELETE_ON_CLOSE)
99     (*fh)->file_->unlink();
100   delete (*fh);
101   return MPI_SUCCESS;
102 }
103
104 int File::del(const char* filename, const Info*)
105 {
106   // get the file with MPI_MODE_DELETE_ON_CLOSE and then close it
107   auto* f = new File(MPI_COMM_SELF, filename, MPI_MODE_DELETE_ON_CLOSE | MPI_MODE_RDWR, nullptr);
108   close(&f);
109   return MPI_SUCCESS;
110 }
111
112 int File::get_position(MPI_Offset* offset) const
113 {
114   *offset = file_->tell()/etype_->get_extent();
115   return MPI_SUCCESS;
116 }
117
118 int File::get_position_shared(MPI_Offset* offset) const
119 {
120   shared_mutex_->lock();
121   *offset = *shared_file_pointer_/etype_->get_extent();
122   shared_mutex_->unlock();
123   return MPI_SUCCESS;
124 }
125
126 int File::seek(MPI_Offset offset, int whence)
127 {
128   switch (whence) {
129     case MPI_SEEK_SET:
130       XBT_VERB("Seeking in MPI_File %s, setting offset %lld", file_->get_path(), offset);
131       file_->seek(offset, SEEK_SET);
132       break;
133     case MPI_SEEK_CUR:
134       XBT_VERB("Seeking in MPI_File %s, current offset + %lld", file_->get_path(), offset);
135       file_->seek(offset, SEEK_CUR);
136       break;
137     case MPI_SEEK_END:
138       XBT_VERB("Seeking in MPI_File %s, end offset + %lld", file_->get_path(), offset);
139       file_->seek(offset, SEEK_END);
140       break;
141     default:
142       return MPI_ERR_FILE;
143   }
144   return MPI_SUCCESS;
145 }
146
147 int File::seek_shared(MPI_Offset offset, int whence)
148 {
149   shared_mutex_->lock();
150   seek(offset, whence);
151   *shared_file_pointer_ = file_->tell();
152   shared_mutex_->unlock();
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   fh->shared_mutex_->lock();
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   fh->shared_mutex_->unlock();
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     fh->shared_mutex_->lock();
214     *(fh->shared_file_pointer_)=fh->file_->tell();
215     fh->shared_mutex_->unlock();
216   }
217   char c;
218   simgrid::smpi::colls::bcast(&c, 1, MPI_BYTE, fh->comm_->size() - 1, fh->comm_);
219   fh->seek(prev, MPI_SEEK_SET);
220   return ret;
221 }
222
223 int File::write(MPI_File fh, void* /*buf*/, int count, const Datatype* datatype, MPI_Status* status)
224 {
225   // get position first as we may be doing non contiguous reads and it will probably be updated badly
226   MPI_Offset position  = fh->file_->tell();
227   MPI_Offset movesize  = datatype->get_extent() * count;
228   MPI_Offset writesize = datatype->size() * count;
229   XBT_DEBUG("Position before write in MPI_File %s : %llu, size %llu", fh->file_->get_path(), fh->file_->tell(), fh->file_->size());
230   MPI_Offset write = fh->file_->write(writesize, true);
231   XBT_VERB("Write in MPI_File %s, %lld bytes written, count %d, writesize %lld bytes, movesize %lld", fh->file_->get_path(), write,
232            count, writesize, movesize);
233   if (writesize != movesize) {
234     fh->file_->seek(position + movesize, SEEK_SET);
235   }
236   XBT_VERB("Position after write in MPI_File %s : %llu", fh->file_->get_path(), fh->file_->tell());
237   if (status != MPI_STATUS_IGNORE)
238     status->count = count * datatype->size();
239   return MPI_SUCCESS;
240 }
241
242 int File::write_shared(MPI_File fh, const void* buf, int count, const Datatype* datatype, MPI_Status* status)
243 {
244   fh->shared_mutex_->lock();
245   XBT_DEBUG("Write shared on %s - Shared ptr before : %lld", fh->file_->get_path(), *(fh->shared_file_pointer_));
246   fh->seek(*(fh->shared_file_pointer_), MPI_SEEK_SET);
247   write(fh, const_cast<void*>(buf), count, datatype, status);
248   *(fh->shared_file_pointer_) = fh->file_->tell();
249   XBT_DEBUG("Write shared on %s - Shared ptr after : %lld", fh->file_->get_path(), *(fh->shared_file_pointer_));
250   fh->seek(*(fh->shared_file_pointer_), MPI_SEEK_SET);
251   fh->shared_mutex_->unlock();
252   return MPI_SUCCESS;
253 }
254
255 int File::write_ordered(MPI_File fh, const void* buf, int count, const Datatype* datatype, MPI_Status* status)
256 {
257   // 0 needs to get the shared pointer value
258   MPI_Offset val;
259   if (fh->comm_->rank() == 0) {
260     val = *(fh->shared_file_pointer_);
261   } else {
262     val = count * datatype->size();
263   }
264   MPI_Offset result;
265   simgrid::smpi::colls::scan(&val, &result, 1, MPI_OFFSET, MPI_SUM, fh->comm_);
266   MPI_Offset prev;
267   fh->get_position(&prev);
268   fh->seek(result, MPI_SEEK_SET);
269   int ret = fh->op_all<simgrid::smpi::File::write>(const_cast<void*>(buf), count, datatype, status);
270   if (fh->comm_->rank() == fh->comm_->size() - 1) {
271     fh->shared_mutex_->lock();
272     *(fh->shared_file_pointer_)=fh->file_->tell();
273     fh->shared_mutex_->unlock();
274   }
275   char c;
276   simgrid::smpi::colls::bcast(&c, 1, MPI_BYTE, fh->comm_->size() - 1, fh->comm_);
277   fh->seek(prev, MPI_SEEK_SET);
278   return ret;
279 }
280
281 int File::set_view(MPI_Offset disp, MPI_Datatype etype, MPI_Datatype filetype, const char* datarep, const Info*)
282 {
283   etype_    = etype;
284   filetype_ = filetype;
285   datarep_  = std::string(datarep);
286   disp_     = disp;
287   if (comm_->rank() == 0){
288     if(disp != MPI_DISPLACEMENT_CURRENT)
289       seek_shared(disp, MPI_SEEK_SET);
290     else
291       seek_shared(0, MPI_SEEK_CUR);
292   }
293   sync();
294   return MPI_SUCCESS;
295 }
296
297 int File::get_view(MPI_Offset* disp, MPI_Datatype* etype, MPI_Datatype* filetype, char* datarep) const
298 {
299   *disp     = disp_;
300   *etype    = etype_;
301   *filetype = filetype_;
302   snprintf(datarep, MPI_MAX_NAME_STRING + 1, "%s", datarep_.c_str());
303   return MPI_SUCCESS;
304 }
305
306 int File::size() const
307 {
308   return file_->size();
309 }
310
311 void File::set_size(int size)
312 {
313   file_->write(size, true);
314 }
315
316 int File::flags() const
317 {
318   return flags_;
319 }
320
321 MPI_Datatype File::etype() const
322 {
323   return etype_;
324 }
325
326 int File::sync()
327 {
328   // no idea
329   return simgrid::smpi::colls::barrier(comm_);
330 }
331
332 MPI_Info File::info()
333 {
334   return info_;
335 }
336
337 void File::set_info(MPI_Info info)
338 {
339   if (info_ != MPI_INFO_NULL)
340     simgrid::smpi::Info::unref(info_);
341   info_ = info;
342   if (info_ != MPI_INFO_NULL)
343     info_->ref();
344 }
345
346 MPI_Comm File::comm() const
347 {
348   return comm_;
349 }
350
351 MPI_Errhandler File::errhandler()
352 {
353   if (errhandler_ != MPI_ERRHANDLER_NULL)
354     errhandler_->ref();
355   return errhandler_;
356 }
357
358 void File::set_errhandler(MPI_Errhandler errhandler)
359 {
360   if (errhandler_ != MPI_ERRHANDLER_NULL)
361     simgrid::smpi::Errhandler::unref(errhandler_);
362   errhandler_ = errhandler;
363   if (errhandler_ != MPI_ERRHANDLER_NULL)
364     errhandler_->ref();
365 }
366
367 File* File::f2c(int id)
368 {
369   return static_cast<File*>(F2C::f2c(id));
370 }
371
372 void File::set_atomicity(bool a){
373   atomicity_ = a;
374 }
375
376 bool File::get_atomicity() const
377 {
378   return atomicity_;
379 }
380
381 } // namespace simgrid::smpi