Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
please sonar: specify that these functions are not to be moved in a namespace
[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 MPI_Datatype Datatype::null_id_=MPI_DATATYPE_NULL;
112
113 Datatype::Datatype(int size,MPI_Aint lb, MPI_Aint ub, int flags) : name_(nullptr), size_(size), lb_(lb), ub_(ub), flags_(flags), attributes_(nullptr), refcount_(1){
114 #if HAVE_MC
115   if(MC_is_active())
116     MC_ignore(&(refcount_), sizeof(refcount_));
117 #endif
118 }
119
120 //for predefined types, so in_use = 0.
121 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), refcount_(0){
122 #if HAVE_MC
123   if(MC_is_active())
124     MC_ignore(&(refcount_), sizeof(refcount_));
125 #endif
126 }
127
128 Datatype::Datatype(Datatype *datatype, int* ret) : name_(nullptr), lb_(datatype->lb_), ub_(datatype->ub_), flags_(datatype->flags_), attributes_(nullptr), refcount_(1)
129 {
130   flags_ &= ~DT_FLAG_PREDEFINED;
131   *ret = MPI_SUCCESS;
132   if(datatype->name_)
133     name_ = xbt_strdup(datatype->name_);
134   if(datatype->attributes_ !=nullptr){
135     attributes_ = xbt_dict_new_homogeneous(nullptr);
136     xbt_dict_cursor_t cursor = nullptr;
137     char* key;
138     int flag;
139     void* value_in;
140     void* value_out;
141     xbt_dict_foreach (datatype->attributes_, cursor, key, value_in) {
142       smpi_type_key_elem elem = smpi_type_keyvals.at(atoi(key));
143       if (elem != nullptr && elem->copy_fn != MPI_NULL_COPY_FN) {
144         *ret = elem->copy_fn(datatype, atoi(key), nullptr, value_in, &value_out, &flag);
145         if (*ret != MPI_SUCCESS) {
146           xbt_dict_cursor_free(&cursor);
147           break;
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(refcount_ >= 0);
158
159   if(flags_ & DT_FLAG_PREDEFINED)
160     return;
161
162   //if still used, mark for deletion
163   if(refcount_!=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 = smpi_type_keyvals.at(atoi(key));
175       if(elem!=nullptr && elem->delete_fn!=nullptr)
176         elem->delete_fn(this,*key, value, &flag);
177     }
178     xbt_dict_free(&attributes_);
179   }
180
181   xbt_free(name_);
182 }
183
184
185 void Datatype::ref(){
186
187   refcount_++;
188
189 #if HAVE_MC
190   if(MC_is_active())
191     MC_ignore(&(refcount_), sizeof(refcount_));
192 #endif
193 }
194
195 void Datatype::unref(MPI_Datatype datatype)
196 {
197   if (datatype->refcount_ > 0)
198     datatype->refcount_--;
199
200   if (datatype->refcount_ == 0  && !(datatype->flags_ & DT_FLAG_PREDEFINED))
201     delete datatype;
202
203 #if HAVE_MC
204   if(MC_is_active())
205     MC_ignore(&(datatype->refcount_), sizeof(datatype->refcount_));
206 #endif
207 }
208
209 void Datatype::commit()
210 {
211   flags_ |= DT_FLAG_COMMITED;
212 }
213
214
215 bool Datatype::is_valid(){
216   return (flags_ & DT_FLAG_COMMITED);
217 }
218
219 size_t Datatype::size(){
220   return size_;
221 }
222
223 int Datatype::flags(){
224   return flags_;
225 }
226
227 void Datatype::addflag(int flag){
228   flags_ &= flag;
229 }
230
231 MPI_Aint Datatype::lb(){
232   return lb_;
233 }
234
235 MPI_Aint Datatype::ub(){
236   return ub_;
237 }
238
239 char* Datatype::name(){
240   return name_;
241 }
242
243
244 int Datatype::extent(MPI_Aint * lb, MPI_Aint * extent){
245   *lb = lb_;
246   *extent = ub_ - lb_;
247   return MPI_SUCCESS;
248 }
249
250 MPI_Aint Datatype::get_extent(){
251   return ub_ - lb_;
252 }
253
254 void Datatype::get_name(char* name, int* length){
255   *length = strlen(name_);
256   strncpy(name, name_, *length+1);
257 }
258
259 void Datatype::set_name(char* name){
260   if(name_!=nullptr &&  (flags_ & DT_FLAG_PREDEFINED) == 0)
261     xbt_free(name_);
262   name_ = xbt_strdup(name);
263 }
264
265 int Datatype::attr_delete(int keyval){
266   smpi_type_key_elem elem = smpi_type_keyvals.at(keyval);
267   if(elem==nullptr)
268     return MPI_ERR_ARG;
269   if(elem->delete_fn!=MPI_NULL_DELETE_FN){
270     void * value = nullptr;
271     int flag;
272     if(this->attr_get(keyval, &value, &flag)==MPI_SUCCESS){
273       int ret = elem->delete_fn(this, keyval, value, &flag);
274       if(ret!=MPI_SUCCESS) 
275         return ret;
276     }
277   }  
278   if(attributes_==nullptr)
279     return MPI_ERR_ARG;
280
281   xbt_dict_remove_ext(attributes_, reinterpret_cast<const char*>(&keyval), sizeof(int));
282   return MPI_SUCCESS;
283 }
284
285
286 int Datatype::attr_get(int keyval, void* attr_value, int* flag){
287   smpi_type_key_elem elem = smpi_type_keyvals.at(keyval);
288   if(elem==nullptr)
289     return MPI_ERR_ARG;
290   if(attributes_==nullptr){
291     *flag=0;
292     return MPI_SUCCESS;
293   }
294   try {
295     *static_cast<void**>(attr_value) = xbt_dict_get_ext(attributes_, reinterpret_cast<const char*>(&keyval), sizeof(int));
296     *flag=1;
297   }
298   catch (xbt_ex& ex) {
299     *flag=0;
300   }
301   return MPI_SUCCESS;
302 }
303
304 int Datatype::attr_put(int keyval, void* attr_value){
305   smpi_type_key_elem elem = smpi_type_keyvals.at(keyval);
306   if(elem==nullptr)
307     return MPI_ERR_ARG;
308   int flag;
309   void* value = nullptr;
310   this->attr_get(keyval, &value, &flag);
311   if(flag!=0 && elem->delete_fn!=MPI_NULL_DELETE_FN){
312     int ret = elem->delete_fn(this, keyval, value, &flag);
313     if(ret!=MPI_SUCCESS) 
314       return ret;
315   }
316   if(attributes_==nullptr)
317     attributes_ = xbt_dict_new_homogeneous(nullptr);
318
319   xbt_dict_set_ext(attributes_, reinterpret_cast<const char*>(&keyval), sizeof(int), attr_value, nullptr);
320   return MPI_SUCCESS;
321 }
322
323 int Datatype::keyval_create(MPI_Type_copy_attr_function* copy_fn, MPI_Type_delete_attr_function* delete_fn, int* keyval, void* extra_state){
324
325   smpi_type_key_elem value = (smpi_type_key_elem) xbt_new0(s_smpi_mpi_type_key_elem_t,1);
326
327   value->copy_fn=copy_fn;
328   value->delete_fn=delete_fn;
329
330   *keyval = type_keyval_id;
331   smpi_type_keyvals.insert({*keyval, value});
332   type_keyval_id++;
333   return MPI_SUCCESS;
334 }
335
336 int Datatype::keyval_free(int* keyval){
337   smpi_type_key_elem elem = smpi_type_keyvals.at(*keyval);
338   if(elem==0){
339     return MPI_ERR_ARG;
340   }
341   smpi_type_keyvals.erase(*keyval);
342   xbt_free(elem);
343   return MPI_SUCCESS;
344 }
345
346
347 int Datatype::pack(void* inbuf, int incount, void* outbuf, int outcount, int* position,MPI_Comm comm){
348   if (outcount - *position < incount*static_cast<int>(size_))
349     return MPI_ERR_BUFFER;
350   Datatype::copy(inbuf, incount, this, static_cast<char*>(outbuf) + *position, outcount, MPI_CHAR);
351   *position += incount * size_;
352   return MPI_SUCCESS;
353 }
354
355 int Datatype::unpack(void* inbuf, int insize, int* position, void* outbuf, int outcount,MPI_Comm comm){
356   if (outcount*(int)size_> insize)
357     return MPI_ERR_BUFFER;
358   Datatype::copy(static_cast<char*>(inbuf) + *position, insize, MPI_CHAR, outbuf, outcount, this);
359   *position += outcount * size_;
360   return MPI_SUCCESS;
361 }
362
363
364 int Datatype::copy(void *sendbuf, int sendcount, MPI_Datatype sendtype,
365                        void *recvbuf, int recvcount, MPI_Datatype recvtype){
366   int count;
367   if(smpi_privatize_global_variables){
368     smpi_switch_data_segment(smpi_process_index());
369   }
370   /* First check if we really have something to do */
371   if (recvcount > 0 && recvbuf != sendbuf) {
372     sendcount *= sendtype->size();
373     recvcount *= recvtype->size();
374     count = sendcount < recvcount ? sendcount : recvcount;
375
376     if(!(sendtype->flags() & DT_FLAG_DERIVED) && !(recvtype->flags() & DT_FLAG_DERIVED)) {
377       if(!smpi_process_get_replaying()) 
378         memcpy(recvbuf, sendbuf, count);
379     }
380     else if (!(sendtype->flags() & DT_FLAG_DERIVED))
381     {
382       recvtype->unserialize( sendbuf, recvbuf, recvcount/recvtype->size(), MPI_REPLACE);
383     }
384     else if (!(recvtype->flags() & DT_FLAG_DERIVED))
385     {
386       sendtype->serialize(sendbuf, recvbuf, sendcount/sendtype->size());
387     }else{
388
389       void * buf_tmp = xbt_malloc(count);
390
391       sendtype->serialize( sendbuf, buf_tmp,count/sendtype->size());
392       recvtype->unserialize( buf_tmp, recvbuf,count/recvtype->size(), MPI_REPLACE);
393
394       xbt_free(buf_tmp);
395     }
396   }
397
398   return sendcount > recvcount ? MPI_ERR_TRUNCATE : MPI_SUCCESS;
399 }
400
401 //Default serialization method : memcpy.
402 void Datatype::serialize( void* noncontiguous_buf, void *contiguous_buf, int count){
403   char* contiguous_buf_char = static_cast<char*>(contiguous_buf);
404   char* noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf)+lb_;
405   memcpy(contiguous_buf_char, noncontiguous_buf_char, count*size_);
406
407 }
408
409 void Datatype::unserialize( void* contiguous_buf, void *noncontiguous_buf, int count, MPI_Op op){
410   char* contiguous_buf_char = static_cast<char*>(contiguous_buf);
411   char* noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf)+lb_;
412   int n=count;
413   if(op!=MPI_OP_NULL)
414     op->apply( contiguous_buf_char, noncontiguous_buf_char, &n, this);
415 }
416
417 int Datatype::create_contiguous(int count, MPI_Datatype old_type, MPI_Aint lb, MPI_Datatype* new_type){
418   if(old_type->flags_ & DT_FLAG_DERIVED){
419     //handle this case as a hvector with stride equals to the extent of the datatype
420     return create_hvector(count, 1, old_type->get_extent(), old_type, new_type);
421   }
422   if(count>0)
423     *new_type = new Type_Contiguous(count * old_type->size(), lb, lb + count * old_type->size(),
424                                    DT_FLAG_DERIVED, count, old_type);
425   else
426     *new_type = new Datatype(count * old_type->size(), lb, lb + count * old_type->size(),0);
427   return MPI_SUCCESS;
428 }
429
430 int Datatype::create_vector(int count, int block_length, int stride, MPI_Datatype old_type, MPI_Datatype* new_type)
431 {
432   int retval;
433   if (block_length<0) 
434     return MPI_ERR_ARG;
435   MPI_Aint lb = 0;
436   MPI_Aint ub = 0;
437   if(count>0){
438     lb=old_type->lb();
439     ub=((count-1)*stride+block_length-1)*old_type->get_extent()+old_type->ub();
440   }
441   if(old_type->flags() & DT_FLAG_DERIVED || stride != block_length){
442     *new_type = new Type_Vector(count * (block_length) * old_type->size(), lb, ub,
443                                    DT_FLAG_DERIVED, count, block_length, stride, old_type);
444     retval=MPI_SUCCESS;
445   }else{
446     /* in this situation the data are contiguous thus it's not required to serialize and unserialize it*/
447     *new_type = new Datatype(count * block_length * old_type->size(), 0, ((count -1) * stride + block_length)*
448                          old_type->size(), DT_FLAG_CONTIGUOUS);
449     retval=MPI_SUCCESS;
450   }
451   return retval;
452 }
453
454
455 int Datatype::create_hvector(int count, int block_length, MPI_Aint stride, MPI_Datatype old_type, MPI_Datatype* new_type)
456 {
457   int retval;
458   if (block_length<0) 
459     return MPI_ERR_ARG;
460   MPI_Aint lb = 0;
461   MPI_Aint ub = 0;
462   if(count>0){
463     lb=old_type->lb();
464     ub=((count-1)*stride)+(block_length-1)*old_type->get_extent()+old_type->ub();
465   }
466   if(old_type->flags() & DT_FLAG_DERIVED || stride != block_length*old_type->get_extent()){
467     *new_type = new Type_Hvector(count * (block_length) * old_type->size(), lb, ub,
468                                    DT_FLAG_DERIVED, count, block_length, stride, old_type);
469     retval=MPI_SUCCESS;
470   }else{
471     /* in this situation the data are contiguous thus it's not required to serialize and unserialize it*/
472     *new_type = new Datatype(count * block_length * old_type->size(), 0, count * block_length * old_type->size(), DT_FLAG_CONTIGUOUS);
473     retval=MPI_SUCCESS;
474   }
475   return retval;
476 }
477
478 int Datatype::create_indexed(int count, int* block_lengths, int* indices, MPI_Datatype old_type, MPI_Datatype* new_type){
479   int size = 0;
480   bool contiguous=true;
481   MPI_Aint lb = 0;
482   MPI_Aint ub = 0;
483   if(count>0){
484     lb=indices[0]*old_type->get_extent();
485     ub=indices[0]*old_type->get_extent() + block_lengths[0]*old_type->ub();
486   }
487
488   for (int i = 0; i < count; i++) {
489     if (block_lengths[i] < 0)
490       return MPI_ERR_ARG;
491     size += block_lengths[i];
492
493     if(indices[i]*old_type->get_extent()+old_type->lb()<lb)
494       lb = indices[i]*old_type->get_extent()+old_type->lb();
495     if(indices[i]*old_type->get_extent()+block_lengths[i]*old_type->ub()>ub)
496       ub = indices[i]*old_type->get_extent()+block_lengths[i]*old_type->ub();
497
498     if ( (i< count -1) && (indices[i]+block_lengths[i] != indices[i+1]) )
499       contiguous=false;
500   }
501   if(old_type->flags_ & DT_FLAG_DERIVED)
502     contiguous=false;
503
504   if(!contiguous){
505     *new_type = new Type_Indexed(size * old_type->size(),lb,ub,
506                                  DT_FLAG_DERIVED|DT_FLAG_DATA, count, block_lengths, indices, old_type);
507   }else{
508     Datatype::create_contiguous(size, old_type, lb, new_type);
509   }
510   return MPI_SUCCESS;
511 }
512
513 int Datatype::create_hindexed(int count, int* block_lengths, MPI_Aint* indices, MPI_Datatype old_type, MPI_Datatype* new_type){
514   int size = 0;
515   bool contiguous=true;
516   MPI_Aint lb = 0;
517   MPI_Aint ub = 0;
518   if(count>0){
519     lb=indices[0] + old_type->lb();
520     ub=indices[0] + block_lengths[0]*old_type->ub();
521   }
522   for (int i = 0; i < count; i++) {
523     if (block_lengths[i] < 0)
524       return MPI_ERR_ARG;
525     size += block_lengths[i];
526
527     if(indices[i]+old_type->lb()<lb) 
528       lb = indices[i]+old_type->lb();
529     if(indices[i]+block_lengths[i]*old_type->ub()>ub) 
530       ub = indices[i]+block_lengths[i]*old_type->ub();
531
532     if ( (i< count -1) && (indices[i]+block_lengths[i]*(static_cast<int>(old_type->size())) != indices[i+1]) )
533       contiguous=false;
534   }
535   if (old_type->flags_ & DT_FLAG_DERIVED || lb!=0)
536     contiguous=false;
537
538   if(!contiguous){
539     *new_type = new Type_Hindexed(size * old_type->size(),lb,ub,
540                                    DT_FLAG_DERIVED|DT_FLAG_DATA, count, block_lengths, indices, old_type);
541   }else{
542     Datatype::create_contiguous(size, old_type, lb, new_type);
543   }
544   return MPI_SUCCESS;
545 }
546
547 int Datatype::create_struct(int count, int* block_lengths, MPI_Aint* indices, MPI_Datatype* old_types, MPI_Datatype* new_type){
548   size_t size = 0;
549   bool contiguous=true;
550   size = 0;
551   MPI_Aint lb = 0;
552   MPI_Aint ub = 0;
553   if(count>0){
554     lb=indices[0] + old_types[0]->lb();
555     ub=indices[0] + block_lengths[0]*old_types[0]->ub();
556   }
557   bool forced_lb=false;
558   bool forced_ub=false;
559   for (int i = 0; i < count; i++) {
560     if (block_lengths[i]<0)
561       return MPI_ERR_ARG;
562     if (old_types[i]->flags_ & DT_FLAG_DERIVED)
563       contiguous=false;
564
565     size += block_lengths[i]*old_types[i]->size();
566     if (old_types[i]==MPI_LB){
567       lb=indices[i];
568       forced_lb=true;
569     }
570     if (old_types[i]==MPI_UB){
571       ub=indices[i];
572       forced_ub=true;
573     }
574
575     if(!forced_lb && indices[i]+old_types[i]->lb()<lb) 
576       lb = indices[i];
577     if(!forced_ub &&  indices[i]+block_lengths[i]*old_types[i]->ub()>ub)
578       ub = indices[i]+block_lengths[i]*old_types[i]->ub();
579
580     if ( (i< count -1) && (indices[i]+block_lengths[i]*static_cast<int>(old_types[i]->size()) != indices[i+1]) )
581       contiguous=false;
582   }
583   if(!contiguous){
584     *new_type = new Type_Struct(size, lb,ub, DT_FLAG_DERIVED|DT_FLAG_DATA, 
585                                 count, block_lengths, indices, old_types);
586   }else{
587     Datatype::create_contiguous(size, MPI_CHAR, lb, new_type);
588   }
589   return MPI_SUCCESS;
590 }
591
592 Datatype* Datatype::f2c(int id){
593   return static_cast<Datatype*>(F2C::f2c(id));
594 }
595
596
597 }
598 }
599