Logo AND Algorithmique Numérique Distribuée

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