Logo AND Algorithmique Numérique Distribuée

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