Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
3c04c85e391a907ab020dac7e02b6015e0c00d2a
[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_DATATYPE_NULL, -1);
80 CREATE_MPI_DATATYPE_NULL(MPI_COMPLEX8, 42);
81 CREATE_MPI_DATATYPE_NULL(MPI_COMPLEX16, 43);
82 CREATE_MPI_DATATYPE_NULL(MPI_COMPLEX32, 44);
83 CREATE_MPI_DATATYPE(MPI_INTEGER1, 45, int);
84 CREATE_MPI_DATATYPE(MPI_INTEGER2, 46, int16_t);
85 CREATE_MPI_DATATYPE(MPI_INTEGER4, 47, int32_t);
86 CREATE_MPI_DATATYPE(MPI_INTEGER8, 48, int64_t);
87 CREATE_MPI_DATATYPE(MPI_INTEGER16, 49, integer128_t);
88
89 CREATE_MPI_DATATYPE(MPI_LONG_DOUBLE_INT, 50, long_double_int);
90
91 CREATE_MPI_DATATYPE_NULL(MPI_UB, 51);
92 CREATE_MPI_DATATYPE_NULL(MPI_LB, 52);
93 CREATE_MPI_DATATYPE(MPI_PACKED, 53, char);
94 // Internal use only
95 CREATE_MPI_DATATYPE(MPI_PTR, 54, void*);
96
97 namespace simgrid{
98 namespace smpi{
99
100 std::unordered_map<int, smpi_key_elem> Datatype::keyvals_; // required by the Keyval class implementation
101 int Datatype::keyval_id_=0; // required by the Keyval class implementation
102 Datatype::Datatype(int ident, int size, MPI_Aint lb, MPI_Aint ub, int flags) : Datatype(size, lb, ub, flags)
103 {
104   id = std::to_string(ident);
105 }
106 Datatype::Datatype(int size,MPI_Aint lb, MPI_Aint ub, int flags) : name_(nullptr), size_(size), lb_(lb), ub_(ub), flags_(flags), refcount_(1){
107 #if SIMGRID_HAVE_MC
108   if(MC_is_active())
109     MC_ignore(&(refcount_), sizeof(refcount_));
110 #endif
111 }
112
113 //for predefined types, so in_use = 0.
114 Datatype::Datatype(char* name, int ident, int size, MPI_Aint lb, MPI_Aint ub, int flags)
115     : name_(name), id(std::to_string(ident)), size_(size), lb_(lb), ub_(ub), flags_(flags), refcount_(0)
116 {
117   id2type_lookup.insert({id, this});
118 #if SIMGRID_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_), refcount_(1)
125 {
126   flags_ &= ~DT_FLAG_PREDEFINED;
127   *ret = MPI_SUCCESS;
128   if(datatype->name_)
129     name_ = xbt_strdup(datatype->name_);
130
131   if (not datatype->attributes()->empty()) {
132     int flag;
133     void* value_out;
134     for(auto it = datatype->attributes()->begin(); it != datatype->attributes()->end(); it++){
135       smpi_key_elem elem = keyvals_.at((*it).first);
136
137       if (elem != nullptr && elem->copy_fn.type_copy_fn != MPI_NULL_COPY_FN) {
138         *ret = elem->copy_fn.type_copy_fn(datatype, (*it).first, nullptr, (*it).second, &value_out, &flag);
139         if (*ret != MPI_SUCCESS) {
140           break;
141         }
142         if (flag){
143           elem->refcount++;
144           attributes()->insert({(*it).first, value_out});
145         }
146       }
147     }
148   }
149 }
150
151 Datatype::~Datatype(){
152   xbt_assert(refcount_ >= 0);
153
154   if(flags_ & DT_FLAG_PREDEFINED)
155     return;
156
157   //if still used, mark for deletion
158   if(refcount_!=0){
159       flags_ |=DT_FLAG_DESTROYED;
160       return;
161   }
162
163   cleanup_attr<Datatype>();
164
165   xbt_free(name_);
166 }
167
168
169 void Datatype::ref(){
170
171   refcount_++;
172
173 #if SIMGRID_HAVE_MC
174   if(MC_is_active())
175     MC_ignore(&(refcount_), sizeof(refcount_));
176 #endif
177 }
178
179 void Datatype::unref(MPI_Datatype datatype)
180 {
181   if (datatype->refcount_ > 0)
182     datatype->refcount_--;
183
184   if (datatype->refcount_ == 0 && not(datatype->flags_ & DT_FLAG_PREDEFINED))
185     delete datatype;
186
187 #if SIMGRID_HAVE_MC
188   if(MC_is_active())
189     MC_ignore(&(datatype->refcount_), sizeof(datatype->refcount_));
190 #endif
191 }
192
193 void Datatype::commit()
194 {
195   flags_ |= DT_FLAG_COMMITED;
196 }
197
198 bool Datatype::is_valid(){
199   return (flags_ & DT_FLAG_COMMITED);
200 }
201
202 bool Datatype::is_basic()
203 {
204   return (flags_ & DT_FLAG_BASIC);
205 }
206
207 const char* Datatype::encode(MPI_Datatype dt)
208 {
209   return dt->id.c_str();
210 }
211
212 MPI_Datatype Datatype::decode(std::string datatype_id)
213 {
214   return id2type_lookup.find(datatype_id)->second;
215 }
216
217 bool Datatype::is_replayable()
218 {
219   return ((this==MPI_BYTE)||(this==MPI_DOUBLE)||(this==MPI_INT)||
220           (this==MPI_CHAR)||(this==MPI_SHORT)||(this==MPI_LONG)||(this==MPI_FLOAT));
221 }
222
223 size_t Datatype::size(){
224   return size_;
225 }
226
227 int Datatype::flags(){
228   return flags_;
229 }
230
231 int Datatype::refcount(){
232   return refcount_;
233 }
234
235 void Datatype::addflag(int flag){
236   flags_ &= flag;
237 }
238
239 MPI_Aint Datatype::lb(){
240   return lb_;
241 }
242
243 MPI_Aint Datatype::ub(){
244   return ub_;
245 }
246
247 char* Datatype::name(){
248   return name_;
249 }
250
251
252 int Datatype::extent(MPI_Aint * lb, MPI_Aint * extent){
253   *lb = lb_;
254   *extent = ub_ - lb_;
255   return MPI_SUCCESS;
256 }
257
258 MPI_Aint Datatype::get_extent(){
259   return ub_ - lb_;
260 }
261
262 void Datatype::get_name(char* name, int* length){
263   *length = strlen(name_);
264   strncpy(name, name_, *length+1);
265 }
266
267 void Datatype::set_name(char* name){
268   if(name_!=nullptr &&  (flags_ & DT_FLAG_PREDEFINED) == 0)
269     xbt_free(name_);
270   name_ = xbt_strdup(name);
271 }
272
273 int Datatype::pack(void* inbuf, int incount, void* outbuf, int outcount, int* position,MPI_Comm comm){
274   if (outcount - *position < incount*static_cast<int>(size_))
275     return MPI_ERR_BUFFER;
276   Datatype::copy(inbuf, incount, this, static_cast<char*>(outbuf) + *position, outcount, MPI_CHAR);
277   *position += incount * size_;
278   return MPI_SUCCESS;
279 }
280
281 int Datatype::unpack(void* inbuf, int insize, int* position, void* outbuf, int outcount,MPI_Comm comm){
282   if (outcount*static_cast<int>(size_)> insize)
283     return MPI_ERR_BUFFER;
284   Datatype::copy(static_cast<char*>(inbuf) + *position, insize, MPI_CHAR, outbuf, outcount, this);
285   *position += outcount * size_;
286   return MPI_SUCCESS;
287 }
288
289
290 int Datatype::copy(void *sendbuf, int sendcount, MPI_Datatype sendtype,
291                        void *recvbuf, int recvcount, MPI_Datatype recvtype){
292
293 // FIXME Handle the case of a partial shared malloc.
294
295   if (smpi_privatize_global_variables == SmpiPrivStrategies::MMAP) {
296     smpi_switch_data_segment(simgrid::s4u::Actor::self());
297   }
298   /* First check if we really have something to do */
299   if (recvcount > 0 && recvbuf != sendbuf) {
300     sendcount *= sendtype->size();
301     recvcount *= recvtype->size();
302     int count = sendcount < recvcount ? sendcount : recvcount;
303
304     if (not(sendtype->flags() & DT_FLAG_DERIVED) && not(recvtype->flags() & DT_FLAG_DERIVED)) {
305       if (not smpi_process()->replaying())
306         memcpy(recvbuf, sendbuf, count);
307     } else if (not(sendtype->flags() & DT_FLAG_DERIVED)) {
308       recvtype->unserialize(sendbuf, recvbuf, count / recvtype->size(), MPI_REPLACE);
309     } else if (not(recvtype->flags() & DT_FLAG_DERIVED)) {
310       sendtype->serialize(sendbuf, recvbuf, count / sendtype->size());
311     }else{
312
313       void * buf_tmp = xbt_malloc(count);
314
315       sendtype->serialize( sendbuf, buf_tmp,count/sendtype->size());
316       recvtype->unserialize( buf_tmp, recvbuf,count/recvtype->size(), MPI_REPLACE);
317
318       xbt_free(buf_tmp);
319     }
320   }
321
322   return sendcount > recvcount ? MPI_ERR_TRUNCATE : MPI_SUCCESS;
323 }
324
325 //Default serialization method : memcpy.
326 void Datatype::serialize( void* noncontiguous_buf, void *contiguous_buf, int count){
327   char* contiguous_buf_char = static_cast<char*>(contiguous_buf);
328   char* noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf)+lb_;
329   memcpy(contiguous_buf_char, noncontiguous_buf_char, count*size_);
330
331 }
332
333 void Datatype::unserialize( void* contiguous_buf, void *noncontiguous_buf, int count, MPI_Op op){
334   char* contiguous_buf_char = static_cast<char*>(contiguous_buf);
335   char* noncontiguous_buf_char = static_cast<char*>(noncontiguous_buf)+lb_;
336   int n=count;
337   if(op!=MPI_OP_NULL)
338     op->apply( contiguous_buf_char, noncontiguous_buf_char, &n, this);
339 }
340
341 int Datatype::create_contiguous(int count, MPI_Datatype old_type, MPI_Aint lb, MPI_Datatype* new_type){
342   if(old_type->flags_ & DT_FLAG_DERIVED){
343     //handle this case as a hvector with stride equals to the extent of the datatype
344     return create_hvector(count, 1, old_type->get_extent(), old_type, new_type);
345   }
346   if(count>0)
347     *new_type = new Type_Contiguous(count * old_type->size(), lb, lb + count * old_type->size(),
348                                    DT_FLAG_DERIVED, count, old_type);
349   else
350     *new_type = new Datatype(count * old_type->size(), lb, lb + count * old_type->size(),0);
351   return MPI_SUCCESS;
352 }
353
354 int Datatype::create_vector(int count, int block_length, int stride, MPI_Datatype old_type, MPI_Datatype* new_type)
355 {
356   int retval;
357   if (block_length<0)
358     return MPI_ERR_ARG;
359   MPI_Aint lb = 0;
360   MPI_Aint ub = 0;
361   if(count>0){
362     lb=old_type->lb();
363     ub=((count-1)*stride+block_length-1)*old_type->get_extent()+old_type->ub();
364   }
365   if(old_type->flags() & DT_FLAG_DERIVED || stride != block_length){
366     *new_type = new Type_Vector(count * (block_length) * old_type->size(), lb, ub,
367                                    DT_FLAG_DERIVED, count, block_length, stride, old_type);
368     retval=MPI_SUCCESS;
369   }else{
370     /* in this situation the data are contiguous thus it's not required to serialize and unserialize it*/
371     *new_type = new Datatype(count * block_length * old_type->size(), 0, ((count -1) * stride + block_length)*
372                          old_type->size(), DT_FLAG_CONTIGUOUS);
373     retval=MPI_SUCCESS;
374   }
375   return retval;
376 }
377
378
379 int Datatype::create_hvector(int count, int block_length, MPI_Aint stride, MPI_Datatype old_type, MPI_Datatype* new_type)
380 {
381   int retval;
382   if (block_length<0)
383     return MPI_ERR_ARG;
384   MPI_Aint lb = 0;
385   MPI_Aint ub = 0;
386   if(count>0){
387     lb=old_type->lb();
388     ub=((count-1)*stride)+(block_length-1)*old_type->get_extent()+old_type->ub();
389   }
390   if(old_type->flags() & DT_FLAG_DERIVED || stride != block_length*old_type->get_extent()){
391     *new_type = new Type_Hvector(count * (block_length) * old_type->size(), lb, ub,
392                                    DT_FLAG_DERIVED, count, block_length, stride, old_type);
393     retval=MPI_SUCCESS;
394   }else{
395     /* in this situation the data are contiguous thus it's not required to serialize and unserialize it*/
396     *new_type = new Datatype(count * block_length * old_type->size(), 0, count * block_length * old_type->size(), DT_FLAG_CONTIGUOUS);
397     retval=MPI_SUCCESS;
398   }
399   return retval;
400 }
401
402 int Datatype::create_indexed(int count, int* block_lengths, int* indices, MPI_Datatype old_type, MPI_Datatype* new_type){
403   int size = 0;
404   bool contiguous=true;
405   MPI_Aint lb = 0;
406   MPI_Aint ub = 0;
407   if(count>0){
408     lb=indices[0]*old_type->get_extent();
409     ub=indices[0]*old_type->get_extent() + block_lengths[0]*old_type->ub();
410   }
411
412   for (int i = 0; i < count; i++) {
413     if (block_lengths[i] < 0)
414       return MPI_ERR_ARG;
415     size += block_lengths[i];
416
417     if(indices[i]*old_type->get_extent()+old_type->lb()<lb)
418       lb = indices[i]*old_type->get_extent()+old_type->lb();
419     if(indices[i]*old_type->get_extent()+block_lengths[i]*old_type->ub()>ub)
420       ub = indices[i]*old_type->get_extent()+block_lengths[i]*old_type->ub();
421
422     if ( (i< count -1) && (indices[i]+block_lengths[i] != indices[i+1]) )
423       contiguous=false;
424   }
425   if(old_type->flags_ & DT_FLAG_DERIVED)
426     contiguous=false;
427
428   if (not contiguous) {
429     *new_type = new Type_Indexed(size * old_type->size(),lb,ub,
430                                  DT_FLAG_DERIVED|DT_FLAG_DATA, count, block_lengths, indices, old_type);
431   }else{
432     Datatype::create_contiguous(size, old_type, lb, new_type);
433   }
434   return MPI_SUCCESS;
435 }
436
437 int Datatype::create_hindexed(int count, int* block_lengths, MPI_Aint* indices, MPI_Datatype old_type, MPI_Datatype* new_type){
438   int size = 0;
439   bool contiguous=true;
440   MPI_Aint lb = 0;
441   MPI_Aint ub = 0;
442   if(count>0){
443     lb=indices[0] + old_type->lb();
444     ub=indices[0] + block_lengths[0]*old_type->ub();
445   }
446   for (int i = 0; i < count; i++) {
447     if (block_lengths[i] < 0)
448       return MPI_ERR_ARG;
449     size += block_lengths[i];
450
451     if(indices[i]+old_type->lb()<lb)
452       lb = indices[i]+old_type->lb();
453     if(indices[i]+block_lengths[i]*old_type->ub()>ub)
454       ub = indices[i]+block_lengths[i]*old_type->ub();
455
456     if ( (i< count -1) && (indices[i]+block_lengths[i]*(static_cast<int>(old_type->size())) != indices[i+1]) )
457       contiguous=false;
458   }
459   if (old_type->flags_ & DT_FLAG_DERIVED || lb!=0)
460     contiguous=false;
461
462   if (not contiguous) {
463     *new_type = new Type_Hindexed(size * old_type->size(),lb,ub,
464                                    DT_FLAG_DERIVED|DT_FLAG_DATA, count, block_lengths, indices, old_type);
465   }else{
466     Datatype::create_contiguous(size, old_type, lb, new_type);
467   }
468   return MPI_SUCCESS;
469 }
470
471 int Datatype::create_struct(int count, int* block_lengths, MPI_Aint* indices, MPI_Datatype* old_types, MPI_Datatype* new_type){
472   size_t size = 0;
473   bool contiguous=true;
474   size = 0;
475   MPI_Aint lb = 0;
476   MPI_Aint ub = 0;
477   if(count>0){
478     lb=indices[0] + old_types[0]->lb();
479     ub=indices[0] + block_lengths[0]*old_types[0]->ub();
480   }
481   bool forced_lb=false;
482   bool forced_ub=false;
483   for (int i = 0; i < count; i++) {
484     if (block_lengths[i]<0)
485       return MPI_ERR_ARG;
486     if (old_types[i]->flags_ & DT_FLAG_DERIVED)
487       contiguous=false;
488
489     size += block_lengths[i]*old_types[i]->size();
490     if (old_types[i]==MPI_LB){
491       lb=indices[i];
492       forced_lb=true;
493     }
494     if (old_types[i]==MPI_UB){
495       ub=indices[i];
496       forced_ub=true;
497     }
498
499     if (not forced_lb && indices[i] + old_types[i]->lb() < lb)
500       lb = indices[i];
501     if (not forced_ub && indices[i] + block_lengths[i] * old_types[i]->ub() > ub)
502       ub = indices[i]+block_lengths[i]*old_types[i]->ub();
503
504     if ( (i< count -1) && (indices[i]+block_lengths[i]*static_cast<int>(old_types[i]->size()) != indices[i+1]) )
505       contiguous=false;
506   }
507   if (not contiguous) {
508     *new_type = new Type_Struct(size, lb,ub, DT_FLAG_DERIVED|DT_FLAG_DATA,
509                                 count, block_lengths, indices, old_types);
510   }else{
511     Datatype::create_contiguous(size, MPI_CHAR, lb, new_type);
512   }
513   return MPI_SUCCESS;
514 }
515
516 int Datatype::create_subarray(int ndims, int* array_of_sizes,
517                              int* array_of_subsizes, int* array_of_starts,
518                              int order, MPI_Datatype oldtype, MPI_Datatype *newtype){
519   MPI_Datatype tmp;
520
521   for (int i = 0; i < ndims; i++) {
522     if (array_of_subsizes[i] > array_of_sizes[i]){
523       XBT_WARN("subarray : array_of_subsizes > array_of_sizes for dim %d",i);
524       return MPI_ERR_ARG;
525     }
526     if (array_of_starts[i] + array_of_subsizes[i] > array_of_sizes[i]){
527       XBT_WARN("subarray : array_of_starts + array_of_subsizes > array_of_sizes for dim %d",i);
528       return MPI_ERR_ARG;
529     }
530   }
531
532   MPI_Aint extent = oldtype->get_extent();
533
534   int i;
535   int step;
536   int end;
537   if( order==MPI_ORDER_C ) {
538       i = ndims - 1;
539       step = -1;
540       end = -1;
541   } else {
542       i = 0;
543       step = 1;
544       end = ndims;
545   }
546
547   MPI_Aint size = (MPI_Aint)array_of_sizes[i] * (MPI_Aint)array_of_sizes[i+step];
548   MPI_Aint lb = (MPI_Aint)array_of_starts[i] + (MPI_Aint)array_of_starts[i+step] *(MPI_Aint)array_of_sizes[i];
549
550   create_vector( array_of_subsizes[i+step], array_of_subsizes[i], array_of_sizes[i],
551                                oldtype, newtype );
552
553   tmp = *newtype;
554
555   for( i += 2 * step; i != end; i += step ) {
556       create_hvector( array_of_subsizes[i], 1, size * extent,
557                                     tmp, newtype );
558       unref(tmp);
559       lb += size * array_of_starts[i];
560       size *= array_of_sizes[i];
561       tmp = *newtype;
562   }
563
564   MPI_Aint lbs[1] = {lb * extent};
565   int sizes [1]={1};
566   //handle LB and UB with a resized call
567   create_hindexed( 1, sizes, lbs, tmp, newtype);
568   unref(tmp);
569
570   tmp = *newtype;
571   create_resized(tmp, 0, extent, newtype);
572
573   unref(tmp);
574   return MPI_SUCCESS;
575 }
576
577 int Datatype::create_resized(MPI_Datatype oldtype,MPI_Aint lb, MPI_Aint extent, MPI_Datatype *newtype){
578   int blocks[3]         = {1, 1, 1};
579   MPI_Aint disps[3]     = {lb, 0, lb + extent};
580   MPI_Datatype types[3] = {MPI_LB, oldtype, MPI_UB};
581
582   *newtype = new simgrid::smpi::Type_Struct(oldtype->size(), lb, lb + extent, DT_FLAG_DERIVED, 3, blocks, disps, types);
583
584   (*newtype)->addflag(~DT_FLAG_COMMITED);
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