1 /* smpi_datatype.cpp -- MPI primitives to handle datatypes */
2 /* Copyright (c) 2009-2015. The SimGrid Team.
3 * All rights reserved. */
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. */
10 #include "simgrid/modelchecker.h"
11 #include "smpi_mpi_dt_private.h"
12 #include "xbt/replay.h"
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_mpi_dt, smpi, "Logging specific to SMPI (datatype)");
22 xbt_dict_t smpi_type_keyvals = nullptr;
23 int type_keyval_id=0;//avoid collisions
26 #define CREATE_MPI_DATATYPE(name, type) \
27 static Datatype mpi_##name ( \
29 sizeof(type), /* size */ \
31 sizeof(type), /* ub = lb + size */ \
32 DT_FLAG_BASIC /* flags */ \
34 const MPI_Datatype name = &mpi_##name;
36 #define CREATE_MPI_DATATYPE_NULL(name) \
37 static Datatype mpi_##name ( \
41 0, /* ub = lb + size */ \
42 DT_FLAG_BASIC /* flags */ \
44 const MPI_Datatype name = &mpi_##name;
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);
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);
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);
100 CREATE_MPI_DATATYPE(MPI_LONG_DOUBLE_INT, long_double_int);
102 CREATE_MPI_DATATYPE_NULL(MPI_UB);
103 CREATE_MPI_DATATYPE_NULL(MPI_LB);
104 CREATE_MPI_DATATYPE(MPI_PACKED, char);
106 CREATE_MPI_DATATYPE(MPI_PTR, void*);
111 Datatype::Datatype(int size,int lb, int ub, int flags) : name_(nullptr), lb_(lb), ub_(ub), flags_(flags), attributes_(nullptr), in_use_(1){
114 MC_ignore(&(in_use_), sizeof(in_use_));
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){
122 MC_ignore(&(in_use_), sizeof(in_use_));
128 Datatype::Datatype(Datatype &datatype) : lb_(datatype.lb_), ub_(datatype.ub_), flags_(datatype.flags_), in_use_(1)
130 flags_ &= ~DT_FLAG_PREDEFINED;
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;
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);
151 xbt_dict_set_ext(attributes_, key, sizeof(int), value_out, nullptr);
157 Datatype::~Datatype(){
158 xbt_assert(in_use_ >= 0);
160 if(flags_ & DT_FLAG_PREDEFINED)
163 //if still used, mark for deletion
165 flags_ |=DT_FLAG_DESTROYED;
169 if(attributes_ !=nullptr){
170 xbt_dict_cursor_t cursor = nullptr;
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);
180 xbt_dict_free(&attributes_);
187 void Datatype::use(){
193 MC_ignore(&(in_use_), sizeof(in_use_));
197 void Datatype::unuse()
207 MC_ignore(&(in_use_), sizeof(in_use_));
211 void Datatype::commit()
213 flags_ |= DT_FLAG_COMMITED;
217 bool Datatype::is_valid(){
218 return (flags_ & DT_FLAG_COMMITED);
221 size_t Datatype::size(){
225 int Datatype::flags(){
229 void Datatype::addflag(int flag){
233 MPI_Aint Datatype::lb(){
237 MPI_Aint Datatype::ub(){
241 char* Datatype::name(){
246 int Datatype::extent(MPI_Aint * lb, MPI_Aint * extent){
252 MPI_Aint Datatype::get_extent(){
256 void Datatype::get_name(char* name, int* length){
257 *length = strlen(name_);
258 strncpy(name, name_, *length+1);
261 void Datatype::set_name(char* name){
262 if(name_!=nullptr && (flags_ & DT_FLAG_PREDEFINED) == 0)
264 name_ = xbt_strdup(name);
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)));
272 if(elem->delete_fn!=MPI_NULL_DELETE_FN){
273 void * value = nullptr;
275 if(smpi_type_attr_get(this, keyval, &value, &flag)==MPI_SUCCESS){
276 int ret = elem->delete_fn(this, keyval, value, &flag);
281 if(attributes_==nullptr)
284 xbt_dict_remove_ext(attributes_, reinterpret_cast<const char*>(&keyval), sizeof(int));
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)));
294 if(attributes_==nullptr){
299 *static_cast<void**>(attr_value) = xbt_dict_get_ext(attributes_, reinterpret_cast<const char*>(&keyval), sizeof(int));
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)));
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);
323 if(attributes_==nullptr)
324 attributes_ = xbt_dict_new_homogeneous(nullptr);
326 xbt_dict_set_ext(attributes_, reinterpret_cast<const char*>(&keyval), sizeof(int), attr_value, nullptr);
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);
334 smpi_type_key_elem value = (smpi_type_key_elem) xbt_new0(s_smpi_mpi_type_key_elem_t,1);
336 value->copy_fn=copy_fn;
337 value->delete_fn=delete_fn;
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);
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)));
351 xbt_dict_remove_ext(smpi_type_keyvals, reinterpret_cast<const char*>(keyval), sizeof(int));
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_;
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_;
374 int Datatype::copy(void *sendbuf, int sendcount, MPI_Datatype sendtype,
375 void *recvbuf, int recvcount, MPI_Datatype recvtype){
377 if(smpi_privatize_global_variables){
378 smpi_switch_data_segment(smpi_process_index());
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;
387 // if(sendtype->sizeof_substruct == 0 && recvtype->sizeof_substruct == 0) {
388 // if(!smpi_process_get_replaying())
389 // memcpy(recvbuf, sendbuf, count);
391 // else if (sendtype->sizeof_substruct == 0)
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);
396 // else if (recvtype->sizeof_substruct == 0)
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);
401 // s_smpi_subtype_t *subtype = static_cast<s_smpi_subtype_t*>(sendtype->substruct);
403 // void * buf_tmp = xbt_malloc(count);
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);
409 // xbt_free(buf_tmp);
413 return sendcount > recvcount ? MPI_ERR_TRUNCATE : MPI_SUCCESS;
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_);
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_;
429 op->apply( contiguous_char, noncontiguous_char, &n, this);