1 /* smpi_datatype.cpp -- MPI primitives to handle datatypes */
2 /* Copyright (c) 2009-2015. The SimGrid Team.
3 * All rights reserved. */
5 /* This program is free software; you can redistribute it and/or modify it
6 * under the terms of the license (GNU LGPL) which comes with this package. */
10 #include "simgrid/modelchecker.h"
11 #include "xbt/replay.h"
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_mpi_dt, smpi, "Logging specific to SMPI (datatype)");
21 xbt_dict_t smpi_type_keyvals = nullptr;
22 int type_keyval_id=0;//avoid collisions
25 #define CREATE_MPI_DATATYPE(name, type) \
26 static Datatype mpi_##name ( \
28 sizeof(type), /* size */ \
30 sizeof(type), /* ub = lb + size */ \
31 DT_FLAG_BASIC /* flags */ \
33 const MPI_Datatype name = &mpi_##name;
35 #define CREATE_MPI_DATATYPE_NULL(name) \
36 static Datatype mpi_##name ( \
40 0, /* ub = lb + size */ \
41 DT_FLAG_BASIC /* flags */ \
43 const MPI_Datatype name = &mpi_##name;
45 // Predefined data types
46 CREATE_MPI_DATATYPE(MPI_CHAR, char);
47 CREATE_MPI_DATATYPE(MPI_SHORT, short);
48 CREATE_MPI_DATATYPE(MPI_INT, int);
49 CREATE_MPI_DATATYPE(MPI_LONG, long);
50 CREATE_MPI_DATATYPE(MPI_LONG_LONG, long long);
51 CREATE_MPI_DATATYPE(MPI_SIGNED_CHAR, signed char);
52 CREATE_MPI_DATATYPE(MPI_UNSIGNED_CHAR, unsigned char);
53 CREATE_MPI_DATATYPE(MPI_UNSIGNED_SHORT, unsigned short);
54 CREATE_MPI_DATATYPE(MPI_UNSIGNED, unsigned int);
55 CREATE_MPI_DATATYPE(MPI_UNSIGNED_LONG, unsigned long);
56 CREATE_MPI_DATATYPE(MPI_UNSIGNED_LONG_LONG, unsigned long long);
57 CREATE_MPI_DATATYPE(MPI_FLOAT, float);
58 CREATE_MPI_DATATYPE(MPI_DOUBLE, double);
59 CREATE_MPI_DATATYPE(MPI_LONG_DOUBLE, long double);
60 CREATE_MPI_DATATYPE(MPI_WCHAR, wchar_t);
61 CREATE_MPI_DATATYPE(MPI_C_BOOL, bool);
62 CREATE_MPI_DATATYPE(MPI_BYTE, int8_t);
63 CREATE_MPI_DATATYPE(MPI_INT8_T, int8_t);
64 CREATE_MPI_DATATYPE(MPI_INT16_T, int16_t);
65 CREATE_MPI_DATATYPE(MPI_INT32_T, int32_t);
66 CREATE_MPI_DATATYPE(MPI_INT64_T, int64_t);
67 CREATE_MPI_DATATYPE(MPI_UINT8_T, uint8_t);
68 CREATE_MPI_DATATYPE(MPI_UINT16_T, uint16_t);
69 CREATE_MPI_DATATYPE(MPI_UINT32_T, uint32_t);
70 CREATE_MPI_DATATYPE(MPI_UINT64_T, uint64_t);
71 CREATE_MPI_DATATYPE(MPI_C_FLOAT_COMPLEX, float _Complex);
72 CREATE_MPI_DATATYPE(MPI_C_DOUBLE_COMPLEX, double _Complex);
73 CREATE_MPI_DATATYPE(MPI_C_LONG_DOUBLE_COMPLEX, long double _Complex);
74 CREATE_MPI_DATATYPE(MPI_AINT, MPI_Aint);
75 CREATE_MPI_DATATYPE(MPI_OFFSET, MPI_Offset);
77 CREATE_MPI_DATATYPE(MPI_FLOAT_INT, float_int);
78 CREATE_MPI_DATATYPE(MPI_LONG_INT, long_int);
79 CREATE_MPI_DATATYPE(MPI_DOUBLE_INT, double_int);
80 CREATE_MPI_DATATYPE(MPI_SHORT_INT, short_int);
81 CREATE_MPI_DATATYPE(MPI_2INT, int_int);
82 CREATE_MPI_DATATYPE(MPI_2FLOAT, float_float);
83 CREATE_MPI_DATATYPE(MPI_2DOUBLE, double_double);
84 CREATE_MPI_DATATYPE(MPI_2LONG, long_long);
86 CREATE_MPI_DATATYPE(MPI_REAL, float);
87 CREATE_MPI_DATATYPE(MPI_REAL4, float);
88 CREATE_MPI_DATATYPE(MPI_REAL8, float);
89 CREATE_MPI_DATATYPE(MPI_REAL16, double);
90 CREATE_MPI_DATATYPE_NULL(MPI_COMPLEX8);
91 CREATE_MPI_DATATYPE_NULL(MPI_COMPLEX16);
92 CREATE_MPI_DATATYPE_NULL(MPI_COMPLEX32);
93 CREATE_MPI_DATATYPE(MPI_INTEGER1, int);
94 CREATE_MPI_DATATYPE(MPI_INTEGER2, int16_t);
95 CREATE_MPI_DATATYPE(MPI_INTEGER4, int32_t);
96 CREATE_MPI_DATATYPE(MPI_INTEGER8, int64_t);
97 CREATE_MPI_DATATYPE(MPI_INTEGER16, integer128_t);
99 CREATE_MPI_DATATYPE(MPI_LONG_DOUBLE_INT, long_double_int);
101 CREATE_MPI_DATATYPE_NULL(MPI_UB);
102 CREATE_MPI_DATATYPE_NULL(MPI_LB);
103 CREATE_MPI_DATATYPE(MPI_PACKED, char);
105 CREATE_MPI_DATATYPE(MPI_PTR, void*);
110 Datatype::Datatype(int size,int lb, int ub, int flags) : name_(nullptr), lb_(lb), ub_(ub), flags_(flags), attributes_(nullptr), in_use_(1){
113 MC_ignore(&(in_use_), sizeof(in_use_));
117 //for predefined types, so in_use = 0.
118 Datatype::Datatype(char* name, int size,int lb, int ub, int flags) : name_(name), lb_(lb), ub_(ub), flags_(flags), attributes_(nullptr), in_use_(0){
121 MC_ignore(&(in_use_), sizeof(in_use_));
127 Datatype::Datatype(Datatype *datatype) : lb_(datatype->lb_), ub_(datatype->ub_), flags_(datatype->flags_), in_use_(1)
129 flags_ &= ~DT_FLAG_PREDEFINED;
131 name_ = xbt_strdup(datatype->name_);
132 if(datatype->attributes_ !=nullptr){
133 attributes_ = xbt_dict_new_homogeneous(nullptr);
134 xbt_dict_cursor_t cursor = nullptr;
139 xbt_dict_foreach (datatype->attributes_, cursor, key, value_in) {
140 smpi_type_key_elem elem =
141 static_cast<smpi_type_key_elem>(xbt_dict_get_or_null_ext(smpi_type_keyvals, key, sizeof(int)));
142 if (elem != nullptr && elem->copy_fn != MPI_NULL_COPY_FN) {
143 int ret = elem->copy_fn(datatype, atoi(key), nullptr, value_in, &value_out, &flag);
144 if (ret != MPI_SUCCESS) {
145 // smpi_datatype_unuse(*new_t);
146 // *new_t = MPI_DATATYPE_NULL;
147 xbt_dict_cursor_free(&cursor);
150 xbt_dict_set_ext(attributes_, key, sizeof(int), value_out, nullptr);
156 Datatype::~Datatype(){
157 xbt_assert(in_use_ >= 0);
159 if(flags_ & DT_FLAG_PREDEFINED)
162 //if still used, mark for deletion
164 flags_ |=DT_FLAG_DESTROYED;
168 if(attributes_ !=nullptr){
169 xbt_dict_cursor_t cursor = nullptr;
173 xbt_dict_foreach(attributes_, cursor, key, value){
174 smpi_type_key_elem elem =
175 static_cast<smpi_type_key_elem>(xbt_dict_get_or_null_ext(smpi_type_keyvals, key, sizeof(int)));
176 if(elem!=nullptr && elem->delete_fn!=nullptr)
177 elem->delete_fn(this,*key, value, &flag);
179 xbt_dict_free(&attributes_);
186 void Datatype::use(){
192 MC_ignore(&(in_use_), sizeof(in_use_));
196 void Datatype::unuse()
206 MC_ignore(&(in_use_), sizeof(in_use_));
210 void Datatype::commit()
212 flags_ |= DT_FLAG_COMMITED;
216 bool Datatype::is_valid(){
217 return (flags_ & DT_FLAG_COMMITED);
220 size_t Datatype::size(){
224 int Datatype::flags(){
228 void Datatype::addflag(int flag){
232 MPI_Aint Datatype::lb(){
236 MPI_Aint Datatype::ub(){
240 char* Datatype::name(){
245 int Datatype::extent(MPI_Aint * lb, MPI_Aint * extent){
251 MPI_Aint Datatype::get_extent(){
255 void Datatype::get_name(char* name, int* length){
256 *length = strlen(name_);
257 strncpy(name, name_, *length+1);
260 void Datatype::set_name(char* name){
261 if(name_!=nullptr && (flags_ & DT_FLAG_PREDEFINED) == 0)
263 name_ = xbt_strdup(name);
266 int Datatype::attr_delete(int keyval){
267 smpi_type_key_elem elem =
268 static_cast<smpi_type_key_elem>(xbt_dict_get_or_null_ext(smpi_type_keyvals, reinterpret_cast<const char*>(&keyval), sizeof(int)));
271 if(elem->delete_fn!=MPI_NULL_DELETE_FN){
272 void * value = nullptr;
274 if(this->attr_get(keyval, &value, &flag)==MPI_SUCCESS){
275 int ret = elem->delete_fn(this, keyval, value, &flag);
280 if(attributes_==nullptr)
283 xbt_dict_remove_ext(attributes_, reinterpret_cast<const char*>(&keyval), sizeof(int));
288 int Datatype::attr_get(int keyval, void* attr_value, int* flag){
289 smpi_type_key_elem elem =
290 static_cast<smpi_type_key_elem>(xbt_dict_get_or_null_ext(smpi_type_keyvals, reinterpret_cast<const char*>(&keyval), sizeof(int)));
293 if(attributes_==nullptr){
298 *static_cast<void**>(attr_value) = xbt_dict_get_ext(attributes_, reinterpret_cast<const char*>(&keyval), sizeof(int));
307 int Datatype::attr_put(int keyval, void* attr_value){
308 if(smpi_type_keyvals==nullptr)
309 smpi_type_keyvals = xbt_dict_new_homogeneous(nullptr);
310 smpi_type_key_elem elem =
311 static_cast<smpi_type_key_elem>(xbt_dict_get_or_null_ext(smpi_type_keyvals, reinterpret_cast<const char*>(&keyval), sizeof(int)));
315 void* value = nullptr;
316 this->attr_get(keyval, &value, &flag);
317 if(flag!=0 && elem->delete_fn!=MPI_NULL_DELETE_FN){
318 int ret = elem->delete_fn(this, keyval, value, &flag);
322 if(attributes_==nullptr)
323 attributes_ = xbt_dict_new_homogeneous(nullptr);
325 xbt_dict_set_ext(attributes_, reinterpret_cast<const char*>(&keyval), sizeof(int), attr_value, nullptr);
329 int Datatype::keyval_create(MPI_Type_copy_attr_function* copy_fn, MPI_Type_delete_attr_function* delete_fn, int* keyval, void* extra_state){
330 if(smpi_type_keyvals==nullptr)
331 smpi_type_keyvals = xbt_dict_new_homogeneous(nullptr);
333 smpi_type_key_elem value = (smpi_type_key_elem) xbt_new0(s_smpi_mpi_type_key_elem_t,1);
335 value->copy_fn=copy_fn;
336 value->delete_fn=delete_fn;
338 *keyval = type_keyval_id;
339 xbt_dict_set_ext(smpi_type_keyvals,reinterpret_cast<const char*>(keyval), sizeof(int),reinterpret_cast<void*>(value), nullptr);
344 int Datatype::keyval_free(int* keyval){
345 smpi_type_key_elem elem =
346 static_cast<smpi_type_key_elem>(xbt_dict_get_or_null_ext(smpi_type_keyvals, reinterpret_cast<const char*>(keyval), sizeof(int)));
350 xbt_dict_remove_ext(smpi_type_keyvals, reinterpret_cast<const char*>(keyval), sizeof(int));
356 int Datatype::pack(void* inbuf, int incount, void* outbuf, int outcount, int* position,MPI_Comm comm){
357 if (outcount - *position < incount*static_cast<int>(size_))
358 return MPI_ERR_BUFFER;
359 Datatype::copy(inbuf, incount, this, static_cast<char*>(outbuf) + *position, outcount, MPI_CHAR);
360 *position += incount * size_;
364 int Datatype::unpack(void* inbuf, int insize, int* position, void* outbuf, int outcount,MPI_Comm comm){
365 if (outcount*(int)size_> insize)
366 return MPI_ERR_BUFFER;
367 Datatype::copy(static_cast<char*>(inbuf) + *position, insize, MPI_CHAR, outbuf, outcount, this);
368 *position += outcount * size_;
373 int Datatype::copy(void *sendbuf, int sendcount, MPI_Datatype sendtype,
374 void *recvbuf, int recvcount, MPI_Datatype recvtype){
376 if(smpi_privatize_global_variables){
377 smpi_switch_data_segment(smpi_process_index());
379 /* First check if we really have something to do */
380 if (recvcount > 0 && recvbuf != sendbuf) {
381 /* FIXME: treat packed cases */
382 sendcount *= sendtype->size();
383 recvcount *= recvtype->size();
384 // count = sendcount < recvcount ? sendcount : recvcount;
386 // if(sendtype->sizeof_substruct == 0 && recvtype->sizeof_substruct == 0) {
387 // if(!smpi_process_get_replaying())
388 // memcpy(recvbuf, sendbuf, count);
390 // else if (sendtype->sizeof_substruct == 0)
392 // s_smpi_subtype_t *subtype = recvtype->substruct);
393 // subtype->unserialize( sendbuf, recvbuf, recvcount/recvtype->size(), subtype, MPI_REPLACE);
395 // else if (recvtype->sizeof_substruct == 0)
397 // s_smpi_subtype_t *subtype = sendtype->substruct);
398 // subtype->serialize(sendbuf, recvbuf, sendcount/sendtype->size(), subtype);
400 // s_smpi_subtype_t *subtype = sendtype->substruct);
402 // void * buf_tmp = xbt_malloc(count);
404 // subtype->serialize( sendbuf, buf_tmp,count/sendtype->size(), subtype);
405 // subtype = recvtype->substruct);
406 // subtype->unserialize( buf_tmp, recvbuf,count/recvtype->size(), subtype, MPI_REPLACE);
408 // xbt_free(buf_tmp);
412 return sendcount > recvcount ? MPI_ERR_TRUNCATE : MPI_SUCCESS;
415 //Default serialization method : memcpy.
416 void Datatype::serialize( void* noncontiguous_buf, void *contiguous_buf, int count){
417 char* contiguous_buf_char = static_cast<char*>(contiguous_buf);
418 char* noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf)+lb_;
419 memcpy(contiguous_buf_char, noncontiguous_buf_char, count*size_);
423 void Datatype::unserialize( void* contiguous_buf, void *noncontiguous_buf, int count, MPI_Op op){
424 char* contiguous_buf_char = static_cast<char*>(contiguous_buf);
425 char* noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf)+lb_;
428 op->apply( contiguous_buf_char, noncontiguous_buf_char, &n, this);
431 int Datatype::create_contiguous(int count, MPI_Datatype old_type, MPI_Aint lb, MPI_Datatype* new_type){
432 if(old_type->flags_ & DT_FLAG_DERIVED){
433 //handle this case as a hvector with stride equals to the extent of the datatype
434 return create_hvector(count, 1, old_type->get_extent(), old_type, new_type);
437 *new_type = new Type_Contiguous(count * old_type->size(), lb, lb + count * old_type->size(),
438 DT_FLAG_DERIVED, count, old_type);
440 *new_type = new Datatype(count * old_type->size(), lb, lb + count * old_type->size(),0);
444 int Datatype::create_vector(int count, int blocklen, int stride, MPI_Datatype old_type, MPI_Datatype* new_type)
453 ub=((count-1)*stride+blocklen-1)*old_type->get_extent()+old_type->ub();
455 if(old_type->flags() & DT_FLAG_DERIVED || stride != blocklen){
456 *new_type = new Type_Vector(count * (blocklen) * old_type->size(), lb, ub,
457 DT_FLAG_DERIVED, count, blocklen, stride, old_type);
460 /* in this situation the data are contiguous thus it's not required to serialize and unserialize it*/
461 *new_type = new Datatype(count * blocklen * old_type->size(), 0, ((count -1) * stride + blocklen)*
462 old_type->size(), DT_FLAG_CONTIGUOUS);
469 int Datatype::create_hvector(int count, int blocklen, MPI_Aint stride, MPI_Datatype old_type, MPI_Datatype* new_type)
478 ub=((count-1)*stride+blocklen-1)*old_type->get_extent()+old_type->ub();
480 if(old_type->flags() & DT_FLAG_DERIVED || stride != blocklen*old_type->get_extent()){
481 *new_type = new Type_Hvector(count * (blocklen) * old_type->size(), lb, ub,
482 DT_FLAG_DERIVED, count, blocklen, stride, old_type);
485 /* in this situation the data are contiguous thus it's not required to serialize and unserialize it*/
486 *new_type = new Datatype(count * blocklen * old_type->size(), 0, count * blocklen * old_type->size(), DT_FLAG_CONTIGUOUS);
492 int Datatype::create_indexed(int count, int* blocklens, int* indices, MPI_Datatype old_type, MPI_Datatype* new_type){
494 bool contiguous=true;
498 lb=indices[0]*old_type->get_extent();
499 ub=indices[0]*old_type->get_extent() + blocklens[0]*old_type->ub();
502 for (int i = 0; i < count; i++) {
503 if (blocklens[i] < 0)
505 size += blocklens[i];
507 if(indices[i]*old_type->get_extent()+old_type->lb()<lb)
508 lb = indices[i]*old_type->get_extent()+old_type->lb();
509 if(indices[i]*old_type->get_extent()+blocklens[i]*old_type->ub()>ub)
510 ub = indices[i]*old_type->get_extent()+blocklens[i]*old_type->ub();
512 if ( (i< count -1) && (indices[i]+blocklens[i] != indices[i+1]) )
515 if(old_type->flags_ & DT_FLAG_DERIVED)
519 *new_type = new Type_Indexed(size * old_type->size(),lb,ub,
520 DT_FLAG_DERIVED|DT_FLAG_DATA, count, blocklens, indices, old_type);
522 *new_type = new Type_Contiguous(size * old_type->size(), lb, ub,
523 DT_FLAG_DERIVED|DT_FLAG_CONTIGUOUS, size, old_type);
528 int Datatype::create_hindexed(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype old_type, MPI_Datatype* new_type){
530 bool contiguous=true;
534 lb=indices[0] + old_type->lb();
535 ub=indices[0] + blocklens[0]*old_type->ub();
537 for (int i = 0; i < count; i++) {
538 if (blocklens[i] < 0)
540 size += blocklens[i];
542 if(indices[i]+old_type->lb()<lb)
543 lb = indices[i]+old_type->lb();
544 if(indices[i]+blocklens[i]*old_type->ub()>ub)
545 ub = indices[i]+blocklens[i]*old_type->ub();
547 if ( (i< count -1) && (indices[i]+blocklens[i]*(static_cast<int>(old_type->size())) != indices[i+1]) )
550 if (old_type->flags_ & DT_FLAG_DERIVED || lb!=0)
554 *new_type = new Type_Hindexed(size * old_type->size(),lb,ub,
555 DT_FLAG_DERIVED|DT_FLAG_DATA, count, blocklens, indices, old_type);
557 *new_type = new Type_Contiguous(size * old_type->size(), lb, ub,
558 DT_FLAG_DERIVED|DT_FLAG_CONTIGUOUS, size, old_type);
563 int Datatype::create_struct(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype* old_types, MPI_Datatype* new_type){
565 bool contiguous=true;
570 lb=indices[0] + old_types[0]->lb();
571 ub=indices[0] + blocklens[0]*old_types[0]->ub();
573 bool forced_lb=false;
574 bool forced_ub=false;
575 for (int i = 0; i < count; i++) {
578 if (old_types[i]->flags_ & DT_FLAG_DERIVED)
581 size += blocklens[i]*old_types[i]->size();
582 if (old_types[i]==MPI_LB){
586 if (old_types[i]==MPI_UB){
591 if(!forced_lb && indices[i]+old_types[i]->lb()<lb)
593 if(!forced_ub && indices[i]+blocklens[i]*old_types[i]->ub()>ub)
594 ub = indices[i]+blocklens[i]*old_types[i]->ub();
596 if ( (i< count -1) && (indices[i]+blocklens[i]*static_cast<int>(old_types[i]->size()) != indices[i+1]) )
600 *new_type = new Type_Struct(size, lb,ub, DT_FLAG_DERIVED|DT_FLAG_DATA,
601 count, blocklens, indices, old_types);
603 *new_type = new Type_Contiguous(size, lb, ub,
604 DT_FLAG_DERIVED|DT_FLAG_CONTIGUOUS, size, MPI_CHAR);
611 Type_Contiguous::Type_Contiguous(int size, int lb, int ub, int flags, int block_count, MPI_Datatype old_type): Datatype(size, lb, ub, flags), block_count_(block_count), old_type_(old_type){
615 Type_Contiguous::~Type_Contiguous(){
619 void Type_Contiguous::use(){
623 void Type_Contiguous::serialize( void* noncontiguous_buf, void *contiguous_buf,
625 char* contiguous_buf_char = static_cast<char*>(contiguous_buf);
626 char* noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf)+lb_;
627 memcpy(contiguous_buf_char, noncontiguous_buf_char, count * block_count_ * old_type_->size());
629 void Type_Contiguous::unserialize( void* contiguous_buf, void *noncontiguous_buf,
630 int count, MPI_Op op){
631 char* contiguous_buf_char = static_cast<char*>(contiguous_buf);
632 char* noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf)+lb_;
633 int n= count*block_count_;
635 op->apply( contiguous_buf_char, noncontiguous_buf_char, &n, old_type_);
641 Type_Vector::Type_Vector(int size,int lb, int ub, int flags, int count, int blocklen, int stride, MPI_Datatype old_type): Datatype(size, lb, ub, flags), block_count_(count), block_length_(blocklen),block_stride_(stride), old_type_(old_type){
645 Type_Vector::~Type_Vector(){
649 void Type_Vector::use(){
654 void Type_Vector::serialize( void* noncontiguous_buf, void *contiguous_buf,
657 char* contiguous_buf_char = static_cast<char*>(contiguous_buf);
658 char* noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf);
660 for (i = 0; i < block_count_ * count; i++) {
661 if (!(old_type_->flags() & DT_FLAG_DERIVED))
662 memcpy(contiguous_buf_char, noncontiguous_buf_char, block_length_ * old_type_->size());
664 old_type_->serialize(noncontiguous_buf_char, contiguous_buf_char, block_length_);
666 contiguous_buf_char += block_length_*old_type_->size();
667 if((i+1)%block_count_ ==0)
668 noncontiguous_buf_char += block_length_*old_type_->get_extent();
670 noncontiguous_buf_char += block_stride_*old_type_->get_extent();
674 void Type_Vector::unserialize( void* contiguous_buf, void *noncontiguous_buf,
675 int count, MPI_Op op){
677 char* contiguous_buf_char = static_cast<char*>(contiguous_buf);
678 char* noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf);
680 for (i = 0; i < block_count_ * count; i++) {
681 if (!(old_type_->flags() & DT_FLAG_DERIVED)){
682 if(op != MPI_OP_NULL)
683 op->apply(contiguous_buf_char, noncontiguous_buf_char, &block_length_,
686 old_type_->unserialize(contiguous_buf_char, noncontiguous_buf_char, block_length_, op);
688 contiguous_buf_char += block_length_*old_type_->size();
689 if((i+1)%block_count_ ==0)
690 noncontiguous_buf_char += block_length_*old_type_->get_extent();
692 noncontiguous_buf_char += block_stride_*old_type_->get_extent();
696 Type_Hvector::Type_Hvector(int size,int lb, int ub, int flags, int count, int blocklen, MPI_Aint stride, MPI_Datatype old_type): Datatype(size, lb, ub, flags), block_count_(count), block_length_(blocklen), block_stride_(stride), old_type_(old_type){
699 Type_Hvector::~Type_Hvector(){
702 void Type_Hvector::use(){
706 void Type_Hvector::serialize( void* noncontiguous_buf, void *contiguous_buf,
709 char* contiguous_buf_char = static_cast<char*>(contiguous_buf);
710 char* noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf);
712 for (i = 0; i < block_count_ * count; i++) {
713 if (!(old_type_->flags() & DT_FLAG_DERIVED))
714 memcpy(contiguous_buf_char, noncontiguous_buf_char, block_length_ * old_type_->size());
716 old_type_->serialize( noncontiguous_buf_char, contiguous_buf_char, block_length_);
718 contiguous_buf_char += block_length_*old_type_->size();
719 if((i+1)%block_count_ ==0)
720 noncontiguous_buf_char += block_length_*old_type_->size();
722 noncontiguous_buf_char += block_stride_;
727 void Type_Hvector::unserialize( void* contiguous_buf, void *noncontiguous_buf,
728 int count, MPI_Op op){
730 char* contiguous_buf_char = static_cast<char*>(contiguous_buf);
731 char* noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf);
733 for (i = 0; i < block_count_ * count; i++) {
734 if (!(old_type_->flags() & DT_FLAG_DERIVED)){
736 op->apply( contiguous_buf_char, noncontiguous_buf_char, &block_length_, old_type_);
738 old_type_->unserialize( contiguous_buf_char, noncontiguous_buf_char, block_length_, op);
739 contiguous_buf_char += block_length_*old_type_->size();
740 if((i+1)%block_count_ ==0)
741 noncontiguous_buf_char += block_length_*old_type_->size();
743 noncontiguous_buf_char += block_stride_;
747 Type_Indexed::Type_Indexed(int size,int lb, int ub, int flags, int count, int* blocklens, int* indices, MPI_Datatype old_type): Datatype(size, lb, ub, flags), block_count_(count), block_lengths_(blocklens), block_indices_(indices), old_type_(old_type){
751 Type_Indexed::~Type_Indexed(){
754 xbt_free(block_lengths_);
755 xbt_free(block_indices_);
759 void Type_Indexed::use(){
763 void Type_Indexed::serialize( void* noncontiguous_buf, void *contiguous_buf,
765 char* contiguous_buf_char = static_cast<char*>(contiguous_buf);
766 char* noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf)+block_indices_[0] * old_type_->size();
767 for (int j = 0; j < count; j++) {
768 for (int i = 0; i < block_count_; i++) {
769 if (!(old_type_->flags() & DT_FLAG_DERIVED))
770 memcpy(contiguous_buf_char, noncontiguous_buf_char, block_lengths_[i] * old_type_->size());
772 old_type_->serialize( noncontiguous_buf_char, contiguous_buf_char, block_lengths_[i]);
774 contiguous_buf_char += block_lengths_[i]*old_type_->size();
775 if (i<block_count_-1)
776 noncontiguous_buf_char =
777 static_cast<char*>(noncontiguous_buf) + block_indices_[i+1]*old_type_->get_extent();
779 noncontiguous_buf_char += block_lengths_[i]*old_type_->get_extent();
781 noncontiguous_buf=static_cast< void*>(noncontiguous_buf_char);
786 void Type_Indexed::unserialize( void* contiguous_buf, void *noncontiguous_buf,
787 int count, MPI_Op op){
788 char* contiguous_buf_char = static_cast<char*>(contiguous_buf);
789 char* noncontiguous_buf_char =
790 static_cast<char*>(noncontiguous_buf)+block_indices_[0]*old_type_->get_extent();
791 for (int j = 0; j < count; j++) {
792 for (int i = 0; i < block_count_; i++) {
793 if (!(old_type_->flags() & DT_FLAG_DERIVED)){
795 op->apply( contiguous_buf_char, noncontiguous_buf_char, &block_lengths_[i],
798 old_type_->unserialize( contiguous_buf_char,noncontiguous_buf_char,block_lengths_[i], op);
800 contiguous_buf_char += block_lengths_[i]*old_type_->size();
801 if (i<block_count_-1)
802 noncontiguous_buf_char =
803 static_cast<char*>(noncontiguous_buf) + block_indices_[i+1]*old_type_->get_extent();
805 noncontiguous_buf_char += block_lengths_[i]*old_type_->get_extent();
807 noncontiguous_buf=static_cast<void*>(noncontiguous_buf_char);
811 Type_Hindexed::Type_Hindexed(int size,int lb, int ub, int flags, int count, int* blocklens, MPI_Aint* indices, MPI_Datatype old_type): Datatype(size, lb, ub, flags), block_count_(count), block_lengths_(blocklens), block_indices_(indices), old_type_(old_type){
815 Type_Hindexed::~Type_Hindexed(){
818 xbt_free(block_lengths_);
819 xbt_free(block_indices_);
823 void Type_Hindexed::use(){
826 void Type_Hindexed::serialize( void* noncontiguous_buf, void *contiguous_buf,
828 char* contiguous_buf_char = static_cast<char*>(contiguous_buf);
829 char* noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf)+ block_indices_[0];
830 for (int j = 0; j < count; j++) {
831 for (int i = 0; i < block_count_; i++) {
832 if (!(old_type_->flags() & DT_FLAG_DERIVED))
833 memcpy(contiguous_buf_char, noncontiguous_buf_char, block_lengths_[i] * old_type_->size());
835 old_type_->serialize(noncontiguous_buf_char, contiguous_buf_char,block_lengths_[i]);
837 contiguous_buf_char += block_lengths_[i]*old_type_->size();
838 if (i<block_count_-1)
839 noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf) + block_indices_[i+1];
841 noncontiguous_buf_char += block_lengths_[i]*old_type_->get_extent();
843 noncontiguous_buf=static_cast<void*>(noncontiguous_buf_char);
847 void Type_Hindexed::unserialize( void* contiguous_buf, void *noncontiguous_buf,
848 int count, MPI_Op op){
849 char* contiguous_buf_char = static_cast<char*>(contiguous_buf);
850 char* noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf)+ block_indices_[0];
851 for (int j = 0; j < count; j++) {
852 for (int i = 0; i < block_count_; i++) {
853 if (!(old_type_->flags() & DT_FLAG_DERIVED)){
855 op->apply( contiguous_buf_char, noncontiguous_buf_char, &block_lengths_[i],
858 old_type_->unserialize( contiguous_buf_char,noncontiguous_buf_char,block_lengths_[i], op);
860 contiguous_buf_char += block_lengths_[i]*old_type_->size();
861 if (i<block_count_-1)
862 noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf) + block_indices_[i+1];
864 noncontiguous_buf_char += block_lengths_[i]*old_type_->get_extent();
866 noncontiguous_buf=static_cast<void*>(noncontiguous_buf_char);
870 Type_Struct::Type_Struct(int size,int lb, int ub, int flags, int count, int* blocklens, MPI_Aint* indices, MPI_Datatype* old_types): Datatype(size, lb, ub, flags), block_count_(count), block_lengths_(blocklens), block_indices_(indices), old_types_(old_types){
871 for (int i = 0; i < count; i++) {
872 old_types_[i]->use();
876 Type_Struct::~Type_Struct(){
877 for (int i = 0; i < block_count_; i++) {
878 old_types_[i]->unuse();
881 xbt_free(block_lengths_);
882 xbt_free(block_indices_);
883 xbt_free(old_types_);
887 void Type_Struct::use(){
888 for (int i = 0; i < block_count_; i++) {
889 old_types_[i]->use();
893 void Type_Struct::serialize( void* noncontiguous_buf, void *contiguous_buf,
895 char* contiguous_buf_char = static_cast<char*>(contiguous_buf);
896 char* noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf)+ block_indices_[0];
897 for (int j = 0; j < count; j++) {
898 for (int i = 0; i < block_count_; i++) {
899 if (!(old_types_[i]->flags() & DT_FLAG_DERIVED))
900 memcpy(contiguous_buf_char, noncontiguous_buf_char, block_lengths_[i] * old_types_[i]->size());
902 old_types_[i]->serialize( noncontiguous_buf_char,contiguous_buf_char,block_lengths_[i]);
905 contiguous_buf_char += block_lengths_[i]*old_types_[i]->size();
906 if (i<block_count_-1)
907 noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf) + block_indices_[i+1];
908 else //let's hope this is MPI_UB ?
909 noncontiguous_buf_char += block_lengths_[i]*old_types_[i]->get_extent();
911 noncontiguous_buf=static_cast<void*>(noncontiguous_buf_char);
915 void Type_Struct::unserialize( void* contiguous_buf, void *noncontiguous_buf,
916 int count, MPI_Op op){
917 char* contiguous_buf_char = static_cast<char*>(contiguous_buf);
918 char* noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf)+ block_indices_[0];
919 for (int j = 0; j < count; j++) {
920 for (int i = 0; i < block_count_; i++) {
921 if (!(old_types_[i]->flags() & DT_FLAG_DERIVED)){
923 op->apply( contiguous_buf_char, noncontiguous_buf_char, &block_lengths_[i], old_types_[i]);
925 old_types_[i]->unserialize( contiguous_buf_char, noncontiguous_buf_char,block_lengths_[i], op);
927 contiguous_buf_char += block_lengths_[i]*old_types_[i]->size();
928 if (i<block_count_-1)
929 noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf) + block_indices_[i+1];
931 noncontiguous_buf_char += block_lengths_[i]*old_types_[i]->get_extent();
933 noncontiguous_buf=reinterpret_cast<void*>(noncontiguous_buf_char);