Logo AND Algorithmique Numérique Distribuée

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