Logo AND Algorithmique Numérique Distribuée

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