1 /* Copyright (c) 2007-2020. 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 = MPI_ERRORS_RETURN;
29 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 if (simgrid::s4u::Host::current()->get_disks().empty())
34 xbt_die("SMPI/IO : Trying to open file on a diskless host ! Add one to your platform file");
36 size_t found=fullname.find('/');
37 //in case no fullpath is provided ... just pick the first mountpoint.
38 if(found==std::string::npos){
39 auto disk = simgrid::s4u::Host::current()->get_disks().front();
41 if (disk->get_host() != simgrid::s4u::Host::current())
42 mount = disk->extension<simgrid::s4u::FileSystemDiskExt>()->get_mount_point(disk->get_host());
44 mount = disk->extension<simgrid::s4u::FileSystemDiskExt>()->get_mount_point();
45 XBT_DEBUG("No absolute path given for file opening, use '%s'", mount.c_str());
47 fullname.insert(0, mount);
50 file_= new simgrid::s4u::File(fullname, nullptr);
52 if (comm_->rank() == 0) {
53 int size= comm_->size() + FP_SIZE;
54 list_ = new char[size];
55 errhandler_= SMPI_default_File_Errhandler;
57 memset(list_, 0, size);
58 shared_file_pointer_ = new MPI_Offset();
59 shared_mutex_ = s4u::Mutex::create();
60 *shared_file_pointer_ = 0;
61 win_=new Win(list_, size, 1, MPI_INFO_NULL, comm_);
63 errhandler_ = MPI_ERRHANDLER_NULL;
64 win_=new Win(list_, 0, 1, MPI_INFO_NULL, comm_);
66 simgrid::smpi::colls::bcast(&shared_file_pointer_, 1, MPI_AINT, 0, comm);
67 simgrid::smpi::colls::bcast(&shared_mutex_, 1, MPI_AINT, 0, comm);
68 if(comm_->rank() != 0)
69 intrusive_ptr_add_ref(&*shared_mutex_);
73 if(comm_->rank() == 0){
74 delete shared_file_pointer_;
79 if (info_ != MPI_INFO_NULL)
80 simgrid::smpi::Info::unref(info_);
81 if (errhandler_ != MPI_ERRHANDLER_NULL)
82 simgrid::smpi::Errhandler::unref(errhandler_);
85 int File::close(MPI_File *fh){
86 XBT_DEBUG("Closing MPI_File %s", (*fh)->file_->get_path());
88 if((*fh)->flags() & MPI_MODE_DELETE_ON_CLOSE)
89 (*fh)->file_->unlink();
94 int File::del(const char* filename, const Info*)
96 //get the file with MPI_MODE_DELETE_ON_CLOSE and then close it
97 File* f = new File(MPI_COMM_SELF,filename,MPI_MODE_DELETE_ON_CLOSE|MPI_MODE_RDWR, nullptr);
102 int File::get_position(MPI_Offset* offset){
103 *offset=file_->tell();
107 int File::get_position_shared(MPI_Offset* offset){
108 shared_mutex_->lock();
109 *offset=*shared_file_pointer_;
110 shared_mutex_->unlock();
114 int File::seek(MPI_Offset offset, int whence){
117 XBT_VERB("Seeking in MPI_File %s, setting offset %lld", file_->get_path(), offset);
118 file_->seek(offset,SEEK_SET);
121 XBT_VERB("Seeking in MPI_File %s, current offset + %lld", file_->get_path(), offset);
122 file_->seek(offset,SEEK_CUR);
125 XBT_VERB("Seeking in MPI_File %s, end offset + %lld", file_->get_path(), offset);
126 file_->seek(offset,SEEK_END);
134 int File::seek_shared(MPI_Offset offset, int whence){
135 shared_mutex_->lock();
137 *shared_file_pointer_=offset;
138 shared_mutex_->unlock();
142 int File::read(MPI_File fh, void* /*buf*/, int count, MPI_Datatype datatype, MPI_Status* status)
144 //get position first as we may be doing non contiguous reads and it will probably be updated badly
145 MPI_Offset position = fh->file_->tell();
146 MPI_Offset movesize = datatype->get_extent()*count;
147 MPI_Offset readsize = datatype->size()*count;
148 XBT_DEBUG("Position before read in MPI_File %s : %llu",fh->file_->get_path(),fh->file_->tell());
149 MPI_Offset read = fh->file_->read(readsize);
150 XBT_VERB("Read in MPI_File %s, %lld bytes read, readsize %lld bytes, movesize %lld", fh->file_->get_path(), read, readsize, movesize);
151 if(readsize!=movesize){
152 fh->file_->seek(position+movesize, SEEK_SET);
154 XBT_VERB("Position after read in MPI_File %s : %llu",fh->file_->get_path(), fh->file_->tell());
155 if(status != MPI_STATUS_IGNORE)
156 status->count=count*datatype->size();
160 /*Ordered and Shared Versions, with RMA-based locks : Based on the model described in :*/
161 /* @InProceedings{10.1007/11557265_15,*/
162 /* author="Latham, Robert and Ross, Robert and Thakur, Rajeev and Toonen, Brian",*/
163 /* title="Implementing MPI-IO Shared File Pointers Without File System Support",*/
164 /* booktitle="Recent Advances in Parallel Virtual Machine and Message Passing Interface",*/
166 /* publisher="Springer Berlin Heidelberg",*/
167 /* address="Berlin, Heidelberg",*/
170 int File::read_shared(MPI_File fh, void *buf, int count, MPI_Datatype datatype, MPI_Status *status){
171 fh->shared_mutex_->lock();
172 fh->seek(*(fh->shared_file_pointer_),MPI_SEEK_SET);
173 read(fh, buf, count, datatype, status);
174 *(fh->shared_file_pointer_)=fh->file_->tell();
175 fh->shared_mutex_->unlock();
179 int File::read_ordered(MPI_File fh, void *buf, int count, MPI_Datatype datatype, MPI_Status *status){
180 //0 needs to get the shared pointer value
182 if(fh->comm_->rank()==0){
183 val=*(fh->shared_file_pointer_);
185 val=count*datatype->size();
189 simgrid::smpi::colls::scan(&val, &result, 1, MPI_OFFSET, MPI_SUM, fh->comm_);
190 fh->seek(result, MPI_SEEK_SET);
191 int ret = fh->op_all<simgrid::smpi::File::read>(buf, count, datatype, status);
192 if(fh->comm_->rank()==fh->comm_->size()-1){
193 fh->shared_mutex_->lock();
194 *(fh->shared_file_pointer_)=fh->file_->tell();
195 fh->shared_mutex_->unlock();
198 simgrid::smpi::colls::bcast(&c, 1, MPI_BYTE, fh->comm_->size() - 1, fh->comm_);
202 int File::write(MPI_File fh, void* /*buf*/, int count, MPI_Datatype datatype, MPI_Status* status)
204 //get position first as we may be doing non contiguous reads and it will probably be updated badly
205 MPI_Offset position = fh->file_->tell();
206 MPI_Offset movesize = datatype->get_extent()*count;
207 MPI_Offset writesize = datatype->size()*count;
208 XBT_DEBUG("Position before write in MPI_File %s : %llu",fh->file_->get_path(),fh->file_->tell());
209 MPI_Offset write = fh->file_->write(writesize, true);
210 XBT_VERB("Write in MPI_File %s, %lld bytes written, readsize %lld bytes, movesize %lld", fh->file_->get_path(), write, writesize, movesize);
211 if(writesize!=movesize){
212 fh->file_->seek(position+movesize, SEEK_SET);
214 XBT_VERB("Position after write in MPI_File %s : %llu",fh->file_->get_path(), fh->file_->tell());
215 if(status != MPI_STATUS_IGNORE)
216 status->count=count*datatype->size();
220 int File::write_shared(MPI_File fh, const void *buf, int count, MPI_Datatype datatype, MPI_Status *status){
221 fh->shared_mutex_->lock();
222 fh->seek(*(fh->shared_file_pointer_),MPI_SEEK_SET);
223 write(fh, const_cast<void*>(buf), count, datatype, status);
224 *(fh->shared_file_pointer_)=fh->file_->tell();
225 fh->shared_mutex_->unlock();
229 int File::write_ordered(MPI_File fh, const void *buf, int count, MPI_Datatype datatype, MPI_Status *status){
230 //0 needs to get the shared pointer value
232 if(fh->comm_->rank()==0){
233 val=*(fh->shared_file_pointer_);
235 val=count*datatype->size();
238 simgrid::smpi::colls::scan(&val, &result, 1, MPI_OFFSET, MPI_SUM, fh->comm_);
239 fh->seek(result, MPI_SEEK_SET);
240 int ret = fh->op_all<simgrid::smpi::File::write>(const_cast<void*>(buf), count, datatype, status);
241 if(fh->comm_->rank()==fh->comm_->size()-1){
242 fh->shared_mutex_->lock();
243 *(fh->shared_file_pointer_)=fh->file_->tell();
244 fh->shared_mutex_->unlock();
247 simgrid::smpi::colls::bcast(&c, 1, MPI_BYTE, fh->comm_->size() - 1, fh->comm_);
251 int File::set_view(MPI_Offset /*disp*/, MPI_Datatype etype, MPI_Datatype filetype, const char* datarep, const Info*)
255 datarep_=std::string(datarep);
256 seek_shared(0,MPI_SEEK_SET);
260 int File::get_view(MPI_Offset* /*disp*/, MPI_Datatype* etype, MPI_Datatype* filetype, char* datarep)
264 snprintf(datarep, MPI_MAX_NAME_STRING+1, "%s", datarep_.c_str());
269 return file_->size();
278 return simgrid::smpi::colls::barrier(comm_);
281 MPI_Info File::info()
283 if (info_ == MPI_INFO_NULL)
289 void File::set_info(MPI_Info info)
291 if (info_ != MPI_INFO_NULL)
292 simgrid::smpi::Info::unref(info_);
294 if (info_ != MPI_INFO_NULL)
298 MPI_Comm File::comm(){
302 MPI_Errhandler File::errhandler()
304 if (errhandler_ != MPI_ERRHANDLER_NULL)
309 void File::set_errhandler(MPI_Errhandler errhandler)
311 if (errhandler_ != MPI_ERRHANDLER_NULL)
312 simgrid::smpi::Errhandler::unref(errhandler_);
313 errhandler_ = errhandler;
314 if (errhandler_ != MPI_ERRHANDLER_NULL)