Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'dev-cpuimpl-used' into 'master'
[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
53   file_ = simgrid::s4u::File::open(fullname, nullptr);
54   list_ = nullptr;
55   if (comm_->rank() == 0) {
56     int size    = comm_->size() + FP_SIZE;
57     list_       = new char[size];
58     errhandler_ = SMPI_default_File_Errhandler;
59     errhandler_->ref();
60     memset(list_, 0, size);
61     shared_file_pointer_  = new MPI_Offset();
62     shared_mutex_         = s4u::Mutex::create();
63     *shared_file_pointer_ = 0;
64     win_                  = new Win(list_, size, 1, MPI_INFO_NULL, comm_);
65   } else {
66     errhandler_ = MPI_ERRHANDLER_NULL;
67     win_        = new Win(list_, 0, 1, MPI_INFO_NULL, comm_);
68   }
69   simgrid::smpi::colls::bcast(&shared_file_pointer_, 1, MPI_AINT, 0, comm);
70   simgrid::smpi::colls::bcast(&shared_mutex_, 1, MPI_AINT, 0, comm);
71   if (comm_->rank() != 0)
72     intrusive_ptr_add_ref(&*shared_mutex_);
73   this->add_f();
74 }
75
76 File::~File()
77 {
78   if (comm_->rank() == 0) {
79     delete shared_file_pointer_;
80     delete[] list_;
81   }
82   simgrid::smpi::Win::del(win_);
83   file_->close();
84   F2C::free_f(this->f2c_id());
85   if (info_ != MPI_INFO_NULL)
86     simgrid::smpi::Info::unref(info_);
87   if (errhandler_ != MPI_ERRHANDLER_NULL)
88     simgrid::smpi::Errhandler::unref(errhandler_);
89 }
90
91 int File::close(MPI_File* fh)
92 {
93   XBT_DEBUG("Closing MPI_File %s", (*fh)->file_->get_path());
94   (*fh)->sync();
95   if ((*fh)->flags() & MPI_MODE_DELETE_ON_CLOSE)
96     (*fh)->file_->unlink();
97   delete (*fh);
98   return MPI_SUCCESS;
99 }
100
101 int File::del(const char* filename, const Info*)
102 {
103   // get the file with MPI_MODE_DELETE_ON_CLOSE and then close it
104   auto* f = new File(MPI_COMM_SELF, filename, MPI_MODE_DELETE_ON_CLOSE | MPI_MODE_RDWR, nullptr);
105   close(&f);
106   return MPI_SUCCESS;
107 }
108
109 int File::get_position(MPI_Offset* offset) const
110 {
111   *offset = file_->tell();
112   return MPI_SUCCESS;
113 }
114
115 int File::get_position_shared(MPI_Offset* offset) const
116 {
117   shared_mutex_->lock();
118   *offset = *shared_file_pointer_;
119   shared_mutex_->unlock();
120   return MPI_SUCCESS;
121 }
122
123 int File::seek(MPI_Offset offset, int whence)
124 {
125   switch (whence) {
126     case MPI_SEEK_SET:
127       XBT_VERB("Seeking in MPI_File %s, setting offset %lld", file_->get_path(), offset);
128       file_->seek(offset, SEEK_SET);
129       break;
130     case MPI_SEEK_CUR:
131       XBT_VERB("Seeking in MPI_File %s, current offset + %lld", file_->get_path(), offset);
132       file_->seek(offset, SEEK_CUR);
133       break;
134     case MPI_SEEK_END:
135       XBT_VERB("Seeking in MPI_File %s, end offset + %lld", file_->get_path(), offset);
136       file_->seek(offset, SEEK_END);
137       break;
138     default:
139       return MPI_ERR_FILE;
140   }
141   return MPI_SUCCESS;
142 }
143
144 int File::seek_shared(MPI_Offset offset, int whence)
145 {
146   shared_mutex_->lock();
147   seek(offset, whence);
148   *shared_file_pointer_ = file_->tell();
149   shared_mutex_->unlock();
150   return MPI_SUCCESS;
151 }
152
153 int File::read(MPI_File fh, void* /*buf*/, int count, const Datatype* datatype, MPI_Status* status)
154 {
155   // get position first as we may be doing non contiguous reads and it will probably be updated badly
156   MPI_Offset position = fh->file_->tell();
157   MPI_Offset movesize = datatype->get_extent() * count;
158   MPI_Offset readsize = datatype->size() * count;
159   XBT_DEBUG("Position before read in MPI_File %s : %llu", fh->file_->get_path(), fh->file_->tell());
160   MPI_Offset read = fh->file_->read(readsize);
161   XBT_VERB("Read in MPI_File %s, %lld bytes read, readsize %lld bytes, movesize %lld", fh->file_->get_path(), read,
162            readsize, movesize);
163   if (readsize != movesize) {
164     fh->file_->seek(position + movesize, SEEK_SET);
165   }
166   XBT_VERB("Position after read in MPI_File %s : %llu", fh->file_->get_path(), fh->file_->tell());
167   if (status != MPI_STATUS_IGNORE)
168     status->count = count * datatype->size();
169   return MPI_SUCCESS;
170 }
171
172 /*Ordered and Shared Versions, with RMA-based locks : Based on the model described in :*/
173 /* @InProceedings{10.1007/11557265_15,*/
174 /* author="Latham, Robert and Ross, Robert and Thakur, Rajeev and Toonen, Brian",*/
175 /* title="Implementing MPI-IO Shared File Pointers Without File System Support",*/
176 /* booktitle="Recent Advances in Parallel Virtual Machine and Message Passing Interface",*/
177 /* year="2005",*/
178 /* publisher="Springer Berlin Heidelberg",*/
179 /* address="Berlin, Heidelberg",*/
180 /* pages="84--93"*/
181 /* }*/
182 int File::read_shared(MPI_File fh, void* buf, int count, const Datatype* datatype, MPI_Status* status)
183 {
184   fh->shared_mutex_->lock();
185   fh->seek(*(fh->shared_file_pointer_), MPI_SEEK_SET);
186   read(fh, buf, count, datatype, status);
187   *(fh->shared_file_pointer_) = fh->file_->tell();
188   fh->shared_mutex_->unlock();
189   return MPI_SUCCESS;
190 }
191
192 int File::read_ordered(MPI_File fh, void* buf, int count, const Datatype* datatype, MPI_Status* status)
193 {
194   // 0 needs to get the shared pointer value
195   MPI_Offset val;
196   if (fh->comm_->rank() == 0) {
197     val = *(fh->shared_file_pointer_);
198   } else {
199     val = count * datatype->size();
200   }
201
202   MPI_Offset result;
203   simgrid::smpi::colls::scan(&val, &result, 1, MPI_OFFSET, MPI_SUM, fh->comm_);
204   fh->seek(result, MPI_SEEK_SET);
205   int ret = fh->op_all<simgrid::smpi::File::read>(buf, count, datatype, status);
206   if (fh->comm_->rank() == fh->comm_->size() - 1) {
207     fh->shared_mutex_->lock();
208     *(fh->shared_file_pointer_)=fh->file_->tell();
209     fh->shared_mutex_->unlock();
210   }
211   char c;
212   simgrid::smpi::colls::bcast(&c, 1, MPI_BYTE, fh->comm_->size() - 1, fh->comm_);
213   return ret;
214 }
215
216 int File::write(MPI_File fh, void* /*buf*/, int count, const Datatype* datatype, MPI_Status* status)
217 {
218   // get position first as we may be doing non contiguous reads and it will probably be updated badly
219   MPI_Offset position  = fh->file_->tell();
220   MPI_Offset movesize  = datatype->get_extent() * count;
221   MPI_Offset writesize = datatype->size() * count;
222   XBT_DEBUG("Position before write in MPI_File %s : %llu", fh->file_->get_path(), fh->file_->tell());
223   MPI_Offset write = fh->file_->write(writesize, true);
224   XBT_VERB("Write in MPI_File %s, %lld bytes written, readsize %lld bytes, movesize %lld", fh->file_->get_path(), write,
225            writesize, movesize);
226   if (writesize != movesize) {
227     fh->file_->seek(position + movesize, SEEK_SET);
228   }
229   XBT_VERB("Position after write in MPI_File %s : %llu", fh->file_->get_path(), fh->file_->tell());
230   if (status != MPI_STATUS_IGNORE)
231     status->count = count * datatype->size();
232   return MPI_SUCCESS;
233 }
234
235 int File::write_shared(MPI_File fh, const void* buf, int count, const Datatype* datatype, MPI_Status* status)
236 {
237   fh->shared_mutex_->lock();
238   XBT_DEBUG("Write shared on %s - Shared ptr before : %lld", fh->file_->get_path(), *(fh->shared_file_pointer_));
239   fh->seek(*(fh->shared_file_pointer_), MPI_SEEK_SET);
240   write(fh, const_cast<void*>(buf), count, datatype, status);
241   *(fh->shared_file_pointer_) = fh->file_->tell();
242   XBT_DEBUG("Write shared on %s - Shared ptr after : %lld", fh->file_->get_path(), *(fh->shared_file_pointer_));
243   fh->shared_mutex_->unlock();
244   return MPI_SUCCESS;
245 }
246
247 int File::write_ordered(MPI_File fh, const void* buf, int count, const Datatype* datatype, MPI_Status* status)
248 {
249   // 0 needs to get the shared pointer value
250   MPI_Offset val;
251   if (fh->comm_->rank() == 0) {
252     val = *(fh->shared_file_pointer_);
253   } else {
254     val = count * datatype->size();
255   }
256   MPI_Offset result;
257   simgrid::smpi::colls::scan(&val, &result, 1, MPI_OFFSET, MPI_SUM, fh->comm_);
258   fh->seek(result, MPI_SEEK_SET);
259   int ret = fh->op_all<simgrid::smpi::File::write>(const_cast<void*>(buf), count, datatype, status);
260   if (fh->comm_->rank() == fh->comm_->size() - 1) {
261     fh->shared_mutex_->lock();
262     *(fh->shared_file_pointer_)=fh->file_->tell();
263     fh->shared_mutex_->unlock();
264   }
265   char c;
266   simgrid::smpi::colls::bcast(&c, 1, MPI_BYTE, fh->comm_->size() - 1, fh->comm_);
267   return ret;
268 }
269
270 int File::set_view(MPI_Offset /*disp*/, MPI_Datatype etype, MPI_Datatype filetype, const char* datarep, const Info*)
271 {
272   etype_    = etype;
273   filetype_ = filetype;
274   datarep_  = std::string(datarep);
275   seek_shared(0, MPI_SEEK_SET);
276   return MPI_SUCCESS;
277 }
278
279 int File::get_view(MPI_Offset* /*disp*/, MPI_Datatype* etype, MPI_Datatype* filetype, char* datarep) const
280 {
281   *etype    = etype_;
282   *filetype = filetype_;
283   snprintf(datarep, MPI_MAX_NAME_STRING + 1, "%s", datarep_.c_str());
284   return MPI_SUCCESS;
285 }
286
287 int File::size() const
288 {
289   return file_->size();
290 }
291
292 void File::set_size(int size)
293 {
294   file_->write(size, true);
295 }
296
297 int File::flags() const
298 {
299   return flags_;
300 }
301
302 int File::sync()
303 {
304   // no idea
305   return simgrid::smpi::colls::barrier(comm_);
306 }
307
308 MPI_Info File::info()
309 {
310   return info_;
311 }
312
313 void File::set_info(MPI_Info info)
314 {
315   if (info_ != MPI_INFO_NULL)
316     simgrid::smpi::Info::unref(info_);
317   info_ = info;
318   if (info_ != MPI_INFO_NULL)
319     info_->ref();
320 }
321
322 MPI_Comm File::comm() const
323 {
324   return comm_;
325 }
326
327 MPI_Errhandler File::errhandler()
328 {
329   if (errhandler_ != MPI_ERRHANDLER_NULL)
330     errhandler_->ref();
331   return errhandler_;
332 }
333
334 void File::set_errhandler(MPI_Errhandler errhandler)
335 {
336   if (errhandler_ != MPI_ERRHANDLER_NULL)
337     simgrid::smpi::Errhandler::unref(errhandler_);
338   errhandler_ = errhandler;
339   if (errhandler_ != MPI_ERRHANDLER_NULL)
340     errhandler_->ref();
341 }
342
343 File* File::f2c(int id)
344 {
345   return static_cast<File*>(F2C::f2c(id));
346 }
347 } // namespace simgrid::smpi