Logo AND Algorithmique Numérique Distribuée

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