Logo AND Algorithmique Numérique Distribuée

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