Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
6006b4d8486c5a8f13cc862451c3c68cf45157e2
[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_file.hpp"\r
12 #include "simgrid/plugins/file_system.h"\r
13 \r
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_io, smpi, "Logging specific to SMPI (RMA operations)");\r
15 \r
16 \r
17 namespace simgrid{\r
18 namespace smpi{\r
19   File::File(MPI_Comm comm, char *filename, int amode, MPI_Info info): comm_(comm), flags_(amode), info_(info){\r
20     file_= new simgrid::s4u::File(filename, nullptr);\r
21   }\r
22   \r
23   File::~File(){\r
24     delete file_;\r
25   }\r
26 \r
27   int File::close(MPI_File *fh){\r
28     XBT_DEBUG("Closing MPI_File %s", (*fh)->file_->get_path());\r
29     (*fh)->sync();\r
30     if((*fh)->flags() & MPI_MODE_DELETE_ON_CLOSE)\r
31       (*fh)->file_->unlink();\r
32     delete (*fh);\r
33     return MPI_SUCCESS;\r
34   }\r
35 \r
36   int File::del(char *filename, MPI_Info info){\r
37     //get the file with MPI_MODE_DELETE_ON_CLOSE and then close it\r
38     File* f = new File(MPI_COMM_SELF,filename,MPI_MODE_DELETE_ON_CLOSE|MPI_MODE_RDWR, nullptr);\r
39     close(&f);\r
40     return MPI_SUCCESS;\r
41   }\r
42 \r
43   int File::get_position(MPI_Offset* offset){\r
44     *offset=file_->tell();\r
45     return MPI_SUCCESS;\r
46   }\r
47 \r
48   int File::seek(MPI_Offset offset, int whence){\r
49     switch(whence){\r
50       case(MPI_SEEK_SET):\r
51         XBT_DEBUG("Seeking in MPI_File %s, setting offset %lld", file_->get_path(), offset);\r
52         file_->seek(offset,SEEK_SET);\r
53         break;\r
54       case(MPI_SEEK_CUR):\r
55         XBT_DEBUG("Seeking in MPI_File %s, current offset + %lld", file_->get_path(), offset);\r
56         file_->seek(offset,SEEK_CUR);\r
57         break;\r
58       case(MPI_SEEK_END):\r
59         XBT_DEBUG("Seeking in MPI_File %s, end offset + %lld", file_->get_path(), offset);\r
60         file_->seek(offset,SEEK_END);\r
61         break;\r
62       default:\r
63         return MPI_ERR_FILE;\r
64     }\r
65     return MPI_SUCCESS;\r
66   }\r
67   \r
68   int File::read(MPI_File fh, void *buf, int count, MPI_Datatype datatype, MPI_Status *status){\r
69     //get position first as we may be doing non contiguous reads and it will probably be updated badly\r
70     MPI_Offset position = fh->file_->tell();\r
71     MPI_Offset movesize = datatype->get_extent()*count;\r
72     MPI_Offset readsize = datatype->size()*count;\r
73     XBT_DEBUG("Position before read in MPI_File %s : %llu",fh->file_->get_path(),fh->file_->tell());\r
74     MPI_Offset read = fh->file_->read(readsize);\r
75     XBT_DEBUG("Read in MPI_File %s, %lld bytes read, readsize %lld bytes, movesize %lld", fh->file_->get_path(), read, readsize, movesize);\r
76     if(readsize!=movesize){\r
77       fh->file_->seek(position+movesize, SEEK_SET);\r
78     }\r
79     XBT_DEBUG("Position after read in MPI_File %s : %llu",fh->file_->get_path(), fh->file_->tell());\r
80     return MPI_SUCCESS;\r
81   }\r
82 \r
83 \r
84   int File::write(MPI_File fh, void *buf, int count, MPI_Datatype datatype, MPI_Status *status){\r
85     //get position first as we may be doing non contiguous reads and it will probably be updated badly\r
86     MPI_Offset position = fh->file_->tell();\r
87     MPI_Offset movesize = datatype->get_extent()*count;\r
88     MPI_Offset writesize = datatype->size()*count;\r
89     XBT_DEBUG("Position before write in MPI_File %s : %llu",fh->file_->get_path(),fh->file_->tell());\r
90     MPI_Offset write = fh->file_->write(writesize);\r
91     XBT_DEBUG("Write in MPI_File %s, %lld bytes read, readsize %lld bytes, movesize %lld", fh->file_->get_path(), write, writesize, movesize);\r
92     if(writesize!=movesize){\r
93       fh->file_->seek(position+movesize, SEEK_SET);\r
94     }\r
95     XBT_DEBUG("Position after write in MPI_File %s : %llu",fh->file_->get_path(), fh->file_->tell());\r
96     return MPI_SUCCESS;\r
97   }\r
98 \r
99   int File::size(){\r
100     return file_->size();\r
101   }\r
102 \r
103   int File::flags(){\r
104     return flags_;\r
105   }\r
106 \r
107   int File::sync(){\r
108     //no idea\r
109     return simgrid::smpi::Colls::barrier(comm_);\r
110   }\r
111 MPI_Info File::info(){\r
112   if(info_== MPI_INFO_NULL)\r
113     info_ = new Info();\r
114   info_->ref();\r
115   return info_;\r
116 }\r
117 \r
118 void File::set_info(MPI_Info info){\r
119   if(info_!= MPI_INFO_NULL)\r
120     info->ref();\r
121   info_=info;\r
122 }\r
123 \r
124 }\r
125 }\r