Logo AND Algorithmique Numérique Distribuée

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