Logo AND Algorithmique Numérique Distribuée

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