Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
First works on the datatypes. Still missing a lot.
[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 "smpi_mpi_dt_private.h"
12 #include "xbt/replay.h"
13 #include <limits.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <string>
18 #include <xbt/ex.hpp>
19
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_mpi_dt, smpi, "Logging specific to SMPI (datatype)");
21
22 xbt_dict_t smpi_type_keyvals = nullptr;
23 int type_keyval_id=0;//avoid collisions
24
25
26 #define CREATE_MPI_DATATYPE(name, type)               \
27   static Datatype mpi_##name (         \
28     (char*) # name,                                   \
29     sizeof(type),   /* size */                        \
30     0,              /* lb */                          \
31     sizeof(type),   /* ub = lb + size */              \
32     DT_FLAG_BASIC  /* flags */                       \
33   );                                                  \
34 const MPI_Datatype name = &mpi_##name;
35
36 #define CREATE_MPI_DATATYPE_NULL(name)                \
37   static Datatype mpi_##name (         \
38     (char*) # name,                                   \
39     0,              /* size */                        \
40     0,              /* lb */                          \
41     0,              /* ub = lb + size */              \
42     DT_FLAG_BASIC  /* flags */                       \
43   );                                                  \
44 const MPI_Datatype name = &mpi_##name;
45
46 // Predefined data types
47 CREATE_MPI_DATATYPE(MPI_CHAR, char);
48 CREATE_MPI_DATATYPE(MPI_SHORT, short);
49 CREATE_MPI_DATATYPE(MPI_INT, int);
50 CREATE_MPI_DATATYPE(MPI_LONG, long);
51 CREATE_MPI_DATATYPE(MPI_LONG_LONG, long long);
52 CREATE_MPI_DATATYPE(MPI_SIGNED_CHAR, signed char);
53 CREATE_MPI_DATATYPE(MPI_UNSIGNED_CHAR, unsigned char);
54 CREATE_MPI_DATATYPE(MPI_UNSIGNED_SHORT, unsigned short);
55 CREATE_MPI_DATATYPE(MPI_UNSIGNED, unsigned int);
56 CREATE_MPI_DATATYPE(MPI_UNSIGNED_LONG, unsigned long);
57 CREATE_MPI_DATATYPE(MPI_UNSIGNED_LONG_LONG, unsigned long long);
58 CREATE_MPI_DATATYPE(MPI_FLOAT, float);
59 CREATE_MPI_DATATYPE(MPI_DOUBLE, double);
60 CREATE_MPI_DATATYPE(MPI_LONG_DOUBLE, long double);
61 CREATE_MPI_DATATYPE(MPI_WCHAR, wchar_t);
62 CREATE_MPI_DATATYPE(MPI_C_BOOL, bool);
63 CREATE_MPI_DATATYPE(MPI_BYTE, int8_t);
64 CREATE_MPI_DATATYPE(MPI_INT8_T, int8_t);
65 CREATE_MPI_DATATYPE(MPI_INT16_T, int16_t);
66 CREATE_MPI_DATATYPE(MPI_INT32_T, int32_t);
67 CREATE_MPI_DATATYPE(MPI_INT64_T, int64_t);
68 CREATE_MPI_DATATYPE(MPI_UINT8_T, uint8_t);
69 CREATE_MPI_DATATYPE(MPI_UINT16_T, uint16_t);
70 CREATE_MPI_DATATYPE(MPI_UINT32_T, uint32_t);
71 CREATE_MPI_DATATYPE(MPI_UINT64_T, uint64_t);
72 CREATE_MPI_DATATYPE(MPI_C_FLOAT_COMPLEX, float _Complex);
73 CREATE_MPI_DATATYPE(MPI_C_DOUBLE_COMPLEX, double _Complex);
74 CREATE_MPI_DATATYPE(MPI_C_LONG_DOUBLE_COMPLEX, long double _Complex);
75 CREATE_MPI_DATATYPE(MPI_AINT, MPI_Aint);
76 CREATE_MPI_DATATYPE(MPI_OFFSET, MPI_Offset);
77
78 CREATE_MPI_DATATYPE(MPI_FLOAT_INT, float_int);
79 CREATE_MPI_DATATYPE(MPI_LONG_INT, long_int);
80 CREATE_MPI_DATATYPE(MPI_DOUBLE_INT, double_int);
81 CREATE_MPI_DATATYPE(MPI_SHORT_INT, short_int);
82 CREATE_MPI_DATATYPE(MPI_2INT, int_int);
83 CREATE_MPI_DATATYPE(MPI_2FLOAT, float_float);
84 CREATE_MPI_DATATYPE(MPI_2DOUBLE, double_double);
85 CREATE_MPI_DATATYPE(MPI_2LONG, long_long);
86
87 CREATE_MPI_DATATYPE(MPI_REAL, float);
88 CREATE_MPI_DATATYPE(MPI_REAL4, float);
89 CREATE_MPI_DATATYPE(MPI_REAL8, float);
90 CREATE_MPI_DATATYPE(MPI_REAL16, double);
91 CREATE_MPI_DATATYPE_NULL(MPI_COMPLEX8);
92 CREATE_MPI_DATATYPE_NULL(MPI_COMPLEX16);
93 CREATE_MPI_DATATYPE_NULL(MPI_COMPLEX32);
94 CREATE_MPI_DATATYPE(MPI_INTEGER1, int);
95 CREATE_MPI_DATATYPE(MPI_INTEGER2, int16_t);
96 CREATE_MPI_DATATYPE(MPI_INTEGER4, int32_t);
97 CREATE_MPI_DATATYPE(MPI_INTEGER8, int64_t);
98 CREATE_MPI_DATATYPE(MPI_INTEGER16, integer128_t);
99
100 CREATE_MPI_DATATYPE(MPI_LONG_DOUBLE_INT, long_double_int);
101
102 CREATE_MPI_DATATYPE_NULL(MPI_UB);
103 CREATE_MPI_DATATYPE_NULL(MPI_LB);
104 CREATE_MPI_DATATYPE(MPI_PACKED, char);
105 // Internal use only
106 CREATE_MPI_DATATYPE(MPI_PTR, void*);
107
108 namespace simgrid{
109 namespace smpi{
110
111 Datatype::Datatype(int size,int lb, int ub, int flags) : name_(nullptr), lb_(lb), ub_(ub), flags_(flags), attributes_(nullptr), in_use_(1){
112 #if HAVE_MC
113   if(MC_is_active())
114     MC_ignore(&(in_use_), sizeof(in_use_));
115 #endif
116 }
117
118 //for predefined types, so in_use = 0.
119 Datatype::Datatype(char* name, int size,int lb, int ub, int flags) : name_(name), lb_(lb), ub_(ub), flags_(flags), attributes_(nullptr), in_use_(0){
120 #if HAVE_MC
121   if(MC_is_active())
122     MC_ignore(&(in_use_), sizeof(in_use_));
123 #endif
124 }
125
126
127 //TODO : subtypes ?
128 Datatype::Datatype(Datatype &datatype) : lb_(datatype.lb_), ub_(datatype.ub_), flags_(datatype.flags_), in_use_(1)
129 {
130   flags_ &= ~DT_FLAG_PREDEFINED;
131   if(datatype.name_)
132     name_ = xbt_strdup(datatype.name_);
133   if(datatype.attributes_ !=nullptr){
134     attributes_ = xbt_dict_new_homogeneous(nullptr);
135     xbt_dict_cursor_t cursor = nullptr;
136     char* key;
137     int flag;
138     void* value_in;
139     void* value_out;
140     xbt_dict_foreach (datatype.attributes_, cursor, key, value_in) {
141       smpi_type_key_elem elem =
142           static_cast<smpi_type_key_elem>(xbt_dict_get_or_null_ext(smpi_type_keyvals, key, sizeof(int)));
143       if (elem != nullptr && elem->copy_fn != MPI_NULL_COPY_FN) {
144         int ret = elem->copy_fn(&datatype, atoi(key), nullptr, value_in, &value_out, &flag);
145         if (ret != MPI_SUCCESS) {
146 //          smpi_datatype_unuse(*new_t);
147 //          *new_t = MPI_DATATYPE_NULL;
148           xbt_dict_cursor_free(&cursor);
149         }
150         if (flag)
151           xbt_dict_set_ext(attributes_, key, sizeof(int), value_out, nullptr);
152       }
153     }
154   }
155 }
156
157 Datatype::~Datatype(){
158   xbt_assert(in_use_ >= 0);
159
160   if(flags_ & DT_FLAG_PREDEFINED)
161     return;
162
163   //if still used, mark for deletion
164   if(in_use_!=0){
165       flags_ |=DT_FLAG_DESTROYED;
166       return;
167   }
168
169   if(attributes_ !=nullptr){
170     xbt_dict_cursor_t cursor = nullptr;
171     char* key;
172     void * value;
173     int flag;
174     xbt_dict_foreach(attributes_, cursor, key, value){
175       smpi_type_key_elem elem =
176           static_cast<smpi_type_key_elem>(xbt_dict_get_or_null_ext(smpi_type_keyvals, key, sizeof(int)));
177       if(elem!=nullptr && elem->delete_fn!=nullptr)
178         elem->delete_fn(this,*key, value, &flag);
179     }
180     xbt_dict_free(&attributes_);
181   }
182
183   xbt_free(name_);
184 }
185
186
187 void Datatype::use(){
188
189   in_use_++;
190
191 #if HAVE_MC
192   if(MC_is_active())
193     MC_ignore(&(in_use_), sizeof(in_use_));
194 #endif
195 }
196
197 void Datatype::unuse()
198 {
199   if (in_use_ > 0)
200     in_use_--;
201
202   if (in_use_ == 0)
203     this->~Datatype();
204
205 #if HAVE_MC
206   if(MC_is_active())
207     MC_ignore(&(in_use_), sizeof(in_use_));
208 #endif
209 }
210
211 void Datatype::commit()
212 {
213   flags_ |= DT_FLAG_COMMITED;
214 }
215
216
217 bool Datatype::is_valid(){
218   return (flags_ & DT_FLAG_COMMITED);
219 }
220
221 size_t Datatype::size(){
222   return size_;
223 }
224
225 int Datatype::flags(){
226   return flags_;
227 }
228
229 void Datatype::addflag(int flag){
230   flags_ &= flag;
231 }
232
233 MPI_Aint Datatype::lb(){
234   return lb_;
235 }
236
237 MPI_Aint Datatype::ub(){
238   return ub_;
239 }
240
241 char* Datatype::name(){
242   return name_;
243 }
244
245
246 int Datatype::extent(MPI_Aint * lb, MPI_Aint * extent){
247   *lb = lb_;
248   *extent = ub_ - lb_;
249   return MPI_SUCCESS;
250 }
251
252 MPI_Aint Datatype::get_extent(){
253   return ub_ - lb_;
254 }
255
256 void Datatype::get_name(char* name, int* length){
257   *length = strlen(name_);
258   strncpy(name, name_, *length+1);
259 }
260
261 void Datatype::set_name(char* name){
262   if(name_!=nullptr &&  (flags_ & DT_FLAG_PREDEFINED) == 0)
263     xbt_free(name_);
264   name_ = xbt_strdup(name);
265 }
266
267 int Datatype::attr_delete(int keyval){
268   smpi_type_key_elem elem =
269     static_cast<smpi_type_key_elem>(xbt_dict_get_or_null_ext(smpi_type_keyvals, reinterpret_cast<const char*>(&keyval), sizeof(int)));
270   if(elem==nullptr)
271     return MPI_ERR_ARG;
272   if(elem->delete_fn!=MPI_NULL_DELETE_FN){
273     void * value = nullptr;
274     int flag;
275     if(smpi_type_attr_get(this, keyval, &value, &flag)==MPI_SUCCESS){
276       int ret = elem->delete_fn(this, keyval, value, &flag);
277       if(ret!=MPI_SUCCESS) 
278         return ret;
279     }
280   }  
281   if(attributes_==nullptr)
282     return MPI_ERR_ARG;
283
284   xbt_dict_remove_ext(attributes_, reinterpret_cast<const char*>(&keyval), sizeof(int));
285   return MPI_SUCCESS;
286 }
287
288
289 int Datatype::attr_get(int keyval, void* attr_value, int* flag){
290   smpi_type_key_elem elem =
291     static_cast<smpi_type_key_elem>(xbt_dict_get_or_null_ext(smpi_type_keyvals, reinterpret_cast<const char*>(&keyval), sizeof(int)));
292   if(elem==nullptr)
293     return MPI_ERR_ARG;
294   if(attributes_==nullptr){
295     *flag=0;
296     return MPI_SUCCESS;
297   }
298   try {
299     *static_cast<void**>(attr_value) = xbt_dict_get_ext(attributes_, reinterpret_cast<const char*>(&keyval), sizeof(int));
300     *flag=1;
301   }
302   catch (xbt_ex& ex) {
303     *flag=0;
304   }
305   return MPI_SUCCESS;
306 }
307
308 int Datatype::attr_put(int keyval, void* attr_value){
309   if(smpi_type_keyvals==nullptr)
310     smpi_type_keyvals = xbt_dict_new_homogeneous(nullptr);
311   smpi_type_key_elem elem =
312      static_cast<smpi_type_key_elem>(xbt_dict_get_or_null_ext(smpi_type_keyvals, reinterpret_cast<const char*>(&keyval), sizeof(int)));
313   if(elem==nullptr)
314     return MPI_ERR_ARG;
315   int flag;
316   void* value = nullptr;
317   smpi_type_attr_get(this, keyval, &value, &flag);
318   if(flag!=0 && elem->delete_fn!=MPI_NULL_DELETE_FN){
319     int ret = elem->delete_fn(this, keyval, value, &flag);
320     if(ret!=MPI_SUCCESS) 
321       return ret;
322   }
323   if(attributes_==nullptr)
324     attributes_ = xbt_dict_new_homogeneous(nullptr);
325
326   xbt_dict_set_ext(attributes_, reinterpret_cast<const char*>(&keyval), sizeof(int), attr_value, nullptr);
327   return MPI_SUCCESS;
328 }
329
330 int Datatype::keyval_create(MPI_Type_copy_attr_function* copy_fn, MPI_Type_delete_attr_function* delete_fn, int* keyval, void* extra_state){
331   if(smpi_type_keyvals==nullptr)
332     smpi_type_keyvals = xbt_dict_new_homogeneous(nullptr);
333
334   smpi_type_key_elem value = (smpi_type_key_elem) xbt_new0(s_smpi_mpi_type_key_elem_t,1);
335
336   value->copy_fn=copy_fn;
337   value->delete_fn=delete_fn;
338
339   *keyval = type_keyval_id;
340   xbt_dict_set_ext(smpi_type_keyvals,reinterpret_cast<const char*>(keyval), sizeof(int),reinterpret_cast<void*>(value), nullptr);
341   type_keyval_id++;
342   return MPI_SUCCESS;
343 }
344
345 int Datatype::keyval_free(int* keyval){
346   smpi_type_key_elem elem =
347     static_cast<smpi_type_key_elem>(xbt_dict_get_or_null_ext(smpi_type_keyvals, reinterpret_cast<const char*>(keyval), sizeof(int)));
348   if(elem==0){
349     return MPI_ERR_ARG;
350   }
351   xbt_dict_remove_ext(smpi_type_keyvals, reinterpret_cast<const char*>(keyval), sizeof(int));
352   xbt_free(elem);
353   return MPI_SUCCESS;
354 }
355
356
357 int Datatype::pack(void* inbuf, int incount, void* outbuf, int outcount, int* position,MPI_Comm comm){
358   if (outcount - *position < incount*static_cast<int>(size_))
359     return MPI_ERR_BUFFER;
360   Datatype::copy(inbuf, incount, this, static_cast<char*>(outbuf) + *position, outcount, MPI_CHAR);
361   *position += incount * size_;
362   return MPI_SUCCESS;
363 }
364
365 int Datatype::unpack(void* inbuf, int insize, int* position, void* outbuf, int outcount,MPI_Comm comm){
366   if (outcount*(int)size_> insize)
367     return MPI_ERR_BUFFER;
368   Datatype::copy(static_cast<char*>(inbuf) + *position, insize, MPI_CHAR, outbuf, outcount, this);
369   *position += outcount * size_;
370   return MPI_SUCCESS;
371 }
372
373
374 int Datatype::copy(void *sendbuf, int sendcount, MPI_Datatype sendtype,
375                        void *recvbuf, int recvcount, MPI_Datatype recvtype){
376 //  int count;
377   if(smpi_privatize_global_variables){
378     smpi_switch_data_segment(smpi_process_index());
379   }
380   /* First check if we really have something to do */
381   if (recvcount > 0 && recvbuf != sendbuf) {
382     /* FIXME: treat packed cases */
383     sendcount *= sendtype->size();
384     recvcount *= recvtype->size();
385 //    count = sendcount < recvcount ? sendcount : recvcount;
386
387 //    if(sendtype->sizeof_substruct == 0 && recvtype->sizeof_substruct == 0) {
388 //      if(!smpi_process_get_replaying()) 
389 //        memcpy(recvbuf, sendbuf, count);
390 //    }
391 //    else if (sendtype->sizeof_substruct == 0)
392 //    {
393 //      s_smpi_subtype_t *subtype =  static_cast<s_smpi_subtype_t*>(recvtype->substruct);
394 //      subtype->unserialize( sendbuf, recvbuf, recvcount/smpi_datatype_size(recvtype), subtype, MPI_REPLACE);
395 //    }
396 //    else if (recvtype->sizeof_substruct == 0)
397 //    {
398 //      s_smpi_subtype_t *subtype =  static_cast<s_smpi_subtype_t*>(sendtype->substruct);
399 //      subtype->serialize(sendbuf, recvbuf, sendcount/smpi_datatype_size(sendtype), subtype);
400 //    }else{
401 //      s_smpi_subtype_t *subtype =  static_cast<s_smpi_subtype_t*>(sendtype->substruct);
402
403 //      void * buf_tmp = xbt_malloc(count);
404
405 //      subtype->serialize( sendbuf, buf_tmp,count/smpi_datatype_size(sendtype), subtype);
406 //      subtype =  static_cast<s_smpi_subtype_t*>(recvtype->substruct);
407 //      subtype->unserialize( buf_tmp, recvbuf,count/smpi_datatype_size(recvtype), subtype, MPI_REPLACE);
408
409 //      xbt_free(buf_tmp);
410 //    }
411   }
412
413   return sendcount > recvcount ? MPI_ERR_TRUNCATE : MPI_SUCCESS;
414 }
415
416 //Default serialization method : memcpy.
417 void Datatype::serialize( void* noncontiguous, void *contiguous, int count, void *type){
418   char* contiguous_char = static_cast<char*>(contiguous);
419   char* noncontiguous_char = static_cast<char*>(noncontiguous)+lb_;
420   memcpy(contiguous_char, noncontiguous_char, count*size_);
421
422 }
423
424 void Datatype::unserialize( void* contiguous, void *noncontiguous, int count, void *type, MPI_Op op){
425   char* contiguous_char = static_cast<char*>(contiguous);
426   char* noncontiguous_char = static_cast<char*>(noncontiguous)+lb_;
427   int n=count;
428   if(op!=MPI_OP_NULL)
429     op->apply( contiguous_char, noncontiguous_char, &n, this);
430 }
431
432
433
434 }
435 }
436