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"
16 Type_Contiguous::Type_Contiguous(int size, MPI_Aint lb, MPI_Aint ub, int flags, int block_count, MPI_Datatype old_type)
17 : Datatype(size, lb, ub, flags), block_count_(block_count), old_type_(old_type)
22 Type_Contiguous::~Type_Contiguous()
24 Datatype::unref(old_type_);
27 void Type_Contiguous::serialize(const void* noncontiguous_buf, void* contiguous_buf, int count)
29 char* contiguous_buf_char = static_cast<char*>(contiguous_buf);
30 const char* noncontiguous_buf_char = static_cast<const char*>(noncontiguous_buf)+lb();
31 memcpy(contiguous_buf_char, noncontiguous_buf_char, count * block_count_ * old_type_->size());
34 void Type_Contiguous::unserialize(const void* contiguous_buf, void* noncontiguous_buf, int count, MPI_Op op)
36 const char* contiguous_buf_char = static_cast<const char*>(contiguous_buf);
37 char* noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf)+lb();
38 int n= count*block_count_;
40 op->apply( contiguous_buf_char, noncontiguous_buf_char, &n, old_type_);
43 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){
46 Type_Hvector::~Type_Hvector(){
47 Datatype::unref(old_type_);
50 void Type_Hvector::serialize(const void* noncontiguous_buf, void *contiguous_buf,
52 char* contiguous_buf_char = static_cast<char*>(contiguous_buf);
53 const char* noncontiguous_buf_char = static_cast<const char*>(noncontiguous_buf);
55 for (int i = 0; i < block_count_ * count; i++) {
56 if (not(old_type_->flags() & DT_FLAG_DERIVED))
57 memcpy(contiguous_buf_char, noncontiguous_buf_char, block_length_ * old_type_->size());
59 old_type_->serialize( noncontiguous_buf_char, contiguous_buf_char, block_length_);
61 contiguous_buf_char += block_length_*old_type_->size();
62 if((i+1)%block_count_ ==0)
63 noncontiguous_buf_char += block_length_*old_type_->size();
65 noncontiguous_buf_char += block_stride_;
69 void Type_Hvector::unserialize(const void* contiguous_buf, void *noncontiguous_buf,
70 int count, MPI_Op op){
71 const char* contiguous_buf_char = static_cast<const char*>(contiguous_buf);
72 char* noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf);
74 for (int i = 0; i < block_count_ * count; i++) {
75 if (not(old_type_->flags() & DT_FLAG_DERIVED)) {
77 op->apply( contiguous_buf_char, noncontiguous_buf_char, &block_length_, old_type_);
79 old_type_->unserialize( contiguous_buf_char, noncontiguous_buf_char, block_length_, op);
80 contiguous_buf_char += block_length_*old_type_->size();
81 if((i+1)%block_count_ ==0)
82 noncontiguous_buf_char += block_length_*old_type_->size();
84 noncontiguous_buf_char += block_stride_;
88 Type_Vector::Type_Vector(int size, MPI_Aint lb, MPI_Aint ub, int flags, int count, int block_length, int stride,
89 MPI_Datatype old_type)
90 : Type_Hvector(size, lb, ub, flags, count, block_length, stride * old_type->get_extent(), old_type)
94 Type_Hindexed::Type_Hindexed(int size, MPI_Aint lb, MPI_Aint ub, int flags, int count, const int* block_lengths,
95 const MPI_Aint* block_indices, MPI_Datatype old_type)
96 : Datatype(size, lb, ub, flags)
98 , block_lengths_(new int[count])
99 , block_indices_(new MPI_Aint[count])
100 , old_type_(old_type)
103 for (int i = 0; i < count; i++) {
104 block_lengths_[i] = block_lengths[i];
105 block_indices_[i] = block_indices[i];
109 Type_Hindexed::Type_Hindexed(int size, MPI_Aint lb, MPI_Aint ub, int flags, int count, const int* block_lengths,
110 const int* block_indices, MPI_Datatype old_type, MPI_Aint factor)
111 : Datatype(size, lb, ub, flags)
112 , block_count_(count)
113 , block_lengths_(new int[count])
114 , block_indices_(new MPI_Aint[count])
115 , old_type_(old_type)
118 for (int i = 0; i < count; i++) {
119 block_lengths_[i] = block_lengths[i];
120 block_indices_[i] = block_indices[i] * factor;
124 Type_Hindexed::~Type_Hindexed()
126 Datatype::unref(old_type_);
128 delete[] block_lengths_;
129 delete[] block_indices_;
133 void Type_Hindexed::serialize(const void* noncontiguous_buf, void *contiguous_buf,
135 char* contiguous_buf_char = static_cast<char*>(contiguous_buf);
136 const char* noncontiguous_buf_iter = static_cast<const char*>(noncontiguous_buf);
137 const char* noncontiguous_buf_char = noncontiguous_buf_iter + block_indices_[0];
138 for (int j = 0; j < count; j++) {
139 for (int i = 0; i < block_count_; i++) {
140 if (not(old_type_->flags() & DT_FLAG_DERIVED))
141 memcpy(contiguous_buf_char, noncontiguous_buf_char, block_lengths_[i] * old_type_->size());
143 old_type_->serialize(noncontiguous_buf_char, contiguous_buf_char,block_lengths_[i]);
145 contiguous_buf_char += block_lengths_[i]*old_type_->size();
146 if (i<block_count_-1)
147 noncontiguous_buf_char = noncontiguous_buf_iter + block_indices_[i+1];
149 noncontiguous_buf_char += block_lengths_[i]*old_type_->get_extent();
151 noncontiguous_buf_iter=noncontiguous_buf_char;
155 void Type_Hindexed::unserialize(const void* contiguous_buf, void *noncontiguous_buf,
156 int count, MPI_Op op){
157 const char* contiguous_buf_char = static_cast<const char*>(contiguous_buf);
158 char* noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf)+ block_indices_[0];
159 for (int j = 0; j < count; j++) {
160 for (int i = 0; i < block_count_; i++) {
161 if (not(old_type_->flags() & DT_FLAG_DERIVED)) {
163 op->apply( contiguous_buf_char, noncontiguous_buf_char, &block_lengths_[i],
166 old_type_->unserialize( contiguous_buf_char,noncontiguous_buf_char,block_lengths_[i], op);
168 contiguous_buf_char += block_lengths_[i]*old_type_->size();
169 if (i<block_count_-1)
170 noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf) + block_indices_[i+1];
172 noncontiguous_buf_char += block_lengths_[i]*old_type_->get_extent();
174 noncontiguous_buf=static_cast<void*>(noncontiguous_buf_char);
178 Type_Indexed::Type_Indexed(int size, MPI_Aint lb, MPI_Aint ub, int flags, int count, const int* block_lengths,
179 const int* block_indices, MPI_Datatype old_type)
180 : Type_Hindexed(size, lb, ub, flags, count, block_lengths, block_indices, old_type, old_type->get_extent())
184 Type_Struct::Type_Struct(int size, MPI_Aint lb, MPI_Aint ub, int flags, int count, const int* block_lengths,
185 const MPI_Aint* block_indices, const MPI_Datatype* old_types)
186 : Datatype(size, lb, ub, flags)
187 , block_count_(count)
188 , block_lengths_(new int[count])
189 , block_indices_(new MPI_Aint[count])
190 , old_types_(new MPI_Datatype[count])
192 for (int i = 0; i < count; i++) {
193 block_lengths_[i]=block_lengths[i];
194 block_indices_[i]=block_indices[i];
195 old_types_[i]=old_types[i];
196 old_types_[i]->ref();
200 Type_Struct::~Type_Struct(){
201 for (int i = 0; i < block_count_; i++) {
202 Datatype::unref(old_types_[i]);
205 delete[] block_lengths_;
206 delete[] block_indices_;
212 void Type_Struct::serialize(const void* noncontiguous_buf, void *contiguous_buf,
214 char* contiguous_buf_char = static_cast<char*>(contiguous_buf);
215 const char* noncontiguous_buf_iter = static_cast<const char*>(noncontiguous_buf);
216 const char* noncontiguous_buf_char = noncontiguous_buf_iter + block_indices_[0];
217 for (int j = 0; j < count; j++) {
218 for (int i = 0; i < block_count_; i++) {
219 if (not(old_types_[i]->flags() & DT_FLAG_DERIVED))
220 memcpy(contiguous_buf_char, noncontiguous_buf_char, block_lengths_[i] * old_types_[i]->size());
222 old_types_[i]->serialize( noncontiguous_buf_char,contiguous_buf_char,block_lengths_[i]);
225 contiguous_buf_char += block_lengths_[i]*old_types_[i]->size();
226 if (i<block_count_-1)
227 noncontiguous_buf_char = noncontiguous_buf_iter + block_indices_[i+1];
228 else //let's hope this is MPI_UB ?
229 noncontiguous_buf_char += block_lengths_[i]*old_types_[i]->get_extent();
231 noncontiguous_buf_iter=noncontiguous_buf_char;
235 void Type_Struct::unserialize(const void* contiguous_buf, void *noncontiguous_buf,
236 int count, MPI_Op op){
237 const char* contiguous_buf_char = static_cast<const char*>(contiguous_buf);
238 char* noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf)+ block_indices_[0];
239 for (int j = 0; j < count; j++) {
240 for (int i = 0; i < block_count_; i++) {
241 if (not(old_types_[i]->flags() & DT_FLAG_DERIVED)) {
243 op->apply( contiguous_buf_char, noncontiguous_buf_char, &block_lengths_[i], old_types_[i]);
245 old_types_[i]->unserialize( contiguous_buf_char, noncontiguous_buf_char,block_lengths_[i], op);
247 contiguous_buf_char += block_lengths_[i]*old_types_[i]->size();
248 if (i<block_count_-1)
249 noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf) + block_indices_[i+1];
251 noncontiguous_buf_char += block_lengths_[i]*old_types_[i]->get_extent();
253 noncontiguous_buf=static_cast<void*>(noncontiguous_buf_char);