Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Make Datatype::contents_ a private std::unique_ptr.
[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 }
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   set_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   set_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   const std::array<int, 3> ints = {{count, block_length, stride}};
120   set_contents(MPI_COMBINER_VECTOR, 3, ints.data(), 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   std::vector<int> ints(count + 1);
139   ints[0]=count;
140   for(int i=1;i<=count;i++)
141     ints[i]=block_lengths[i-1];
142   set_contents(MPI_COMBINER_HINDEXED, count + 1, ints.data(), count, block_indices, 1, &old_type);
143   old_type_->ref();
144   for (int i = 0; i < count; i++) {
145     block_lengths_[i] = block_lengths[i];
146     block_indices_[i] = block_indices[i];
147   }
148 }
149
150 Type_Hindexed::Type_Hindexed(int size, MPI_Aint lb, MPI_Aint ub, int flags, int count, const int* block_lengths,
151                              const int* block_indices, MPI_Datatype old_type, MPI_Aint factor)
152     : Datatype(size, lb, ub, flags)
153     , block_count_(count)
154     , block_lengths_(new int[count])
155     , block_indices_(new MPI_Aint[count])
156     , old_type_(old_type)
157 {
158   old_type_->ref();
159   for (int i = 0; i < count; i++) {
160     block_lengths_[i] = block_lengths[i];
161     block_indices_[i] = block_indices[i] * factor;
162   }
163 }
164
165 int Type_Hindexed::clone(MPI_Datatype* type)
166 {
167   *type = new Type_Hindexed(this->size(), this->lb(), this->ub(), this->flags(), this->block_count_, this->block_lengths_, this->block_indices_, this->old_type_);
168   (*type)->copy_attrs(this);
169   return MPI_SUCCESS;
170 }
171
172 Type_Hindexed::~Type_Hindexed()
173 {
174   Datatype::unref(old_type_);
175   if(refcount()==0){
176     delete[] block_lengths_;
177     delete[] block_indices_;
178   }
179 }
180
181 void Type_Hindexed::serialize(const void* noncontiguous_buf, void *contiguous_buf,
182                 int count){
183   auto* contiguous_buf_char          = static_cast<char*>(contiguous_buf);
184   const auto* noncontiguous_buf_iter = static_cast<const char*>(noncontiguous_buf);
185   const auto* noncontiguous_buf_char = noncontiguous_buf_iter + block_indices_[0];
186   for (int j = 0; j < count; j++) {
187     for (int i = 0; i < block_count_; i++) {
188       if (not(old_type_->flags() & DT_FLAG_DERIVED))
189         memcpy(contiguous_buf_char, noncontiguous_buf_char, block_lengths_[i] * old_type_->size());
190       else
191         old_type_->serialize(noncontiguous_buf_char, contiguous_buf_char,block_lengths_[i]);
192
193       contiguous_buf_char += block_lengths_[i]*old_type_->size();
194       if (i<block_count_-1)
195         noncontiguous_buf_char = noncontiguous_buf_iter + block_indices_[i+1];
196       else
197         noncontiguous_buf_char += block_lengths_[i]*old_type_->get_extent();
198     }
199     noncontiguous_buf_iter=noncontiguous_buf_char;
200   }
201 }
202
203 void Type_Hindexed::unserialize(const void* contiguous_buf, void *noncontiguous_buf,
204                           int count, MPI_Op op){
205   const auto* contiguous_buf_char = static_cast<const char*>(contiguous_buf);
206   auto* noncontiguous_buf_char    = static_cast<char*>(noncontiguous_buf) + block_indices_[0];
207   for (int j = 0; j < count; j++) {
208     for (int i = 0; i < block_count_; i++) {
209       if (not(old_type_->flags() & DT_FLAG_DERIVED)) {
210         if(op!=MPI_OP_NULL)
211           op->apply( contiguous_buf_char, noncontiguous_buf_char, &block_lengths_[i],
212                             old_type_);
213       }else
214         old_type_->unserialize( contiguous_buf_char,noncontiguous_buf_char,block_lengths_[i], op);
215
216       contiguous_buf_char += block_lengths_[i]*old_type_->size();
217       if (i<block_count_-1)
218         noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf) + block_indices_[i+1];
219       else
220         noncontiguous_buf_char += block_lengths_[i]*old_type_->get_extent();
221     }
222     noncontiguous_buf=static_cast<void*>(noncontiguous_buf_char);
223   }
224 }
225
226 Type_Indexed::Type_Indexed(int size, MPI_Aint lb, MPI_Aint ub, int flags, int count, const int* block_lengths,
227                            const int* block_indices, MPI_Datatype old_type)
228     : Type_Hindexed(size, lb, ub, flags, count, block_lengths, block_indices, old_type, old_type->get_extent())
229 {
230   std::vector<int> ints(2 * count + 1);
231   ints[0]=count;
232   for(int i=1;i<=count;i++)
233     ints[i]=block_lengths[i-1];
234   for(int i=count+1;i<=2*count;i++)
235     ints[i]=block_indices[i-count-1];
236   set_contents(MPI_COMBINER_INDEXED, 2 * count + 1, ints.data(), 0, nullptr, 1, &old_type);
237 }
238
239 int Type_Indexed::clone(MPI_Datatype* type)
240
241   *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_);
242   (*type)->copy_attrs(this);
243   return MPI_SUCCESS;
244 }
245
246 Type_Struct::Type_Struct(int size, MPI_Aint lb, MPI_Aint ub, int flags, int count, const int* block_lengths,
247                          const MPI_Aint* block_indices, const MPI_Datatype* old_types)
248     : Datatype(size, lb, ub, flags)
249     , block_count_(count)
250     , block_lengths_(new int[count])
251     , block_indices_(new MPI_Aint[count])
252     , old_types_(new MPI_Datatype[count])
253 {
254   std::vector<int> ints(count + 1);
255   ints[0]=count;
256   for(int i=1;i<=count;i++)
257     ints[i]=block_lengths[i-1];
258   set_contents(MPI_COMBINER_INDEXED, count + 1, ints.data(), count, block_indices, count, old_types);
259   for (int i = 0; i < count; i++) {
260     block_lengths_[i]=block_lengths[i];
261     block_indices_[i]=block_indices[i];
262     old_types_[i]=old_types[i];
263     old_types_[i]->ref();
264   }
265 }
266
267 Type_Struct::~Type_Struct(){
268   for (int i = 0; i < block_count_; i++) {
269     Datatype::unref(old_types_[i]);
270   }
271   if(refcount()==0){
272     delete[] block_lengths_;
273     delete[] block_indices_;
274     delete[] old_types_;
275   }
276 }
277
278 int Type_Struct::clone(MPI_Datatype* type)
279 {
280   *type = new Type_Struct(this->size(), this->lb(), this->ub(), this->flags(), this->block_count_, this->block_lengths_, this->block_indices_, this->old_types_);
281   (*type)->copy_attrs(this);
282   return MPI_SUCCESS;
283 }
284
285 void Type_Struct::serialize(const void* noncontiguous_buf, void *contiguous_buf,
286                         int count){
287   auto* contiguous_buf_char          = static_cast<char*>(contiguous_buf);
288   const auto* noncontiguous_buf_iter = static_cast<const char*>(noncontiguous_buf);
289   const auto* noncontiguous_buf_char = noncontiguous_buf_iter + block_indices_[0];
290   for (int j = 0; j < count; j++) {
291     for (int i = 0; i < block_count_; i++) {
292       if (not(old_types_[i]->flags() & DT_FLAG_DERIVED))
293         memcpy(contiguous_buf_char, noncontiguous_buf_char, block_lengths_[i] * old_types_[i]->size());
294       else
295         old_types_[i]->serialize( noncontiguous_buf_char,contiguous_buf_char,block_lengths_[i]);
296
297
298       contiguous_buf_char += block_lengths_[i]*old_types_[i]->size();
299       if (i<block_count_-1)
300         noncontiguous_buf_char = noncontiguous_buf_iter + block_indices_[i+1];
301       else //let's hope this is MPI_UB ?
302         noncontiguous_buf_char += block_lengths_[i]*old_types_[i]->get_extent();
303     }
304     noncontiguous_buf_iter=noncontiguous_buf_char;
305   }
306 }
307
308 void Type_Struct::unserialize(const void* contiguous_buf, void *noncontiguous_buf,
309                               int count, MPI_Op op){
310   const auto* contiguous_buf_char = static_cast<const char*>(contiguous_buf);
311   auto* noncontiguous_buf_char    = static_cast<char*>(noncontiguous_buf) + block_indices_[0];
312   for (int j = 0; j < count; j++) {
313     for (int i = 0; i < block_count_; i++) {
314       if (not(old_types_[i]->flags() & DT_FLAG_DERIVED)) {
315         if(op!=MPI_OP_NULL)
316           op->apply( contiguous_buf_char, noncontiguous_buf_char, &block_lengths_[i], old_types_[i]);
317       }else
318         old_types_[i]->unserialize( contiguous_buf_char, noncontiguous_buf_char,block_lengths_[i], op);
319
320       contiguous_buf_char += block_lengths_[i]*old_types_[i]->size();
321       if (i<block_count_-1)
322         noncontiguous_buf_char =  static_cast<char*>(noncontiguous_buf) + block_indices_[i+1];
323       else
324         noncontiguous_buf_char += block_lengths_[i]*old_types_[i]->get_extent();
325     }
326     noncontiguous_buf=static_cast<void*>(noncontiguous_buf_char);
327   }
328 }
329
330 }
331 }