Logo AND Algorithmique Numérique Distribuée

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