Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines for 2023.
[simgrid.git] / src / smpi / mpi / smpi_datatype.cpp
1 /* smpi_datatype.cpp -- MPI primitives to handle datatypes                  */
2 /* Copyright (c) 2009-2023. 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 "private.hpp"
8 #include "simgrid/modelchecker.h"
9 #include "smpi_datatype_derived.hpp"
10 #include "smpi_op.hpp"
11 #include "src/instr/instr_private.hpp"
12 #include "src/smpi/include/smpi_actor.hpp"
13
14 #include <algorithm>
15 #include <array>
16 #include <functional>
17 #include <string>
18 #include <complex>
19
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_datatype, smpi, "Logging specific to SMPI (datatype)");
21
22 static std::unordered_map<std::string, simgrid::smpi::Datatype*> id2type_lookup;
23
24 #define CREATE_MPI_DATATYPE(name, id, type, flag)                                                                            \
25   simgrid::smpi::Datatype _XBT_CONCAT(smpi_MPI_, name)((char*)"MPI_"#name, (id), sizeof(type), /* size */   \
26                                                          0,                                               /* lb */     \
27                                                          sizeof(type), /* ub = lb + size */                            \
28                                                          DT_FLAG_BASIC | flag /* flags */                                     \
29                                                          );
30
31 #define CREATE_MPI_DATATYPE_NULL(name, id)                                                                             \
32   simgrid::smpi::Datatype _XBT_CONCAT(smpi_MPI_, name)((char*)"MPI_"#name, (id), 0, /* size */              \
33                                                          0,                                    /* lb */                \
34                                                          0,                                    /* ub = lb + size */    \
35                                                          DT_FLAG_BASIC                         /* flags */             \
36                                                          );
37
38 // Predefined data types
39 CREATE_MPI_DATATYPE_NULL(DATATYPE_NULL, -1)
40 CREATE_MPI_DATATYPE(DOUBLE, 0, double, DT_FLAG_FP)
41 CREATE_MPI_DATATYPE(INT, 1, int, DT_FLAG_C_INTEGER)
42 CREATE_MPI_DATATYPE(CHAR, 2, char, DT_FLAG_C_INTEGER)
43 CREATE_MPI_DATATYPE(SHORT, 3, short, DT_FLAG_C_INTEGER)
44 CREATE_MPI_DATATYPE(LONG, 4, long, DT_FLAG_C_INTEGER)
45 CREATE_MPI_DATATYPE(FLOAT, 5, float, DT_FLAG_FP)
46 CREATE_MPI_DATATYPE(BYTE, 6, int8_t, DT_FLAG_BYTE)
47 CREATE_MPI_DATATYPE(LONG_LONG, 7, long long, DT_FLAG_C_INTEGER)
48 CREATE_MPI_DATATYPE(SIGNED_CHAR, 8, signed char, DT_FLAG_C_INTEGER)
49 CREATE_MPI_DATATYPE(UNSIGNED_CHAR, 9, unsigned char, DT_FLAG_C_INTEGER)
50 CREATE_MPI_DATATYPE(UNSIGNED_SHORT, 10, unsigned short, DT_FLAG_C_INTEGER)
51 CREATE_MPI_DATATYPE(UNSIGNED, 11, unsigned int, DT_FLAG_C_INTEGER)
52 CREATE_MPI_DATATYPE(UNSIGNED_LONG, 12, unsigned long, DT_FLAG_C_INTEGER)
53 CREATE_MPI_DATATYPE(UNSIGNED_LONG_LONG, 13, unsigned long long, DT_FLAG_C_INTEGER)
54 CREATE_MPI_DATATYPE(LONG_DOUBLE, 14, long double, DT_FLAG_FP)
55 CREATE_MPI_DATATYPE(WCHAR, 15, wchar_t, DT_FLAG_BASIC)
56 CREATE_MPI_DATATYPE(C_BOOL, 16, bool, DT_FLAG_LOGICAL)
57 CREATE_MPI_DATATYPE(INT8_T, 17, int8_t, DT_FLAG_C_INTEGER)
58 CREATE_MPI_DATATYPE(INT16_T, 18, int16_t, DT_FLAG_C_INTEGER)
59 CREATE_MPI_DATATYPE(INT32_T, 19, int32_t, DT_FLAG_C_INTEGER)
60 CREATE_MPI_DATATYPE(INT64_T, 20, int64_t, DT_FLAG_C_INTEGER)
61 CREATE_MPI_DATATYPE(UINT8_T, 21, uint8_t, DT_FLAG_C_INTEGER)
62 CREATE_MPI_DATATYPE(UINT16_T, 22, uint16_t, DT_FLAG_C_INTEGER)
63 CREATE_MPI_DATATYPE(UINT32_T, 23, uint32_t, DT_FLAG_C_INTEGER)
64 CREATE_MPI_DATATYPE(UINT64_T, 24, uint64_t, DT_FLAG_C_INTEGER)
65 CREATE_MPI_DATATYPE(C_FLOAT_COMPLEX, 25, float _Complex, DT_FLAG_COMPLEX)
66 CREATE_MPI_DATATYPE(C_DOUBLE_COMPLEX, 26, double _Complex, DT_FLAG_COMPLEX)
67 CREATE_MPI_DATATYPE(C_LONG_DOUBLE_COMPLEX, 27, long double _Complex, DT_FLAG_COMPLEX)
68 CREATE_MPI_DATATYPE(AINT, 28, MPI_Aint, DT_FLAG_MULTILANG)
69 CREATE_MPI_DATATYPE(OFFSET, 29, MPI_Offset, DT_FLAG_MULTILANG)
70
71 CREATE_MPI_DATATYPE(FLOAT_INT, 30, float_int, DT_FLAG_REDUCTION)
72 CREATE_MPI_DATATYPE(LONG_INT, 31, long_int, DT_FLAG_REDUCTION)
73 CREATE_MPI_DATATYPE(DOUBLE_INT, 32, double_int, DT_FLAG_REDUCTION)
74 CREATE_MPI_DATATYPE(SHORT_INT, 33, short_int, DT_FLAG_REDUCTION)
75 CREATE_MPI_DATATYPE(2INT, 34, int_int, DT_FLAG_REDUCTION)
76 CREATE_MPI_DATATYPE(2FLOAT, 35, float_float, DT_FLAG_REDUCTION)
77 CREATE_MPI_DATATYPE(2DOUBLE, 36, double_double, DT_FLAG_REDUCTION)
78 CREATE_MPI_DATATYPE(2LONG, 37, long_long, DT_FLAG_REDUCTION)
79
80 CREATE_MPI_DATATYPE(REAL, 38, float, DT_FLAG_FP)
81 CREATE_MPI_DATATYPE(REAL4, 39, float, DT_FLAG_FP)
82 CREATE_MPI_DATATYPE(REAL8, 40, double, DT_FLAG_FP)
83 CREATE_MPI_DATATYPE(REAL16, 41, long double, DT_FLAG_FP)
84 CREATE_MPI_DATATYPE(COMPLEX8, 42, float_float, DT_FLAG_COMPLEX)
85 CREATE_MPI_DATATYPE(COMPLEX16, 43, double_double, DT_FLAG_COMPLEX)
86 CREATE_MPI_DATATYPE(COMPLEX32, 44, double_double, DT_FLAG_COMPLEX)
87 CREATE_MPI_DATATYPE(INTEGER1, 45, int, DT_FLAG_F_INTEGER)
88 CREATE_MPI_DATATYPE(INTEGER2, 46, int16_t, DT_FLAG_F_INTEGER)
89 CREATE_MPI_DATATYPE(INTEGER4, 47, int32_t, DT_FLAG_F_INTEGER)
90 CREATE_MPI_DATATYPE(INTEGER8, 48, int64_t, DT_FLAG_F_INTEGER)
91 CREATE_MPI_DATATYPE(INTEGER16, 49, integer128_t, DT_FLAG_F_INTEGER)
92
93 CREATE_MPI_DATATYPE(LONG_DOUBLE_INT, 50, long_double_int, DT_FLAG_REDUCTION)
94 CREATE_MPI_DATATYPE(CXX_BOOL, 51, bool, DT_FLAG_LOGICAL)
95 CREATE_MPI_DATATYPE(CXX_FLOAT_COMPLEX, 52, std::complex<float>, DT_FLAG_COMPLEX)
96 CREATE_MPI_DATATYPE(CXX_DOUBLE_COMPLEX, 53, std::complex<double>, DT_FLAG_COMPLEX)
97 CREATE_MPI_DATATYPE(CXX_LONG_DOUBLE_COMPLEX, 54, std::complex<long double>, DT_FLAG_COMPLEX)
98
99 CREATE_MPI_DATATYPE_NULL(UB, 55)
100 CREATE_MPI_DATATYPE_NULL(LB, 56)
101 CREATE_MPI_DATATYPE(PACKED, 57, char, DT_FLAG_PREDEFINED)
102 // Internal use only
103 CREATE_MPI_DATATYPE(PTR, 58, void*, DT_FLAG_PREDEFINED)
104 CREATE_MPI_DATATYPE(COUNT, 59, long long, DT_FLAG_MULTILANG)
105 MPI_Datatype MPI_PTR = &smpi_MPI_PTR;
106
107 namespace simgrid::smpi {
108
109 std::unordered_map<int, smpi_key_elem> Datatype::keyvals_; // required by the Keyval class implementation
110 int Datatype::keyval_id_=0; // required by the Keyval class implementation
111 Datatype::Datatype(int ident, int size, MPI_Aint lb, MPI_Aint ub, int flags) : Datatype(size, lb, ub, flags)
112 {
113   id = std::to_string(ident);
114 }
115
116 Datatype::Datatype(int size, MPI_Aint lb, MPI_Aint ub, int flags) : size_(size), lb_(lb), ub_(ub), flags_(flags)
117 {
118   this->add_f();
119 #if SIMGRID_HAVE_MC
120   if(MC_is_active())
121     MC_ignore(&refcount_, sizeof refcount_);
122 #endif
123 }
124
125 // for predefined types, so refcount_ = 0.
126 Datatype::Datatype(const char* name, int ident, int size, MPI_Aint lb, MPI_Aint ub, int flags)
127     : name_(name), id(std::to_string(ident)), size_(size), lb_(lb), ub_(ub), flags_(flags), refcount_(0)
128 {
129   id2type_lookup.try_emplace(id, this);
130 #if SIMGRID_HAVE_MC
131   if(MC_is_active())
132     MC_ignore(&refcount_, sizeof refcount_);
133 #endif
134 }
135
136 Datatype::Datatype(Datatype* datatype, int* ret)
137     : size_(datatype->size_), lb_(datatype->lb_), ub_(datatype->ub_), flags_(datatype->flags_), duplicated_datatype_(datatype)
138 {
139   this->add_f();
140   datatype->ref();
141   *ret = this->copy_attrs(datatype);
142 }
143
144 Datatype::~Datatype()
145 {
146   xbt_assert(refcount_ >= 0);
147
148   if(flags_ & DT_FLAG_PREDEFINED)
149     return;
150   //prevent further usage
151   flags_ &= ~ DT_FLAG_COMMITED;
152   if(duplicated_datatype_ != MPI_DATATYPE_NULL)
153     unref(duplicated_datatype_);
154   F2C::free_f(this->f2c_id());
155   //if still used, mark for deletion
156   if(refcount_!=0){
157       flags_ |=DT_FLAG_DESTROYED;
158       return;
159   }
160   cleanup_attr<Datatype>();
161 }
162
163 int Datatype::copy_attrs(Datatype* datatype){
164   flags_ &= ~DT_FLAG_PREDEFINED;
165
166   set_contents(MPI_COMBINER_DUP, 0, nullptr, 0, nullptr, 1, &datatype);
167   for (auto const& [key, value] : datatype->attributes()) {
168     auto elem_it = keyvals_.find(key);
169     xbt_assert(elem_it != keyvals_.end(), "Keyval not found for Datatype: %d", key);
170
171     smpi_key_elem& elem = elem_it->second;
172     int ret             = MPI_SUCCESS;
173     int flag            = 0;
174     void* value_out     = nullptr;
175     if (elem.copy_fn.type_copy_fn == MPI_TYPE_DUP_FN) {
176       value_out = value;
177       flag      = 1;
178     } else if (elem.copy_fn.type_copy_fn != MPI_NULL_COPY_FN) {
179       ret = elem.copy_fn.type_copy_fn(datatype, key, elem.extra_state, value, &value_out, &flag);
180     }
181     if (ret != MPI_SUCCESS)
182       return ret;
183
184     if (elem.copy_fn.type_copy_fn_fort != MPI_NULL_COPY_FN) {
185       value_out = xbt_new(int, 1);
186       if (*(int*)*elem.copy_fn.type_copy_fn_fort == 1) { // MPI_TYPE_DUP_FN
187         memcpy(value_out, value, sizeof(int));
188         flag = 1;
189       } else { // not null, nor dup
190         elem.copy_fn.type_copy_fn_fort(datatype, key, elem.extra_state, value, value_out, &flag, &ret);
191       }
192       if (ret != MPI_SUCCESS) {
193         xbt_free(value_out);
194         return ret;
195       }
196     }
197     if (flag) {
198       elem.refcount++;
199       attributes().try_emplace(key, value_out);
200     }
201   }
202   return MPI_SUCCESS;
203 }
204
205 int Datatype::clone(MPI_Datatype* type){
206   int ret;
207   *type = new Datatype(this, &ret);
208   return ret;
209 }
210
211 void Datatype::ref()
212 {
213   refcount_++;
214
215 #if SIMGRID_HAVE_MC
216   if(MC_is_active())
217     MC_ignore(&refcount_, sizeof refcount_);
218 #endif
219 }
220
221 void Datatype::unref(MPI_Datatype datatype)
222 {
223   if (datatype->refcount_ > 0)
224     datatype->refcount_--;
225
226 #if SIMGRID_HAVE_MC
227   if(MC_is_active())
228     MC_ignore(&datatype->refcount_, sizeof datatype->refcount_);
229 #endif
230
231   if (datatype->refcount_ == 0 && not(datatype->flags_ & DT_FLAG_PREDEFINED))
232     delete datatype;
233 }
234
235 void Datatype::commit()
236 {
237   flags_ |= DT_FLAG_COMMITED;
238 }
239
240 bool Datatype::is_valid() const
241 {
242   return (flags_ & DT_FLAG_COMMITED);
243 }
244
245 bool Datatype::is_basic() const
246 {
247   return (flags_ & DT_FLAG_BASIC);
248 }
249
250 MPI_Datatype Datatype::decode(const std::string& datatype_id)
251 {
252   return id2type_lookup.find(datatype_id)->second;
253 }
254
255 void Datatype::addflag(int flag){
256   flags_ &= flag;
257 }
258
259 int Datatype::extent(MPI_Aint* lb, MPI_Aint* extent) const
260 {
261   *lb = lb_;
262   *extent = ub_ - lb_;
263   return MPI_SUCCESS;
264 }
265
266 void Datatype::get_name(char* name, int* length) const
267 {
268   *length = static_cast<int>(name_.length());
269   if (not name_.empty()) {
270     name_.copy(name, *length);
271     name[*length] = '\0';
272   }
273 }
274
275 void Datatype::set_name(const char* name)
276 {
277   name_ = name;
278 }
279
280 int Datatype::pack(const void* inbuf, int incount, void* outbuf, int outcount, int* position, const Comm*)
281 {
282   if (outcount - *position < incount*static_cast<int>(size_))
283     return MPI_ERR_OTHER;
284   Datatype::copy(inbuf, incount, this, static_cast<char*>(outbuf) + *position, outcount, MPI_CHAR);
285   *position += incount * size_;
286   return MPI_SUCCESS;
287 }
288
289 int Datatype::unpack(const void* inbuf, int insize, int* position, void* outbuf, int outcount, const Comm*)
290 {
291   if (outcount*static_cast<int>(size_)> insize)
292     return MPI_ERR_OTHER;
293   Datatype::copy(static_cast<const char*>(inbuf) + *position, insize, MPI_CHAR, outbuf, outcount, this);
294   *position += outcount * size_;
295   return MPI_SUCCESS;
296 }
297
298 int Datatype::get_contents(int max_integers, int max_addresses, int max_datatypes, int* array_of_integers,
299                            MPI_Aint* array_of_addresses, MPI_Datatype* array_of_datatypes) const
300 {
301   if(contents_==nullptr)
302     return MPI_ERR_ARG;
303   if (static_cast<unsigned>(max_integers) < contents_->integers_.size())
304     return MPI_ERR_COUNT;
305   std::copy(begin(contents_->integers_), end(contents_->integers_), array_of_integers);
306   if (static_cast<unsigned>(max_addresses) < contents_->addresses_.size())
307     return MPI_ERR_COUNT;
308   std::copy(begin(contents_->addresses_), end(contents_->addresses_), array_of_addresses);
309   if (static_cast<unsigned>(max_datatypes) < contents_->datatypes_.size())
310     return MPI_ERR_COUNT;
311   std::copy(begin(contents_->datatypes_), end(contents_->datatypes_), array_of_datatypes);
312   for (auto& datatype : contents_->datatypes_)
313     datatype->ref();
314   return MPI_SUCCESS;
315 }
316
317 int Datatype::get_envelope(int* num_integers, int* num_addresses, int* num_datatypes, int* combiner) const
318 {
319   if(contents_==nullptr){
320     *num_integers = 0;
321     *num_addresses = 0;
322     *num_datatypes = 0;
323     *combiner = MPI_COMBINER_NAMED;
324   }else{
325     *num_integers  = contents_->integers_.size();
326     *num_addresses = contents_->addresses_.size();
327     *num_datatypes = contents_->datatypes_.size();
328     *combiner = contents_->combiner_;
329   }
330   return MPI_SUCCESS;
331 }
332
333 int Datatype::copy(const void* sendbuf, int sendcount, MPI_Datatype sendtype, void* recvbuf, int recvcount,
334                    MPI_Datatype recvtype)
335 {
336   // FIXME Handle the case of a partial shared malloc.
337
338   smpi_switch_data_segment(simgrid::s4u::Actor::self());
339
340   /* First check if we really have something to do */
341   size_t offset = 0;
342   std::vector<std::pair<size_t, size_t>> private_blocks;
343   if(smpi_is_shared(sendbuf,private_blocks,&offset)
344        && (private_blocks.size()==1
345        && (private_blocks[0].second - private_blocks[0].first)==(unsigned long)(sendcount * sendtype->get_extent()))){
346     XBT_VERB("sendbuf is shared. Ignoring copies");
347     return 0;
348   }
349   if(smpi_is_shared(recvbuf,private_blocks,&offset)
350        && (private_blocks.size()==1
351        && (private_blocks[0].second - private_blocks[0].first)==(unsigned long)(recvcount * recvtype->get_extent()))){
352     XBT_VERB("recvbuf is shared. Ignoring copies");
353     return 0;
354   }
355
356   if (recvcount > 0 && recvbuf != sendbuf) {
357     sendcount *= sendtype->size();
358     recvcount *= recvtype->size();
359     int count = sendcount < recvcount ? sendcount : recvcount;
360     XBT_DEBUG("Copying %d bytes from %p to %p", count, sendbuf, recvbuf);
361     if (not(sendtype->flags() & DT_FLAG_DERIVED) && not(recvtype->flags() & DT_FLAG_DERIVED)) {
362       if (not smpi_process()->replaying() && count > 0)
363         memcpy(recvbuf, sendbuf, count);
364     } else if (not(sendtype->flags() & DT_FLAG_DERIVED)) {
365       recvtype->unserialize(sendbuf, recvbuf, count / recvtype->size(), MPI_REPLACE);
366     } else if (not(recvtype->flags() & DT_FLAG_DERIVED)) {
367       sendtype->serialize(sendbuf, recvbuf, count / sendtype->size());
368     } else if(sendtype->size() != 0 && recvtype->size() != 0){
369       void * buf_tmp = xbt_malloc(count);
370       sendtype->serialize( sendbuf, buf_tmp,count/sendtype->size());
371       recvtype->unserialize( buf_tmp, recvbuf,count/recvtype->size(), MPI_REPLACE);
372       xbt_free(buf_tmp);
373     }
374   }
375
376   return sendcount > recvcount ? MPI_ERR_TRUNCATE : MPI_SUCCESS;
377 }
378
379 //Default serialization method : memcpy.
380 void Datatype::serialize(const void* noncontiguous_buf, void* contiguous_buf, int count)
381 {
382   auto* contiguous_buf_char          = static_cast<char*>(contiguous_buf);
383   const auto* noncontiguous_buf_char = static_cast<const char*>(noncontiguous_buf) + lb_;
384   memcpy(contiguous_buf_char, noncontiguous_buf_char, count*size_);
385 }
386
387 void Datatype::unserialize(const void* contiguous_buf, void *noncontiguous_buf, int count, MPI_Op op){
388   const auto* contiguous_buf_char = static_cast<const char*>(contiguous_buf);
389   auto* noncontiguous_buf_char    = static_cast<char*>(noncontiguous_buf) + lb_;
390   int n=count;
391   if(op!=MPI_OP_NULL)
392     op->apply( contiguous_buf_char, noncontiguous_buf_char, &n, this);
393 }
394
395 int Datatype::create_contiguous(int count, MPI_Datatype old_type, MPI_Aint lb, MPI_Datatype* new_type){
396   if(old_type->flags_ & DT_FLAG_DERIVED){
397     //handle this case as a hvector with stride equals to the extent of the datatype
398     return create_hvector(count, 1, old_type->get_extent(), old_type, new_type);
399   }
400   if(count>0)
401     *new_type = new Type_Contiguous(count * old_type->size(), lb, lb + count * old_type->size(),
402                                    DT_FLAG_DERIVED, count, old_type);
403   else
404     *new_type = new Datatype(count * old_type->size(), lb, lb + count * old_type->size(),0);
405   return MPI_SUCCESS;
406 }
407
408 int Datatype::create_vector(int count, int block_length, int stride, MPI_Datatype old_type, MPI_Datatype* new_type)
409 {
410   int retval;
411   if (block_length<0)
412     return MPI_ERR_ARG;
413   MPI_Aint lb = 0;
414   MPI_Aint ub = 0;
415   if(count>0){
416     lb=old_type->lb();
417     ub=((count-1)*stride+block_length-1)*old_type->get_extent()+old_type->ub();
418   }
419   if(old_type->flags() & DT_FLAG_DERIVED || stride != block_length){
420     *new_type = new Type_Vector(old_type->size() * block_length * count, lb, ub, DT_FLAG_DERIVED, count, block_length,
421                                 stride, old_type);
422     retval=MPI_SUCCESS;
423   }else{
424     /* in this situation the data are contiguous thus it's not required to serialize and unserialize it*/
425     *new_type =
426         new Datatype(old_type->size() * block_length * count, 0,
427                      old_type->size() * ((count - 1) * stride + block_length), DT_FLAG_CONTIGUOUS | DT_FLAG_DERIVED);
428     const std::array<int, 3> ints = {{count, block_length, stride}};
429     (*new_type)->set_contents(MPI_COMBINER_VECTOR, 3, ints.data(), 0, nullptr, 1, &old_type);
430     retval=MPI_SUCCESS;
431   }
432   return retval;
433 }
434
435
436 int Datatype::create_hvector(int count, int block_length, MPI_Aint stride, MPI_Datatype old_type, MPI_Datatype* new_type)
437 {
438   int retval;
439   if (block_length<0)
440     return MPI_ERR_ARG;
441   MPI_Aint lb = 0;
442   MPI_Aint ub = 0;
443   if(count>0){
444     lb=old_type->lb();
445     ub=((count-1)*stride)+(block_length-1)*old_type->get_extent()+old_type->ub();
446   }
447   if(old_type->flags() & DT_FLAG_DERIVED || stride != block_length*old_type->get_extent()){
448     *new_type = new Type_Hvector(old_type->size() * block_length * count, lb, ub, DT_FLAG_DERIVED, count, block_length,
449                                  stride, old_type);
450     retval=MPI_SUCCESS;
451   }else{
452     /* in this situation the data are contiguous thus it's not required to serialize and unserialize it*/
453     *new_type = new Datatype(old_type->size() * block_length * count, 0, old_type->size() * block_length * count,
454                              DT_FLAG_CONTIGUOUS | DT_FLAG_DERIVED);
455     const std::array<int, 2> ints = {{count, block_length}};
456     (*new_type)->set_contents(MPI_COMBINER_HVECTOR, 2, ints.data(), 1, &stride, 1, &old_type);
457     retval=MPI_SUCCESS;
458   }
459   return retval;
460 }
461
462 int Datatype::create_indexed(int count, const int* block_lengths, const int* indices, MPI_Datatype old_type, MPI_Datatype* new_type){
463   int size = 0;
464   bool contiguous=true;
465   MPI_Aint lb = 0;
466   MPI_Aint ub = 0;
467   if(count>0){
468     lb=indices[0]*old_type->get_extent();
469     ub=indices[0]*old_type->get_extent() + block_lengths[0]*old_type->ub();
470   }
471
472   for (int i = 0; i < count; i++) {
473     if (block_lengths[i] < 0)
474       return MPI_ERR_ARG;
475     size += block_lengths[i];
476
477     if(indices[i]*old_type->get_extent()+old_type->lb()<lb)
478       lb = indices[i]*old_type->get_extent()+old_type->lb();
479     if(indices[i]*old_type->get_extent()+block_lengths[i]*old_type->ub()>ub)
480       ub = indices[i]*old_type->get_extent()+block_lengths[i]*old_type->ub();
481
482     if ( (i< count -1) && (indices[i]+block_lengths[i] != indices[i+1]) )
483       contiguous=false;
484   }
485   if(old_type->flags_ & DT_FLAG_DERIVED)
486     contiguous=false;
487
488   if (not contiguous) {
489     *new_type = new Type_Indexed(size * old_type->size(),lb,ub,
490                                  DT_FLAG_DERIVED|DT_FLAG_DATA, count, block_lengths, indices, old_type);
491   }else{
492     Datatype::create_contiguous(size, old_type, lb, new_type);
493   }
494   return MPI_SUCCESS;
495 }
496
497 int Datatype::create_hindexed(int count, const int* block_lengths, const MPI_Aint* indices, MPI_Datatype old_type, MPI_Datatype* new_type){
498   int size = 0;
499   bool contiguous=true;
500   MPI_Aint lb = 0;
501   MPI_Aint ub = 0;
502   if(count>0){
503     lb=indices[0] + old_type->lb();
504     ub=indices[0] + block_lengths[0]*old_type->ub();
505   }
506   for (int i = 0; i < count; i++) {
507     if (block_lengths[i] < 0)
508       return MPI_ERR_ARG;
509     size += block_lengths[i];
510
511     if(indices[i]+old_type->lb()<lb)
512       lb = indices[i]+old_type->lb();
513     if(indices[i]+block_lengths[i]*old_type->ub()>ub)
514       ub = indices[i]+block_lengths[i]*old_type->ub();
515
516     if ((i < count - 1) && (indices[i] + static_cast<MPI_Aint>(old_type->size()) * block_lengths[i] != indices[i + 1]))
517       contiguous=false;
518   }
519   if (old_type->flags_ & DT_FLAG_DERIVED || lb!=0)
520     contiguous=false;
521
522   if (not contiguous) {
523     *new_type = new Type_Hindexed(size * old_type->size(),lb,ub,
524                                    DT_FLAG_DERIVED|DT_FLAG_DATA, count, block_lengths, indices, old_type);
525   }else{
526     Datatype::create_contiguous(size, old_type, lb, new_type);
527   }
528   return MPI_SUCCESS;
529 }
530
531 int Datatype::create_struct(int count, const int* block_lengths, const MPI_Aint* indices, const MPI_Datatype* old_types, MPI_Datatype* new_type){
532   size_t size = 0;
533   bool contiguous=true;
534   size = 0;
535   MPI_Aint lb = 0;
536   MPI_Aint ub = 0;
537   if(count>0){
538     lb=indices[0] + old_types[0]->lb();
539     ub=indices[0] + block_lengths[0]*old_types[0]->ub();
540   }
541   bool forced_lb=false;
542   bool forced_ub=false;
543   for (int i = 0; i < count; i++) {
544     if (block_lengths[i]<0)
545       return MPI_ERR_ARG;
546     if (old_types[i]->flags_ & DT_FLAG_DERIVED)
547       contiguous=false;
548
549     size += block_lengths[i]*old_types[i]->size();
550     if (old_types[i]==MPI_LB){
551       lb=indices[i];
552       forced_lb=true;
553     }
554     if (old_types[i]==MPI_UB){
555       ub=indices[i];
556       forced_ub=true;
557     }
558
559     if (not forced_lb && indices[i] + old_types[i]->lb() < lb)
560       lb = indices[i];
561     if (not forced_ub && indices[i] + block_lengths[i] * old_types[i]->ub() > ub)
562       ub = indices[i]+block_lengths[i]*old_types[i]->ub();
563
564     if ((i < count - 1) &&
565         (indices[i] + static_cast<MPI_Aint>(old_types[i]->size() * block_lengths[i]) != indices[i + 1]))
566       contiguous=false;
567   }
568   if (not contiguous) {
569     *new_type = new Type_Struct(size, lb,ub, DT_FLAG_DERIVED|DT_FLAG_DATA,
570                                 count, block_lengths, indices, old_types);
571   }else{
572     Datatype::create_contiguous(size, MPI_CHAR, lb, new_type);
573   }
574   return MPI_SUCCESS;
575 }
576
577 int Datatype::create_subarray(int ndims, const int* array_of_sizes,
578                              const int* array_of_subsizes, const int* array_of_starts,
579                              int order, MPI_Datatype oldtype, MPI_Datatype *newtype){
580   MPI_Datatype tmp;
581
582   for (int i = 0; i < ndims; i++) {
583     if (array_of_subsizes[i] > array_of_sizes[i]){
584       XBT_WARN("subarray : array_of_subsizes > array_of_sizes for dim %d",i);
585       return MPI_ERR_ARG;
586     }
587     if (array_of_starts[i] + array_of_subsizes[i] > array_of_sizes[i]){
588       XBT_WARN("subarray : array_of_starts + array_of_subsizes > array_of_sizes for dim %d",i);
589       return MPI_ERR_ARG;
590     }
591   }
592
593   MPI_Aint extent = oldtype->get_extent();
594
595   int i;
596   int step;
597   int end;
598   if( order==MPI_ORDER_C ) {
599       i = ndims - 1;
600       step = -1;
601       end = -1;
602   } else {
603       i = 0;
604       step = 1;
605       end = ndims;
606   }
607
608   MPI_Aint size = (MPI_Aint)array_of_sizes[i] * (MPI_Aint)array_of_sizes[i+step];
609   MPI_Aint lb = (MPI_Aint)array_of_starts[i] + (MPI_Aint)array_of_starts[i+step] *(MPI_Aint)array_of_sizes[i];
610
611   create_vector( array_of_subsizes[i+step], array_of_subsizes[i], array_of_sizes[i],
612                                oldtype, newtype );
613
614   tmp = *newtype;
615
616   for( i += 2 * step; i != end; i += step ) {
617       create_hvector( array_of_subsizes[i], 1, size * extent,
618                                     tmp, newtype );
619       unref(tmp);
620       lb += size * array_of_starts[i];
621       size *= array_of_sizes[i];
622       tmp = *newtype;
623   }
624
625   const MPI_Aint lbs = lb * extent;
626   const int sizes    = 1;
627   //handle LB and UB with a resized call
628   create_hindexed(1, &sizes, &lbs, tmp, newtype);
629   unref(tmp);
630
631   tmp = *newtype;
632   create_resized(tmp, 0, extent, newtype);
633
634   unref(tmp);
635   return MPI_SUCCESS;
636 }
637
638 int Datatype::create_resized(MPI_Datatype oldtype,MPI_Aint lb, MPI_Aint extent, MPI_Datatype *newtype){
639   const std::array<int, 3> blocks         = {{1, 1, 1}};
640   const std::array<MPI_Aint, 3> disps     = {{lb, 0, lb + extent}};
641   const std::array<MPI_Datatype, 3> types = {{MPI_LB, oldtype, MPI_UB}};
642
643   *newtype = new simgrid::smpi::Type_Struct(oldtype->size(), lb, lb + extent, DT_FLAG_DERIVED, 3, blocks.data(),
644                                             disps.data(), types.data());
645
646   (*newtype)->addflag(~DT_FLAG_COMMITED);
647   return MPI_SUCCESS;
648 }
649
650 Datatype* Datatype::f2c(int id)
651 {
652   return static_cast<Datatype*>(F2C::f2c(id));
653 }
654
655 } // namespace simgrid::smpi