Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Continuing work on datatypes
[simgrid.git] / src / smpi / smpi_datatype.cpp
1 /* smpi_datatype.cpp -- MPI primitives to handle datatypes                      */
2 /* Copyright (c) 2009-2015. The SimGrid Team.
3  * All rights reserved.                                                     */
4
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. */
7
8 #include "mc/mc.h"
9 #include "private.h"
10 #include "simgrid/modelchecker.h"
11 #include "xbt/replay.h"
12 #include <limits.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <string>
17 #include <xbt/ex.hpp>
18
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_mpi_dt, smpi, "Logging specific to SMPI (datatype)");
20
21 xbt_dict_t smpi_type_keyvals = nullptr;
22 int type_keyval_id=0;//avoid collisions
23
24
25 #define CREATE_MPI_DATATYPE(name, type)               \
26   static Datatype mpi_##name (         \
27     (char*) # name,                                   \
28     sizeof(type),   /* size */                        \
29     0,              /* lb */                          \
30     sizeof(type),   /* ub = lb + size */              \
31     DT_FLAG_BASIC  /* flags */                       \
32   );                                                  \
33 const MPI_Datatype name = &mpi_##name;
34
35 #define CREATE_MPI_DATATYPE_NULL(name)                \
36   static Datatype mpi_##name (         \
37     (char*) # name,                                   \
38     0,              /* size */                        \
39     0,              /* lb */                          \
40     0,              /* ub = lb + size */              \
41     DT_FLAG_BASIC  /* flags */                       \
42   );                                                  \
43 const MPI_Datatype name = &mpi_##name;
44
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);
76
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);
85
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);
98
99 CREATE_MPI_DATATYPE(MPI_LONG_DOUBLE_INT, long_double_int);
100
101 CREATE_MPI_DATATYPE_NULL(MPI_UB);
102 CREATE_MPI_DATATYPE_NULL(MPI_LB);
103 CREATE_MPI_DATATYPE(MPI_PACKED, char);
104 // Internal use only
105 CREATE_MPI_DATATYPE(MPI_PTR, void*);
106
107 namespace simgrid{
108 namespace smpi{
109
110 Datatype::Datatype(int size,int lb, int ub, int flags) : name_(nullptr), lb_(lb), ub_(ub), flags_(flags), attributes_(nullptr), in_use_(1){
111 #if HAVE_MC
112   if(MC_is_active())
113     MC_ignore(&(in_use_), sizeof(in_use_));
114 #endif
115 }
116
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){
119 #if HAVE_MC
120   if(MC_is_active())
121     MC_ignore(&(in_use_), sizeof(in_use_));
122 #endif
123 }
124
125
126 //TODO : subtypes ?
127 Datatype::Datatype(Datatype *datatype) : lb_(datatype->lb_), ub_(datatype->ub_), flags_(datatype->flags_), in_use_(1)
128 {
129   flags_ &= ~DT_FLAG_PREDEFINED;
130   if(datatype->name_)
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;
135     char* key;
136     int flag;
137     void* value_in;
138     void* value_out;
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);
148         }
149         if (flag)
150           xbt_dict_set_ext(attributes_, key, sizeof(int), value_out, nullptr);
151       }
152     }
153   }
154 }
155
156 Datatype::~Datatype(){
157   xbt_assert(in_use_ >= 0);
158
159   if(flags_ & DT_FLAG_PREDEFINED)
160     return;
161
162   //if still used, mark for deletion
163   if(in_use_!=0){
164       flags_ |=DT_FLAG_DESTROYED;
165       return;
166   }
167
168   if(attributes_ !=nullptr){
169     xbt_dict_cursor_t cursor = nullptr;
170     char* key;
171     void * value;
172     int flag;
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);
178     }
179     xbt_dict_free(&attributes_);
180   }
181
182   xbt_free(name_);
183 }
184
185
186 void Datatype::use(){
187
188   in_use_++;
189
190 #if HAVE_MC
191   if(MC_is_active())
192     MC_ignore(&(in_use_), sizeof(in_use_));
193 #endif
194 }
195
196 void Datatype::unuse()
197 {
198   if (in_use_ > 0)
199     in_use_--;
200
201   if (in_use_ == 0)
202     this->~Datatype();
203
204 #if HAVE_MC
205   if(MC_is_active())
206     MC_ignore(&(in_use_), sizeof(in_use_));
207 #endif
208 }
209
210 void Datatype::commit()
211 {
212   flags_ |= DT_FLAG_COMMITED;
213 }
214
215
216 bool Datatype::is_valid(){
217   return (flags_ & DT_FLAG_COMMITED);
218 }
219
220 size_t Datatype::size(){
221   return size_;
222 }
223
224 int Datatype::flags(){
225   return flags_;
226 }
227
228 void Datatype::addflag(int flag){
229   flags_ &= flag;
230 }
231
232 MPI_Aint Datatype::lb(){
233   return lb_;
234 }
235
236 MPI_Aint Datatype::ub(){
237   return ub_;
238 }
239
240 char* Datatype::name(){
241   return name_;
242 }
243
244
245 int Datatype::extent(MPI_Aint * lb, MPI_Aint * extent){
246   *lb = lb_;
247   *extent = ub_ - lb_;
248   return MPI_SUCCESS;
249 }
250
251 MPI_Aint Datatype::get_extent(){
252   return ub_ - lb_;
253 }
254
255 void Datatype::get_name(char* name, int* length){
256   *length = strlen(name_);
257   strncpy(name, name_, *length+1);
258 }
259
260 void Datatype::set_name(char* name){
261   if(name_!=nullptr &&  (flags_ & DT_FLAG_PREDEFINED) == 0)
262     xbt_free(name_);
263   name_ = xbt_strdup(name);
264 }
265
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)));
269   if(elem==nullptr)
270     return MPI_ERR_ARG;
271   if(elem->delete_fn!=MPI_NULL_DELETE_FN){
272     void * value = nullptr;
273     int flag;
274     if(this->attr_get(keyval, &value, &flag)==MPI_SUCCESS){
275       int ret = elem->delete_fn(this, keyval, value, &flag);
276       if(ret!=MPI_SUCCESS) 
277         return ret;
278     }
279   }  
280   if(attributes_==nullptr)
281     return MPI_ERR_ARG;
282
283   xbt_dict_remove_ext(attributes_, reinterpret_cast<const char*>(&keyval), sizeof(int));
284   return MPI_SUCCESS;
285 }
286
287
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)));
291   if(elem==nullptr)
292     return MPI_ERR_ARG;
293   if(attributes_==nullptr){
294     *flag=0;
295     return MPI_SUCCESS;
296   }
297   try {
298     *static_cast<void**>(attr_value) = xbt_dict_get_ext(attributes_, reinterpret_cast<const char*>(&keyval), sizeof(int));
299     *flag=1;
300   }
301   catch (xbt_ex& ex) {
302     *flag=0;
303   }
304   return MPI_SUCCESS;
305 }
306
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)));
312   if(elem==nullptr)
313     return MPI_ERR_ARG;
314   int flag;
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);
319     if(ret!=MPI_SUCCESS) 
320       return ret;
321   }
322   if(attributes_==nullptr)
323     attributes_ = xbt_dict_new_homogeneous(nullptr);
324
325   xbt_dict_set_ext(attributes_, reinterpret_cast<const char*>(&keyval), sizeof(int), attr_value, nullptr);
326   return MPI_SUCCESS;
327 }
328
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);
332
333   smpi_type_key_elem value = (smpi_type_key_elem) xbt_new0(s_smpi_mpi_type_key_elem_t,1);
334
335   value->copy_fn=copy_fn;
336   value->delete_fn=delete_fn;
337
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);
340   type_keyval_id++;
341   return MPI_SUCCESS;
342 }
343
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)));
347   if(elem==0){
348     return MPI_ERR_ARG;
349   }
350   xbt_dict_remove_ext(smpi_type_keyvals, reinterpret_cast<const char*>(keyval), sizeof(int));
351   xbt_free(elem);
352   return MPI_SUCCESS;
353 }
354
355
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_;
361   return MPI_SUCCESS;
362 }
363
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_;
369   return MPI_SUCCESS;
370 }
371
372
373 int Datatype::copy(void *sendbuf, int sendcount, MPI_Datatype sendtype,
374                        void *recvbuf, int recvcount, MPI_Datatype recvtype){
375 //  int count;
376   if(smpi_privatize_global_variables){
377     smpi_switch_data_segment(smpi_process_index());
378   }
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;
385
386 //    if(sendtype->sizeof_substruct == 0 && recvtype->sizeof_substruct == 0) {
387 //      if(!smpi_process_get_replaying()) 
388 //        memcpy(recvbuf, sendbuf, count);
389 //    }
390 //    else if (sendtype->sizeof_substruct == 0)
391 //    {
392 //      s_smpi_subtype_t *subtype =  recvtype->substruct);
393 //      subtype->unserialize( sendbuf, recvbuf, recvcount/recvtype->size(), subtype, MPI_REPLACE);
394 //    }
395 //    else if (recvtype->sizeof_substruct == 0)
396 //    {
397 //      s_smpi_subtype_t *subtype =  sendtype->substruct);
398 //      subtype->serialize(sendbuf, recvbuf, sendcount/sendtype->size(), subtype);
399 //    }else{
400 //      s_smpi_subtype_t *subtype =  sendtype->substruct);
401
402 //      void * buf_tmp = xbt_malloc(count);
403
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);
407
408 //      xbt_free(buf_tmp);
409 //    }
410   }
411
412   return sendcount > recvcount ? MPI_ERR_TRUNCATE : MPI_SUCCESS;
413 }
414
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_);
420
421 }
422
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_;
426   int n=count;
427   if(op!=MPI_OP_NULL)
428     op->apply( contiguous_buf_char, noncontiguous_buf_char, &n, this);
429 }
430
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);
435   }
436   if(count>0)
437     *new_type = new Type_Contiguous(count * old_type->size(), lb, lb + count * old_type->size(),
438                                    DT_FLAG_DERIVED, count, old_type);
439   else
440     *new_type = new Datatype(count * old_type->size(), lb, lb + count * old_type->size(),0);
441   return MPI_SUCCESS;
442 }
443
444 int Datatype::create_vector(int count, int blocklen, int stride, MPI_Datatype old_type, MPI_Datatype* new_type)
445 {
446   int retval;
447   if (blocklen<0) 
448     return MPI_ERR_ARG;
449   MPI_Aint lb = 0;
450   MPI_Aint ub = 0;
451   if(count>0){
452     lb=old_type->lb();
453     ub=((count-1)*stride+blocklen-1)*old_type->get_extent()+old_type->ub();
454   }
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);
458     retval=MPI_SUCCESS;
459   }else{
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);
463     retval=MPI_SUCCESS;
464   }
465   return retval;
466 }
467
468
469 int Datatype::create_hvector(int count, int blocklen, MPI_Aint stride, MPI_Datatype old_type, MPI_Datatype* new_type)
470 {
471   int retval;
472   if (blocklen<0) 
473     return MPI_ERR_ARG;
474   MPI_Aint lb = 0;
475   MPI_Aint ub = 0;
476   if(count>0){
477     lb=old_type->lb();
478     ub=((count-1)*stride+blocklen-1)*old_type->get_extent()+old_type->ub();
479   }
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);
483     retval=MPI_SUCCESS;
484   }else{
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);
487     retval=MPI_SUCCESS;
488   }
489   return retval;
490 }
491
492 int Datatype::create_indexed(int count, int* blocklens, int* indices, MPI_Datatype old_type, MPI_Datatype* new_type){
493   int size = 0;
494   bool contiguous=true;
495   MPI_Aint lb = 0;
496   MPI_Aint ub = 0;
497   if(count>0){
498     lb=indices[0]*old_type->get_extent();
499     ub=indices[0]*old_type->get_extent() + blocklens[0]*old_type->ub();
500   }
501
502   for (int i = 0; i < count; i++) {
503     if (blocklens[i] < 0)
504       return MPI_ERR_ARG;
505     size += blocklens[i];
506
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();
511
512     if ( (i< count -1) && (indices[i]+blocklens[i] != indices[i+1]) )
513       contiguous=false;
514   }
515   if(old_type->flags_ & DT_FLAG_DERIVED)
516     contiguous=false;
517
518   if(!contiguous){
519     *new_type = new Type_Indexed(size * old_type->size(),lb,ub,
520                                  DT_FLAG_DERIVED|DT_FLAG_DATA, count, blocklens, indices, old_type);
521   }else{
522     *new_type = new Type_Contiguous(size * old_type->size(), lb, ub,
523                                     DT_FLAG_DERIVED|DT_FLAG_CONTIGUOUS, size, old_type);
524   }
525   return MPI_SUCCESS;
526 }
527
528 int Datatype::create_hindexed(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype old_type, MPI_Datatype* new_type){
529   int size = 0;
530   bool contiguous=true;
531   MPI_Aint lb = 0;
532   MPI_Aint ub = 0;
533   if(count>0){
534     lb=indices[0] + old_type->lb();
535     ub=indices[0] + blocklens[0]*old_type->ub();
536   }
537   for (int i = 0; i < count; i++) {
538     if (blocklens[i] < 0)
539       return MPI_ERR_ARG;
540     size += blocklens[i];
541
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();
546
547     if ( (i< count -1) && (indices[i]+blocklens[i]*(static_cast<int>(old_type->size())) != indices[i+1]) )
548       contiguous=false;
549   }
550   if (old_type->flags_ & DT_FLAG_DERIVED || lb!=0)
551     contiguous=false;
552
553   if(!contiguous){
554     *new_type = new Type_Hindexed(size * old_type->size(),lb,ub,
555                                    DT_FLAG_DERIVED|DT_FLAG_DATA, count, blocklens, indices, old_type);
556   }else{
557     *new_type = new Type_Contiguous(size * old_type->size(), lb, ub,
558                                    DT_FLAG_DERIVED|DT_FLAG_CONTIGUOUS, size, old_type);
559   }
560   return MPI_SUCCESS;
561 }
562
563 int Datatype::create_struct(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype* old_types, MPI_Datatype* new_type){
564   size_t size = 0;
565   bool contiguous=true;
566   size = 0;
567   MPI_Aint lb = 0;
568   MPI_Aint ub = 0;
569   if(count>0){
570     lb=indices[0] + old_types[0]->lb();
571     ub=indices[0] + blocklens[0]*old_types[0]->ub();
572   }
573   bool forced_lb=false;
574   bool forced_ub=false;
575   for (int i = 0; i < count; i++) {
576     if (blocklens[i]<0)
577       return MPI_ERR_ARG;
578     if (old_types[i]->flags_ & DT_FLAG_DERIVED)
579       contiguous=false;
580
581     size += blocklens[i]*old_types[i]->size();
582     if (old_types[i]==MPI_LB){
583       lb=indices[i];
584       forced_lb=true;
585     }
586     if (old_types[i]==MPI_UB){
587       ub=indices[i];
588       forced_ub=true;
589     }
590
591     if(!forced_lb && indices[i]+old_types[i]->lb()<lb) 
592       lb = indices[i];
593     if(!forced_ub &&  indices[i]+blocklens[i]*old_types[i]->ub()>ub)
594       ub = indices[i]+blocklens[i]*old_types[i]->ub();
595
596     if ( (i< count -1) && (indices[i]+blocklens[i]*static_cast<int>(old_types[i]->size()) != indices[i+1]) )
597       contiguous=false;
598   }
599   if(!contiguous){
600     *new_type = new Type_Struct(size, lb,ub, DT_FLAG_DERIVED|DT_FLAG_DATA, 
601                                 count, blocklens, indices, old_types);
602   }else{
603     *new_type = new Type_Contiguous(size, lb, ub,
604                                    DT_FLAG_DERIVED|DT_FLAG_CONTIGUOUS, size, MPI_CHAR);
605   }
606   return MPI_SUCCESS;
607 }
608
609
610
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){
612   old_type_->use();
613 }
614
615 Type_Contiguous::~Type_Contiguous(){
616   old_type_->unuse();
617 }
618
619 void Type_Contiguous::use(){
620   old_type_->use();
621 };
622
623 void Type_Contiguous::serialize( void* noncontiguous_buf, void *contiguous_buf, 
624                             int count){
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());
628 }
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_;
634   if(op!=MPI_OP_NULL)
635     op->apply( contiguous_buf_char, noncontiguous_buf_char, &n, old_type_);
636 }
637
638
639
640
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){
642 old_type_->use();
643 }
644
645 Type_Vector::~Type_Vector(){
646   old_type_->unuse();
647 }
648
649 void Type_Vector::use(){
650   old_type_->use();
651 }
652
653
654 void Type_Vector::serialize( void* noncontiguous_buf, void *contiguous_buf, 
655                             int count){
656   int i;
657   char* contiguous_buf_char = static_cast<char*>(contiguous_buf);
658   char* noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf);
659
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());
663       else        
664         old_type_->serialize(noncontiguous_buf_char, contiguous_buf_char, block_length_);
665
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();
669     else
670       noncontiguous_buf_char += block_stride_*old_type_->get_extent();
671   }
672 }
673
674 void Type_Vector::unserialize( void* contiguous_buf, void *noncontiguous_buf, 
675                               int count, MPI_Op op){
676   int i;
677   char* contiguous_buf_char = static_cast<char*>(contiguous_buf);
678   char* noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf);
679
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_,
684           old_type_);
685     }else
686       old_type_->unserialize(contiguous_buf_char, noncontiguous_buf_char, block_length_, op);
687
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();
691     else
692       noncontiguous_buf_char += block_stride_*old_type_->get_extent();
693   }
694 }
695
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){
697   old_type->use();
698 }
699 Type_Hvector::~Type_Hvector(){
700   old_type_->unuse();
701 }
702 void Type_Hvector::use(){
703   old_type_->use();
704 }
705
706 void Type_Hvector::serialize( void* noncontiguous_buf, void *contiguous_buf, 
707                     int count){
708   int i;
709   char* contiguous_buf_char = static_cast<char*>(contiguous_buf);
710   char* noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf);
711
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());
715     else
716       old_type_->serialize( noncontiguous_buf_char, contiguous_buf_char, block_length_);
717
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();
721     else
722       noncontiguous_buf_char += block_stride_;
723   }
724 }
725
726
727 void Type_Hvector::unserialize( void* contiguous_buf, void *noncontiguous_buf, 
728                               int count, MPI_Op op){
729   int i;
730   char* contiguous_buf_char = static_cast<char*>(contiguous_buf);
731   char* noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf);
732
733   for (i = 0; i < block_count_ * count; i++) {
734     if (!(old_type_->flags() & DT_FLAG_DERIVED)){
735       if(op!=MPI_OP_NULL) 
736         op->apply( contiguous_buf_char, noncontiguous_buf_char, &block_length_, old_type_);
737     }else
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();
742     else
743       noncontiguous_buf_char += block_stride_;
744   }
745 }
746
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){
748   old_type->use();
749 }
750
751 Type_Indexed::~Type_Indexed(){
752   old_type_->unuse();
753   if(in_use_==0){
754     xbt_free(block_lengths_);
755     xbt_free(block_indices_);
756   }
757 }
758
759 void Type_Indexed::use(){
760   old_type_->use();
761 }
762
763 void Type_Indexed::serialize( void* noncontiguous_buf, void *contiguous_buf, 
764                         int count){
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());
771       else
772         old_type_->serialize( noncontiguous_buf_char, contiguous_buf_char, block_lengths_[i]);
773
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();
778       else
779         noncontiguous_buf_char += block_lengths_[i]*old_type_->get_extent();
780     }
781     noncontiguous_buf=static_cast< void*>(noncontiguous_buf_char);
782   }
783 }
784
785
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)){
794         if(op!=MPI_OP_NULL) 
795           op->apply( contiguous_buf_char, noncontiguous_buf_char, &block_lengths_[i],
796                     old_type_);
797       }else
798         old_type_->unserialize( contiguous_buf_char,noncontiguous_buf_char,block_lengths_[i], op);
799
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();
804       else
805         noncontiguous_buf_char += block_lengths_[i]*old_type_->get_extent();
806     }
807     noncontiguous_buf=static_cast<void*>(noncontiguous_buf_char);
808   }
809 }
810
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){
812   old_type_->use();
813 }
814
815     Type_Hindexed::~Type_Hindexed(){
816   old_type_->unuse();
817   if(in_use_==0){
818     xbt_free(block_lengths_);
819     xbt_free(block_indices_);
820   }
821 }
822
823 void Type_Hindexed::use(){
824   old_type_->use();
825 }
826 void Type_Hindexed::serialize( void* noncontiguous_buf, void *contiguous_buf, 
827                 int count){
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());
834       else
835         old_type_->serialize(noncontiguous_buf_char, contiguous_buf_char,block_lengths_[i]);
836
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];
840       else
841         noncontiguous_buf_char += block_lengths_[i]*old_type_->get_extent();
842     }
843     noncontiguous_buf=static_cast<void*>(noncontiguous_buf_char);
844   }
845 }
846
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)){
854         if(op!=MPI_OP_NULL) 
855           op->apply( contiguous_buf_char, noncontiguous_buf_char, &block_lengths_[i],
856                             old_type_);
857       }else
858         old_type_->unserialize( contiguous_buf_char,noncontiguous_buf_char,block_lengths_[i], op);
859
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];
863       else
864         noncontiguous_buf_char += block_lengths_[i]*old_type_->get_extent();
865     }
866     noncontiguous_buf=static_cast<void*>(noncontiguous_buf_char);
867   }
868 }
869
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();
873   }
874 }
875
876 Type_Struct::~Type_Struct(){
877   for (int i = 0; i < block_count_; i++) {
878     old_types_[i]->unuse();
879   }
880   if(in_use_==0){
881     xbt_free(block_lengths_);
882     xbt_free(block_indices_);
883     xbt_free(old_types_);
884   }
885 }
886
887 void Type_Struct::use(){
888   for (int i = 0; i < block_count_; i++) {
889     old_types_[i]->use();
890   }
891 }
892
893 void Type_Struct::serialize( void* noncontiguous_buf, void *contiguous_buf, 
894                         int count){
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());
901       else
902         old_types_[i]->serialize( noncontiguous_buf_char,contiguous_buf_char,block_lengths_[i]);
903
904
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();
910     }
911     noncontiguous_buf=static_cast<void*>(noncontiguous_buf_char);
912   }
913 }
914
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)){
922         if(op!=MPI_OP_NULL) 
923           op->apply( contiguous_buf_char, noncontiguous_buf_char, &block_lengths_[i], old_types_[i]);
924       }else
925         old_types_[i]->unserialize( contiguous_buf_char, noncontiguous_buf_char,block_lengths_[i], op);
926
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];
930       else
931         noncontiguous_buf_char += block_lengths_[i]*old_types_[i]->get_extent();
932     }
933     noncontiguous_buf=reinterpret_cast<void*>(noncontiguous_buf_char);
934   }
935 }
936
937 }
938 }
939