1 /* smpi_datatype.cpp -- MPI primitives to handle datatypes */
2 /* Copyright (c) 2009-2019. 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), block_count_(count), old_type_(old_type)
99 block_lengths_ = new int[count];
100 block_indices_ = new MPI_Aint[count];
101 for (int i = 0; i < count; i++) {
102 block_lengths_[i] = block_lengths[i];
103 block_indices_[i] = block_indices[i];
107 Type_Hindexed::Type_Hindexed(int size, MPI_Aint lb, MPI_Aint ub, int flags, int count, const int* block_lengths,
108 const int* block_indices, MPI_Datatype old_type, MPI_Aint factor)
109 : Datatype(size, lb, ub, flags), block_count_(count), old_type_(old_type)
112 block_lengths_ = new int[count];
113 block_indices_ = new MPI_Aint[count];
114 for (int i = 0; i < count; i++) {
115 block_lengths_[i] = block_lengths[i];
116 block_indices_[i] = block_indices[i] * factor;
120 Type_Hindexed::~Type_Hindexed()
122 Datatype::unref(old_type_);
124 delete[] block_lengths_;
125 delete[] block_indices_;
129 void Type_Hindexed::serialize(const void* noncontiguous_buf, void *contiguous_buf,
131 char* contiguous_buf_char = static_cast<char*>(contiguous_buf);
132 const char* noncontiguous_buf_iter = static_cast<const char*>(noncontiguous_buf);
133 const char* noncontiguous_buf_char = noncontiguous_buf_iter + block_indices_[0];
134 for (int j = 0; j < count; j++) {
135 for (int i = 0; i < block_count_; i++) {
136 if (not(old_type_->flags() & DT_FLAG_DERIVED))
137 memcpy(contiguous_buf_char, noncontiguous_buf_char, block_lengths_[i] * old_type_->size());
139 old_type_->serialize(noncontiguous_buf_char, contiguous_buf_char,block_lengths_[i]);
141 contiguous_buf_char += block_lengths_[i]*old_type_->size();
142 if (i<block_count_-1)
143 noncontiguous_buf_char = noncontiguous_buf_iter + block_indices_[i+1];
145 noncontiguous_buf_char += block_lengths_[i]*old_type_->get_extent();
147 noncontiguous_buf_iter=noncontiguous_buf_char;
151 void Type_Hindexed::unserialize(const void* contiguous_buf, void *noncontiguous_buf,
152 int count, MPI_Op op){
153 const char* contiguous_buf_char = static_cast<const char*>(contiguous_buf);
154 char* noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf)+ block_indices_[0];
155 for (int j = 0; j < count; j++) {
156 for (int i = 0; i < block_count_; i++) {
157 if (not(old_type_->flags() & DT_FLAG_DERIVED)) {
159 op->apply( contiguous_buf_char, noncontiguous_buf_char, &block_lengths_[i],
162 old_type_->unserialize( contiguous_buf_char,noncontiguous_buf_char,block_lengths_[i], op);
164 contiguous_buf_char += block_lengths_[i]*old_type_->size();
165 if (i<block_count_-1)
166 noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf) + block_indices_[i+1];
168 noncontiguous_buf_char += block_lengths_[i]*old_type_->get_extent();
170 noncontiguous_buf=static_cast<void*>(noncontiguous_buf_char);
174 Type_Indexed::Type_Indexed(int size, MPI_Aint lb, MPI_Aint ub, int flags, int count, const int* block_lengths,
175 const int* block_indices, MPI_Datatype old_type)
176 : Type_Hindexed(size, lb, ub, flags, count, block_lengths, block_indices, old_type, old_type->get_extent())
180 Type_Struct::Type_Struct(int size,MPI_Aint lb, MPI_Aint ub, int flags, int count, const int* block_lengths, const MPI_Aint* block_indices, const MPI_Datatype* old_types): Datatype(size, lb, ub, flags), block_count_(count){
181 block_lengths_= new int[count];
182 block_indices_= new MPI_Aint[count];
183 old_types_= new MPI_Datatype[count];
184 for (int i = 0; i < count; i++) {
185 block_lengths_[i]=block_lengths[i];
186 block_indices_[i]=block_indices[i];
187 old_types_[i]=old_types[i];
188 old_types_[i]->ref();
192 Type_Struct::~Type_Struct(){
193 for (int i = 0; i < block_count_; i++) {
194 Datatype::unref(old_types_[i]);
197 delete[] block_lengths_;
198 delete[] block_indices_;
204 void Type_Struct::serialize(const void* noncontiguous_buf, void *contiguous_buf,
206 char* contiguous_buf_char = static_cast<char*>(contiguous_buf);
207 const char* noncontiguous_buf_iter = static_cast<const char*>(noncontiguous_buf);
208 const char* noncontiguous_buf_char = noncontiguous_buf_iter + block_indices_[0];
209 for (int j = 0; j < count; j++) {
210 for (int i = 0; i < block_count_; i++) {
211 if (not(old_types_[i]->flags() & DT_FLAG_DERIVED))
212 memcpy(contiguous_buf_char, noncontiguous_buf_char, block_lengths_[i] * old_types_[i]->size());
214 old_types_[i]->serialize( noncontiguous_buf_char,contiguous_buf_char,block_lengths_[i]);
217 contiguous_buf_char += block_lengths_[i]*old_types_[i]->size();
218 if (i<block_count_-1)
219 noncontiguous_buf_char = noncontiguous_buf_iter + block_indices_[i+1];
220 else //let's hope this is MPI_UB ?
221 noncontiguous_buf_char += block_lengths_[i]*old_types_[i]->get_extent();
223 noncontiguous_buf_iter=noncontiguous_buf_char;
227 void Type_Struct::unserialize(const void* contiguous_buf, void *noncontiguous_buf,
228 int count, MPI_Op op){
229 const char* contiguous_buf_char = static_cast<const char*>(contiguous_buf);
230 char* noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf)+ block_indices_[0];
231 for (int j = 0; j < count; j++) {
232 for (int i = 0; i < block_count_; i++) {
233 if (not(old_types_[i]->flags() & DT_FLAG_DERIVED)) {
235 op->apply( contiguous_buf_char, noncontiguous_buf_char, &block_lengths_[i], old_types_[i]);
237 old_types_[i]->unserialize( contiguous_buf_char, noncontiguous_buf_char,block_lengths_[i], op);
239 contiguous_buf_char += block_lengths_[i]*old_types_[i]->size();
240 if (i<block_count_-1)
241 noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf) + block_indices_[i+1];
243 noncontiguous_buf_char += block_lengths_[i]*old_types_[i]->get_extent();
245 noncontiguous_buf=static_cast<void*>(noncontiguous_buf_char);