Logo AND Algorithmique Numérique Distribuée

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