Logo AND Algorithmique Numérique Distribuée

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