1 /* smpi_datatype.cpp -- MPI primitives to handle datatypes */
2 /* Copyright (c) 2009-2017. 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 Type_Contiguous::Type_Contiguous(int size, MPI_Aint lb, MPI_Aint ub, int flags, int block_count, MPI_Datatype old_type)
15 : Datatype(size, lb, ub, flags), block_count_(block_count), old_type_(old_type)
20 Type_Contiguous::~Type_Contiguous()
22 Datatype::unref(old_type_);
25 void Type_Contiguous::serialize(void* noncontiguous_buf, void* contiguous_buf, int count)
27 char* contiguous_buf_char = static_cast<char*>(contiguous_buf);
28 char* noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf)+lb();
29 memcpy(contiguous_buf_char, noncontiguous_buf_char, count * block_count_ * old_type_->size());
32 void Type_Contiguous::unserialize(void* contiguous_buf, void* noncontiguous_buf, int count, MPI_Op op)
34 char* contiguous_buf_char = static_cast<char*>(contiguous_buf);
35 char* noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf)+lb();
36 int n= count*block_count_;
38 op->apply( contiguous_buf_char, noncontiguous_buf_char, &n, old_type_);
41 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){
44 Type_Hvector::~Type_Hvector(){
45 Datatype::unref(old_type_);
48 void Type_Hvector::serialize( void* noncontiguous_buf, void *contiguous_buf,
50 char* contiguous_buf_char = static_cast<char*>(contiguous_buf);
51 char* noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf);
53 for (int i = 0; i < block_count_ * count; i++) {
54 if (not(old_type_->flags() & DT_FLAG_DERIVED))
55 memcpy(contiguous_buf_char, noncontiguous_buf_char, block_length_ * old_type_->size());
57 old_type_->serialize( noncontiguous_buf_char, contiguous_buf_char, block_length_);
59 contiguous_buf_char += block_length_*old_type_->size();
60 if((i+1)%block_count_ ==0)
61 noncontiguous_buf_char += block_length_*old_type_->size();
63 noncontiguous_buf_char += block_stride_;
67 void Type_Hvector::unserialize( void* contiguous_buf, void *noncontiguous_buf,
68 int count, MPI_Op op){
69 char* contiguous_buf_char = static_cast<char*>(contiguous_buf);
70 char* noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf);
72 for (int i = 0; i < block_count_ * count; i++) {
73 if (not(old_type_->flags() & DT_FLAG_DERIVED)) {
75 op->apply( contiguous_buf_char, noncontiguous_buf_char, &block_length_, old_type_);
77 old_type_->unserialize( contiguous_buf_char, noncontiguous_buf_char, block_length_, op);
78 contiguous_buf_char += block_length_*old_type_->size();
79 if((i+1)%block_count_ ==0)
80 noncontiguous_buf_char += block_length_*old_type_->size();
82 noncontiguous_buf_char += block_stride_;
86 Type_Vector::Type_Vector(int size, MPI_Aint lb, MPI_Aint ub, int flags, int count, int block_length, int stride,
87 MPI_Datatype old_type)
88 : Type_Hvector(size, lb, ub, flags, count, block_length, stride * old_type->get_extent(), old_type)
92 Type_Hindexed::Type_Hindexed(int size, MPI_Aint lb, MPI_Aint ub, int flags, int count, int* block_lengths,
93 MPI_Aint* block_indices, MPI_Datatype old_type)
94 : Datatype(size, lb, ub, flags), block_count_(count), old_type_(old_type)
97 block_lengths_ = new int[count];
98 block_indices_ = new MPI_Aint[count];
99 for (int i = 0; i < count; i++) {
100 block_lengths_[i] = block_lengths[i];
101 block_indices_[i] = block_indices[i];
105 Type_Hindexed::Type_Hindexed(int size, MPI_Aint lb, MPI_Aint ub, int flags, int count, int* block_lengths,
106 int* block_indices, MPI_Datatype old_type, MPI_Aint factor)
107 : Datatype(size, lb, ub, flags), block_count_(count), old_type_(old_type)
110 block_lengths_ = new int[count];
111 block_indices_ = new MPI_Aint[count];
112 for (int i = 0; i < count; i++) {
113 block_lengths_[i] = block_lengths[i];
114 block_indices_[i] = block_indices[i] * factor;
118 Type_Hindexed::~Type_Hindexed()
120 Datatype::unref(old_type_);
122 delete[] block_lengths_;
123 delete[] block_indices_;
127 void Type_Hindexed::serialize( void* noncontiguous_buf, void *contiguous_buf,
129 char* contiguous_buf_char = static_cast<char*>(contiguous_buf);
130 char* noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf)+ block_indices_[0];
131 for (int j = 0; j < count; j++) {
132 for (int i = 0; i < block_count_; i++) {
133 if (not(old_type_->flags() & DT_FLAG_DERIVED))
134 memcpy(contiguous_buf_char, noncontiguous_buf_char, block_lengths_[i] * old_type_->size());
136 old_type_->serialize(noncontiguous_buf_char, contiguous_buf_char,block_lengths_[i]);
138 contiguous_buf_char += block_lengths_[i]*old_type_->size();
139 if (i<block_count_-1)
140 noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf) + block_indices_[i+1];
142 noncontiguous_buf_char += block_lengths_[i]*old_type_->get_extent();
144 noncontiguous_buf=static_cast<void*>(noncontiguous_buf_char);
148 void Type_Hindexed::unserialize( void* contiguous_buf, void *noncontiguous_buf,
149 int count, MPI_Op op){
150 char* contiguous_buf_char = static_cast<char*>(contiguous_buf);
151 char* noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf)+ block_indices_[0];
152 for (int j = 0; j < count; j++) {
153 for (int i = 0; i < block_count_; i++) {
154 if (not(old_type_->flags() & DT_FLAG_DERIVED)) {
156 op->apply( contiguous_buf_char, noncontiguous_buf_char, &block_lengths_[i],
159 old_type_->unserialize( contiguous_buf_char,noncontiguous_buf_char,block_lengths_[i], op);
161 contiguous_buf_char += block_lengths_[i]*old_type_->size();
162 if (i<block_count_-1)
163 noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf) + block_indices_[i+1];
165 noncontiguous_buf_char += block_lengths_[i]*old_type_->get_extent();
167 noncontiguous_buf=static_cast<void*>(noncontiguous_buf_char);
171 Type_Indexed::Type_Indexed(int size, MPI_Aint lb, MPI_Aint ub, int flags, int count, int* block_lengths,
172 int* block_indices, MPI_Datatype old_type)
173 : Type_Hindexed(size, lb, ub, flags, count, block_lengths, block_indices, old_type, old_type->get_extent())
177 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){
178 block_lengths_= new int[count];
179 block_indices_= new MPI_Aint[count];
180 old_types_= new MPI_Datatype[count];
181 for (int i = 0; i < count; i++) {
182 block_lengths_[i]=block_lengths[i];
183 block_indices_[i]=block_indices[i];
184 old_types_[i]=old_types[i];
185 old_types_[i]->ref();
189 Type_Struct::~Type_Struct(){
190 for (int i = 0; i < block_count_; i++) {
191 Datatype::unref(old_types_[i]);
194 delete[] block_lengths_;
195 delete[] block_indices_;
201 void Type_Struct::serialize( void* noncontiguous_buf, void *contiguous_buf,
203 char* contiguous_buf_char = static_cast<char*>(contiguous_buf);
204 char* noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf)+ block_indices_[0];
205 for (int j = 0; j < count; j++) {
206 for (int i = 0; i < block_count_; i++) {
207 if (not(old_types_[i]->flags() & DT_FLAG_DERIVED))
208 memcpy(contiguous_buf_char, noncontiguous_buf_char, block_lengths_[i] * old_types_[i]->size());
210 old_types_[i]->serialize( noncontiguous_buf_char,contiguous_buf_char,block_lengths_[i]);
213 contiguous_buf_char += block_lengths_[i]*old_types_[i]->size();
214 if (i<block_count_-1)
215 noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf) + block_indices_[i+1];
216 else //let's hope this is MPI_UB ?
217 noncontiguous_buf_char += block_lengths_[i]*old_types_[i]->get_extent();
219 noncontiguous_buf=static_cast<void*>(noncontiguous_buf_char);
223 void Type_Struct::unserialize( void* contiguous_buf, void *noncontiguous_buf,
224 int count, MPI_Op op){
225 char* contiguous_buf_char = static_cast<char*>(contiguous_buf);
226 char* noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf)+ block_indices_[0];
227 for (int j = 0; j < count; j++) {
228 for (int i = 0; i < block_count_; i++) {
229 if (not(old_types_[i]->flags() & DT_FLAG_DERIVED)) {
231 op->apply( contiguous_buf_char, noncontiguous_buf_char, &block_lengths_[i], old_types_[i]);
233 old_types_[i]->unserialize( contiguous_buf_char, noncontiguous_buf_char,block_lengths_[i], op);
235 contiguous_buf_char += block_lengths_[i]*old_types_[i]->size();
236 if (i<block_count_-1)
237 noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf) + block_indices_[i+1];
239 noncontiguous_buf_char += block_lengths_[i]*old_types_[i]->get_extent();
241 noncontiguous_buf=static_cast<void*>(noncontiguous_buf_char);