1 /* smpi_datatype.cpp -- MPI primitives to handle datatypes */
2 /* Copyright (c) 2009-2021. 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, int number_of_integers, const int* integers, int number_of_addresses,
18 const MPI_Aint* addresses, int number_of_datatypes, const MPI_Datatype* datatypes)
20 , integers_(integers, integers + number_of_integers)
21 , addresses_(addresses, addresses + number_of_addresses)
22 , datatypes_(datatypes, datatypes + number_of_datatypes)
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)
29 contents_ = new Datatype_contents(MPI_COMBINER_CONTIGUOUS, 1, &block_count, 0, nullptr, 1, &old_type);
33 Type_Contiguous::~Type_Contiguous()
35 Datatype::unref(old_type_);
38 int Type_Contiguous::clone(MPI_Datatype* type)
40 *type = new Type_Contiguous(this->size(), this->lb(), this->ub(), this->flags(), this->block_count_, this->old_type_);
41 (*type)->copy_attrs(this);
45 void Type_Contiguous::serialize(const void* noncontiguous_buf, void* contiguous_buf, int count)
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());
52 void Type_Contiguous::unserialize(const void* contiguous_buf, void* noncontiguous_buf, int count, MPI_Op op)
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_;
58 op->apply( contiguous_buf_char, noncontiguous_buf_char, &n, old_type_);
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 contents_ = new Datatype_contents(MPI_COMBINER_HVECTOR, 2, ints.data(), 1, &stride, 1, &old_type);
66 Type_Hvector::~Type_Hvector(){
67 Datatype::unref(old_type_);
70 int Type_Hvector::clone(MPI_Datatype* type)
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);
77 void Type_Hvector::serialize(const void* noncontiguous_buf, void *contiguous_buf,
79 auto* contiguous_buf_char = static_cast<char*>(contiguous_buf);
80 const auto* noncontiguous_buf_char = static_cast<const char*>(noncontiguous_buf);
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());
86 old_type_->serialize( noncontiguous_buf_char, contiguous_buf_char, block_length_);
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();
92 noncontiguous_buf_char += block_stride_;
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);
101 for (int i = 0; i < block_count_ * count; i++) {
102 if (not(old_type_->flags() & DT_FLAG_DERIVED)) {
104 op->apply( contiguous_buf_char, noncontiguous_buf_char, &block_length_, old_type_);
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();
111 noncontiguous_buf_char += block_stride_;
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)
120 const std::array<int, 3> ints = {{count, block_length, stride}};
121 contents_ = new Datatype_contents(MPI_COMBINER_VECTOR, 3, ints.data(), 0, nullptr, 1, &old_type);
124 int Type_Vector::clone(MPI_Datatype* type)
126 *type = new Type_Vector(this->size(), this->lb(), this->ub(), this->flags(), this->block_count_, this->block_length_, this->block_stride_, this->old_type_);
127 (*type)->copy_attrs(this);
131 Type_Hindexed::Type_Hindexed(int size, MPI_Aint lb, MPI_Aint ub, int flags, int count, const int* block_lengths,
132 const MPI_Aint* block_indices, MPI_Datatype old_type)
133 : Datatype(size, lb, ub, flags)
134 , block_count_(count)
135 , block_lengths_(new int[count])
136 , block_indices_(new MPI_Aint[count])
137 , old_type_(old_type)
139 std::vector<int> ints(count + 1);
141 for(int i=1;i<=count;i++)
142 ints[i]=block_lengths[i-1];
143 contents_ = new Datatype_contents(MPI_COMBINER_HINDEXED, count + 1, ints.data(), count, block_indices, 1, &old_type);
145 for (int i = 0; i < count; i++) {
146 block_lengths_[i] = block_lengths[i];
147 block_indices_[i] = block_indices[i];
151 Type_Hindexed::Type_Hindexed(int size, MPI_Aint lb, MPI_Aint ub, int flags, int count, const int* block_lengths,
152 const int* block_indices, MPI_Datatype old_type, MPI_Aint factor)
153 : Datatype(size, lb, ub, flags)
154 , block_count_(count)
155 , block_lengths_(new int[count])
156 , block_indices_(new MPI_Aint[count])
157 , old_type_(old_type)
160 for (int i = 0; i < count; i++) {
161 block_lengths_[i] = block_lengths[i];
162 block_indices_[i] = block_indices[i] * factor;
166 int Type_Hindexed::clone(MPI_Datatype* type)
168 *type = new Type_Hindexed(this->size(), this->lb(), this->ub(), this->flags(), this->block_count_, this->block_lengths_, this->block_indices_, this->old_type_);
169 (*type)->copy_attrs(this);
173 Type_Hindexed::~Type_Hindexed()
175 Datatype::unref(old_type_);
177 delete[] block_lengths_;
178 delete[] block_indices_;
182 void Type_Hindexed::serialize(const void* noncontiguous_buf, void *contiguous_buf,
184 auto* contiguous_buf_char = static_cast<char*>(contiguous_buf);
185 const auto* noncontiguous_buf_iter = static_cast<const char*>(noncontiguous_buf);
186 const auto* noncontiguous_buf_char = noncontiguous_buf_iter + block_indices_[0];
187 for (int j = 0; j < count; j++) {
188 for (int i = 0; i < block_count_; i++) {
189 if (not(old_type_->flags() & DT_FLAG_DERIVED))
190 memcpy(contiguous_buf_char, noncontiguous_buf_char, block_lengths_[i] * old_type_->size());
192 old_type_->serialize(noncontiguous_buf_char, contiguous_buf_char,block_lengths_[i]);
194 contiguous_buf_char += block_lengths_[i]*old_type_->size();
195 if (i<block_count_-1)
196 noncontiguous_buf_char = noncontiguous_buf_iter + block_indices_[i+1];
198 noncontiguous_buf_char += block_lengths_[i]*old_type_->get_extent();
200 noncontiguous_buf_iter=noncontiguous_buf_char;
204 void Type_Hindexed::unserialize(const void* contiguous_buf, void *noncontiguous_buf,
205 int count, MPI_Op op){
206 const auto* contiguous_buf_char = static_cast<const char*>(contiguous_buf);
207 auto* noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf) + block_indices_[0];
208 for (int j = 0; j < count; j++) {
209 for (int i = 0; i < block_count_; i++) {
210 if (not(old_type_->flags() & DT_FLAG_DERIVED)) {
212 op->apply( contiguous_buf_char, noncontiguous_buf_char, &block_lengths_[i],
215 old_type_->unserialize( contiguous_buf_char,noncontiguous_buf_char,block_lengths_[i], op);
217 contiguous_buf_char += block_lengths_[i]*old_type_->size();
218 if (i<block_count_-1)
219 noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf) + block_indices_[i+1];
221 noncontiguous_buf_char += block_lengths_[i]*old_type_->get_extent();
223 noncontiguous_buf=static_cast<void*>(noncontiguous_buf_char);
227 Type_Indexed::Type_Indexed(int size, MPI_Aint lb, MPI_Aint ub, int flags, int count, const int* block_lengths,
228 const int* block_indices, MPI_Datatype old_type)
229 : Type_Hindexed(size, lb, ub, flags, count, block_lengths, block_indices, old_type, old_type->get_extent())
232 std::vector<int> ints(2 * count + 1);
234 for(int i=1;i<=count;i++)
235 ints[i]=block_lengths[i-1];
236 for(int i=count+1;i<=2*count;i++)
237 ints[i]=block_indices[i-count-1];
238 contents_ = new Datatype_contents(MPI_COMBINER_INDEXED, 2 * count + 1, ints.data(), 0, nullptr, 1, &old_type);
241 int Type_Indexed::clone(MPI_Datatype* type)
243 *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_);
244 (*type)->copy_attrs(this);
248 Type_Struct::Type_Struct(int size, MPI_Aint lb, MPI_Aint ub, int flags, int count, const int* block_lengths,
249 const MPI_Aint* block_indices, const MPI_Datatype* old_types)
250 : Datatype(size, lb, ub, flags)
251 , block_count_(count)
252 , block_lengths_(new int[count])
253 , block_indices_(new MPI_Aint[count])
254 , old_types_(new MPI_Datatype[count])
256 std::vector<int> ints(count + 1);
258 for(int i=1;i<=count;i++)
259 ints[i]=block_lengths[i-1];
261 new Datatype_contents(MPI_COMBINER_INDEXED, count + 1, ints.data(), count, block_indices, count, old_types);
262 for (int i = 0; i < count; i++) {
263 block_lengths_[i]=block_lengths[i];
264 block_indices_[i]=block_indices[i];
265 old_types_[i]=old_types[i];
266 old_types_[i]->ref();
270 Type_Struct::~Type_Struct(){
271 for (int i = 0; i < block_count_; i++) {
272 Datatype::unref(old_types_[i]);
275 delete[] block_lengths_;
276 delete[] block_indices_;
281 int Type_Struct::clone(MPI_Datatype* type)
283 *type = new Type_Struct(this->size(), this->lb(), this->ub(), this->flags(), this->block_count_, this->block_lengths_, this->block_indices_, this->old_types_);
284 (*type)->copy_attrs(this);
288 void Type_Struct::serialize(const void* noncontiguous_buf, void *contiguous_buf,
290 auto* contiguous_buf_char = static_cast<char*>(contiguous_buf);
291 const auto* noncontiguous_buf_iter = static_cast<const char*>(noncontiguous_buf);
292 const auto* noncontiguous_buf_char = noncontiguous_buf_iter + block_indices_[0];
293 for (int j = 0; j < count; j++) {
294 for (int i = 0; i < block_count_; i++) {
295 if (not(old_types_[i]->flags() & DT_FLAG_DERIVED))
296 memcpy(contiguous_buf_char, noncontiguous_buf_char, block_lengths_[i] * old_types_[i]->size());
298 old_types_[i]->serialize( noncontiguous_buf_char,contiguous_buf_char,block_lengths_[i]);
301 contiguous_buf_char += block_lengths_[i]*old_types_[i]->size();
302 if (i<block_count_-1)
303 noncontiguous_buf_char = noncontiguous_buf_iter + block_indices_[i+1];
304 else //let's hope this is MPI_UB ?
305 noncontiguous_buf_char += block_lengths_[i]*old_types_[i]->get_extent();
307 noncontiguous_buf_iter=noncontiguous_buf_char;
311 void Type_Struct::unserialize(const void* contiguous_buf, void *noncontiguous_buf,
312 int count, MPI_Op op){
313 const auto* contiguous_buf_char = static_cast<const char*>(contiguous_buf);
314 auto* noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf) + block_indices_[0];
315 for (int j = 0; j < count; j++) {
316 for (int i = 0; i < block_count_; i++) {
317 if (not(old_types_[i]->flags() & DT_FLAG_DERIVED)) {
319 op->apply( contiguous_buf_char, noncontiguous_buf_char, &block_lengths_[i], old_types_[i]);
321 old_types_[i]->unserialize( contiguous_buf_char, noncontiguous_buf_char,block_lengths_[i], op);
323 contiguous_buf_char += block_lengths_[i]*old_types_[i]->size();
324 if (i<block_count_-1)
325 noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf) + block_indices_[i+1];
327 noncontiguous_buf_char += block_lengths_[i]*old_types_[i]->get_extent();
329 noncontiguous_buf=static_cast<void*>(noncontiguous_buf_char);