Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
a7cfe397f5262f0a678ba194e0262e1362dc9381
[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::seek(MPI_Offset offset, int whence){\r
44     switch(whence){\r
45       case(MPI_SEEK_SET):\r
46         XBT_DEBUG("Seeking in MPI_File %s, setting offset %lld", file_->get_path(), offset);\r
47         file_->seek(offset,SEEK_SET);\r
48         break;\r
49       case(MPI_SEEK_CUR):\r
50         XBT_DEBUG("Seeking in MPI_File %s, current offset + %lld", file_->get_path(), offset);\r
51         file_->seek(offset,SEEK_CUR);\r
52         break;\r
53       case(MPI_SEEK_END):\r
54         XBT_DEBUG("Seeking in MPI_File %s, end offset + %lld", file_->get_path(), offset);\r
55         file_->seek(offset,SEEK_END);\r
56         break;\r
57       default:\r
58         return MPI_ERR_FILE;\r
59     }\r
60     return MPI_SUCCESS;\r
61   }\r
62   \r
63   int File::read(MPI_File fh, void *buf, int count, MPI_Datatype datatype, MPI_Status *status){\r
64     //get position first as we may be doing non contiguous reads and it will probably be updated badly\r
65     MPI_Offset position = fh->file_->tell();\r
66     MPI_Offset movesize = datatype->get_extent()*count;\r
67     MPI_Offset readsize = datatype->size()*count;\r
68     XBT_DEBUG("Position before read in MPI_File %s : %llu",fh->file_->get_path(),fh->file_->tell());\r
69     MPI_Offset read = fh->file_->read(readsize);\r
70     XBT_DEBUG("Read in MPI_File %s, %lld bytes read, readsize %lld bytes, movesize %lld", fh->file_->get_path(), read, readsize, movesize);\r
71     if(readsize!=movesize){\r
72       fh->file_->seek(position+movesize, SEEK_SET);\r
73     }\r
74     XBT_DEBUG("Position after read in MPI_File %s : %llu",fh->file_->get_path(), fh->file_->tell());\r
75     return MPI_SUCCESS;\r
76   }\r
77 \r
78 \r
79   int File::write(MPI_File fh, void *buf, int count, MPI_Datatype datatype, MPI_Status *status){\r
80     //get position first as we may be doing non contiguous reads and it will probably be updated badly\r
81     MPI_Offset position = fh->file_->tell();\r
82     MPI_Offset movesize = datatype->get_extent()*count;\r
83     MPI_Offset writesize = datatype->size()*count;\r
84     XBT_DEBUG("Position before write in MPI_File %s : %llu",fh->file_->get_path(),fh->file_->tell());\r
85     MPI_Offset write = fh->file_->write(writesize);\r
86     XBT_DEBUG("Write in MPI_File %s, %lld bytes read, readsize %lld bytes, movesize %lld", fh->file_->get_path(), write, writesize, movesize);\r
87     if(writesize!=movesize){\r
88       fh->file_->seek(position+movesize, SEEK_SET);\r
89     }\r
90     XBT_DEBUG("Position after write in MPI_File %s : %llu",fh->file_->get_path(), fh->file_->tell());\r
91     return MPI_SUCCESS;\r
92   }\r
93 \r
94   int File::size(){\r
95     return file_->size();\r
96   }\r
97 \r
98   int File::flags(){\r
99     return flags_;\r
100   }\r
101 \r
102   int File::sync(){\r
103     //no idea\r
104     return simgrid::smpi::Colls::barrier(comm_);\r
105   }\r
106 }\r
107 }\r