Logo AND Algorithmique Numérique Distribuée

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