Logo AND Algorithmique Numérique Distribuée

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