Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
no need for casts with maps
[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 <unordered_map>
18 #include <xbt/ex.hpp>
19
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_datatype, smpi, "Logging specific to SMPI (datatype)");
21
22 std::unordered_map<int, smpi_type_key_elem> smpi_type_keyvals;
23 int type_keyval_id=0;//avoid collisions
24
25
26 #define CREATE_MPI_DATATYPE(name, type)               \
27   static Datatype mpi_##name (         \
28     (char*) # name,                                   \
29     sizeof(type),   /* size */                        \
30     0,              /* lb */                          \
31     sizeof(type),   /* ub = lb + size */              \
32     DT_FLAG_BASIC  /* flags */                       \
33   );                                                  \
34 const MPI_Datatype name = &mpi_##name;
35
36 #define CREATE_MPI_DATATYPE_NULL(name)                \
37   static Datatype mpi_##name (         \
38     (char*) # name,                                   \
39     0,              /* size */                        \
40     0,              /* lb */                          \
41     0,              /* ub = lb + size */              \
42     DT_FLAG_BASIC  /* flags */                       \
43   );                                                  \
44 const MPI_Datatype name = &mpi_##name;
45
46 // Predefined data types
47 CREATE_MPI_DATATYPE(MPI_CHAR, char);
48 CREATE_MPI_DATATYPE(MPI_SHORT, short);
49 CREATE_MPI_DATATYPE(MPI_INT, int);
50 CREATE_MPI_DATATYPE(MPI_LONG, long);
51 CREATE_MPI_DATATYPE(MPI_LONG_LONG, long long);
52 CREATE_MPI_DATATYPE(MPI_SIGNED_CHAR, signed char);
53 CREATE_MPI_DATATYPE(MPI_UNSIGNED_CHAR, unsigned char);
54 CREATE_MPI_DATATYPE(MPI_UNSIGNED_SHORT, unsigned short);
55 CREATE_MPI_DATATYPE(MPI_UNSIGNED, unsigned int);
56 CREATE_MPI_DATATYPE(MPI_UNSIGNED_LONG, unsigned long);
57 CREATE_MPI_DATATYPE(MPI_UNSIGNED_LONG_LONG, unsigned long long);
58 CREATE_MPI_DATATYPE(MPI_FLOAT, float);
59 CREATE_MPI_DATATYPE(MPI_DOUBLE, double);
60 CREATE_MPI_DATATYPE(MPI_LONG_DOUBLE, long double);
61 CREATE_MPI_DATATYPE(MPI_WCHAR, wchar_t);
62 CREATE_MPI_DATATYPE(MPI_C_BOOL, bool);
63 CREATE_MPI_DATATYPE(MPI_BYTE, int8_t);
64 CREATE_MPI_DATATYPE(MPI_INT8_T, int8_t);
65 CREATE_MPI_DATATYPE(MPI_INT16_T, int16_t);
66 CREATE_MPI_DATATYPE(MPI_INT32_T, int32_t);
67 CREATE_MPI_DATATYPE(MPI_INT64_T, int64_t);
68 CREATE_MPI_DATATYPE(MPI_UINT8_T, uint8_t);
69 CREATE_MPI_DATATYPE(MPI_UINT16_T, uint16_t);
70 CREATE_MPI_DATATYPE(MPI_UINT32_T, uint32_t);
71 CREATE_MPI_DATATYPE(MPI_UINT64_T, uint64_t);
72 CREATE_MPI_DATATYPE(MPI_C_FLOAT_COMPLEX, float _Complex);
73 CREATE_MPI_DATATYPE(MPI_C_DOUBLE_COMPLEX, double _Complex);
74 CREATE_MPI_DATATYPE(MPI_C_LONG_DOUBLE_COMPLEX, long double _Complex);
75 CREATE_MPI_DATATYPE(MPI_AINT, MPI_Aint);
76 CREATE_MPI_DATATYPE(MPI_OFFSET, MPI_Offset);
77
78 CREATE_MPI_DATATYPE(MPI_FLOAT_INT, float_int);
79 CREATE_MPI_DATATYPE(MPI_LONG_INT, long_int);
80 CREATE_MPI_DATATYPE(MPI_DOUBLE_INT, double_int);
81 CREATE_MPI_DATATYPE(MPI_SHORT_INT, short_int);
82 CREATE_MPI_DATATYPE(MPI_2INT, int_int);
83 CREATE_MPI_DATATYPE(MPI_2FLOAT, float_float);
84 CREATE_MPI_DATATYPE(MPI_2DOUBLE, double_double);
85 CREATE_MPI_DATATYPE(MPI_2LONG, long_long);
86
87 CREATE_MPI_DATATYPE(MPI_REAL, float);
88 CREATE_MPI_DATATYPE(MPI_REAL4, float);
89 CREATE_MPI_DATATYPE(MPI_REAL8, float);
90 CREATE_MPI_DATATYPE(MPI_REAL16, double);
91 CREATE_MPI_DATATYPE_NULL(MPI_COMPLEX8);
92 CREATE_MPI_DATATYPE_NULL(MPI_COMPLEX16);
93 CREATE_MPI_DATATYPE_NULL(MPI_COMPLEX32);
94 CREATE_MPI_DATATYPE(MPI_INTEGER1, int);
95 CREATE_MPI_DATATYPE(MPI_INTEGER2, int16_t);
96 CREATE_MPI_DATATYPE(MPI_INTEGER4, int32_t);
97 CREATE_MPI_DATATYPE(MPI_INTEGER8, int64_t);
98 CREATE_MPI_DATATYPE(MPI_INTEGER16, integer128_t);
99
100 CREATE_MPI_DATATYPE(MPI_LONG_DOUBLE_INT, long_double_int);
101
102 CREATE_MPI_DATATYPE_NULL(MPI_UB);
103 CREATE_MPI_DATATYPE_NULL(MPI_LB);
104 CREATE_MPI_DATATYPE(MPI_PACKED, char);
105 // Internal use only
106 CREATE_MPI_DATATYPE(MPI_PTR, void*);
107
108 namespace simgrid{
109 namespace smpi{
110
111 Datatype::Datatype(int size,MPI_Aint lb, MPI_Aint ub, int flags) : name_(nullptr), size_(size), lb_(lb), ub_(ub), flags_(flags), attributes_(nullptr), in_use_(1){
112 #if HAVE_MC
113   if(MC_is_active())
114     MC_ignore(&(in_use_), sizeof(in_use_));
115 #endif
116 }
117
118 //for predefined types, so in_use = 0.
119 Datatype::Datatype(char* name, int size,MPI_Aint lb, MPI_Aint ub, int flags) : name_(name), size_(size), lb_(lb), ub_(ub), flags_(flags), attributes_(nullptr), in_use_(0){
120 #if HAVE_MC
121   if(MC_is_active())
122     MC_ignore(&(in_use_), sizeof(in_use_));
123 #endif
124 }
125
126 Datatype::Datatype(Datatype *datatype, int* ret) : name_(nullptr), lb_(datatype->lb_), ub_(datatype->ub_), flags_(datatype->flags_), attributes_(nullptr), in_use_(1)
127 {
128   flags_ &= ~DT_FLAG_PREDEFINED;
129   *ret = MPI_SUCCESS;
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 = smpi_type_keyvals.at(atoi(key));
141       if (elem != nullptr && elem->copy_fn != MPI_NULL_COPY_FN) {
142         *ret = elem->copy_fn(datatype, atoi(key), nullptr, value_in, &value_out, &flag);
143         if (*ret != MPI_SUCCESS) {
144           xbt_dict_cursor_free(&cursor);
145           break;
146         }
147         if (flag)
148           xbt_dict_set_ext(attributes_, key, sizeof(int), value_out, nullptr);
149       }
150     }
151   }
152 }
153
154 Datatype::~Datatype(){
155   xbt_assert(in_use_ >= 0);
156
157   if(flags_ & DT_FLAG_PREDEFINED)
158     return;
159
160   //if still used, mark for deletion
161   if(in_use_!=0){
162       flags_ |=DT_FLAG_DESTROYED;
163       return;
164   }
165
166   if(attributes_ !=nullptr){
167     xbt_dict_cursor_t cursor = nullptr;
168     char* key;
169     void * value;
170     int flag;
171     xbt_dict_foreach(attributes_, cursor, key, value){
172       smpi_type_key_elem elem = smpi_type_keyvals.at(atoi(key));
173       if(elem!=nullptr && elem->delete_fn!=nullptr)
174         elem->delete_fn(this,*key, value, &flag);
175     }
176     xbt_dict_free(&attributes_);
177   }
178
179   xbt_free(name_);
180 }
181
182
183 void Datatype::use(){
184
185   in_use_++;
186
187 #if HAVE_MC
188   if(MC_is_active())
189     MC_ignore(&(in_use_), sizeof(in_use_));
190 #endif
191 }
192
193 void Datatype::unuse()
194 {
195   if (in_use_ > 0)
196     in_use_--;
197
198   if (in_use_ == 0)
199     this->~Datatype();
200
201 #if HAVE_MC
202   if(MC_is_active())
203     MC_ignore(&(in_use_), sizeof(in_use_));
204 #endif
205 }
206
207 void Datatype::commit()
208 {
209   flags_ |= DT_FLAG_COMMITED;
210 }
211
212
213 bool Datatype::is_valid(){
214   return (flags_ & DT_FLAG_COMMITED);
215 }
216
217 size_t Datatype::size(){
218   return size_;
219 }
220
221 int Datatype::flags(){
222   return flags_;
223 }
224
225 void Datatype::addflag(int flag){
226   flags_ &= flag;
227 }
228
229 MPI_Aint Datatype::lb(){
230   return lb_;
231 }
232
233 MPI_Aint Datatype::ub(){
234   return ub_;
235 }
236
237 char* Datatype::name(){
238   return name_;
239 }
240
241
242 int Datatype::extent(MPI_Aint * lb, MPI_Aint * extent){
243   *lb = lb_;
244   *extent = ub_ - lb_;
245   return MPI_SUCCESS;
246 }
247
248 MPI_Aint Datatype::get_extent(){
249   return ub_ - lb_;
250 }
251
252 void Datatype::get_name(char* name, int* length){
253   *length = strlen(name_);
254   strncpy(name, name_, *length+1);
255 }
256
257 void Datatype::set_name(char* name){
258   if(name_!=nullptr &&  (flags_ & DT_FLAG_PREDEFINED) == 0)
259     xbt_free(name_);
260   name_ = xbt_strdup(name);
261 }
262
263 int Datatype::attr_delete(int keyval){
264   smpi_type_key_elem elem = smpi_type_keyvals.at(keyval);
265   if(elem==nullptr)
266     return MPI_ERR_ARG;
267   if(elem->delete_fn!=MPI_NULL_DELETE_FN){
268     void * value = nullptr;
269     int flag;
270     if(this->attr_get(keyval, &value, &flag)==MPI_SUCCESS){
271       int ret = elem->delete_fn(this, keyval, value, &flag);
272       if(ret!=MPI_SUCCESS) 
273         return ret;
274     }
275   }  
276   if(attributes_==nullptr)
277     return MPI_ERR_ARG;
278
279   xbt_dict_remove_ext(attributes_, reinterpret_cast<const char*>(&keyval), sizeof(int));
280   return MPI_SUCCESS;
281 }
282
283
284 int Datatype::attr_get(int keyval, void* attr_value, int* flag){
285   smpi_type_key_elem elem = smpi_type_keyvals.at(keyval);
286   if(elem==nullptr)
287     return MPI_ERR_ARG;
288   if(attributes_==nullptr){
289     *flag=0;
290     return MPI_SUCCESS;
291   }
292   try {
293     *static_cast<void**>(attr_value) = xbt_dict_get_ext(attributes_, reinterpret_cast<const char*>(&keyval), sizeof(int));
294     *flag=1;
295   }
296   catch (xbt_ex& ex) {
297     *flag=0;
298   }
299   return MPI_SUCCESS;
300 }
301
302 int Datatype::attr_put(int keyval, void* attr_value){
303   smpi_type_key_elem elem = smpi_type_keyvals.at(keyval);
304   if(elem==nullptr)
305     return MPI_ERR_ARG;
306   int flag;
307   void* value = nullptr;
308   this->attr_get(keyval, &value, &flag);
309   if(flag!=0 && elem->delete_fn!=MPI_NULL_DELETE_FN){
310     int ret = elem->delete_fn(this, keyval, value, &flag);
311     if(ret!=MPI_SUCCESS) 
312       return ret;
313   }
314   if(attributes_==nullptr)
315     attributes_ = xbt_dict_new_homogeneous(nullptr);
316
317   xbt_dict_set_ext(attributes_, reinterpret_cast<const char*>(&keyval), sizeof(int), attr_value, nullptr);
318   return MPI_SUCCESS;
319 }
320
321 int Datatype::keyval_create(MPI_Type_copy_attr_function* copy_fn, MPI_Type_delete_attr_function* delete_fn, int* keyval, void* extra_state){
322
323   smpi_type_key_elem value = (smpi_type_key_elem) xbt_new0(s_smpi_mpi_type_key_elem_t,1);
324
325   value->copy_fn=copy_fn;
326   value->delete_fn=delete_fn;
327
328   *keyval = type_keyval_id;
329   smpi_type_keyvals.insert({*keyval, value});
330   type_keyval_id++;
331   return MPI_SUCCESS;
332 }
333
334 int Datatype::keyval_free(int* keyval){
335   smpi_type_key_elem elem = smpi_type_keyvals.at(*keyval);
336   if(elem==0){
337     return MPI_ERR_ARG;
338   }
339   smpi_type_keyvals.erase(*keyval);
340   xbt_free(elem);
341   return MPI_SUCCESS;
342 }
343
344
345 int Datatype::pack(void* inbuf, int incount, void* outbuf, int outcount, int* position,MPI_Comm comm){
346   if (outcount - *position < incount*static_cast<int>(size_))
347     return MPI_ERR_BUFFER;
348   Datatype::copy(inbuf, incount, this, static_cast<char*>(outbuf) + *position, outcount, MPI_CHAR);
349   *position += incount * size_;
350   return MPI_SUCCESS;
351 }
352
353 int Datatype::unpack(void* inbuf, int insize, int* position, void* outbuf, int outcount,MPI_Comm comm){
354   if (outcount*(int)size_> insize)
355     return MPI_ERR_BUFFER;
356   Datatype::copy(static_cast<char*>(inbuf) + *position, insize, MPI_CHAR, outbuf, outcount, this);
357   *position += outcount * size_;
358   return MPI_SUCCESS;
359 }
360
361
362 int Datatype::copy(void *sendbuf, int sendcount, MPI_Datatype sendtype,
363                        void *recvbuf, int recvcount, MPI_Datatype recvtype){
364   int count;
365   if(smpi_privatize_global_variables){
366     smpi_switch_data_segment(smpi_process_index());
367   }
368   /* First check if we really have something to do */
369   if (recvcount > 0 && recvbuf != sendbuf) {
370     sendcount *= sendtype->size();
371     recvcount *= recvtype->size();
372     count = sendcount < recvcount ? sendcount : recvcount;
373
374     if(!(sendtype->flags() & DT_FLAG_DERIVED) && !(recvtype->flags() & DT_FLAG_DERIVED)) {
375       if(!smpi_process_get_replaying()) 
376         memcpy(recvbuf, sendbuf, count);
377     }
378     else if (!(sendtype->flags() & DT_FLAG_DERIVED))
379     {
380       recvtype->unserialize( sendbuf, recvbuf, recvcount/recvtype->size(), MPI_REPLACE);
381     }
382     else if (!(recvtype->flags() & DT_FLAG_DERIVED))
383     {
384       sendtype->serialize(sendbuf, recvbuf, sendcount/sendtype->size());
385     }else{
386
387       void * buf_tmp = xbt_malloc(count);
388
389       sendtype->serialize( sendbuf, buf_tmp,count/sendtype->size());
390       recvtype->unserialize( buf_tmp, recvbuf,count/recvtype->size(), MPI_REPLACE);
391
392       xbt_free(buf_tmp);
393     }
394   }
395
396   return sendcount > recvcount ? MPI_ERR_TRUNCATE : MPI_SUCCESS;
397 }
398
399 //Default serialization method : memcpy.
400 void Datatype::serialize( void* noncontiguous_buf, void *contiguous_buf, int count){
401   char* contiguous_buf_char = static_cast<char*>(contiguous_buf);
402   char* noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf)+lb_;
403   memcpy(contiguous_buf_char, noncontiguous_buf_char, count*size_);
404
405 }
406
407 void Datatype::unserialize( void* contiguous_buf, void *noncontiguous_buf, int count, MPI_Op op){
408   char* contiguous_buf_char = static_cast<char*>(contiguous_buf);
409   char* noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf)+lb_;
410   int n=count;
411   if(op!=MPI_OP_NULL)
412     op->apply( contiguous_buf_char, noncontiguous_buf_char, &n, this);
413 }
414
415 int Datatype::create_contiguous(int count, MPI_Datatype old_type, MPI_Aint lb, MPI_Datatype* new_type){
416   if(old_type->flags_ & DT_FLAG_DERIVED){
417     //handle this case as a hvector with stride equals to the extent of the datatype
418     return create_hvector(count, 1, old_type->get_extent(), old_type, new_type);
419   }
420   if(count>0)
421     *new_type = new Type_Contiguous(count * old_type->size(), lb, lb + count * old_type->size(),
422                                    DT_FLAG_DERIVED, count, old_type);
423   else
424     *new_type = new Datatype(count * old_type->size(), lb, lb + count * old_type->size(),0);
425   return MPI_SUCCESS;
426 }
427
428 int Datatype::create_vector(int count, int block_length, int stride, MPI_Datatype old_type, MPI_Datatype* new_type)
429 {
430   int retval;
431   if (block_length<0) 
432     return MPI_ERR_ARG;
433   MPI_Aint lb = 0;
434   MPI_Aint ub = 0;
435   if(count>0){
436     lb=old_type->lb();
437     ub=((count-1)*stride+block_length-1)*old_type->get_extent()+old_type->ub();
438   }
439   if(old_type->flags() & DT_FLAG_DERIVED || stride != block_length){
440     *new_type = new Type_Vector(count * (block_length) * old_type->size(), lb, ub,
441                                    DT_FLAG_DERIVED, count, block_length, stride, old_type);
442     retval=MPI_SUCCESS;
443   }else{
444     /* in this situation the data are contiguous thus it's not required to serialize and unserialize it*/
445     *new_type = new Datatype(count * block_length * old_type->size(), 0, ((count -1) * stride + block_length)*
446                          old_type->size(), DT_FLAG_CONTIGUOUS);
447     retval=MPI_SUCCESS;
448   }
449   return retval;
450 }
451
452
453 int Datatype::create_hvector(int count, int block_length, MPI_Aint stride, MPI_Datatype old_type, MPI_Datatype* new_type)
454 {
455   int retval;
456   if (block_length<0) 
457     return MPI_ERR_ARG;
458   MPI_Aint lb = 0;
459   MPI_Aint ub = 0;
460   if(count>0){
461     lb=old_type->lb();
462     ub=((count-1)*stride)+(block_length-1)*old_type->get_extent()+old_type->ub();
463   }
464   if(old_type->flags() & DT_FLAG_DERIVED || stride != block_length*old_type->get_extent()){
465     *new_type = new Type_Hvector(count * (block_length) * old_type->size(), lb, ub,
466                                    DT_FLAG_DERIVED, count, block_length, stride, old_type);
467     retval=MPI_SUCCESS;
468   }else{
469     /* in this situation the data are contiguous thus it's not required to serialize and unserialize it*/
470     *new_type = new Datatype(count * block_length * old_type->size(), 0, count * block_length * old_type->size(), DT_FLAG_CONTIGUOUS);
471     retval=MPI_SUCCESS;
472   }
473   return retval;
474 }
475
476 int Datatype::create_indexed(int count, int* block_lengths, int* indices, MPI_Datatype old_type, MPI_Datatype* new_type){
477   int size = 0;
478   bool contiguous=true;
479   MPI_Aint lb = 0;
480   MPI_Aint ub = 0;
481   if(count>0){
482     lb=indices[0]*old_type->get_extent();
483     ub=indices[0]*old_type->get_extent() + block_lengths[0]*old_type->ub();
484   }
485
486   for (int i = 0; i < count; i++) {
487     if (block_lengths[i] < 0)
488       return MPI_ERR_ARG;
489     size += block_lengths[i];
490
491     if(indices[i]*old_type->get_extent()+old_type->lb()<lb)
492       lb = indices[i]*old_type->get_extent()+old_type->lb();
493     if(indices[i]*old_type->get_extent()+block_lengths[i]*old_type->ub()>ub)
494       ub = indices[i]*old_type->get_extent()+block_lengths[i]*old_type->ub();
495
496     if ( (i< count -1) && (indices[i]+block_lengths[i] != indices[i+1]) )
497       contiguous=false;
498   }
499   if(old_type->flags_ & DT_FLAG_DERIVED)
500     contiguous=false;
501
502   if(!contiguous){
503     *new_type = new Type_Indexed(size * old_type->size(),lb,ub,
504                                  DT_FLAG_DERIVED|DT_FLAG_DATA, count, block_lengths, indices, old_type);
505   }else{
506     Datatype::create_contiguous(size, old_type, lb, new_type);
507   }
508   return MPI_SUCCESS;
509 }
510
511 int Datatype::create_hindexed(int count, int* block_lengths, MPI_Aint* indices, MPI_Datatype old_type, MPI_Datatype* new_type){
512   int size = 0;
513   bool contiguous=true;
514   MPI_Aint lb = 0;
515   MPI_Aint ub = 0;
516   if(count>0){
517     lb=indices[0] + old_type->lb();
518     ub=indices[0] + block_lengths[0]*old_type->ub();
519   }
520   for (int i = 0; i < count; i++) {
521     if (block_lengths[i] < 0)
522       return MPI_ERR_ARG;
523     size += block_lengths[i];
524
525     if(indices[i]+old_type->lb()<lb) 
526       lb = indices[i]+old_type->lb();
527     if(indices[i]+block_lengths[i]*old_type->ub()>ub) 
528       ub = indices[i]+block_lengths[i]*old_type->ub();
529
530     if ( (i< count -1) && (indices[i]+block_lengths[i]*(static_cast<int>(old_type->size())) != indices[i+1]) )
531       contiguous=false;
532   }
533   if (old_type->flags_ & DT_FLAG_DERIVED || lb!=0)
534     contiguous=false;
535
536   if(!contiguous){
537     *new_type = new Type_Hindexed(size * old_type->size(),lb,ub,
538                                    DT_FLAG_DERIVED|DT_FLAG_DATA, count, block_lengths, indices, old_type);
539   }else{
540     Datatype::create_contiguous(size, old_type, lb, new_type);
541   }
542   return MPI_SUCCESS;
543 }
544
545 int Datatype::create_struct(int count, int* block_lengths, MPI_Aint* indices, MPI_Datatype* old_types, MPI_Datatype* new_type){
546   size_t size = 0;
547   bool contiguous=true;
548   size = 0;
549   MPI_Aint lb = 0;
550   MPI_Aint ub = 0;
551   if(count>0){
552     lb=indices[0] + old_types[0]->lb();
553     ub=indices[0] + block_lengths[0]*old_types[0]->ub();
554   }
555   bool forced_lb=false;
556   bool forced_ub=false;
557   for (int i = 0; i < count; i++) {
558     if (block_lengths[i]<0)
559       return MPI_ERR_ARG;
560     if (old_types[i]->flags_ & DT_FLAG_DERIVED)
561       contiguous=false;
562
563     size += block_lengths[i]*old_types[i]->size();
564     if (old_types[i]==MPI_LB){
565       lb=indices[i];
566       forced_lb=true;
567     }
568     if (old_types[i]==MPI_UB){
569       ub=indices[i];
570       forced_ub=true;
571     }
572
573     if(!forced_lb && indices[i]+old_types[i]->lb()<lb) 
574       lb = indices[i];
575     if(!forced_ub &&  indices[i]+block_lengths[i]*old_types[i]->ub()>ub)
576       ub = indices[i]+block_lengths[i]*old_types[i]->ub();
577
578     if ( (i< count -1) && (indices[i]+block_lengths[i]*static_cast<int>(old_types[i]->size()) != indices[i+1]) )
579       contiguous=false;
580   }
581   if(!contiguous){
582     *new_type = new Type_Struct(size, lb,ub, DT_FLAG_DERIVED|DT_FLAG_DATA, 
583                                 count, block_lengths, indices, old_types);
584   }else{
585     Datatype::create_contiguous(size, MPI_CHAR, lb, new_type);
586   }
587   return MPI_SUCCESS;
588 }
589
590
591 }
592 }
593