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)
24 for (auto& datatype : datatypes_)
28 Datatype_contents::~Datatype_contents()
30 for (auto& datatype : datatypes_)
31 Datatype::unref(datatype);
34 Type_Contiguous::Type_Contiguous(int size, MPI_Aint lb, MPI_Aint ub, int flags, int block_count, MPI_Datatype old_type)
35 : Datatype(size, lb, ub, flags), block_count_(block_count), old_type_(old_type)
37 set_contents(MPI_COMBINER_CONTIGUOUS, 1, &block_count, 0, nullptr, 1, &old_type);
41 Type_Contiguous::~Type_Contiguous()
43 Datatype::unref(old_type_);
46 int Type_Contiguous::clone(MPI_Datatype* type)
48 *type = new Type_Contiguous(this->size(), this->lb(), this->ub(), this->flags(), this->block_count_, this->old_type_);
49 (*type)->copy_attrs(this);
53 void Type_Contiguous::serialize(const void* noncontiguous_buf, void* contiguous_buf, int count)
55 auto* contiguous_buf_char = static_cast<char*>(contiguous_buf);
56 const auto* noncontiguous_buf_char = static_cast<const char*>(noncontiguous_buf) + lb();
57 memcpy(contiguous_buf_char, noncontiguous_buf_char, count * block_count_ * old_type_->size());
60 void Type_Contiguous::unserialize(const void* contiguous_buf, void* noncontiguous_buf, int count, MPI_Op op)
62 const auto* contiguous_buf_char = static_cast<const char*>(contiguous_buf);
63 auto* noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf) + lb();
64 int n= count*block_count_;
66 op->apply( contiguous_buf_char, noncontiguous_buf_char, &n, old_type_);
69 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){
70 const std::array<int, 2> ints = {{count, block_length}};
71 set_contents(MPI_COMBINER_HVECTOR, 2, ints.data(), 1, &stride, 1, &old_type);
74 Type_Hvector::~Type_Hvector(){
75 Datatype::unref(old_type_);
78 int Type_Hvector::clone(MPI_Datatype* type)
80 *type = new Type_Hvector(this->size(), this->lb(), this->ub(), this->flags(), this->block_count_, this->block_length_, this->block_stride_, this->old_type_);
81 (*type)->copy_attrs(this);
85 void Type_Hvector::serialize(const void* noncontiguous_buf, void *contiguous_buf,
87 auto* contiguous_buf_char = static_cast<char*>(contiguous_buf);
88 const auto* noncontiguous_buf_char = static_cast<const char*>(noncontiguous_buf);
90 for (int i = 0; i < block_count_ * count; i++) {
91 if (not(old_type_->flags() & DT_FLAG_DERIVED))
92 memcpy(contiguous_buf_char, noncontiguous_buf_char, block_length_ * old_type_->size());
94 old_type_->serialize( noncontiguous_buf_char, contiguous_buf_char, block_length_);
96 contiguous_buf_char += block_length_*old_type_->size();
97 if((i+1)%block_count_ ==0)
98 noncontiguous_buf_char += block_length_*old_type_->size();
100 noncontiguous_buf_char += block_stride_;
104 void Type_Hvector::unserialize(const void* contiguous_buf, void *noncontiguous_buf,
105 int count, MPI_Op op){
106 const auto* contiguous_buf_char = static_cast<const char*>(contiguous_buf);
107 auto* noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf);
109 for (int i = 0; i < block_count_ * count; i++) {
110 if (not(old_type_->flags() & DT_FLAG_DERIVED)) {
112 op->apply( contiguous_buf_char, noncontiguous_buf_char, &block_length_, old_type_);
114 old_type_->unserialize( contiguous_buf_char, noncontiguous_buf_char, block_length_, op);
115 contiguous_buf_char += block_length_*old_type_->size();
116 if((i+1)%block_count_ ==0)
117 noncontiguous_buf_char += block_length_*old_type_->size();
119 noncontiguous_buf_char += block_stride_;
123 Type_Vector::Type_Vector(int size, MPI_Aint lb, MPI_Aint ub, int flags, int count, int block_length, int stride,
124 MPI_Datatype old_type)
125 : Type_Hvector(size, lb, ub, flags, count, block_length, stride * old_type->get_extent(), old_type)
127 const std::array<int, 3> ints = {{count, block_length, stride}};
128 set_contents(MPI_COMBINER_VECTOR, 3, ints.data(), 0, nullptr, 1, &old_type);
131 int Type_Vector::clone(MPI_Datatype* type)
133 *type = new Type_Vector(this->size(), this->lb(), this->ub(), this->flags(), this->block_count_, this->block_length_, this->block_stride_, this->old_type_);
134 (*type)->copy_attrs(this);
138 Type_Hindexed::Type_Hindexed(int size, MPI_Aint lb, MPI_Aint ub, int flags, int count, const int* block_lengths,
139 const MPI_Aint* block_indices, MPI_Datatype old_type)
140 : Datatype(size, lb, ub, flags)
141 , block_count_(count)
142 , block_lengths_(new int[count])
143 , block_indices_(new MPI_Aint[count])
144 , old_type_(old_type)
146 std::vector<int> ints(count + 1);
148 for(int i=1;i<=count;i++)
149 ints[i]=block_lengths[i-1];
150 set_contents(MPI_COMBINER_HINDEXED, count + 1, ints.data(), count, block_indices, 1, &old_type);
152 for (int i = 0; i < count; i++) {
153 block_lengths_[i] = block_lengths[i];
154 block_indices_[i] = block_indices[i];
158 Type_Hindexed::Type_Hindexed(int size, MPI_Aint lb, MPI_Aint ub, int flags, int count, const int* block_lengths,
159 const int* block_indices, MPI_Datatype old_type, MPI_Aint factor)
160 : Datatype(size, lb, ub, flags)
161 , block_count_(count)
162 , block_lengths_(new int[count])
163 , block_indices_(new MPI_Aint[count])
164 , old_type_(old_type)
167 for (int i = 0; i < count; i++) {
168 block_lengths_[i] = block_lengths[i];
169 block_indices_[i] = block_indices[i] * factor;
173 int Type_Hindexed::clone(MPI_Datatype* type)
175 *type = new Type_Hindexed(this->size(), this->lb(), this->ub(), this->flags(), this->block_count_, this->block_lengths_, this->block_indices_, this->old_type_);
176 (*type)->copy_attrs(this);
180 Type_Hindexed::~Type_Hindexed()
182 Datatype::unref(old_type_);
184 delete[] block_lengths_;
185 delete[] block_indices_;
189 void Type_Hindexed::serialize(const void* noncontiguous_buf, void *contiguous_buf,
191 auto* contiguous_buf_char = static_cast<char*>(contiguous_buf);
192 const auto* noncontiguous_buf_iter = static_cast<const char*>(noncontiguous_buf);
193 const auto* noncontiguous_buf_char = noncontiguous_buf_iter + block_indices_[0];
194 for (int j = 0; j < count; j++) {
195 for (int i = 0; i < block_count_; i++) {
196 if (not(old_type_->flags() & DT_FLAG_DERIVED))
197 memcpy(contiguous_buf_char, noncontiguous_buf_char, block_lengths_[i] * old_type_->size());
199 old_type_->serialize(noncontiguous_buf_char, contiguous_buf_char,block_lengths_[i]);
201 contiguous_buf_char += block_lengths_[i]*old_type_->size();
202 if (i<block_count_-1)
203 noncontiguous_buf_char = noncontiguous_buf_iter + block_indices_[i+1];
205 noncontiguous_buf_char += block_lengths_[i]*old_type_->get_extent();
207 noncontiguous_buf_iter=noncontiguous_buf_char;
211 void Type_Hindexed::unserialize(const void* contiguous_buf, void *noncontiguous_buf,
212 int count, MPI_Op op){
213 const auto* contiguous_buf_char = static_cast<const char*>(contiguous_buf);
214 auto* noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf) + block_indices_[0];
215 for (int j = 0; j < count; j++) {
216 for (int i = 0; i < block_count_; i++) {
217 if (not(old_type_->flags() & DT_FLAG_DERIVED)) {
219 op->apply( contiguous_buf_char, noncontiguous_buf_char, &block_lengths_[i],
222 old_type_->unserialize( contiguous_buf_char,noncontiguous_buf_char,block_lengths_[i], op);
224 contiguous_buf_char += block_lengths_[i]*old_type_->size();
225 if (i<block_count_-1)
226 noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf) + block_indices_[i+1];
228 noncontiguous_buf_char += block_lengths_[i]*old_type_->get_extent();
230 noncontiguous_buf=static_cast<void*>(noncontiguous_buf_char);
234 Type_Indexed::Type_Indexed(int size, MPI_Aint lb, MPI_Aint ub, int flags, int count, const int* block_lengths,
235 const int* block_indices, MPI_Datatype old_type)
236 : Type_Hindexed(size, lb, ub, flags, count, block_lengths, block_indices, old_type, old_type->get_extent())
238 std::vector<int> ints(2 * count + 1);
240 for(int i=1;i<=count;i++)
241 ints[i]=block_lengths[i-1];
242 for(int i=count+1;i<=2*count;i++)
243 ints[i]=block_indices[i-count-1];
244 set_contents(MPI_COMBINER_INDEXED, 2 * count + 1, ints.data(), 0, nullptr, 1, &old_type);
247 int Type_Indexed::clone(MPI_Datatype* type)
249 *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_);
250 (*type)->copy_attrs(this);
254 Type_Struct::Type_Struct(int size, MPI_Aint lb, MPI_Aint ub, int flags, int count, const int* block_lengths,
255 const MPI_Aint* block_indices, const MPI_Datatype* old_types)
256 : Datatype(size, lb, ub, flags)
257 , block_count_(count)
258 , block_lengths_(new int[count])
259 , block_indices_(new MPI_Aint[count])
260 , old_types_(new MPI_Datatype[count])
262 std::vector<int> ints(count + 1);
264 for(int i=1;i<=count;i++)
265 ints[i]=block_lengths[i-1];
266 set_contents(MPI_COMBINER_INDEXED, count + 1, ints.data(), count, block_indices, count, old_types);
267 for (int i = 0; i < count; i++) {
268 block_lengths_[i]=block_lengths[i];
269 block_indices_[i]=block_indices[i];
270 old_types_[i]=old_types[i];
271 old_types_[i]->ref();
275 Type_Struct::~Type_Struct(){
276 for (int i = 0; i < block_count_; i++) {
277 Datatype::unref(old_types_[i]);
280 delete[] block_lengths_;
281 delete[] block_indices_;
286 int Type_Struct::clone(MPI_Datatype* type)
288 *type = new Type_Struct(this->size(), this->lb(), this->ub(), this->flags(), this->block_count_, this->block_lengths_, this->block_indices_, this->old_types_);
289 (*type)->copy_attrs(this);
293 void Type_Struct::serialize(const void* noncontiguous_buf, void *contiguous_buf,
295 auto* contiguous_buf_char = static_cast<char*>(contiguous_buf);
296 const auto* noncontiguous_buf_iter = static_cast<const char*>(noncontiguous_buf);
297 const auto* noncontiguous_buf_char = noncontiguous_buf_iter + block_indices_[0];
298 for (int j = 0; j < count; j++) {
299 for (int i = 0; i < block_count_; i++) {
300 if (not(old_types_[i]->flags() & DT_FLAG_DERIVED))
301 memcpy(contiguous_buf_char, noncontiguous_buf_char, block_lengths_[i] * old_types_[i]->size());
303 old_types_[i]->serialize( noncontiguous_buf_char,contiguous_buf_char,block_lengths_[i]);
306 contiguous_buf_char += block_lengths_[i]*old_types_[i]->size();
307 if (i<block_count_-1)
308 noncontiguous_buf_char = noncontiguous_buf_iter + block_indices_[i+1];
309 else //let's hope this is MPI_UB ?
310 noncontiguous_buf_char += block_lengths_[i]*old_types_[i]->get_extent();
312 noncontiguous_buf_iter=noncontiguous_buf_char;
316 void Type_Struct::unserialize(const void* contiguous_buf, void *noncontiguous_buf,
317 int count, MPI_Op op){
318 const auto* contiguous_buf_char = static_cast<const char*>(contiguous_buf);
319 auto* noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf) + block_indices_[0];
320 for (int j = 0; j < count; j++) {
321 for (int i = 0; i < block_count_; i++) {
322 if (not(old_types_[i]->flags() & DT_FLAG_DERIVED)) {
324 op->apply( contiguous_buf_char, noncontiguous_buf_char, &block_lengths_[i], old_types_[i]);
326 old_types_[i]->unserialize( contiguous_buf_char, noncontiguous_buf_char,block_lengths_[i], op);
328 contiguous_buf_char += block_lengths_[i]*old_types_[i]->size();
329 if (i<block_count_-1)
330 noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf) + block_indices_[i+1];
332 noncontiguous_buf_char += block_lengths_[i]*old_types_[i]->get_extent();
334 noncontiguous_buf=static_cast<void*>(noncontiguous_buf_char);