Logo AND Algorithmique Numérique Distribuée

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