1 /* smpi_datatype.cpp -- MPI primitives to handle datatypes */
2 /* Copyright (c) 2009-2023. 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"
14 namespace simgrid::smpi {
16 Datatype_contents::Datatype_contents(int combiner, int number_of_integers, const int* integers, int number_of_addresses,
17 const MPI_Aint* addresses, int number_of_datatypes, const MPI_Datatype* datatypes)
19 , integers_(integers, integers + number_of_integers)
20 , addresses_(addresses, addresses + number_of_addresses)
21 , datatypes_(datatypes, datatypes + number_of_datatypes)
23 for (auto& datatype : datatypes_)
27 Datatype_contents::~Datatype_contents()
29 for (auto& datatype : datatypes_)
30 Datatype::unref(datatype);
33 Type_Contiguous::Type_Contiguous(int size, MPI_Aint lb, MPI_Aint ub, int flags, int block_count, MPI_Datatype old_type)
34 : Datatype(size, lb, ub, flags), block_count_(block_count), old_type_(old_type)
36 set_contents(MPI_COMBINER_CONTIGUOUS, 1, &block_count, 0, nullptr, 1, &old_type);
40 Type_Contiguous::~Type_Contiguous()
42 Datatype::unref(old_type_);
45 int Type_Contiguous::clone(MPI_Datatype* type)
47 *type = new Type_Contiguous(this->size(), this->lb(), this->ub(), this->flags(), this->block_count_, this->old_type_);
48 (*type)->copy_attrs(this);
52 void Type_Contiguous::serialize(const void* noncontiguous_buf, void* contiguous_buf, int count)
54 auto* contiguous_buf_char = static_cast<char*>(contiguous_buf);
55 const auto* noncontiguous_buf_char = static_cast<const char*>(noncontiguous_buf) + lb();
56 memcpy(contiguous_buf_char, noncontiguous_buf_char, old_type_->size() * count * block_count_);
59 void Type_Contiguous::unserialize(const void* contiguous_buf, void* noncontiguous_buf, int count, MPI_Op op)
61 const auto* contiguous_buf_char = static_cast<const char*>(contiguous_buf);
62 auto* noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf) + lb();
63 int n= count*block_count_;
65 op->apply( contiguous_buf_char, noncontiguous_buf_char, &n, old_type_);
68 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){
69 const std::array<int, 2> ints = {{count, block_length}};
70 set_contents(MPI_COMBINER_HVECTOR, 2, ints.data(), 1, &stride, 1, &old_type);
73 Type_Hvector::~Type_Hvector(){
74 Datatype::unref(old_type_);
77 int Type_Hvector::clone(MPI_Datatype* type)
79 *type = new Type_Hvector(this->size(), this->lb(), this->ub(), this->flags(), this->block_count_, this->block_length_, this->block_stride_, this->old_type_);
80 (*type)->copy_attrs(this);
84 void Type_Hvector::serialize(const void* noncontiguous_buf, void *contiguous_buf,
86 auto* contiguous_buf_char = static_cast<char*>(contiguous_buf);
87 const auto* noncontiguous_buf_char = static_cast<const char*>(noncontiguous_buf);
89 for (int i = 0; i < block_count_ * count; i++) {
90 if (not(old_type_->flags() & DT_FLAG_DERIVED))
91 memcpy(contiguous_buf_char, noncontiguous_buf_char, block_length_ * old_type_->size());
93 old_type_->serialize( noncontiguous_buf_char, contiguous_buf_char, block_length_);
95 contiguous_buf_char += block_length_*old_type_->size();
96 if((i+1)%block_count_ ==0)
97 noncontiguous_buf_char += block_length_*old_type_->size();
99 noncontiguous_buf_char += block_stride_;
103 void Type_Hvector::unserialize(const void* contiguous_buf, void *noncontiguous_buf,
104 int count, MPI_Op op){
105 const auto* contiguous_buf_char = static_cast<const char*>(contiguous_buf);
106 auto* noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf);
108 for (int i = 0; i < block_count_ * count; i++) {
109 if (not(old_type_->flags() & DT_FLAG_DERIVED)) {
111 op->apply( contiguous_buf_char, noncontiguous_buf_char, &block_length_, old_type_);
113 old_type_->unserialize( contiguous_buf_char, noncontiguous_buf_char, block_length_, op);
114 contiguous_buf_char += block_length_*old_type_->size();
115 if((i+1)%block_count_ ==0)
116 noncontiguous_buf_char += block_length_*old_type_->size();
118 noncontiguous_buf_char += block_stride_;
122 Type_Vector::Type_Vector(int size, MPI_Aint lb, MPI_Aint ub, int flags, int count, int block_length, int stride,
123 MPI_Datatype old_type)
124 : Type_Hvector(size, lb, ub, flags, count, block_length, stride * old_type->get_extent(), old_type)
126 const std::array<int, 3> ints = {{count, block_length, stride}};
127 set_contents(MPI_COMBINER_VECTOR, 3, ints.data(), 0, nullptr, 1, &old_type);
130 int Type_Vector::clone(MPI_Datatype* type)
132 *type = new Type_Vector(this->size(), this->lb(), this->ub(), this->flags(), this->block_count_, this->block_length_, this->block_stride_, this->old_type_);
133 (*type)->copy_attrs(this);
137 Type_Hindexed::Type_Hindexed(int size, MPI_Aint lb, MPI_Aint ub, int flags, int count, const int* block_lengths,
138 const MPI_Aint* block_indices, MPI_Datatype old_type)
139 : Datatype(size, lb, ub, flags)
140 , block_count_(count)
141 , block_lengths_(new int[count])
142 , block_indices_(new MPI_Aint[count])
143 , old_type_(old_type)
145 std::vector<int> ints(count + 1);
147 for(int i=1;i<=count;i++)
148 ints[i]=block_lengths[i-1];
149 set_contents(MPI_COMBINER_HINDEXED, count + 1, ints.data(), count, block_indices, 1, &old_type);
151 for (int i = 0; i < count; i++) {
152 block_lengths_[i] = block_lengths[i];
153 block_indices_[i] = block_indices[i];
157 Type_Hindexed::Type_Hindexed(int size, MPI_Aint lb, MPI_Aint ub, int flags, int count, const int* block_lengths,
158 const int* block_indices, MPI_Datatype old_type, MPI_Aint factor)
159 : Datatype(size, lb, ub, flags)
160 , block_count_(count)
161 , block_lengths_(new int[count])
162 , block_indices_(new MPI_Aint[count])
163 , old_type_(old_type)
166 for (int i = 0; i < count; i++) {
167 block_lengths_[i] = block_lengths[i];
168 block_indices_[i] = block_indices[i] * factor;
172 int Type_Hindexed::clone(MPI_Datatype* type)
174 *type = new Type_Hindexed(this->size(), this->lb(), this->ub(), this->flags(), this->block_count_, this->block_lengths_, this->block_indices_, this->old_type_);
175 (*type)->copy_attrs(this);
179 Type_Hindexed::~Type_Hindexed()
181 Datatype::unref(old_type_);
183 delete[] block_lengths_;
184 delete[] block_indices_;
188 void Type_Hindexed::serialize(const void* noncontiguous_buf, void *contiguous_buf,
190 auto* contiguous_buf_char = static_cast<char*>(contiguous_buf);
191 const auto* noncontiguous_buf_iter = static_cast<const char*>(noncontiguous_buf);
192 const auto* noncontiguous_buf_char = noncontiguous_buf_iter + block_indices_[0];
193 for (int j = 0; j < count; j++) {
194 for (int i = 0; i < block_count_; i++) {
195 if (not(old_type_->flags() & DT_FLAG_DERIVED))
196 memcpy(contiguous_buf_char, noncontiguous_buf_char, block_lengths_[i] * old_type_->size());
198 old_type_->serialize(noncontiguous_buf_char, contiguous_buf_char,block_lengths_[i]);
200 contiguous_buf_char += block_lengths_[i]*old_type_->size();
201 if (i<block_count_-1)
202 noncontiguous_buf_char = noncontiguous_buf_iter + block_indices_[i+1];
204 noncontiguous_buf_char += block_lengths_[i]*old_type_->get_extent();
206 noncontiguous_buf_iter=noncontiguous_buf_char;
210 void Type_Hindexed::unserialize(const void* contiguous_buf, void *noncontiguous_buf,
211 int count, MPI_Op op){
212 const auto* contiguous_buf_char = static_cast<const char*>(contiguous_buf);
213 auto* noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf) + block_indices_[0];
214 for (int j = 0; j < count; j++) {
215 for (int i = 0; i < block_count_; i++) {
216 if (not(old_type_->flags() & DT_FLAG_DERIVED)) {
218 op->apply( contiguous_buf_char, noncontiguous_buf_char, &block_lengths_[i],
221 old_type_->unserialize( contiguous_buf_char,noncontiguous_buf_char,block_lengths_[i], op);
223 contiguous_buf_char += block_lengths_[i]*old_type_->size();
224 if (i<block_count_-1)
225 noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf) + block_indices_[i+1];
227 noncontiguous_buf_char += block_lengths_[i]*old_type_->get_extent();
229 noncontiguous_buf=static_cast<void*>(noncontiguous_buf_char);
233 Type_Indexed::Type_Indexed(int size, MPI_Aint lb, MPI_Aint ub, int flags, int count, const int* block_lengths,
234 const int* block_indices, MPI_Datatype old_type)
235 : Type_Hindexed(size, lb, ub, flags, count, block_lengths, block_indices, old_type, old_type->get_extent())
237 std::vector<int> ints(2 * count + 1);
239 for(int i=1;i<=count;i++)
240 ints[i]=block_lengths[i-1];
241 for(int i=count+1;i<=2*count;i++)
242 ints[i]=block_indices[i-count-1];
243 set_contents(MPI_COMBINER_INDEXED, 2 * count + 1, ints.data(), 0, nullptr, 1, &old_type);
246 int Type_Indexed::clone(MPI_Datatype* type)
248 *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_);
249 (*type)->copy_attrs(this);
253 Type_Struct::Type_Struct(int size, MPI_Aint lb, MPI_Aint ub, int flags, int count, const int* block_lengths,
254 const MPI_Aint* block_indices, const MPI_Datatype* old_types)
255 : Datatype(size, lb, ub, flags)
256 , block_count_(count)
257 , block_lengths_(new int[count])
258 , block_indices_(new MPI_Aint[count])
259 , old_types_(new MPI_Datatype[count])
261 std::vector<int> ints(count + 1);
263 for(int i=1;i<=count;i++)
264 ints[i]=block_lengths[i-1];
265 set_contents(MPI_COMBINER_INDEXED, count + 1, ints.data(), count, block_indices, count, old_types);
266 for (int i = 0; i < count; i++) {
267 block_lengths_[i]=block_lengths[i];
268 block_indices_[i]=block_indices[i];
269 old_types_[i]=old_types[i];
270 old_types_[i]->ref();
274 Type_Struct::~Type_Struct(){
275 for (int i = 0; i < block_count_; i++) {
276 Datatype::unref(old_types_[i]);
279 delete[] block_lengths_;
280 delete[] block_indices_;
285 int Type_Struct::clone(MPI_Datatype* type)
287 *type = new Type_Struct(this->size(), this->lb(), this->ub(), this->flags(), this->block_count_, this->block_lengths_, this->block_indices_, this->old_types_);
288 (*type)->copy_attrs(this);
292 void Type_Struct::serialize(const void* noncontiguous_buf, void *contiguous_buf,
294 auto* contiguous_buf_char = static_cast<char*>(contiguous_buf);
295 const auto* noncontiguous_buf_iter = static_cast<const char*>(noncontiguous_buf);
296 const auto* noncontiguous_buf_char = noncontiguous_buf_iter + block_indices_[0];
297 for (int j = 0; j < count; j++) {
298 for (int i = 0; i < block_count_; i++) {
299 if (not(old_types_[i]->flags() & DT_FLAG_DERIVED))
300 memcpy(contiguous_buf_char, noncontiguous_buf_char, block_lengths_[i] * old_types_[i]->size());
302 old_types_[i]->serialize( noncontiguous_buf_char,contiguous_buf_char,block_lengths_[i]);
305 contiguous_buf_char += block_lengths_[i]*old_types_[i]->size();
306 if (i<block_count_-1)
307 noncontiguous_buf_char = noncontiguous_buf_iter + block_indices_[i+1];
308 else //let's hope this is MPI_UB ?
309 noncontiguous_buf_char += block_lengths_[i]*old_types_[i]->get_extent();
311 noncontiguous_buf_iter=noncontiguous_buf_char;
315 void Type_Struct::unserialize(const void* contiguous_buf, void *noncontiguous_buf,
316 int count, MPI_Op op){
317 const auto* contiguous_buf_char = static_cast<const char*>(contiguous_buf);
318 auto* noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf) + block_indices_[0];
319 for (int j = 0; j < count; j++) {
320 for (int i = 0; i < block_count_; i++) {
321 if (not(old_types_[i]->flags() & DT_FLAG_DERIVED)) {
323 op->apply( contiguous_buf_char, noncontiguous_buf_char, &block_lengths_[i], old_types_[i]);
325 old_types_[i]->unserialize( contiguous_buf_char, noncontiguous_buf_char,block_lengths_[i], op);
327 contiguous_buf_char += block_lengths_[i]*old_types_[i]->size();
328 if (i<block_count_-1)
329 noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf) + block_indices_[i+1];
331 noncontiguous_buf_char += block_lengths_[i]*old_types_[i]->get_extent();
333 noncontiguous_buf=static_cast<void*>(noncontiguous_buf_char);
337 } // namespace simgrid::smpi