1 /* Copyright (c) 2007-2022. The SimGrid Team. All rights reserved. */
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. */
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"
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"
20 #define FP_SIZE sizeof(MPI_Offset)
22 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_io, smpi, "Logging specific to SMPI (RMA operations)");
24 MPI_Errhandler SMPI_default_File_Errhandler = _smpi_cfg_default_errhandler_is_error ? MPI_ERRORS_ARE_FATAL : MPI_ERRORS_RETURN;;
26 namespace simgrid::smpi {
28 File::File(MPI_Comm comm, const char* filename, int amode, MPI_Info info) : comm_(comm), flags_(amode), info_(info)
30 if (info_ != MPI_INFO_NULL)
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");
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 auto disk = simgrid::s4u::Host::current()->get_disks().front();
40 if (disk->get_host() != simgrid::s4u::Host::current())
41 mount = disk->extension<simgrid::s4u::FileSystemDiskExt>()->get_mount_point(disk->get_host());
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);
49 fullname.insert(0, mount);
53 file_ = simgrid::s4u::File::open(fullname, nullptr);
55 if (comm_->rank() == 0) {
56 int size = comm_->size() + FP_SIZE;
57 list_ = new char[size];
58 errhandler_ = SMPI_default_File_Errhandler;
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_);
66 errhandler_ = MPI_ERRHANDLER_NULL;
67 win_ = new Win(list_, 0, 1, MPI_INFO_NULL, comm_);
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_);
78 if (comm_->rank() == 0) {
79 delete shared_file_pointer_;
82 simgrid::smpi::Win::del(win_);
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_);
91 int File::close(MPI_File* fh)
93 XBT_DEBUG("Closing MPI_File %s", (*fh)->file_->get_path());
95 if ((*fh)->flags() & MPI_MODE_DELETE_ON_CLOSE)
96 (*fh)->file_->unlink();
101 int File::del(const char* filename, const Info*)
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);
109 int File::get_position(MPI_Offset* offset) const
111 *offset = file_->tell();
115 int File::get_position_shared(MPI_Offset* offset) const
117 shared_mutex_->lock();
118 *offset = *shared_file_pointer_;
119 shared_mutex_->unlock();
123 int File::seek(MPI_Offset offset, int whence)
127 XBT_VERB("Seeking in MPI_File %s, setting offset %lld", file_->get_path(), offset);
128 file_->seek(offset, SEEK_SET);
131 XBT_VERB("Seeking in MPI_File %s, current offset + %lld", file_->get_path(), offset);
132 file_->seek(offset, SEEK_CUR);
135 XBT_VERB("Seeking in MPI_File %s, end offset + %lld", file_->get_path(), offset);
136 file_->seek(offset, SEEK_END);
144 int File::seek_shared(MPI_Offset offset, int whence)
146 shared_mutex_->lock();
147 seek(offset, whence);
148 *shared_file_pointer_ = file_->tell();
149 shared_mutex_->unlock();
153 int File::read(MPI_File fh, void* /*buf*/, int count, const Datatype* datatype, MPI_Status* status)
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,
163 if (readsize != movesize) {
164 fh->file_->seek(position + movesize, SEEK_SET);
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();
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",*/
178 /* publisher="Springer Berlin Heidelberg",*/
179 /* address="Berlin, Heidelberg",*/
182 int File::read_shared(MPI_File fh, void* buf, int count, const Datatype* datatype, MPI_Status* status)
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();
192 int File::read_ordered(MPI_File fh, void* buf, int count, const Datatype* datatype, MPI_Status* status)
194 // 0 needs to get the shared pointer value
196 if (fh->comm_->rank() == 0) {
197 val = *(fh->shared_file_pointer_);
199 val = count * datatype->size();
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();
212 simgrid::smpi::colls::bcast(&c, 1, MPI_BYTE, fh->comm_->size() - 1, fh->comm_);
216 int File::write(MPI_File fh, void* /*buf*/, int count, const Datatype* datatype, MPI_Status* status)
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);
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();
235 int File::write_shared(MPI_File fh, const void* buf, int count, const Datatype* datatype, MPI_Status* status)
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();
247 int File::write_ordered(MPI_File fh, const void* buf, int count, const Datatype* datatype, MPI_Status* status)
249 // 0 needs to get the shared pointer value
251 if (fh->comm_->rank() == 0) {
252 val = *(fh->shared_file_pointer_);
254 val = count * datatype->size();
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();
266 simgrid::smpi::colls::bcast(&c, 1, MPI_BYTE, fh->comm_->size() - 1, fh->comm_);
270 int File::set_view(MPI_Offset /*disp*/, MPI_Datatype etype, MPI_Datatype filetype, const char* datarep, const Info*)
273 filetype_ = filetype;
274 datarep_ = std::string(datarep);
275 seek_shared(0, MPI_SEEK_SET);
279 int File::get_view(MPI_Offset* /*disp*/, MPI_Datatype* etype, MPI_Datatype* filetype, char* datarep) const
282 *filetype = filetype_;
283 snprintf(datarep, MPI_MAX_NAME_STRING + 1, "%s", datarep_.c_str());
287 int File::size() const
289 return file_->size();
292 void File::set_size(int size)
294 file_->write(size, true);
297 int File::flags() const
305 return simgrid::smpi::colls::barrier(comm_);
308 MPI_Info File::info()
313 void File::set_info(MPI_Info info)
315 if (info_ != MPI_INFO_NULL)
316 simgrid::smpi::Info::unref(info_);
318 if (info_ != MPI_INFO_NULL)
322 MPI_Comm File::comm() const
327 MPI_Errhandler File::errhandler()
329 if (errhandler_ != MPI_ERRHANDLER_NULL)
334 void File::set_errhandler(MPI_Errhandler errhandler)
336 if (errhandler_ != MPI_ERRHANDLER_NULL)
337 simgrid::smpi::Errhandler::unref(errhandler_);
338 errhandler_ = errhandler;
339 if (errhandler_ != MPI_ERRHANDLER_NULL)
343 File* File::f2c(int id)
345 return static_cast<File*>(F2C::f2c(id));
347 } // namespace simgrid::smpi