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(void* noncontiguous_buf, void* contiguous_buf, int count)
29 char* contiguous_buf_char = static_cast<char*>(contiguous_buf);
30 char* noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf)+lb();
31 memcpy(contiguous_buf_char, noncontiguous_buf_char, count * block_count_ * old_type_->size());
34 void Type_Contiguous::unserialize(void* contiguous_buf, void* noncontiguous_buf, int count, MPI_Op op)
36 char* contiguous_buf_char = static_cast<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( void* noncontiguous_buf, void *contiguous_buf,
52 char* contiguous_buf_char = static_cast<char*>(contiguous_buf);
53 char* noncontiguous_buf_char = static_cast<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( void* contiguous_buf, void *noncontiguous_buf,
70 int count, MPI_Op op){
71 char* contiguous_buf_char = static_cast<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, int* block_lengths,
95 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, int* block_lengths,
108 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( void* noncontiguous_buf, void *contiguous_buf,
131 char* contiguous_buf_char = static_cast<char*>(contiguous_buf);
132 char* noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf)+ block_indices_[0];
133 for (int j = 0; j < count; j++) {
134 for (int i = 0; i < block_count_; i++) {
135 if (not(old_type_->flags() & DT_FLAG_DERIVED))
136 memcpy(contiguous_buf_char, noncontiguous_buf_char, block_lengths_[i] * old_type_->size());
138 old_type_->serialize(noncontiguous_buf_char, contiguous_buf_char,block_lengths_[i]);
140 contiguous_buf_char += block_lengths_[i]*old_type_->size();
141 if (i<block_count_-1)
142 noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf) + block_indices_[i+1];
144 noncontiguous_buf_char += block_lengths_[i]*old_type_->get_extent();
146 noncontiguous_buf=static_cast<void*>(noncontiguous_buf_char);
150 void Type_Hindexed::unserialize( void* contiguous_buf, void *noncontiguous_buf,
151 int count, MPI_Op op){
152 char* contiguous_buf_char = static_cast<char*>(contiguous_buf);
153 char* noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf)+ block_indices_[0];
154 for (int j = 0; j < count; j++) {
155 for (int i = 0; i < block_count_; i++) {
156 if (not(old_type_->flags() & DT_FLAG_DERIVED)) {
158 op->apply( contiguous_buf_char, noncontiguous_buf_char, &block_lengths_[i],
161 old_type_->unserialize( contiguous_buf_char,noncontiguous_buf_char,block_lengths_[i], op);
163 contiguous_buf_char += block_lengths_[i]*old_type_->size();
164 if (i<block_count_-1)
165 noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf) + block_indices_[i+1];
167 noncontiguous_buf_char += block_lengths_[i]*old_type_->get_extent();
169 noncontiguous_buf=static_cast<void*>(noncontiguous_buf_char);
173 Type_Indexed::Type_Indexed(int size, MPI_Aint lb, MPI_Aint ub, int flags, int count, int* block_lengths,
174 int* block_indices, MPI_Datatype old_type)
175 : Type_Hindexed(size, lb, ub, flags, count, block_lengths, block_indices, old_type, old_type->get_extent())
179 Type_Struct::Type_Struct(int size,MPI_Aint lb, MPI_Aint ub, int flags, int count, int* block_lengths, MPI_Aint* block_indices, MPI_Datatype* old_types): Datatype(size, lb, ub, flags), block_count_(count), block_lengths_(block_lengths), block_indices_(block_indices), old_types_(old_types){
180 block_lengths_= new int[count];
181 block_indices_= new MPI_Aint[count];
182 old_types_= new MPI_Datatype[count];
183 for (int i = 0; i < count; i++) {
184 block_lengths_[i]=block_lengths[i];
185 block_indices_[i]=block_indices[i];
186 old_types_[i]=old_types[i];
187 old_types_[i]->ref();
191 Type_Struct::~Type_Struct(){
192 for (int i = 0; i < block_count_; i++) {
193 Datatype::unref(old_types_[i]);
196 delete[] block_lengths_;
197 delete[] block_indices_;
203 void Type_Struct::serialize( void* noncontiguous_buf, void *contiguous_buf,
205 char* contiguous_buf_char = static_cast<char*>(contiguous_buf);
206 char* noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf)+ block_indices_[0];
207 for (int j = 0; j < count; j++) {
208 for (int i = 0; i < block_count_; i++) {
209 if (not(old_types_[i]->flags() & DT_FLAG_DERIVED))
210 memcpy(contiguous_buf_char, noncontiguous_buf_char, block_lengths_[i] * old_types_[i]->size());
212 old_types_[i]->serialize( noncontiguous_buf_char,contiguous_buf_char,block_lengths_[i]);
215 contiguous_buf_char += block_lengths_[i]*old_types_[i]->size();
216 if (i<block_count_-1)
217 noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf) + block_indices_[i+1];
218 else //let's hope this is MPI_UB ?
219 noncontiguous_buf_char += block_lengths_[i]*old_types_[i]->get_extent();
221 noncontiguous_buf=static_cast<void*>(noncontiguous_buf_char);
225 void Type_Struct::unserialize( void* contiguous_buf, void *noncontiguous_buf,
226 int count, MPI_Op op){
227 char* contiguous_buf_char = static_cast<char*>(contiguous_buf);
228 char* noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf)+ block_indices_[0];
229 for (int j = 0; j < count; j++) {
230 for (int i = 0; i < block_count_; i++) {
231 if (not(old_types_[i]->flags() & DT_FLAG_DERIVED)) {
233 op->apply( contiguous_buf_char, noncontiguous_buf_char, &block_lengths_[i], old_types_[i]);
235 old_types_[i]->unserialize( contiguous_buf_char, noncontiguous_buf_char,block_lengths_[i], op);
237 contiguous_buf_char += block_lengths_[i]*old_types_[i]->size();
238 if (i<block_count_-1)
239 noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf) + block_indices_[i+1];
241 noncontiguous_buf_char += block_lengths_[i]*old_types_[i]->get_extent();
243 noncontiguous_buf=static_cast<void*>(noncontiguous_buf_char);