1 /* Copyright (c) 2007-2019. The SimGrid Team. All rights reserved. */
\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
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
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_io, smpi, "Logging specific to SMPI (RMA operations)");
\r
17 #define FP_SIZE sizeof(MPI_Offset)
\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
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
32 win_=new Win(list_, 0, 1, MPI_INFO_NULL, comm_);
\r
40 int File::close(MPI_File *fh){
\r
41 XBT_DEBUG("Closing MPI_File %s", (*fh)->file_->get_path());
\r
43 if((*fh)->flags() & MPI_MODE_DELETE_ON_CLOSE)
\r
44 (*fh)->file_->unlink();
\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
56 int File::get_position(MPI_Offset* offset){
\r
57 *offset=file_->tell();
\r
61 int File::get_position_shared(MPI_Offset* offset){
\r
63 *offset=shared_file_pointer_;
\r
68 int File::seek(MPI_Offset offset, int whence){
\r
71 XBT_DEBUG("Seeking in MPI_File %s, setting offset %lld", file_->get_path(), offset);
\r
72 file_->seek(offset,SEEK_SET);
\r
75 XBT_DEBUG("Seeking in MPI_File %s, current offset + %lld", file_->get_path(), offset);
\r
76 file_->seek(offset,SEEK_CUR);
\r
79 XBT_DEBUG("Seeking in MPI_File %s, end offset + %lld", file_->get_path(), offset);
\r
80 file_->seek(offset,SEEK_END);
\r
83 return MPI_ERR_FILE;
\r
88 int File::seek_shared(MPI_Offset offset, int whence){
\r
90 seek(offset,whence);
\r
91 shared_file_pointer_=file_->tell();
\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
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
112 int File::read_shared(MPI_File fh, void *buf, int count, MPI_Datatype datatype, MPI_Status *status){
\r
114 fh->seek(fh->shared_file_pointer_,MPI_SEEK_SET);
\r
115 read(fh, buf, count, datatype, status);
\r
116 fh->shared_file_pointer_=fh->file_->tell();
\r
118 return MPI_SUCCESS;
\r
121 int File::read_ordered(MPI_File fh, void *buf, int count, MPI_Datatype datatype, MPI_Status *status){
\r
122 //0 needs to get the shared pointer value
\r
123 if(fh->comm_->rank()==0){
\r
127 fh->shared_file_pointer_=count*datatype->size();
\r
130 simgrid::smpi::Colls::scan(&(fh->shared_file_pointer_), &result, 1, MPI_OFFSET, MPI_SUM, fh->comm_);
\r
131 fh->seek(result, MPI_SEEK_SET);
\r
132 int ret = fh->op_all<simgrid::smpi::File::read>(buf, count, datatype, status);
\r
133 if(fh->comm_->rank()==fh->comm_->size()-1){
\r
138 simgrid::smpi::Colls::bcast(&c, 1, MPI_BYTE, fh->comm_->size()-1, fh->comm_);
\r
142 int File::write(MPI_File fh, void *buf, int count, MPI_Datatype datatype, MPI_Status *status){
\r
143 //get position first as we may be doing non contiguous reads and it will probably be updated badly
\r
144 MPI_Offset position = fh->file_->tell();
\r
145 MPI_Offset movesize = datatype->get_extent()*count;
\r
146 MPI_Offset writesize = datatype->size()*count;
\r
147 XBT_DEBUG("Position before write in MPI_File %s : %llu",fh->file_->get_path(),fh->file_->tell());
\r
148 MPI_Offset write = fh->file_->write(writesize);
\r
149 XBT_DEBUG("Write in MPI_File %s, %lld bytes read, readsize %lld bytes, movesize %lld", fh->file_->get_path(), write, writesize, movesize);
\r
150 if(writesize!=movesize){
\r
151 fh->file_->seek(position+movesize, SEEK_SET);
\r
153 XBT_DEBUG("Position after write in MPI_File %s : %llu",fh->file_->get_path(), fh->file_->tell());
\r
154 status->count=count*datatype->size();
\r
155 return MPI_SUCCESS;
\r
158 int File::write_shared(MPI_File fh, void *buf, int count, MPI_Datatype datatype, MPI_Status *status){
\r
160 fh->seek(fh->shared_file_pointer_,MPI_SEEK_SET);
\r
161 write(fh, buf, count, datatype, status);
\r
162 fh->shared_file_pointer_=fh->file_->tell();
\r
164 return MPI_SUCCESS;
\r
167 int File::write_ordered(MPI_File fh, void *buf, int count, MPI_Datatype datatype, MPI_Status *status){
\r
168 //0 needs to get the shared pointer value
\r
169 if(fh->comm_->rank()==0){
\r
173 fh->shared_file_pointer_=count*datatype->size();
\r
176 simgrid::smpi::Colls::scan(&(fh->shared_file_pointer_), &result, 1, MPI_OFFSET, MPI_SUM, fh->comm_);
\r
177 fh->seek(result, MPI_SEEK_SET);
\r
178 int ret = fh->op_all<simgrid::smpi::File::write>(buf, count, datatype, status);
\r
179 if(fh->comm_->rank()==fh->comm_->size()-1){
\r
184 simgrid::smpi::Colls::bcast(&c, 1, MPI_BYTE, fh->comm_->size()-1, fh->comm_);
\r
189 return file_->size();
\r
198 return simgrid::smpi::Colls::barrier(comm_);
\r
203 int rank = comm_->rank();
\r
204 int size = comm_->size();
\r
205 char waitlist[size];
\r
209 win_->lock(MPI_LOCK_EXCLUSIVE, 0, 0);
\r
210 win_->put(&lock, 1, MPI_CHAR, 0, FP_SIZE+rank, 1, MPI_CHAR);
\r
211 win_->get(waitlist, size, MPI_CHAR, 0, FP_SIZE, size, MPI_CHAR);
\r
212 win_->get(&shared_file_pointer_ , 1 , MPI_OFFSET , 0 , 0, 1, MPI_OFFSET);
\r
214 for (i = 0; i < size; i++) {
\r
215 if (waitlist[i] == 1 && i != rank) {
\r
216 // wait for the lock
\r
217 MPI_Recv(&lock, 1, MPI_CHAR, MPI_ANY_SOURCE, tag, comm_, MPI_STATUS_IGNORE);
\r
226 int rank = comm_->rank();
\r
227 int size = comm_->size();
\r
228 char waitlist[size];
\r
232 win_->lock(MPI_LOCK_EXCLUSIVE, 0, 0);
\r
233 win_->put(&lock, 1, MPI_CHAR, 0, FP_SIZE+rank, 1, MPI_CHAR);
\r
234 win_->get(waitlist, size, MPI_CHAR, 0, FP_SIZE, size, MPI_CHAR);
\r
235 shared_file_pointer_=file_->tell();
\r
236 win_->put(&shared_file_pointer_, 1 , MPI_OFFSET , 0 , 0, 1, MPI_OFFSET);
\r
239 next = (rank + 1 + size) % size;
\r
240 for (i = 0; i < size; i++, next = (next + 1) % size) {
\r
241 if (waitlist[next] == 1) {
\r
242 MPI_Send(&lock, 1, MPI_CHAR, next, tag, comm_);
\r
249 MPI_Info File::info(){
\r
250 if(info_== MPI_INFO_NULL)
\r
251 info_ = new Info();
\r
256 void File::set_info(MPI_Info info){
\r
257 if(info_!= MPI_INFO_NULL)
\r