Logo AND Algorithmique Numérique Distribuée

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