Logo AND Algorithmique Numérique Distribuée

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