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 Type_Contiguous* Type_Contiguous::clone()
58 return new Type_Contiguous(this->size(), this->lb(), this->ub(), this->flags(), this->block_count_, this->old_type_);
61 void Type_Contiguous::serialize(const void* noncontiguous_buf, void* contiguous_buf, int count)
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());
68 void Type_Contiguous::unserialize(const void* contiguous_buf, void* noncontiguous_buf, int count, MPI_Op op)
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_;
74 op->apply( contiguous_buf_char, noncontiguous_buf_char, &n, old_type_);
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);
82 Type_Hvector::~Type_Hvector(){
83 Datatype::unref(old_type_);
86 Type_Hvector* Type_Hvector::clone()
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_);
91 void Type_Hvector::serialize(const void* noncontiguous_buf, void *contiguous_buf,
93 char* contiguous_buf_char = static_cast<char*>(contiguous_buf);
94 const char* noncontiguous_buf_char = static_cast<const char*>(noncontiguous_buf);
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());
100 old_type_->serialize( noncontiguous_buf_char, contiguous_buf_char, block_length_);
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();
106 noncontiguous_buf_char += block_stride_;
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);
115 for (int i = 0; i < block_count_ * count; i++) {
116 if (not(old_type_->flags() & DT_FLAG_DERIVED)) {
118 op->apply( contiguous_buf_char, noncontiguous_buf_char, &block_length_, old_type_);
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();
125 noncontiguous_buf_char += block_stride_;
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)
134 int ints[3] = {count, block_length, stride};
135 contents_ = new Datatype_contents(MPI_COMBINER_VECTOR, 3, ints, 0, nullptr, 1, &old_type);
138 Type_Vector* Type_Vector::clone()
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_);
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)
151 int* ints = new int[count+1];
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);
158 for (int i = 0; i < count; i++) {
159 block_lengths_[i] = block_lengths[i];
160 block_indices_[i] = block_indices[i];
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)
173 for (int i = 0; i < count; i++) {
174 block_lengths_[i] = block_lengths[i];
175 block_indices_[i] = block_indices[i] * factor;
179 Type_Hindexed* Type_Hindexed::clone()
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_);
184 Type_Hindexed::~Type_Hindexed()
186 Datatype::unref(old_type_);
188 delete[] block_lengths_;
189 delete[] block_indices_;
193 void Type_Hindexed::serialize(const void* noncontiguous_buf, void *contiguous_buf,
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());
203 old_type_->serialize(noncontiguous_buf_char, contiguous_buf_char,block_lengths_[i]);
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];
209 noncontiguous_buf_char += block_lengths_[i]*old_type_->get_extent();
211 noncontiguous_buf_iter=noncontiguous_buf_char;
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)) {
223 op->apply( contiguous_buf_char, noncontiguous_buf_char, &block_lengths_[i],
226 old_type_->unserialize( contiguous_buf_char,noncontiguous_buf_char,block_lengths_[i], op);
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];
232 noncontiguous_buf_char += block_lengths_[i]*old_type_->get_extent();
234 noncontiguous_buf=static_cast<void*>(noncontiguous_buf_char);
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())
243 int* ints = new int[2*count+1];
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);
253 Type_Indexed* Type_Indexed::clone()
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_);
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])
266 int* ints = new int[count+1];
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);
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();
280 Type_Struct::~Type_Struct(){
281 for (int i = 0; i < block_count_; i++) {
282 Datatype::unref(old_types_[i]);
285 delete[] block_lengths_;
286 delete[] block_indices_;
291 Type_Struct* Type_Struct::clone()
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_);
296 void Type_Struct::serialize(const void* noncontiguous_buf, void *contiguous_buf,
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());
306 old_types_[i]->serialize( noncontiguous_buf_char,contiguous_buf_char,block_lengths_[i]);
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();
315 noncontiguous_buf_iter=noncontiguous_buf_char;
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)) {
327 op->apply( contiguous_buf_char, noncontiguous_buf_char, &block_lengths_[i], old_types_[i]);
329 old_types_[i]->unserialize( contiguous_buf_char, noncontiguous_buf_char,block_lengths_[i], op);
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];
335 noncontiguous_buf_char += block_lengths_[i]*old_types_[i]->get_extent();
337 noncontiguous_buf=static_cast<void*>(noncontiguous_buf_char);