Logo AND Algorithmique Numérique Distribuée

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