Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Unlike errors on communicators and windows, the default behavior for files is to...
[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/plugins/file_system.h"
17
18 #define FP_SIZE sizeof(MPI_Offset)
19
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_io, smpi, "Logging specific to SMPI (RMA operations)");
21
22 namespace simgrid{
23 namespace smpi{
24
25   File::File(MPI_Comm comm, const char *filename, int amode, MPI_Info info): comm_(comm), flags_(amode), info_(info) {
26     file_= new simgrid::s4u::File(filename, nullptr);
27     list_=nullptr;
28     if (comm_->rank() == 0) {
29       int size= comm_->size() + FP_SIZE;
30       list_ = new char[size];
31       errhandler_=MPI_ERRORS_RETURN;
32       memset(list_, 0, size);
33       shared_file_pointer_ = new MPI_Offset();
34       shared_mutex_ = s4u::Mutex::create();
35       *shared_file_pointer_ = 0;
36       win_=new Win(list_, size, 1, MPI_INFO_NULL, comm_);
37     }else{
38       win_=new Win(list_, 0, 1, MPI_INFO_NULL, comm_);
39     }
40     simgrid::smpi::Colls::bcast(&shared_file_pointer_, 1, MPI_AINT, 0, comm);
41     simgrid::smpi::Colls::bcast(&shared_mutex_, 1, MPI_AINT, 0, comm);
42     if(comm_->rank() != 0)
43       intrusive_ptr_add_ref(&*shared_mutex_);
44   }
45
46   File::~File(){
47     if(comm_->rank() == 0){
48       delete shared_file_pointer_;
49       delete[] list_;
50     }
51     delete win_;
52     delete file_;
53   }
54
55   int File::close(MPI_File *fh){
56     XBT_DEBUG("Closing MPI_File %s", (*fh)->file_->get_path());
57     (*fh)->sync();
58     if((*fh)->flags() & MPI_MODE_DELETE_ON_CLOSE)
59       (*fh)->file_->unlink();
60     delete (*fh);
61     return MPI_SUCCESS;
62   }
63
64   int File::del(const char* filename, MPI_Info)
65   {
66     //get the file with MPI_MODE_DELETE_ON_CLOSE and then close it
67     File* f = new File(MPI_COMM_SELF,filename,MPI_MODE_DELETE_ON_CLOSE|MPI_MODE_RDWR, nullptr);
68     close(&f);
69     return MPI_SUCCESS;
70   }
71
72   int File::get_position(MPI_Offset* offset){
73     *offset=file_->tell();
74     return MPI_SUCCESS;
75   }
76
77   int File::get_position_shared(MPI_Offset* offset){
78     shared_mutex_->lock();
79     *offset=*shared_file_pointer_;
80     shared_mutex_->unlock();
81     return MPI_SUCCESS;
82   }
83
84   int File::seek(MPI_Offset offset, int whence){
85     switch(whence){
86       case(MPI_SEEK_SET):
87         XBT_VERB("Seeking in MPI_File %s, setting offset %lld", file_->get_path(), offset);
88         file_->seek(offset,SEEK_SET);
89         break;
90       case(MPI_SEEK_CUR):
91         XBT_VERB("Seeking in MPI_File %s, current offset + %lld", file_->get_path(), offset);
92         file_->seek(offset,SEEK_CUR);
93         break;
94       case(MPI_SEEK_END):
95         XBT_VERB("Seeking in MPI_File %s, end offset + %lld", file_->get_path(), offset);
96         file_->seek(offset,SEEK_END);
97         break;
98       default:
99         return MPI_ERR_FILE;
100     }
101     return MPI_SUCCESS;
102   }
103
104   int File::seek_shared(MPI_Offset offset, int whence){
105     shared_mutex_->lock();
106     seek(offset,whence);
107     *shared_file_pointer_=offset;
108     shared_mutex_->unlock();
109     return MPI_SUCCESS;
110   }
111
112   int File::read(MPI_File fh, void* /*buf*/, int count, MPI_Datatype datatype, MPI_Status* status)
113   {
114     //get position first as we may be doing non contiguous reads and it will probably be updated badly
115     MPI_Offset position = fh->file_->tell();
116     MPI_Offset movesize = datatype->get_extent()*count;
117     MPI_Offset readsize = datatype->size()*count;
118     XBT_DEBUG("Position before read in MPI_File %s : %llu",fh->file_->get_path(),fh->file_->tell());
119     MPI_Offset read = fh->file_->read(readsize);
120     XBT_VERB("Read in MPI_File %s, %lld bytes read, readsize %lld bytes, movesize %lld", fh->file_->get_path(), read, readsize, movesize);
121     if(readsize!=movesize){
122       fh->file_->seek(position+movesize, SEEK_SET);
123     }
124     XBT_VERB("Position after read in MPI_File %s : %llu",fh->file_->get_path(), fh->file_->tell());
125     status->count=count*datatype->size();
126     return MPI_SUCCESS;
127   }
128
129   /*Ordered and Shared Versions, with RMA-based locks : Based on the model described in :*/
130   /* @InProceedings{10.1007/11557265_15,*/
131   /* author="Latham, Robert and Ross, Robert and Thakur, Rajeev and Toonen, Brian",*/ 
132   /* title="Implementing MPI-IO Shared File Pointers Without File System Support",*/
133   /* booktitle="Recent Advances in Parallel Virtual Machine and Message Passing Interface",*/
134   /* year="2005",*/
135   /* publisher="Springer Berlin Heidelberg",*/
136   /* address="Berlin, Heidelberg",*/
137   /* pages="84--93"*/
138   /* }*/
139   int File::read_shared(MPI_File fh, void *buf, int count, MPI_Datatype datatype, MPI_Status *status){
140     fh->shared_mutex_->lock();
141     fh->seek(*(fh->shared_file_pointer_),MPI_SEEK_SET);
142     read(fh, buf, count, datatype, status);
143     *(fh->shared_file_pointer_)=fh->file_->tell();
144     fh->shared_mutex_->unlock();
145     return MPI_SUCCESS;
146   }
147
148   int File::read_ordered(MPI_File fh, void *buf, int count, MPI_Datatype datatype, MPI_Status *status){
149     //0 needs to get the shared pointer value
150     MPI_Offset val;
151     if(fh->comm_->rank()==0){
152       val=*(fh->shared_file_pointer_);
153     }else{
154       val=count*datatype->size();
155     }
156
157     MPI_Offset result;
158     simgrid::smpi::Colls::scan(&val, &result, 1, MPI_OFFSET, MPI_SUM, fh->comm_);
159     fh->seek(result, MPI_SEEK_SET);
160     int ret = fh->op_all<simgrid::smpi::File::read>(buf, count, datatype, status);
161     if(fh->comm_->rank()==fh->comm_->size()-1){
162       fh->shared_mutex_->lock();
163       *(fh->shared_file_pointer_)=fh->file_->tell();
164       fh->shared_mutex_->unlock();
165     }
166     char c;
167     simgrid::smpi::Colls::bcast(&c, 1, MPI_BYTE, fh->comm_->size()-1, fh->comm_);
168     return ret;
169   }
170
171   int File::write(MPI_File fh, void* /*buf*/, int count, MPI_Datatype datatype, MPI_Status* status)
172   {
173     //get position first as we may be doing non contiguous reads and it will probably be updated badly
174     MPI_Offset position = fh->file_->tell();
175     MPI_Offset movesize = datatype->get_extent()*count;
176     MPI_Offset writesize = datatype->size()*count;
177     XBT_DEBUG("Position before write in MPI_File %s : %llu",fh->file_->get_path(),fh->file_->tell());
178     MPI_Offset write = fh->file_->write(writesize, 1);
179     XBT_VERB("Write in MPI_File %s, %lld bytes written, readsize %lld bytes, movesize %lld", fh->file_->get_path(), write, writesize, movesize);
180     if(writesize!=movesize){
181       fh->file_->seek(position+movesize, SEEK_SET);
182     }
183     XBT_VERB("Position after write in MPI_File %s : %llu",fh->file_->get_path(), fh->file_->tell());
184     status->count=count*datatype->size();
185     return MPI_SUCCESS;
186   }
187
188   int File::write_shared(MPI_File fh, const void *buf, int count, MPI_Datatype datatype, MPI_Status *status){
189     fh->shared_mutex_->lock();
190     fh->seek(*(fh->shared_file_pointer_),MPI_SEEK_SET);
191     write(fh, const_cast<void*>(buf), count, datatype, status);
192     *(fh->shared_file_pointer_)=fh->file_->tell();
193     fh->shared_mutex_->unlock();
194     return MPI_SUCCESS;
195   }
196
197   int File::write_ordered(MPI_File fh, const void *buf, int count, MPI_Datatype datatype, MPI_Status *status){
198     //0 needs to get the shared pointer value
199     MPI_Offset val;
200     if(fh->comm_->rank()==0){
201       val=*(fh->shared_file_pointer_);
202     }else{
203       val=count*datatype->size();
204     }
205     MPI_Offset result;
206     simgrid::smpi::Colls::scan(&val, &result, 1, MPI_OFFSET, MPI_SUM, fh->comm_);
207     fh->seek(result, MPI_SEEK_SET);
208     int ret = fh->op_all<simgrid::smpi::File::write>(const_cast<void*>(buf), count, datatype, status);
209     if(fh->comm_->rank()==fh->comm_->size()-1){
210       fh->shared_mutex_->lock();
211       *(fh->shared_file_pointer_)=fh->file_->tell();
212       fh->shared_mutex_->unlock();
213     }
214     char c;
215     simgrid::smpi::Colls::bcast(&c, 1, MPI_BYTE, fh->comm_->size()-1, fh->comm_);
216     return ret;
217   }
218
219   int File::size(){
220     return file_->size();
221   }
222
223   int File::flags(){
224     return flags_;
225   }
226
227   int File::sync(){
228     //no idea
229     return simgrid::smpi::Colls::barrier(comm_);
230   }
231
232   MPI_Info File::info(){
233     if(info_== MPI_INFO_NULL)
234       info_ = new Info();
235     info_->ref();
236     return info_;
237   }
238
239   void File::set_info(MPI_Info info){
240     if(info_!= MPI_INFO_NULL)
241       info->ref();
242     info_=info;
243   }
244
245   MPI_Comm File::comm(){
246     return comm_;
247   }
248
249   MPI_Errhandler File::errhandler(){
250     return errhandler_;
251   }
252
253   void File::set_errhandler(MPI_Errhandler errhandler){
254     errhandler_=errhandler;
255     if(errhandler_!= MPI_ERRHANDLER_NULL)
256       errhandler->ref();
257   }
258 }
259 }