Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[project-description] Fix extraction of the ns-3 version.
[simgrid.git] / src / smpi / include / smpi_file.hpp
1 /* Copyright (c) 2010-2022. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #ifndef SMPI_FILE_HPP_INCLUDED
8 #define SMPI_FILE_HPP_INCLUDED
9 #include "simgrid/plugins/file_system.h"
10 #include "smpi_comm.hpp"
11 #include "smpi_coll.hpp"
12 #include "smpi_datatype.hpp"
13 #include "smpi_errhandler.hpp"
14 #include "smpi_info.hpp"
15 #include  <algorithm>
16
17 XBT_LOG_EXTERNAL_CATEGORY(smpi_pmpi);
18
19 namespace simgrid::smpi {
20 class File : public F2C{
21   MPI_Comm comm_;
22   int flags_;
23   simgrid::s4u::File* file_;
24   MPI_Info info_;
25   MPI_Offset* shared_file_pointer_;
26   s4u::MutexPtr shared_mutex_;
27   MPI_Win win_;
28   char* list_;
29   MPI_Errhandler errhandler_;
30   MPI_Datatype etype_;
31   MPI_Datatype filetype_;
32   std::string datarep_;
33
34   public:
35   File(MPI_Comm comm, const char *filename, int amode, MPI_Info info);
36   File(const File&) = delete;
37   File& operator=(const File&) = delete;
38   ~File() override;
39   int size() const;
40   int get_position(MPI_Offset* offset) const;
41   int get_position_shared(MPI_Offset* offset) const;
42   int flags() const;
43   MPI_Comm comm() const;
44   std::string name() const override {return file_ ? std::string("MPI_File: ")+ std::string(file_->get_path()): std::string("MPI_File");}
45
46   int sync();
47   int seek(MPI_Offset offset, int whence);
48   int seek_shared(MPI_Offset offset, int whence);
49   int set_view(MPI_Offset disp, MPI_Datatype etype, MPI_Datatype filetype, const char* datarep, const Info* info);
50   int get_view(MPI_Offset* disp, MPI_Datatype* etype, MPI_Datatype* filetype, char* datarep) const;
51   MPI_Info info();
52   void set_info( MPI_Info info);
53   void set_size(int size);
54   static int read(MPI_File fh, void* buf, int count, const Datatype* datatype, MPI_Status* status);
55   static int read_shared(MPI_File fh, void* buf, int count, const Datatype* datatype, MPI_Status* status);
56   static int read_ordered(MPI_File fh, void* buf, int count, const Datatype* datatype, MPI_Status* status);
57   static int write(MPI_File fh, void* buf, int count, const Datatype* datatype, MPI_Status* status);
58   static int write_shared(MPI_File fh, const void* buf, int count, const Datatype* datatype, MPI_Status* status);
59   static int write_ordered(MPI_File fh, const void* buf, int count, const Datatype* datatype, MPI_Status* status);
60   template <int (*T)(MPI_File, void*, int, const Datatype*, MPI_Status*)>
61   int op_all(void* buf, int count, const Datatype* datatype, MPI_Status* status);
62   static int close(MPI_File *fh);
63   static int del(const char* filename, const Info* info);
64   MPI_Errhandler errhandler();
65   void set_errhandler( MPI_Errhandler errhandler);
66   static File* f2c(int id);
67 };
68
69 /* Read_all, Write_all : loosely based on */
70 /* @article{Thakur:1996:ETM:245875.245879,*/
71 /* author = {Thakur, Rajeev and Choudhary, Alok},*/
72 /* title = {An Extended Two-phase Method for Accessing Sections of Out-of-core Arrays},*/
73 /* journal = {Sci. Program.},*/
74 /* issue_date = {Winter 1996},*/
75 /* pages = {301--317},*/
76 /* }*/
77 template <int (*T)(MPI_File, void*, int, const Datatype*, MPI_Status*)>
78 int File::op_all(void* buf, int count, const Datatype* datatype, MPI_Status* status)
79 {
80   // get min and max offsets from everyone.
81   int size              = comm_->size();
82   int rank              = comm_->rank();
83   MPI_Offset min_offset = file_->tell();
84   MPI_Offset max_offset =
85       min_offset +
86       count * datatype->get_extent(); // cheating, as we don't care about exact data location, we can skip extent
87   std::vector<MPI_Offset> min_offsets(size);
88   std::vector<MPI_Offset> max_offsets(size);
89   simgrid::smpi::colls::allgather(&min_offset, 1, MPI_OFFSET, min_offsets.data(), 1, MPI_OFFSET, comm_);
90   simgrid::smpi::colls::allgather(&max_offset, 1, MPI_OFFSET, max_offsets.data(), 1, MPI_OFFSET, comm_);
91   MPI_Offset min = min_offset;
92   MPI_Offset max = max_offset;
93   MPI_Offset tot = 0;
94   int empty      = 1;
95   for (int i = 0; i < size; i++) {
96     if (min_offsets[i] != max_offsets[i])
97       empty = 0;
98     tot += (max_offsets[i] - min_offsets[i]);
99     if (min_offsets[i] < min)
100       min = min_offsets[i];
101     if (max_offsets[i] > max)
102       max = max_offsets[i];
103   }
104
105   XBT_CDEBUG(smpi_pmpi, "my offsets to read : %lld:%lld, global min and max %lld:%lld", min_offset, max_offset, min,
106              max);
107   if (empty == 1) {
108     if (status != MPI_STATUS_IGNORE)
109       status->count = 0;
110     return MPI_SUCCESS;
111   }
112   if (max - min == tot && (datatype->flags() & DT_FLAG_CONTIGUOUS)) {
113     // contiguous. Just have each proc perform its read
114     if (status != MPI_STATUS_IGNORE)
115       status->count = count * datatype->size();
116     return T(this, buf, count, datatype, status);
117   }
118
119   // Interleaved case : How much do I need to read, and whom to send it ?
120   MPI_Offset my_chunk_start = (max - min + 1) / size * rank;
121   MPI_Offset my_chunk_end   = ((max - min + 1) / size * (rank + 1));
122   XBT_CDEBUG(smpi_pmpi, "my chunks to read : %lld:%lld", my_chunk_start, my_chunk_end);
123   std::vector<int> send_sizes(size);
124   std::vector<int> recv_sizes(size);
125   std::vector<int> send_disps(size);
126   std::vector<int> recv_disps(size);
127   int total_sent  = 0;
128   for (int i = 0; i < size; i++) {
129     send_sizes[i] = 0;
130     send_disps[i] = 0; // cheat to avoid issues when send>recv as we use recv buffer
131     if ((my_chunk_start >= min_offsets[i] && my_chunk_start < max_offsets[i]) ||
132         ((my_chunk_end <= max_offsets[i]) && my_chunk_end > min_offsets[i])) {
133       send_sizes[i] = (std::min(max_offsets[i] - 1, my_chunk_end - 1) - std::max(min_offsets[i], my_chunk_start));
134       // store min and max offset to actually read
135       min_offset = std::min(min_offset, min_offsets[i]);
136       total_sent += send_sizes[i];
137       XBT_CDEBUG(smpi_pmpi, "will have to send %d bytes to %d", send_sizes[i], i);
138     }
139   }
140   min_offset = std::max(min_offset, my_chunk_start);
141
142   // merge the ranges of every process
143   std::vector<std::pair<MPI_Offset, MPI_Offset>> ranges;
144   for (int i = 0; i < size; ++i)
145     ranges.emplace_back(min_offsets[i], max_offsets[i]);
146   std::sort(ranges.begin(), ranges.end());
147   std::vector<std::pair<MPI_Offset, MPI_Offset>> chunks;
148   chunks.push_back(ranges[0]);
149
150   unsigned int nchunks = 0;
151   for (unsigned i = 1; i < ranges.size(); i++) {
152     if (ranges[i].second > chunks[nchunks].second) {
153       // else range included - ignore
154       if (ranges[i].first > chunks[nchunks].second) {
155         // new disjoint range
156         chunks.push_back(ranges[i]);
157         nchunks++;
158       } else {
159         // merge ranges
160         chunks[nchunks].second = ranges[i].second;
161       }
162     }
163   }
164   // what do I need to read ?
165   MPI_Offset totreads = 0;
166   for (auto const& [chunk_start, chunk_end] : chunks) {
167     if (chunk_end < my_chunk_start)
168       continue;
169     else if (chunk_start > my_chunk_end)
170       continue;
171     else
172       totreads += (std::min(chunk_end, my_chunk_end - 1) - std::max(chunk_start, my_chunk_start));
173   }
174   XBT_CDEBUG(smpi_pmpi, "will have to access %lld from my chunk", totreads);
175
176   unsigned char* sendbuf = smpi_get_tmp_sendbuffer(total_sent);
177
178   if (totreads > 0) {
179     seek(min_offset, MPI_SEEK_SET);
180     T(this, sendbuf, totreads / datatype->size(), datatype, status);
181   }
182   simgrid::smpi::colls::alltoall(send_sizes.data(), 1, MPI_INT, recv_sizes.data(), 1, MPI_INT, comm_);
183   int total_recv = 0;
184   for (int i = 0; i < size; i++) {
185     recv_disps[i] = total_recv;
186     total_recv += recv_sizes[i];
187   }
188   // Set buf value to avoid copying dumb data
189   simgrid::smpi::colls::alltoallv(sendbuf, send_sizes.data(), send_disps.data(), MPI_BYTE, buf, recv_sizes.data(),
190                                   recv_disps.data(), MPI_BYTE, comm_);
191   if (status != MPI_STATUS_IGNORE)
192     status->count = count * datatype->size();
193   smpi_free_tmp_buffer(sendbuf);
194   return MPI_SUCCESS;
195 }
196 } // namespace simgrid::smpi
197
198 #endif