1 /* smpi_datatype.cpp -- MPI primitives to handle datatypes */
2 /* Copyright (c) 2009-2020. The SimGrid Team. All rights reserved. */
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. */
7 #include "smpi_datatype_derived.hpp"
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)
25 integers_=new int[number_of_integers_];
26 for(int i=0; i<number_of_integers_; i++){
27 integers_[i]=integers[i];
29 addresses_=new MPI_Aint[number_of_addresses_];
30 for(int i=0; i<number_of_addresses_; i++){
31 addresses_[i]=addresses[i];
33 datatypes_=new MPI_Datatype[number_of_datatypes_];
34 for(int i=0; i<number_of_datatypes_; i++){
35 datatypes_[i]=datatypes[i];
38 Datatype_contents::~Datatype_contents(){
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)
47 contents_ = new Datatype_contents(MPI_COMBINER_CONTIGUOUS, 1, &size, 0, nullptr, 1, &old_type);
51 Type_Contiguous::~Type_Contiguous()
53 Datatype::unref(old_type_);
56 int Type_Contiguous::clone(MPI_Datatype* type)
58 *type = new Type_Contiguous(this->size(), this->lb(), this->ub(), this->flags(), this->block_count_, this->old_type_);
59 (*type)->copy_attrs(this);
63 void Type_Contiguous::serialize(const void* noncontiguous_buf, void* contiguous_buf, int count)
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());
70 void Type_Contiguous::unserialize(const void* contiguous_buf, void* noncontiguous_buf, int count, MPI_Op op)
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_;
76 op->apply( contiguous_buf_char, noncontiguous_buf_char, &n, old_type_);
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);
84 Type_Hvector::~Type_Hvector(){
85 Datatype::unref(old_type_);
88 int Type_Hvector::clone(MPI_Datatype* type)
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);
95 void Type_Hvector::serialize(const void* noncontiguous_buf, void *contiguous_buf,
97 char* contiguous_buf_char = static_cast<char*>(contiguous_buf);
98 const char* noncontiguous_buf_char = static_cast<const char*>(noncontiguous_buf);
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());
104 old_type_->serialize( noncontiguous_buf_char, contiguous_buf_char, block_length_);
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();
110 noncontiguous_buf_char += block_stride_;
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);
119 for (int i = 0; i < block_count_ * count; i++) {
120 if (not(old_type_->flags() & DT_FLAG_DERIVED)) {
122 op->apply( contiguous_buf_char, noncontiguous_buf_char, &block_length_, old_type_);
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();
129 noncontiguous_buf_char += block_stride_;
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)
138 int ints[3] = {count, block_length, stride};
139 contents_ = new Datatype_contents(MPI_COMBINER_VECTOR, 3, ints, 0, nullptr, 1, &old_type);
142 int Type_Vector::clone(MPI_Datatype* type)
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);
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)
157 int* ints = new int[count+1];
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);
164 for (int i = 0; i < count; i++) {
165 block_lengths_[i] = block_lengths[i];
166 block_indices_[i] = block_indices[i];
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)
179 for (int i = 0; i < count; i++) {
180 block_lengths_[i] = block_lengths[i];
181 block_indices_[i] = block_indices[i] * factor;
185 int Type_Hindexed::clone(MPI_Datatype* type)
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);
192 Type_Hindexed::~Type_Hindexed()
194 Datatype::unref(old_type_);
196 delete[] block_lengths_;
197 delete[] block_indices_;
201 void Type_Hindexed::serialize(const void* noncontiguous_buf, void *contiguous_buf,
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());
211 old_type_->serialize(noncontiguous_buf_char, contiguous_buf_char,block_lengths_[i]);
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];
217 noncontiguous_buf_char += block_lengths_[i]*old_type_->get_extent();
219 noncontiguous_buf_iter=noncontiguous_buf_char;
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)) {
231 op->apply( contiguous_buf_char, noncontiguous_buf_char, &block_lengths_[i],
234 old_type_->unserialize( contiguous_buf_char,noncontiguous_buf_char,block_lengths_[i], op);
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];
240 noncontiguous_buf_char += block_lengths_[i]*old_type_->get_extent();
242 noncontiguous_buf=static_cast<void*>(noncontiguous_buf_char);
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())
251 int* ints = new int[2*count+1];
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);
261 int Type_Indexed::clone(MPI_Datatype* type)
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);
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])
276 int* ints = new int[count+1];
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);
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();
290 Type_Struct::~Type_Struct(){
291 for (int i = 0; i < block_count_; i++) {
292 Datatype::unref(old_types_[i]);
295 delete[] block_lengths_;
296 delete[] block_indices_;
301 int Type_Struct::clone(MPI_Datatype* type)
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);
308 void Type_Struct::serialize(const void* noncontiguous_buf, void *contiguous_buf,
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());
318 old_types_[i]->serialize( noncontiguous_buf_char,contiguous_buf_char,block_lengths_[i]);
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();
327 noncontiguous_buf_iter=noncontiguous_buf_char;
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)) {
339 op->apply( contiguous_buf_char, noncontiguous_buf_char, &block_lengths_[i], old_types_[i]);
341 old_types_[i]->unserialize( contiguous_buf_char, noncontiguous_buf_char,block_lengths_[i], op);
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];
347 noncontiguous_buf_char += block_lengths_[i]*old_types_[i]->get_extent();
349 noncontiguous_buf=static_cast<void*>(noncontiguous_buf_char);