Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines for 2023.
[simgrid.git] / src / smpi / mpi / smpi_datatype_derived.cpp
1 /* smpi_datatype.cpp -- MPI primitives to handle datatypes                  */
2 /* Copyright (c) 2009-2023. The SimGrid Team. 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 #include "smpi_datatype_derived.hpp"
8 #include "smpi_op.hpp"
9 #include <xbt/log.h>
10
11 #include <array>
12 #include <cstring>
13
14 namespace simgrid::smpi {
15
16 Datatype_contents::Datatype_contents(int combiner, int number_of_integers, const int* integers, int number_of_addresses,
17                                      const MPI_Aint* addresses, int number_of_datatypes, const MPI_Datatype* datatypes)
18     : combiner_(combiner)
19     , integers_(integers, integers + number_of_integers)
20     , addresses_(addresses, addresses + number_of_addresses)
21     , datatypes_(datatypes, datatypes + number_of_datatypes)
22 {
23   for (auto& datatype : datatypes_)
24     datatype->ref();
25 }
26
27 Datatype_contents::~Datatype_contents()
28 {
29   for (auto& datatype : datatypes_)
30     Datatype::unref(datatype);
31 }
32
33 Type_Contiguous::Type_Contiguous(int size, MPI_Aint lb, MPI_Aint ub, int flags, int block_count, MPI_Datatype old_type)
34     : Datatype(size, lb, ub, flags), block_count_(block_count), old_type_(old_type)
35 {
36   set_contents(MPI_COMBINER_CONTIGUOUS, 1, &block_count, 0, nullptr, 1, &old_type);
37   old_type_->ref();
38 }
39
40 Type_Contiguous::~Type_Contiguous()
41 {
42   Datatype::unref(old_type_);
43 }
44
45 int Type_Contiguous::clone(MPI_Datatype* type)
46 {
47   *type = new Type_Contiguous(this->size(), this->lb(), this->ub(), this->flags(), this->block_count_, this->old_type_);
48   (*type)->copy_attrs(this);
49   return MPI_SUCCESS;
50 }
51
52 void Type_Contiguous::serialize(const void* noncontiguous_buf, void* contiguous_buf, int count)
53 {
54   auto* contiguous_buf_char          = static_cast<char*>(contiguous_buf);
55   const auto* noncontiguous_buf_char = static_cast<const char*>(noncontiguous_buf) + lb();
56   memcpy(contiguous_buf_char, noncontiguous_buf_char, old_type_->size() * count * block_count_);
57 }
58
59 void Type_Contiguous::unserialize(const void* contiguous_buf, void* noncontiguous_buf, int count, MPI_Op op)
60 {
61   const auto* contiguous_buf_char = static_cast<const char*>(contiguous_buf);
62   auto* noncontiguous_buf_char    = static_cast<char*>(noncontiguous_buf) + lb();
63   int n= count*block_count_;
64   if(op!=MPI_OP_NULL)
65     op->apply( contiguous_buf_char, noncontiguous_buf_char, &n, old_type_);
66 }
67
68 Type_Hvector::Type_Hvector(int size,MPI_Aint lb, MPI_Aint ub, int flags, int count, int block_length, MPI_Aint stride, MPI_Datatype old_type): Datatype(size, lb, ub, flags), block_count_(count), block_length_(block_length), block_stride_(stride), old_type_(old_type){
69   const std::array<int, 2> ints = {{count, block_length}};
70   set_contents(MPI_COMBINER_HVECTOR, 2, ints.data(), 1, &stride, 1, &old_type);
71   old_type->ref();
72 }
73 Type_Hvector::~Type_Hvector(){
74   Datatype::unref(old_type_);
75 }
76
77 int Type_Hvector::clone(MPI_Datatype* type)
78 {
79   *type = new Type_Hvector(this->size(), this->lb(), this->ub(), this->flags(), this->block_count_, this->block_length_, this->block_stride_, this->old_type_);
80   (*type)->copy_attrs(this);
81   return MPI_SUCCESS;
82 }
83
84 void Type_Hvector::serialize(const void* noncontiguous_buf, void *contiguous_buf,
85                     int count){
86   auto* contiguous_buf_char          = static_cast<char*>(contiguous_buf);
87   const auto* noncontiguous_buf_char = static_cast<const char*>(noncontiguous_buf);
88
89   for (int i = 0; i < block_count_ * count; i++) {
90     if (not(old_type_->flags() & DT_FLAG_DERIVED))
91       memcpy(contiguous_buf_char, noncontiguous_buf_char, block_length_ * old_type_->size());
92     else
93       old_type_->serialize( noncontiguous_buf_char, contiguous_buf_char, block_length_);
94
95     contiguous_buf_char += block_length_*old_type_->size();
96     if((i+1)%block_count_ ==0)
97       noncontiguous_buf_char += block_length_*old_type_->size();
98     else
99       noncontiguous_buf_char += block_stride_;
100   }
101 }
102
103 void Type_Hvector::unserialize(const void* contiguous_buf, void *noncontiguous_buf,
104                               int count, MPI_Op op){
105   const auto* contiguous_buf_char = static_cast<const char*>(contiguous_buf);
106   auto* noncontiguous_buf_char    = static_cast<char*>(noncontiguous_buf);
107
108   for (int i = 0; i < block_count_ * count; i++) {
109     if (not(old_type_->flags() & DT_FLAG_DERIVED)) {
110       if(op!=MPI_OP_NULL)
111         op->apply( contiguous_buf_char, noncontiguous_buf_char, &block_length_, old_type_);
112     }else
113       old_type_->unserialize( contiguous_buf_char, noncontiguous_buf_char, block_length_, op);
114     contiguous_buf_char += block_length_*old_type_->size();
115     if((i+1)%block_count_ ==0)
116       noncontiguous_buf_char += block_length_*old_type_->size();
117     else
118       noncontiguous_buf_char += block_stride_;
119   }
120 }
121
122 Type_Vector::Type_Vector(int size, MPI_Aint lb, MPI_Aint ub, int flags, int count, int block_length, int stride,
123                          MPI_Datatype old_type)
124     : Type_Hvector(size, lb, ub, flags, count, block_length, stride * old_type->get_extent(), old_type)
125 {
126   const std::array<int, 3> ints = {{count, block_length, stride}};
127   set_contents(MPI_COMBINER_VECTOR, 3, ints.data(), 0, nullptr, 1, &old_type);
128 }
129
130 int Type_Vector::clone(MPI_Datatype* type)
131 {
132   *type = new Type_Vector(this->size(), this->lb(), this->ub(), this->flags(), this->block_count_, this->block_length_, this->block_stride_, this->old_type_);
133   (*type)->copy_attrs(this);
134   return MPI_SUCCESS;
135 }
136
137 Type_Hindexed::Type_Hindexed(int size, MPI_Aint lb, MPI_Aint ub, int flags, int count, const int* block_lengths,
138                              const MPI_Aint* block_indices, MPI_Datatype old_type)
139     : Datatype(size, lb, ub, flags)
140     , block_count_(count)
141     , block_lengths_(new int[count])
142     , block_indices_(new MPI_Aint[count])
143     , old_type_(old_type)
144 {
145   std::vector<int> ints(count + 1);
146   ints[0]=count;
147   for(int i=1;i<=count;i++)
148     ints[i]=block_lengths[i-1];
149   set_contents(MPI_COMBINER_HINDEXED, count + 1, ints.data(), count, block_indices, 1, &old_type);
150   old_type_->ref();
151   for (int i = 0; i < count; i++) {
152     block_lengths_[i] = block_lengths[i];
153     block_indices_[i] = block_indices[i];
154   }
155 }
156
157 Type_Hindexed::Type_Hindexed(int size, MPI_Aint lb, MPI_Aint ub, int flags, int count, const int* block_lengths,
158                              const int* block_indices, MPI_Datatype old_type, MPI_Aint factor)
159     : Datatype(size, lb, ub, flags)
160     , block_count_(count)
161     , block_lengths_(new int[count])
162     , block_indices_(new MPI_Aint[count])
163     , old_type_(old_type)
164 {
165   old_type_->ref();
166   for (int i = 0; i < count; i++) {
167     block_lengths_[i] = block_lengths[i];
168     block_indices_[i] = block_indices[i] * factor;
169   }
170 }
171
172 int Type_Hindexed::clone(MPI_Datatype* type)
173 {
174   *type = new Type_Hindexed(this->size(), this->lb(), this->ub(), this->flags(), this->block_count_, this->block_lengths_, this->block_indices_, this->old_type_);
175   (*type)->copy_attrs(this);
176   return MPI_SUCCESS;
177 }
178
179 Type_Hindexed::~Type_Hindexed()
180 {
181   Datatype::unref(old_type_);
182   if(refcount()==0){
183     delete[] block_lengths_;
184     delete[] block_indices_;
185   }
186 }
187
188 void Type_Hindexed::serialize(const void* noncontiguous_buf, void *contiguous_buf,
189                 int count){
190   auto* contiguous_buf_char          = static_cast<char*>(contiguous_buf);
191   const auto* noncontiguous_buf_iter = static_cast<const char*>(noncontiguous_buf);
192   const auto* noncontiguous_buf_char = noncontiguous_buf_iter + block_indices_[0];
193   for (int j = 0; j < count; j++) {
194     for (int i = 0; i < block_count_; i++) {
195       if (not(old_type_->flags() & DT_FLAG_DERIVED))
196         memcpy(contiguous_buf_char, noncontiguous_buf_char, block_lengths_[i] * old_type_->size());
197       else
198         old_type_->serialize(noncontiguous_buf_char, contiguous_buf_char,block_lengths_[i]);
199
200       contiguous_buf_char += block_lengths_[i]*old_type_->size();
201       if (i<block_count_-1)
202         noncontiguous_buf_char = noncontiguous_buf_iter + block_indices_[i+1];
203       else
204         noncontiguous_buf_char += block_lengths_[i]*old_type_->get_extent();
205     }
206     noncontiguous_buf_iter=noncontiguous_buf_char;
207   }
208 }
209
210 void Type_Hindexed::unserialize(const void* contiguous_buf, void *noncontiguous_buf,
211                           int count, MPI_Op op){
212   const auto* contiguous_buf_char = static_cast<const char*>(contiguous_buf);
213   auto* noncontiguous_buf_char    = static_cast<char*>(noncontiguous_buf) + block_indices_[0];
214   for (int j = 0; j < count; j++) {
215     for (int i = 0; i < block_count_; i++) {
216       if (not(old_type_->flags() & DT_FLAG_DERIVED)) {
217         if(op!=MPI_OP_NULL)
218           op->apply( contiguous_buf_char, noncontiguous_buf_char, &block_lengths_[i],
219                             old_type_);
220       }else
221         old_type_->unserialize( contiguous_buf_char,noncontiguous_buf_char,block_lengths_[i], op);
222
223       contiguous_buf_char += block_lengths_[i]*old_type_->size();
224       if (i<block_count_-1)
225         noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf) + block_indices_[i+1];
226       else
227         noncontiguous_buf_char += block_lengths_[i]*old_type_->get_extent();
228     }
229     noncontiguous_buf=static_cast<void*>(noncontiguous_buf_char);
230   }
231 }
232
233 Type_Indexed::Type_Indexed(int size, MPI_Aint lb, MPI_Aint ub, int flags, int count, const int* block_lengths,
234                            const int* block_indices, MPI_Datatype old_type)
235     : Type_Hindexed(size, lb, ub, flags, count, block_lengths, block_indices, old_type, old_type->get_extent())
236 {
237   std::vector<int> ints(2 * count + 1);
238   ints[0]=count;
239   for(int i=1;i<=count;i++)
240     ints[i]=block_lengths[i-1];
241   for(int i=count+1;i<=2*count;i++)
242     ints[i]=block_indices[i-count-1];
243   set_contents(MPI_COMBINER_INDEXED, 2 * count + 1, ints.data(), 0, nullptr, 1, &old_type);
244 }
245
246 int Type_Indexed::clone(MPI_Datatype* type)
247 {
248   *type = new Type_Indexed(this->size(), this->lb(), this->ub(), this->flags(), this->block_count_, this->block_lengths_, (int*)(this->block_indices_), this->old_type_);
249   (*type)->copy_attrs(this);
250   return MPI_SUCCESS;
251 }
252
253 Type_Struct::Type_Struct(int size, MPI_Aint lb, MPI_Aint ub, int flags, int count, const int* block_lengths,
254                          const MPI_Aint* block_indices, const MPI_Datatype* old_types)
255     : Datatype(size, lb, ub, flags)
256     , block_count_(count)
257     , block_lengths_(new int[count])
258     , block_indices_(new MPI_Aint[count])
259     , old_types_(new MPI_Datatype[count])
260 {
261   std::vector<int> ints(count + 1);
262   ints[0]=count;
263   for(int i=1;i<=count;i++)
264     ints[i]=block_lengths[i-1];
265   set_contents(MPI_COMBINER_INDEXED, count + 1, ints.data(), count, block_indices, count, old_types);
266   for (int i = 0; i < count; i++) {
267     block_lengths_[i]=block_lengths[i];
268     block_indices_[i]=block_indices[i];
269     old_types_[i]=old_types[i];
270     old_types_[i]->ref();
271   }
272 }
273
274 Type_Struct::~Type_Struct(){
275   for (int i = 0; i < block_count_; i++) {
276     Datatype::unref(old_types_[i]);
277   }
278   if(refcount()==0){
279     delete[] block_lengths_;
280     delete[] block_indices_;
281     delete[] old_types_;
282   }
283 }
284
285 int Type_Struct::clone(MPI_Datatype* type)
286 {
287   *type = new Type_Struct(this->size(), this->lb(), this->ub(), this->flags(), this->block_count_, this->block_lengths_, this->block_indices_, this->old_types_);
288   (*type)->copy_attrs(this);
289   return MPI_SUCCESS;
290 }
291
292 void Type_Struct::serialize(const void* noncontiguous_buf, void *contiguous_buf,
293                         int count){
294   auto* contiguous_buf_char          = static_cast<char*>(contiguous_buf);
295   const auto* noncontiguous_buf_iter = static_cast<const char*>(noncontiguous_buf);
296   const auto* noncontiguous_buf_char = noncontiguous_buf_iter + block_indices_[0];
297   for (int j = 0; j < count; j++) {
298     for (int i = 0; i < block_count_; i++) {
299       if (not(old_types_[i]->flags() & DT_FLAG_DERIVED))
300         memcpy(contiguous_buf_char, noncontiguous_buf_char, block_lengths_[i] * old_types_[i]->size());
301       else
302         old_types_[i]->serialize( noncontiguous_buf_char,contiguous_buf_char,block_lengths_[i]);
303
304
305       contiguous_buf_char += block_lengths_[i]*old_types_[i]->size();
306       if (i<block_count_-1)
307         noncontiguous_buf_char = noncontiguous_buf_iter + block_indices_[i+1];
308       else //let's hope this is MPI_UB ?
309         noncontiguous_buf_char += block_lengths_[i]*old_types_[i]->get_extent();
310     }
311     noncontiguous_buf_iter=noncontiguous_buf_char;
312   }
313 }
314
315 void Type_Struct::unserialize(const void* contiguous_buf, void *noncontiguous_buf,
316                               int count, MPI_Op op){
317   const auto* contiguous_buf_char = static_cast<const char*>(contiguous_buf);
318   auto* noncontiguous_buf_char    = static_cast<char*>(noncontiguous_buf) + block_indices_[0];
319   for (int j = 0; j < count; j++) {
320     for (int i = 0; i < block_count_; i++) {
321       if (not(old_types_[i]->flags() & DT_FLAG_DERIVED)) {
322         if(op!=MPI_OP_NULL)
323           op->apply( contiguous_buf_char, noncontiguous_buf_char, &block_lengths_[i], old_types_[i]);
324       }else
325         old_types_[i]->unserialize( contiguous_buf_char, noncontiguous_buf_char,block_lengths_[i], op);
326
327       contiguous_buf_char += block_lengths_[i]*old_types_[i]->size();
328       if (i<block_count_-1)
329         noncontiguous_buf_char =  static_cast<char*>(noncontiguous_buf) + block_indices_[i+1];
330       else
331         noncontiguous_buf_char += block_lengths_[i]*old_types_[i]->get_extent();
332     }
333     noncontiguous_buf=static_cast<void*>(noncontiguous_buf_char);
334   }
335 }
336
337 } // namespace simgrid::smpi