Logo AND Algorithmique Numérique Distribuée

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