Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
cleanups debug messages for read_all write_all
[simgrid.git] / src / smpi / mpi / smpi_file.cpp
1 /* Copyright (c) 2007-2019. The SimGrid Team. All rights reserved.          */\r
2 \r
3 /* This program is free software; you can redistribute it and/or modify it\r
4  * under the terms of the license (GNU LGPL) which comes with this package. */\r
5 #include "private.hpp"\r
6 \r
7 #include "smpi_comm.hpp"\r
8 #include "smpi_coll.hpp"\r
9 #include "smpi_datatype.hpp"\r
10 #include "smpi_info.hpp"\r
11 #include "smpi_win.hpp"\r
12 //setup here, because we have templates in smpi_file we want to log\r
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_io, smpi, "Logging specific to SMPI (RMA operations)");\r
14 \r
15 #include "smpi_file.hpp"\r
16 #include "smpi_status.hpp"\r
17 #include "simgrid/plugins/file_system.h"\r
18 \r
19 #define FP_SIZE sizeof(MPI_Offset)\r
20 \r
21 \r
22 namespace simgrid{\r
23 namespace smpi{\r
24 \r
25   File::File(MPI_Comm comm, char *filename, int amode, MPI_Info info): comm_(comm), flags_(amode), info_(info), shared_file_pointer_(0) {\r
26     file_= new simgrid::s4u::File(filename, nullptr);\r
27     list_=nullptr;\r
28     if (comm_->rank() == 0) {\r
29       int size= comm_->size() + FP_SIZE;\r
30       list_ = new char[size];\r
31       memset(list_, 0, size);\r
32       win_=new Win(list_, size, 1, MPI_INFO_NULL, comm_);\r
33     }else{\r
34       win_=new Win(list_, 0, 1, MPI_INFO_NULL, comm_);\r
35     }\r
36   }\r
37 \r
38   File::~File(){\r
39     delete file_;\r
40   }\r
41 \r
42   int File::close(MPI_File *fh){\r
43     XBT_DEBUG("Closing MPI_File %s", (*fh)->file_->get_path());\r
44     (*fh)->sync();\r
45     if((*fh)->flags() & MPI_MODE_DELETE_ON_CLOSE)\r
46       (*fh)->file_->unlink();\r
47     delete (*fh);\r
48     return MPI_SUCCESS;\r
49   }\r
50 \r
51   int File::del(char *filename, MPI_Info info){\r
52     //get the file with MPI_MODE_DELETE_ON_CLOSE and then close it\r
53     File* f = new File(MPI_COMM_SELF,filename,MPI_MODE_DELETE_ON_CLOSE|MPI_MODE_RDWR, nullptr);\r
54     close(&f);\r
55     return MPI_SUCCESS;\r
56   }\r
57 \r
58   int File::get_position(MPI_Offset* offset){\r
59     *offset=file_->tell();\r
60     return MPI_SUCCESS;\r
61   }\r
62 \r
63   int File::get_position_shared(MPI_Offset* offset){\r
64     lock();\r
65     *offset=shared_file_pointer_;\r
66     unlock();\r
67     return MPI_SUCCESS;\r
68   }\r
69 \r
70   int File::seek(MPI_Offset offset, int whence){\r
71     switch(whence){\r
72       case(MPI_SEEK_SET):\r
73         XBT_VERB("Seeking in MPI_File %s, setting offset %lld", file_->get_path(), offset);\r
74         file_->seek(offset,SEEK_SET);\r
75         break;\r
76       case(MPI_SEEK_CUR):\r
77         XBT_VERB("Seeking in MPI_File %s, current offset + %lld", file_->get_path(), offset);\r
78         file_->seek(offset,SEEK_CUR);\r
79         break;\r
80       case(MPI_SEEK_END):\r
81         XBT_VERB("Seeking in MPI_File %s, end offset + %lld", file_->get_path(), offset);\r
82         file_->seek(offset,SEEK_END);\r
83         break;\r
84       default:\r
85         return MPI_ERR_FILE;\r
86     }\r
87     return MPI_SUCCESS;\r
88   }\r
89 \r
90   int File::seek_shared(MPI_Offset offset, int whence){\r
91     lock();\r
92     seek(offset,whence);\r
93     shared_file_pointer_=file_->tell();\r
94     unlock();\r
95     return MPI_SUCCESS;\r
96   }\r
97 \r
98   int File::read(MPI_File fh, void *buf, int count, MPI_Datatype datatype, MPI_Status *status){\r
99     //get position first as we may be doing non contiguous reads and it will probably be updated badly\r
100     MPI_Offset position = fh->file_->tell();\r
101     MPI_Offset movesize = datatype->get_extent()*count;\r
102     MPI_Offset readsize = datatype->size()*count;\r
103     XBT_DEBUG("Position before read in MPI_File %s : %llu",fh->file_->get_path(),fh->file_->tell());\r
104     MPI_Offset read = fh->file_->read(readsize);\r
105     XBT_VERB("Read in MPI_File %s, %lld bytes read, readsize %lld bytes, movesize %lld", fh->file_->get_path(), read, readsize, movesize);\r
106     if(readsize!=movesize){\r
107       fh->file_->seek(position+movesize, SEEK_SET);\r
108     }\r
109     XBT_VERB("Position after read in MPI_File %s : %llu",fh->file_->get_path(), fh->file_->tell());\r
110     status->count=count*datatype->size();\r
111     return MPI_SUCCESS;\r
112   }\r
113 \r
114   /*Ordered and Shared Versions, with RMA-based locks : Based on the model described in :*/\r
115   /* @InProceedings{10.1007/11557265_15,*/\r
116   /* author="Latham, Robert and Ross, Robert and Thakur, Rajeev and Toonen, Brian",*/ \r
117   /* title="Implementing MPI-IO Shared File Pointers Without File System Support",*/\r
118   /* booktitle="Recent Advances in Parallel Virtual Machine and Message Passing Interface",*/\r
119   /* year="2005",*/\r
120   /* publisher="Springer Berlin Heidelberg",*/\r
121   /* address="Berlin, Heidelberg",*/\r
122   /* pages="84--93"*/\r
123   /* }*/\r
124   int File::read_shared(MPI_File fh, void *buf, int count, MPI_Datatype datatype, MPI_Status *status){\r
125     fh->lock();\r
126     fh->seek(fh->shared_file_pointer_,MPI_SEEK_SET);\r
127     read(fh, buf, count, datatype, status);\r
128     fh->shared_file_pointer_=fh->file_->tell();\r
129     fh->unlock();\r
130     return MPI_SUCCESS;\r
131   }\r
132 \r
133   int File::read_ordered(MPI_File fh, void *buf, int count, MPI_Datatype datatype, MPI_Status *status){\r
134     //0 needs to get the shared pointer value\r
135     if(fh->comm_->rank()==0){\r
136       fh->lock();\r
137       fh->unlock();\r
138     }else{\r
139       fh->shared_file_pointer_=count*datatype->size();\r
140     }\r
141     MPI_Offset result;\r
142     simgrid::smpi::Colls::scan(&(fh->shared_file_pointer_), &result, 1, MPI_OFFSET, MPI_SUM, fh->comm_);\r
143     fh->seek(result, MPI_SEEK_SET);\r
144     int ret = fh->op_all<simgrid::smpi::File::read>(buf, count, datatype, status);\r
145     if(fh->comm_->rank()==fh->comm_->size()-1){\r
146       fh->lock();\r
147       fh->unlock();\r
148     }\r
149     char c;\r
150     simgrid::smpi::Colls::bcast(&c, 1, MPI_BYTE, fh->comm_->size()-1, fh->comm_);\r
151     return ret;\r
152   }\r
153 \r
154   int File::write(MPI_File fh, void *buf, int count, MPI_Datatype datatype, MPI_Status *status){\r
155     //get position first as we may be doing non contiguous reads and it will probably be updated badly\r
156     MPI_Offset position = fh->file_->tell();\r
157     MPI_Offset movesize = datatype->get_extent()*count;\r
158     MPI_Offset writesize = datatype->size()*count;\r
159     XBT_DEBUG("Position before write in MPI_File %s : %llu",fh->file_->get_path(),fh->file_->tell());\r
160     MPI_Offset write = fh->file_->write(writesize);\r
161     XBT_VERB("Write in MPI_File %s, %lld bytes written, readsize %lld bytes, movesize %lld", fh->file_->get_path(), write, writesize, movesize);\r
162     if(writesize!=movesize){\r
163       fh->file_->seek(position+movesize, SEEK_SET);\r
164     }\r
165     XBT_VERB("Position after write in MPI_File %s : %llu",fh->file_->get_path(), fh->file_->tell());\r
166     status->count=count*datatype->size();\r
167     return MPI_SUCCESS;\r
168   }\r
169 \r
170   int File::write_shared(MPI_File fh, void *buf, int count, MPI_Datatype datatype, MPI_Status *status){\r
171     fh->lock();\r
172     fh->seek(fh->shared_file_pointer_,MPI_SEEK_SET);\r
173     write(fh, buf, count, datatype, status);\r
174     fh->shared_file_pointer_=fh->file_->tell();\r
175     fh->unlock();\r
176     return MPI_SUCCESS;\r
177   }\r
178 \r
179   int File::write_ordered(MPI_File fh, void *buf, int count, MPI_Datatype datatype, MPI_Status *status){\r
180     //0 needs to get the shared pointer value\r
181     if(fh->comm_->rank()==0){\r
182       fh->lock();\r
183       fh->unlock();\r
184     }else{\r
185       fh->shared_file_pointer_=count*datatype->size();\r
186     }\r
187     MPI_Offset result;\r
188     simgrid::smpi::Colls::scan(&(fh->shared_file_pointer_), &result, 1, MPI_OFFSET, MPI_SUM, fh->comm_);\r
189     fh->seek(result, MPI_SEEK_SET);\r
190     int ret = fh->op_all<simgrid::smpi::File::write>(buf, count, datatype, status);\r
191     if(fh->comm_->rank()==fh->comm_->size()-1){\r
192       fh->lock();\r
193       fh->unlock();\r
194     }\r
195     char c;\r
196     simgrid::smpi::Colls::bcast(&c, 1, MPI_BYTE, fh->comm_->size()-1, fh->comm_);\r
197     return ret;\r
198   }\r
199 \r
200   int File::size(){\r
201     return file_->size();\r
202   }\r
203 \r
204   int File::flags(){\r
205     return flags_;\r
206   }\r
207 \r
208   int File::sync(){\r
209     //no idea\r
210     return simgrid::smpi::Colls::barrier(comm_);\r
211   }\r
212 \r
213   int File::lock()\r
214 {\r
215   int rank = comm_->rank();\r
216   int size = comm_->size();\r
217   char waitlist[size];\r
218   char lock = 1;\r
219   int tag=444;\r
220   int i;\r
221   win_->lock(MPI_LOCK_EXCLUSIVE, 0, 0);\r
222   win_->put(&lock, 1, MPI_CHAR, 0, FP_SIZE+rank, 1, MPI_CHAR);\r
223   win_->get(waitlist, size, MPI_CHAR, 0, FP_SIZE, size, MPI_CHAR);\r
224   win_->get(&shared_file_pointer_ , 1 , MPI_OFFSET , 0 , 0, 1, MPI_OFFSET);\r
225   win_->unlock(0);\r
226   for (i = 0; i < size; i++) {\r
227     if (waitlist[i] == 1 && i != rank) {\r
228       // wait for the lock\r
229       MPI_Recv(&lock, 1, MPI_CHAR, MPI_ANY_SOURCE, tag, comm_, MPI_STATUS_IGNORE);\r
230       break;\r
231     }\r
232   }\r
233   return 0;\r
234 }\r
235 \r
236 int File::unlock()\r
237 {\r
238   int rank = comm_->rank();\r
239   int size = comm_->size();\r
240   char waitlist[size];\r
241   char lock = 0;\r
242   int tag=444;\r
243   int i, next;\r
244   win_->lock(MPI_LOCK_EXCLUSIVE, 0, 0);\r
245   win_->put(&lock, 1, MPI_CHAR, 0, FP_SIZE+rank, 1, MPI_CHAR);\r
246   win_->get(waitlist, size, MPI_CHAR, 0, FP_SIZE, size, MPI_CHAR);\r
247   shared_file_pointer_=file_->tell();\r
248   win_->put(&shared_file_pointer_, 1 , MPI_OFFSET , 0 , 0, 1, MPI_OFFSET);\r
249 \r
250   win_->unlock(0);\r
251   next = (rank + 1 + size) % size;\r
252   for (i = 0; i < size; i++, next = (next + 1) % size) {\r
253     if (waitlist[next] == 1) {\r
254       MPI_Send(&lock, 1, MPI_CHAR, next, tag, comm_);\r
255       break;\r
256     }\r
257   }\r
258   return 0;\r
259 }\r
260 \r
261 MPI_Info File::info(){\r
262   if(info_== MPI_INFO_NULL)\r
263     info_ = new Info();\r
264   info_->ref();\r
265   return info_;\r
266 }\r
267 \r
268 void File::set_info(MPI_Info info){\r
269   if(info_!= MPI_INFO_NULL)\r
270     info->ref();\r
271   info_=info;\r
272 }\r
273 \r
274 }\r
275 }\r