Logo AND Algorithmique Numérique Distribuée

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