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 void Type_Contiguous::serialize(const void* noncontiguous_buf, void* contiguous_buf, int count)
58 char* contiguous_buf_char = static_cast<char*>(contiguous_buf);
59 const char* noncontiguous_buf_char = static_cast<const char*>(noncontiguous_buf)+lb();
60 memcpy(contiguous_buf_char, noncontiguous_buf_char, count * block_count_ * old_type_->size());
63 void Type_Contiguous::unserialize(const void* contiguous_buf, void* noncontiguous_buf, int count, MPI_Op op)
65 const char* contiguous_buf_char = static_cast<const char*>(contiguous_buf);
66 char* noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf)+lb();
67 int n= count*block_count_;
69 op->apply( contiguous_buf_char, noncontiguous_buf_char, &n, old_type_);
72 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){
73 int ints[2] = {count, block_length};
74 contents_ = new Datatype_contents(MPI_COMBINER_HVECTOR, 2, ints, 1, &stride, 1, &old_type);
77 Type_Hvector::~Type_Hvector(){
78 Datatype::unref(old_type_);
81 void Type_Hvector::serialize(const void* noncontiguous_buf, void *contiguous_buf,
83 char* contiguous_buf_char = static_cast<char*>(contiguous_buf);
84 const char* noncontiguous_buf_char = static_cast<const char*>(noncontiguous_buf);
86 for (int i = 0; i < block_count_ * count; i++) {
87 if (not(old_type_->flags() & DT_FLAG_DERIVED))
88 memcpy(contiguous_buf_char, noncontiguous_buf_char, block_length_ * old_type_->size());
90 old_type_->serialize( noncontiguous_buf_char, contiguous_buf_char, block_length_);
92 contiguous_buf_char += block_length_*old_type_->size();
93 if((i+1)%block_count_ ==0)
94 noncontiguous_buf_char += block_length_*old_type_->size();
96 noncontiguous_buf_char += block_stride_;
100 void Type_Hvector::unserialize(const void* contiguous_buf, void *noncontiguous_buf,
101 int count, MPI_Op op){
102 const char* contiguous_buf_char = static_cast<const char*>(contiguous_buf);
103 char* noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf);
105 for (int i = 0; i < block_count_ * count; i++) {
106 if (not(old_type_->flags() & DT_FLAG_DERIVED)) {
108 op->apply( contiguous_buf_char, noncontiguous_buf_char, &block_length_, old_type_);
110 old_type_->unserialize( contiguous_buf_char, noncontiguous_buf_char, block_length_, op);
111 contiguous_buf_char += block_length_*old_type_->size();
112 if((i+1)%block_count_ ==0)
113 noncontiguous_buf_char += block_length_*old_type_->size();
115 noncontiguous_buf_char += block_stride_;
119 Type_Vector::Type_Vector(int size, MPI_Aint lb, MPI_Aint ub, int flags, int count, int block_length, int stride,
120 MPI_Datatype old_type)
121 : Type_Hvector(size, lb, ub, flags, count, block_length, stride * old_type->get_extent(), old_type)
124 int ints[3] = {count, block_length, stride};
125 contents_ = new Datatype_contents(MPI_COMBINER_VECTOR, 3, ints, 0, nullptr, 1, &old_type);
128 Type_Hindexed::Type_Hindexed(int size, MPI_Aint lb, MPI_Aint ub, int flags, int count, const int* block_lengths,
129 const MPI_Aint* block_indices, MPI_Datatype old_type)
130 : Datatype(size, lb, ub, flags)
131 , block_count_(count)
132 , block_lengths_(new int[count])
133 , block_indices_(new MPI_Aint[count])
134 , old_type_(old_type)
136 int* ints = new int[count+1];
138 for(int i=1;i<=count;i++)
139 ints[i]=block_lengths[i-1];
140 contents_ = new Datatype_contents(MPI_COMBINER_HINDEXED, count+1, ints, count, block_indices, 1, &old_type);
143 for (int i = 0; i < count; i++) {
144 block_lengths_[i] = block_lengths[i];
145 block_indices_[i] = block_indices[i];
149 Type_Hindexed::Type_Hindexed(int size, MPI_Aint lb, MPI_Aint ub, int flags, int count, const int* block_lengths,
150 const int* block_indices, MPI_Datatype old_type, MPI_Aint factor)
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)
158 for (int i = 0; i < count; i++) {
159 block_lengths_[i] = block_lengths[i];
160 block_indices_[i] = block_indices[i] * factor;
164 Type_Hindexed::~Type_Hindexed()
166 Datatype::unref(old_type_);
168 delete[] block_lengths_;
169 delete[] block_indices_;
173 void Type_Hindexed::serialize(const void* noncontiguous_buf, void *contiguous_buf,
175 char* contiguous_buf_char = static_cast<char*>(contiguous_buf);
176 const char* noncontiguous_buf_iter = static_cast<const char*>(noncontiguous_buf);
177 const char* noncontiguous_buf_char = noncontiguous_buf_iter + block_indices_[0];
178 for (int j = 0; j < count; j++) {
179 for (int i = 0; i < block_count_; i++) {
180 if (not(old_type_->flags() & DT_FLAG_DERIVED))
181 memcpy(contiguous_buf_char, noncontiguous_buf_char, block_lengths_[i] * old_type_->size());
183 old_type_->serialize(noncontiguous_buf_char, contiguous_buf_char,block_lengths_[i]);
185 contiguous_buf_char += block_lengths_[i]*old_type_->size();
186 if (i<block_count_-1)
187 noncontiguous_buf_char = noncontiguous_buf_iter + block_indices_[i+1];
189 noncontiguous_buf_char += block_lengths_[i]*old_type_->get_extent();
191 noncontiguous_buf_iter=noncontiguous_buf_char;
195 void Type_Hindexed::unserialize(const void* contiguous_buf, void *noncontiguous_buf,
196 int count, MPI_Op op){
197 const char* contiguous_buf_char = static_cast<const char*>(contiguous_buf);
198 char* noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf)+ block_indices_[0];
199 for (int j = 0; j < count; j++) {
200 for (int i = 0; i < block_count_; i++) {
201 if (not(old_type_->flags() & DT_FLAG_DERIVED)) {
203 op->apply( contiguous_buf_char, noncontiguous_buf_char, &block_lengths_[i],
206 old_type_->unserialize( contiguous_buf_char,noncontiguous_buf_char,block_lengths_[i], op);
208 contiguous_buf_char += block_lengths_[i]*old_type_->size();
209 if (i<block_count_-1)
210 noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf) + block_indices_[i+1];
212 noncontiguous_buf_char += block_lengths_[i]*old_type_->get_extent();
214 noncontiguous_buf=static_cast<void*>(noncontiguous_buf_char);
218 Type_Indexed::Type_Indexed(int size, MPI_Aint lb, MPI_Aint ub, int flags, int count, const int* block_lengths,
219 const int* block_indices, MPI_Datatype old_type)
220 : Type_Hindexed(size, lb, ub, flags, count, block_lengths, block_indices, old_type, old_type->get_extent())
223 int* ints = new int[2*count+1];
225 for(int i=1;i<=count;i++)
226 ints[i]=block_lengths[i-1];
227 for(int i=count+1;i<=2*count;i++)
228 ints[i]=block_indices[i-count-1];
229 contents_ = new Datatype_contents(MPI_COMBINER_INDEXED, 2*count+1, ints, 0, nullptr, 1, &old_type);
233 Type_Struct::Type_Struct(int size, MPI_Aint lb, MPI_Aint ub, int flags, int count, const int* block_lengths,
234 const MPI_Aint* block_indices, const MPI_Datatype* old_types)
235 : Datatype(size, lb, ub, flags)
236 , block_count_(count)
237 , block_lengths_(new int[count])
238 , block_indices_(new MPI_Aint[count])
239 , old_types_(new MPI_Datatype[count])
241 int* ints = new int[count+1];
243 for(int i=1;i<=count;i++)
244 ints[i]=block_lengths[i-1];
245 contents_ = new Datatype_contents(MPI_COMBINER_INDEXED, count+1, ints, count, block_indices, count, old_types);
247 for (int i = 0; i < count; i++) {
248 block_lengths_[i]=block_lengths[i];
249 block_indices_[i]=block_indices[i];
250 old_types_[i]=old_types[i];
251 old_types_[i]->ref();
255 Type_Struct::~Type_Struct(){
256 for (int i = 0; i < block_count_; i++) {
257 Datatype::unref(old_types_[i]);
260 delete[] block_lengths_;
261 delete[] block_indices_;
267 void Type_Struct::serialize(const void* noncontiguous_buf, void *contiguous_buf,
269 char* contiguous_buf_char = static_cast<char*>(contiguous_buf);
270 const char* noncontiguous_buf_iter = static_cast<const char*>(noncontiguous_buf);
271 const char* noncontiguous_buf_char = noncontiguous_buf_iter + block_indices_[0];
272 for (int j = 0; j < count; j++) {
273 for (int i = 0; i < block_count_; i++) {
274 if (not(old_types_[i]->flags() & DT_FLAG_DERIVED))
275 memcpy(contiguous_buf_char, noncontiguous_buf_char, block_lengths_[i] * old_types_[i]->size());
277 old_types_[i]->serialize( noncontiguous_buf_char,contiguous_buf_char,block_lengths_[i]);
280 contiguous_buf_char += block_lengths_[i]*old_types_[i]->size();
281 if (i<block_count_-1)
282 noncontiguous_buf_char = noncontiguous_buf_iter + block_indices_[i+1];
283 else //let's hope this is MPI_UB ?
284 noncontiguous_buf_char += block_lengths_[i]*old_types_[i]->get_extent();
286 noncontiguous_buf_iter=noncontiguous_buf_char;
290 void Type_Struct::unserialize(const void* contiguous_buf, void *noncontiguous_buf,
291 int count, MPI_Op op){
292 const char* contiguous_buf_char = static_cast<const char*>(contiguous_buf);
293 char* noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf)+ block_indices_[0];
294 for (int j = 0; j < count; j++) {
295 for (int i = 0; i < block_count_; i++) {
296 if (not(old_types_[i]->flags() & DT_FLAG_DERIVED)) {
298 op->apply( contiguous_buf_char, noncontiguous_buf_char, &block_lengths_[i], old_types_[i]);
300 old_types_[i]->unserialize( contiguous_buf_char, noncontiguous_buf_char,block_lengths_[i], op);
302 contiguous_buf_char += block_lengths_[i]*old_types_[i]->size();
303 if (i<block_count_-1)
304 noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf) + block_indices_[i+1];
306 noncontiguous_buf_char += block_lengths_[i]*old_types_[i]->get_extent();
308 noncontiguous_buf=static_cast<void*>(noncontiguous_buf_char);