1 /* smpi_mpi_dt.c -- MPI primitives to handle datatypes */
2 /* FIXME: a very incomplete implementation */
4 /* Copyright (c) 2009-2015. The SimGrid Team.
5 * All rights reserved. */
7 /* This program is free software; you can redistribute it and/or modify it
8 * under the terms of the license (GNU LGPL) which comes with this package. */
15 #include "smpi_mpi_dt_private.h"
17 #include "xbt/replay.h"
18 #include "simgrid/modelchecker.h"
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_mpi_dt, smpi, "Logging specific to SMPI (datatype)");
22 xbt_dict_t smpi_type_keyvals = NULL;
23 int type_keyval_id=0;//avoid collisions
25 #define CREATE_MPI_DATATYPE(name, type) \
26 static s_smpi_mpi_datatype_t mpi_##name = { \
28 sizeof(type), /* size */ \
29 0, /*was 1 sizeof_substruct*/ \
31 sizeof(type), /* ub = lb + size */ \
32 DT_FLAG_BASIC, /* flags */ \
33 NULL, /* attributes */ \
34 NULL, /* pointer on extended struct*/ \
35 0 /* in_use counter */ \
37 const MPI_Datatype name = &mpi_##name;
39 #define CREATE_MPI_DATATYPE_NULL(name) \
40 static s_smpi_mpi_datatype_t mpi_##name = { \
43 0, /* was 1 sizeof_substruct*/ \
45 0, /* ub = lb + size */ \
46 DT_FLAG_BASIC, /* flags */ \
47 NULL, /* attributes */ \
48 NULL, /* pointer on extended struct*/ \
49 0 /* in_use counter */ \
51 const MPI_Datatype name = &mpi_##name;
53 //The following are datatypes for the MPI functions MPI_MAXLOC and MPI_MINLOC.
94 // Predefined data types
95 CREATE_MPI_DATATYPE(MPI_CHAR, char);
96 CREATE_MPI_DATATYPE(MPI_SHORT, short);
97 CREATE_MPI_DATATYPE(MPI_INT, int);
98 CREATE_MPI_DATATYPE(MPI_LONG, long);
99 CREATE_MPI_DATATYPE(MPI_LONG_LONG, long long);
100 CREATE_MPI_DATATYPE(MPI_SIGNED_CHAR, signed char);
101 CREATE_MPI_DATATYPE(MPI_UNSIGNED_CHAR, unsigned char);
102 CREATE_MPI_DATATYPE(MPI_UNSIGNED_SHORT, unsigned short);
103 CREATE_MPI_DATATYPE(MPI_UNSIGNED, unsigned int);
104 CREATE_MPI_DATATYPE(MPI_UNSIGNED_LONG, unsigned long);
105 CREATE_MPI_DATATYPE(MPI_UNSIGNED_LONG_LONG, unsigned long long);
106 CREATE_MPI_DATATYPE(MPI_FLOAT, float);
107 CREATE_MPI_DATATYPE(MPI_DOUBLE, double);
108 CREATE_MPI_DATATYPE(MPI_LONG_DOUBLE, long double);
109 CREATE_MPI_DATATYPE(MPI_WCHAR, wchar_t);
110 CREATE_MPI_DATATYPE(MPI_C_BOOL, bool);
111 CREATE_MPI_DATATYPE(MPI_BYTE, int8_t);
112 CREATE_MPI_DATATYPE(MPI_INT8_T, int8_t);
113 CREATE_MPI_DATATYPE(MPI_INT16_T, int16_t);
114 CREATE_MPI_DATATYPE(MPI_INT32_T, int32_t);
115 CREATE_MPI_DATATYPE(MPI_INT64_T, int64_t);
116 CREATE_MPI_DATATYPE(MPI_UINT8_T, uint8_t);
117 CREATE_MPI_DATATYPE(MPI_UINT16_T, uint16_t);
118 CREATE_MPI_DATATYPE(MPI_UINT32_T, uint32_t);
119 CREATE_MPI_DATATYPE(MPI_UINT64_T, uint64_t);
120 CREATE_MPI_DATATYPE(MPI_C_FLOAT_COMPLEX, float _Complex);
121 CREATE_MPI_DATATYPE(MPI_C_DOUBLE_COMPLEX, double _Complex);
122 CREATE_MPI_DATATYPE(MPI_C_LONG_DOUBLE_COMPLEX, long double _Complex);
123 CREATE_MPI_DATATYPE(MPI_AINT, MPI_Aint);
124 CREATE_MPI_DATATYPE(MPI_OFFSET, MPI_Offset);
126 CREATE_MPI_DATATYPE(MPI_FLOAT_INT, float_int);
127 CREATE_MPI_DATATYPE(MPI_LONG_INT, long_int);
128 CREATE_MPI_DATATYPE(MPI_DOUBLE_INT, double_int);
129 CREATE_MPI_DATATYPE(MPI_SHORT_INT, short_int);
130 CREATE_MPI_DATATYPE(MPI_2INT, int_int);
131 CREATE_MPI_DATATYPE(MPI_2FLOAT, float_float);
132 CREATE_MPI_DATATYPE(MPI_2DOUBLE, double_double);
133 CREATE_MPI_DATATYPE(MPI_2LONG, long_long);
135 CREATE_MPI_DATATYPE(MPI_REAL, float);
136 CREATE_MPI_DATATYPE(MPI_REAL4, float);
137 CREATE_MPI_DATATYPE(MPI_REAL8, float);
138 CREATE_MPI_DATATYPE(MPI_REAL16, double);
139 CREATE_MPI_DATATYPE_NULL(MPI_COMPLEX8);
140 CREATE_MPI_DATATYPE_NULL(MPI_COMPLEX16);
141 CREATE_MPI_DATATYPE_NULL(MPI_COMPLEX32);
142 CREATE_MPI_DATATYPE(MPI_INTEGER1, int);
143 CREATE_MPI_DATATYPE(MPI_INTEGER2, int16_t);
144 CREATE_MPI_DATATYPE(MPI_INTEGER4, int32_t);
145 CREATE_MPI_DATATYPE(MPI_INTEGER8, int64_t);
146 CREATE_MPI_DATATYPE(MPI_INTEGER16, integer128_t);
148 CREATE_MPI_DATATYPE(MPI_LONG_DOUBLE_INT, long_double_int);
150 CREATE_MPI_DATATYPE_NULL(MPI_UB);
151 CREATE_MPI_DATATYPE_NULL(MPI_LB);
152 CREATE_MPI_DATATYPE(MPI_PACKED, char);
154 CREATE_MPI_DATATYPE(MPI_PTR, void*);
156 /** Check if the datatype is usable for communications */
157 int is_datatype_valid(MPI_Datatype datatype) {
158 return datatype != MPI_DATATYPE_NULL && (datatype->flags & DT_FLAG_COMMITED);
161 size_t smpi_datatype_size(MPI_Datatype datatype)
163 return datatype->size;
166 MPI_Aint smpi_datatype_lb(MPI_Datatype datatype)
171 MPI_Aint smpi_datatype_ub(MPI_Datatype datatype)
176 int smpi_datatype_dup(MPI_Datatype datatype, MPI_Datatype* new_t)
179 *new_t= xbt_new(s_smpi_mpi_datatype_t,1);
180 memcpy(*new_t, datatype, sizeof(s_smpi_mpi_datatype_t));
181 if (datatype->sizeof_substruct){
182 (*new_t)->substruct=xbt_malloc(datatype->sizeof_substruct);
183 memcpy((*new_t)->substruct, datatype->substruct, datatype->sizeof_substruct);
186 (*new_t)->name = xbt_strdup(datatype->name);
187 if(datatype->attributes !=NULL){
188 (*new_t)->attributes=xbt_dict_new();
189 xbt_dict_cursor_t cursor = NULL;
194 xbt_dict_foreach(datatype->attributes, cursor, key, value_in){
195 smpi_type_key_elem elem =
196 static_cast<smpi_type_key_elem>(xbt_dict_get_or_null_ext(smpi_type_keyvals, (const char*)key, sizeof(int)));
197 if(elem && elem->copy_fn!=MPI_NULL_COPY_FN){
198 ret = elem->copy_fn(datatype, *key, NULL, value_in, &value_out, &flag );
199 if(ret!=MPI_SUCCESS){
200 *new_t=MPI_DATATYPE_NULL;
204 xbt_dict_set_ext((*new_t)->attributes, (const char*)key, sizeof(int),value_out, NULL);
211 int smpi_datatype_extent(MPI_Datatype datatype, MPI_Aint * lb, MPI_Aint * extent)
213 if(datatype == MPI_DATATYPE_NULL){
219 *extent = datatype->ub - datatype->lb;
223 MPI_Aint smpi_datatype_get_extent(MPI_Datatype datatype){
224 if(datatype == MPI_DATATYPE_NULL){
227 return datatype->ub - datatype->lb;
230 void smpi_datatype_get_name(MPI_Datatype datatype, char* name, int* length){
231 *length = strlen(datatype->name);
232 strcpy(name, datatype->name);
235 void smpi_datatype_set_name(MPI_Datatype datatype, char* name){
236 datatype->name = xbt_strdup(name);;
239 int smpi_datatype_copy(void *sendbuf, int sendcount, MPI_Datatype sendtype,
240 void *recvbuf, int recvcount, MPI_Datatype recvtype)
243 if(smpi_privatize_global_variables){
244 smpi_switch_data_segment(smpi_process_index());
246 /* First check if we really have something to do */
247 if (recvcount > 0 && recvbuf != sendbuf) {
248 /* FIXME: treat packed cases */
249 sendcount *= smpi_datatype_size(sendtype);
250 recvcount *= smpi_datatype_size(recvtype);
251 count = sendcount < recvcount ? sendcount : recvcount;
253 if(sendtype->sizeof_substruct == 0 && recvtype->sizeof_substruct == 0) {
254 if(!smpi_process_get_replaying()) memcpy(recvbuf, sendbuf, count);
256 else if (sendtype->sizeof_substruct == 0)
258 s_smpi_subtype_t *subtype = static_cast<s_smpi_subtype_t*>(recvtype->substruct);
259 subtype->unserialize( sendbuf, recvbuf, recvcount/smpi_datatype_size(recvtype), subtype, MPI_REPLACE);
261 else if (recvtype->sizeof_substruct == 0)
263 s_smpi_subtype_t *subtype = static_cast<s_smpi_subtype_t*>(sendtype->substruct);
264 subtype->serialize(sendbuf, recvbuf, sendcount/smpi_datatype_size(sendtype), subtype);
266 s_smpi_subtype_t *subtype = static_cast<s_smpi_subtype_t*>(sendtype->substruct);
268 void * buf_tmp = xbt_malloc(count);
270 subtype->serialize( sendbuf, buf_tmp,count/smpi_datatype_size(sendtype), subtype);
271 subtype = static_cast<s_smpi_subtype_t*>(recvtype->substruct);
272 subtype->unserialize( buf_tmp, recvbuf,count/smpi_datatype_size(recvtype), subtype, MPI_REPLACE);
278 return sendcount > recvcount ? MPI_ERR_TRUNCATE : MPI_SUCCESS;
282 * Copies noncontiguous data into contiguous memory.
283 * @param contiguous_vector - output vector
284 * @param noncontiguous_vector - input vector
285 * @param type - pointer contening :
286 * - stride - stride of between noncontiguous data
287 * - block_length - the width or height of blocked matrix
288 * - count - the number of rows of matrix
290 void serialize_vector( const void *noncontiguous_vector, void *contiguous_vector, int count, void *type)
292 s_smpi_mpi_vector_t* type_c = (s_smpi_mpi_vector_t*)type;
294 char* contiguous_vector_char = (char*)contiguous_vector;
295 char* noncontiguous_vector_char = (char*)noncontiguous_vector;
297 for (i = 0; i < type_c->block_count * count; i++) {
298 if (type_c->old_type->sizeof_substruct == 0)
299 memcpy(contiguous_vector_char, noncontiguous_vector_char, type_c->block_length * type_c->size_oldtype);
301 ((s_smpi_subtype_t*)type_c->old_type->substruct)->serialize( noncontiguous_vector_char,
302 contiguous_vector_char,
303 type_c->block_length, type_c->old_type->substruct);
305 contiguous_vector_char += type_c->block_length*type_c->size_oldtype;
306 if((i+1)%type_c->block_count ==0)
307 noncontiguous_vector_char += type_c->block_length*smpi_datatype_get_extent(type_c->old_type);
309 noncontiguous_vector_char += type_c->block_stride*smpi_datatype_get_extent(type_c->old_type);
314 * Copies contiguous data into noncontiguous memory.
315 * @param noncontiguous_vector - output vector
316 * @param contiguous_vector - input vector
317 * @param type - pointer contening :
318 * - stride - stride of between noncontiguous data
319 * - block_length - the width or height of blocked matrix
320 * - count - the number of rows of matrix
322 void unserialize_vector( const void *contiguous_vector, void *noncontiguous_vector, int count, void *type, MPI_Op op)
324 s_smpi_mpi_vector_t* type_c = (s_smpi_mpi_vector_t*)type;
327 char* contiguous_vector_char = (char*)contiguous_vector;
328 char* noncontiguous_vector_char = (char*)noncontiguous_vector;
330 for (i = 0; i < type_c->block_count * count; i++) {
331 if (type_c->old_type->sizeof_substruct == 0)
332 smpi_op_apply(op, contiguous_vector_char, noncontiguous_vector_char, &type_c->block_length,
334 /* memcpy(noncontiguous_vector_char,
335 contiguous_vector_char, type_c->block_length * type_c->size_oldtype);*/
337 ((s_smpi_subtype_t*)type_c->old_type->substruct)->unserialize(contiguous_vector_char, noncontiguous_vector_char,
338 type_c->block_length,type_c->old_type->substruct,
340 contiguous_vector_char += type_c->block_length*type_c->size_oldtype;
341 if((i+1)%type_c->block_count ==0)
342 noncontiguous_vector_char += type_c->block_length*smpi_datatype_get_extent(type_c->old_type);
344 noncontiguous_vector_char += type_c->block_stride*smpi_datatype_get_extent(type_c->old_type);
348 /* Create a Sub type vector to be able to serialize and unserialize it the structure s_smpi_mpi_vector_t is derived
349 * from s_smpi_subtype which required the functions unserialize and serialize */
350 s_smpi_mpi_vector_t* smpi_datatype_vector_create( int block_stride, int block_length, int block_count,
351 MPI_Datatype old_type, int size_oldtype){
352 s_smpi_mpi_vector_t *new_t= xbt_new(s_smpi_mpi_vector_t,1);
353 new_t->base.serialize = &serialize_vector;
354 new_t->base.unserialize = &unserialize_vector;
355 new_t->base.subtype_free = &free_vector;
356 new_t->base.subtype_use = &use_vector;
357 new_t->block_stride = block_stride;
358 new_t->block_length = block_length;
359 new_t->block_count = block_count;
360 smpi_datatype_use(old_type);
361 new_t->old_type = old_type;
362 new_t->size_oldtype = size_oldtype;
366 void smpi_datatype_create(MPI_Datatype* new_type, int size,int lb, int ub, int sizeof_substruct, void *struct_type,
368 MPI_Datatype new_t= xbt_new(s_smpi_mpi_datatype_t,1);
371 new_t->sizeof_substruct = size>0? sizeof_substruct:0;
374 new_t->flags = flags;
375 new_t->substruct = struct_type;
377 new_t->attributes=NULL;
382 MC_ignore(&(new_t->in_use), sizeof(new_t->in_use));
386 void smpi_datatype_free(MPI_Datatype* type){
387 xbt_assert((*type)->in_use >= 0);
388 if((*type)->attributes !=NULL){
389 xbt_dict_cursor_t cursor = NULL;
393 xbt_dict_foreach((*type)->attributes, cursor, key, value){
394 smpi_type_key_elem elem =
395 static_cast<smpi_type_key_elem>(xbt_dict_get_or_null_ext(smpi_type_keyvals, (const char*)key, sizeof(int)));
396 if(elem && elem->delete_fn)
397 elem->delete_fn(*type,*key, value, &flag);
401 if((*type)->flags & DT_FLAG_PREDEFINED)return;
403 //if still used, mark for deletion
404 if((*type)->in_use!=0){
405 (*type)->flags |=DT_FLAG_DESTROYED;
409 if ((*type)->sizeof_substruct != 0){
410 //((s_smpi_subtype_t *)(*type)->substruct)->subtype_free(type);
411 xbt_free((*type)->substruct);
413 if ((*type)->name != NULL){
414 xbt_free((*type)->name);
416 *type = MPI_DATATYPE_NULL;
419 void smpi_datatype_use(MPI_Datatype type){
421 if(type)type->in_use++;
422 XBT_INFO("using type %p, counter %d ", type, type->in_use);
423 if(type->sizeof_substruct!=0){
424 ((s_smpi_subtype_t *)(type)->substruct)->subtype_use(&type);
428 MC_ignore(&(type->in_use), sizeof(type->in_use));
432 void smpi_datatype_unuse(MPI_Datatype type){
433 if (type->in_use > 0)
436 XBT_INFO("unusing type %p, counter %d ", type, type->in_use);
437 if(type->sizeof_substruct!=0){
438 ((s_smpi_subtype_t *)(type)->substruct)->subtype_free(&type);
441 if(type && type->in_use == 0){
442 MPI_Datatype t = type;
443 if (!(type->flags & DT_FLAG_DESTROYED))
444 smpi_datatype_free(&type);
445 if(t->flags & DT_FLAG_PREDEFINED) return;
450 MC_ignore(&(type->in_use), sizeof(type->in_use));
454 /*Contiguous Implementation*/
456 /* Copies noncontiguous data into contiguous memory.
457 * @param contiguous_hvector - output hvector
458 * @param noncontiguous_hvector - input hvector
459 * @param type - pointer contening :
460 * - stride - stride of between noncontiguous data, in bytes
461 * - block_length - the width or height of blocked matrix
462 * - count - the number of rows of matrix
464 void serialize_contiguous( const void *noncontiguous_hvector, void *contiguous_hvector, int count, void *type)
466 s_smpi_mpi_contiguous_t* type_c = (s_smpi_mpi_contiguous_t*)type;
467 char* contiguous_vector_char = (char*)contiguous_hvector;
468 char* noncontiguous_vector_char = (char*)noncontiguous_hvector+type_c->lb;
469 memcpy(contiguous_vector_char, noncontiguous_vector_char, count* type_c->block_count * type_c->size_oldtype);
471 /* Copies contiguous data into noncontiguous memory.
472 * @param noncontiguous_vector - output hvector
473 * @param contiguous_vector - input hvector
474 * @param type - pointer contening :
475 * - stride - stride of between noncontiguous data, in bytes
476 * - block_length - the width or height of blocked matrix
477 * - count - the number of rows of matrix
479 void unserialize_contiguous(const void *contiguous_vector, void *noncontiguous_vector, int count, void *type, MPI_Op op)
481 s_smpi_mpi_contiguous_t* type_c = (s_smpi_mpi_contiguous_t*)type;
482 char* contiguous_vector_char = (char*)contiguous_vector;
483 char* noncontiguous_vector_char = (char*)noncontiguous_vector+type_c->lb;
484 int n= count* type_c->block_count;
485 smpi_op_apply(op, contiguous_vector_char, noncontiguous_vector_char, &n, &type_c->old_type);
486 /*memcpy(noncontiguous_vector_char, contiguous_vector_char, count* type_c->block_count * type_c->size_oldtype);*/
489 void free_contiguous(MPI_Datatype* d){
490 smpi_datatype_unuse(((s_smpi_mpi_indexed_t *)(*d)->substruct)->old_type);
493 void use_contiguous(MPI_Datatype* d){
494 smpi_datatype_use(((s_smpi_mpi_indexed_t *)(*d)->substruct)->old_type);
497 /* Create a Sub type contiguous to be able to serialize and unserialize it the structure s_smpi_mpi_contiguous_t is
498 * erived from s_smpi_subtype which required the functions unserialize and serialize */
499 s_smpi_mpi_contiguous_t* smpi_datatype_contiguous_create( MPI_Aint lb, int block_count, MPI_Datatype old_type,
501 s_smpi_mpi_contiguous_t *new_t= xbt_new(s_smpi_mpi_contiguous_t,1);
502 new_t->base.serialize = &serialize_contiguous;
503 new_t->base.unserialize = &unserialize_contiguous;
504 new_t->base.subtype_free = &free_contiguous;
505 new_t->base.subtype_use = &use_contiguous;
507 new_t->block_count = block_count;
508 new_t->old_type = old_type;
509 smpi_datatype_use(old_type);
510 new_t->size_oldtype = size_oldtype;
511 smpi_datatype_use(old_type);
515 int smpi_datatype_contiguous(int count, MPI_Datatype old_type, MPI_Datatype* new_type, MPI_Aint lb)
518 if(old_type->sizeof_substruct){
519 //handle this case as a hvector with stride equals to the extent of the datatype
520 return smpi_datatype_hvector(count, 1, smpi_datatype_get_extent(old_type), old_type, new_type);
523 s_smpi_mpi_contiguous_t* subtype = smpi_datatype_contiguous_create( lb, count, old_type,smpi_datatype_size(old_type));
525 smpi_datatype_create(new_type, count * smpi_datatype_size(old_type),lb,lb + count * smpi_datatype_size(old_type),
526 sizeof(s_smpi_mpi_contiguous_t),subtype, DT_FLAG_CONTIGUOUS);
531 int smpi_datatype_vector(int count, int blocklen, int stride, MPI_Datatype old_type, MPI_Datatype* new_type)
534 if (blocklen<0) return MPI_ERR_ARG;
538 lb=smpi_datatype_lb(old_type);
539 ub=((count-1)*stride+blocklen-1)*smpi_datatype_get_extent(old_type)+smpi_datatype_ub(old_type);
541 if(old_type->sizeof_substruct || stride != blocklen){
543 s_smpi_mpi_vector_t* subtype = smpi_datatype_vector_create(stride, blocklen, count, old_type,
544 smpi_datatype_size(old_type));
545 smpi_datatype_create(new_type, count * (blocklen) * smpi_datatype_size(old_type), lb, ub, sizeof(s_smpi_mpi_vector_t), subtype,
549 /* in this situation the data are contignous thus it's not required to serialize and unserialize it*/
550 smpi_datatype_create(new_type, count * blocklen * smpi_datatype_size(old_type), 0, ((count -1) * stride + blocklen)*
551 smpi_datatype_size(old_type), 0, NULL, DT_FLAG_VECTOR|DT_FLAG_CONTIGUOUS);
557 void free_vector(MPI_Datatype* d){
558 smpi_datatype_unuse(((s_smpi_mpi_indexed_t *)(*d)->substruct)->old_type);
561 void use_vector(MPI_Datatype* d){
562 smpi_datatype_use(((s_smpi_mpi_indexed_t *)(*d)->substruct)->old_type);
565 /* Hvector Implementation - Vector with stride in bytes */
567 /* Copies noncontiguous data into contiguous memory.
568 * @param contiguous_hvector - output hvector
569 * @param noncontiguous_hvector - input hvector
570 * @param type - pointer contening :
571 * - stride - stride of between noncontiguous data, in bytes
572 * - block_length - the width or height of blocked matrix
573 * - count - the number of rows of matrix
575 void serialize_hvector( const void *noncontiguous_hvector, void *contiguous_hvector, int count, void *type)
577 s_smpi_mpi_hvector_t* type_c = (s_smpi_mpi_hvector_t*)type;
579 char* contiguous_vector_char = (char*)contiguous_hvector;
580 char* noncontiguous_vector_char = (char*)noncontiguous_hvector;
582 for (i = 0; i < type_c->block_count * count; i++) {
583 if (type_c->old_type->sizeof_substruct == 0)
584 memcpy(contiguous_vector_char, noncontiguous_vector_char, type_c->block_length * type_c->size_oldtype);
586 ((s_smpi_subtype_t*)type_c->old_type->substruct)->serialize( noncontiguous_vector_char,
587 contiguous_vector_char,
588 type_c->block_length, type_c->old_type->substruct);
590 contiguous_vector_char += type_c->block_length*type_c->size_oldtype;
591 if((i+1)%type_c->block_count ==0)
592 noncontiguous_vector_char += type_c->block_length*type_c->size_oldtype;
594 noncontiguous_vector_char += type_c->block_stride;
597 /* Copies contiguous data into noncontiguous memory.
598 * @param noncontiguous_vector - output hvector
599 * @param contiguous_vector - input hvector
600 * @param type - pointer contening :
601 * - stride - stride of between noncontiguous data, in bytes
602 * - block_length - the width or height of blocked matrix
603 * - count - the number of rows of matrix
605 void unserialize_hvector( const void *contiguous_vector, void *noncontiguous_vector, int count, void *type, MPI_Op op)
607 s_smpi_mpi_hvector_t* type_c = (s_smpi_mpi_hvector_t*)type;
610 char* contiguous_vector_char = (char*)contiguous_vector;
611 char* noncontiguous_vector_char = (char*)noncontiguous_vector;
613 for (i = 0; i < type_c->block_count * count; i++) {
614 if (type_c->old_type->sizeof_substruct == 0)
615 smpi_op_apply(op, contiguous_vector_char, noncontiguous_vector_char, &type_c->block_length, &type_c->old_type);
616 /*memcpy(noncontiguous_vector_char,
617 contiguous_vector_char, type_c->block_length * type_c->size_oldtype);*/
619 ((s_smpi_subtype_t*)type_c->old_type->substruct)->unserialize( contiguous_vector_char, noncontiguous_vector_char,
620 type_c->block_length, type_c->old_type->substruct,
622 contiguous_vector_char += type_c->block_length*type_c->size_oldtype;
623 if((i+1)%type_c->block_count ==0)
624 noncontiguous_vector_char += type_c->block_length*type_c->size_oldtype;
626 noncontiguous_vector_char += type_c->block_stride;
630 /* Create a Sub type vector to be able to serialize and unserialize it the structure s_smpi_mpi_vector_t is derived
631 * from s_smpi_subtype which required the functions unserialize and serialize
634 s_smpi_mpi_hvector_t* smpi_datatype_hvector_create( MPI_Aint block_stride, int block_length, int block_count,
635 MPI_Datatype old_type, int size_oldtype){
636 s_smpi_mpi_hvector_t *new_t= xbt_new(s_smpi_mpi_hvector_t,1);
637 new_t->base.serialize = &serialize_hvector;
638 new_t->base.unserialize = &unserialize_hvector;
639 new_t->base.subtype_free = &free_hvector;
640 new_t->base.subtype_use = &use_hvector;
641 new_t->block_stride = block_stride;
642 new_t->block_length = block_length;
643 new_t->block_count = block_count;
644 new_t->old_type = old_type;
645 new_t->size_oldtype = size_oldtype;
646 smpi_datatype_use(old_type);
650 //do nothing for vector types
651 void free_hvector(MPI_Datatype* d){
652 smpi_datatype_unuse(((s_smpi_mpi_indexed_t *)(*d)->substruct)->old_type);
655 void use_hvector(MPI_Datatype* d){
656 smpi_datatype_use(((s_smpi_mpi_indexed_t *)(*d)->substruct)->old_type);
659 int smpi_datatype_hvector(int count, int blocklen, MPI_Aint stride, MPI_Datatype old_type, MPI_Datatype* new_type)
662 if (blocklen<0) return MPI_ERR_ARG;
666 lb=smpi_datatype_lb(old_type);
667 ub=((count-1)*stride)+(blocklen-1)*smpi_datatype_get_extent(old_type)+smpi_datatype_ub(old_type);
669 if(old_type->sizeof_substruct || stride != blocklen*smpi_datatype_get_extent(old_type)){
670 s_smpi_mpi_hvector_t* subtype = smpi_datatype_hvector_create( stride, blocklen, count, old_type,
671 smpi_datatype_size(old_type));
673 smpi_datatype_create(new_type, count * blocklen * smpi_datatype_size(old_type), lb,ub, sizeof(s_smpi_mpi_hvector_t), subtype, DT_FLAG_VECTOR);
676 smpi_datatype_create(new_type, count * blocklen * smpi_datatype_size(old_type),0,count * blocklen *
677 smpi_datatype_size(old_type), 0, NULL, DT_FLAG_VECTOR|DT_FLAG_CONTIGUOUS);
683 /* Indexed Implementation */
685 /* Copies noncontiguous data into contiguous memory.
686 * @param contiguous_indexed - output indexed
687 * @param noncontiguous_indexed - input indexed
688 * @param type - pointer contening :
689 * - block_lengths - the width or height of blocked matrix
690 * - block_indices - indices of each data, in element
691 * - count - the number of rows of matrix
693 void serialize_indexed( const void *noncontiguous_indexed, void *contiguous_indexed, int count, void *type)
695 s_smpi_mpi_indexed_t* type_c = (s_smpi_mpi_indexed_t*)type;
697 char* contiguous_indexed_char = (char*)contiguous_indexed;
698 char* noncontiguous_indexed_char = (char*)noncontiguous_indexed+type_c->block_indices[0] * type_c->size_oldtype;
699 for(j=0; j<count;j++){
700 for (i = 0; i < type_c->block_count; i++) {
701 if (type_c->old_type->sizeof_substruct == 0)
702 memcpy(contiguous_indexed_char, noncontiguous_indexed_char, type_c->block_lengths[i] * type_c->size_oldtype);
704 ((s_smpi_subtype_t*)type_c->old_type->substruct)->serialize( noncontiguous_indexed_char,
705 contiguous_indexed_char,
706 type_c->block_lengths[i],
707 type_c->old_type->substruct);
709 contiguous_indexed_char += type_c->block_lengths[i]*type_c->size_oldtype;
710 if (i<type_c->block_count-1)
711 noncontiguous_indexed_char =
712 (char*)noncontiguous_indexed + type_c->block_indices[i+1]*smpi_datatype_get_extent(type_c->old_type);
714 noncontiguous_indexed_char += type_c->block_lengths[i]*smpi_datatype_get_extent(type_c->old_type);
716 noncontiguous_indexed=(void*)noncontiguous_indexed_char;
719 /* Copies contiguous data into noncontiguous memory.
720 * @param noncontiguous_indexed - output indexed
721 * @param contiguous_indexed - input indexed
722 * @param type - pointer contening :
723 * - block_lengths - the width or height of blocked matrix
724 * - block_indices - indices of each data, in element
725 * - count - the number of rows of matrix
727 void unserialize_indexed( const void *contiguous_indexed, void *noncontiguous_indexed, int count, void *type, MPI_Op op)
729 s_smpi_mpi_indexed_t* type_c = (s_smpi_mpi_indexed_t*)type;
731 char* contiguous_indexed_char = (char*)contiguous_indexed;
732 char* noncontiguous_indexed_char =
733 (char*)noncontiguous_indexed+type_c->block_indices[0]*smpi_datatype_get_extent(type_c->old_type);
734 for(j=0; j<count;j++){
735 for (i = 0; i < type_c->block_count; i++) {
736 if (type_c->old_type->sizeof_substruct == 0)
737 smpi_op_apply(op, contiguous_indexed_char, noncontiguous_indexed_char, &type_c->block_lengths[i],
739 /*memcpy(noncontiguous_indexed_char ,
740 contiguous_indexed_char, type_c->block_lengths[i] * type_c->size_oldtype);*/
742 ((s_smpi_subtype_t*)type_c->old_type->substruct)->unserialize( contiguous_indexed_char,
743 noncontiguous_indexed_char,
744 type_c->block_lengths[i],
745 type_c->old_type->substruct, op);
747 contiguous_indexed_char += type_c->block_lengths[i]*type_c->size_oldtype;
748 if (i<type_c->block_count-1)
749 noncontiguous_indexed_char =
750 (char*)noncontiguous_indexed + type_c->block_indices[i+1]*smpi_datatype_get_extent(type_c->old_type);
752 noncontiguous_indexed_char += type_c->block_lengths[i]*smpi_datatype_get_extent(type_c->old_type);
754 noncontiguous_indexed=(void*)noncontiguous_indexed_char;
758 void free_indexed(MPI_Datatype* type){
759 if((*type)->in_use==0){
760 xbt_free(((s_smpi_mpi_indexed_t *)(*type)->substruct)->block_lengths);
761 xbt_free(((s_smpi_mpi_indexed_t *)(*type)->substruct)->block_indices);
763 smpi_datatype_unuse(((s_smpi_mpi_indexed_t *)(*type)->substruct)->old_type);
766 void use_indexed(MPI_Datatype* type){
767 smpi_datatype_use(((s_smpi_mpi_indexed_t *)(*type)->substruct)->old_type);
771 /* Create a Sub type indexed to be able to serialize and unserialize it the structure s_smpi_mpi_indexed_t is derived
772 * from s_smpi_subtype which required the functions unserialize and serialize */
773 s_smpi_mpi_indexed_t* smpi_datatype_indexed_create( int* block_lengths, int* block_indices, int block_count,
774 MPI_Datatype old_type, int size_oldtype){
775 s_smpi_mpi_indexed_t *new_t= xbt_new(s_smpi_mpi_indexed_t,1);
776 new_t->base.serialize = &serialize_indexed;
777 new_t->base.unserialize = &unserialize_indexed;
778 new_t->base.subtype_free = &free_indexed;
779 new_t->base.subtype_use = &use_indexed;
780 //TODO : add a custom function for each time to clean these
781 new_t->block_lengths= xbt_new(int, block_count);
782 new_t->block_indices= xbt_new(int, block_count);
784 for(i=0;i<block_count;i++){
785 new_t->block_lengths[i]=block_lengths[i];
786 new_t->block_indices[i]=block_indices[i];
788 new_t->block_count = block_count;
789 smpi_datatype_use(old_type);
790 new_t->old_type = old_type;
791 new_t->size_oldtype = size_oldtype;
795 int smpi_datatype_indexed(int count, int* blocklens, int* indices, MPI_Datatype old_type, MPI_Datatype* new_type)
804 lb=indices[0]*smpi_datatype_get_extent(old_type);
805 ub=indices[0]*smpi_datatype_get_extent(old_type) + blocklens[0]*smpi_datatype_ub(old_type);
808 for(i=0; i< count; i++){
811 size += blocklens[i];
813 if(indices[i]*smpi_datatype_get_extent(old_type)+smpi_datatype_lb(old_type)<lb)
814 lb = indices[i]*smpi_datatype_get_extent(old_type)+smpi_datatype_lb(old_type);
815 if(indices[i]*smpi_datatype_get_extent(old_type)+blocklens[i]*smpi_datatype_ub(old_type)>ub)
816 ub = indices[i]*smpi_datatype_get_extent(old_type)+blocklens[i]*smpi_datatype_ub(old_type);
818 if ( (i< count -1) && (indices[i]+blocklens[i] != indices[i+1]) )contiguous=0;
820 if (old_type->sizeof_substruct != 0)
824 s_smpi_mpi_indexed_t* subtype = smpi_datatype_indexed_create( blocklens, indices, count, old_type,
825 smpi_datatype_size(old_type));
826 smpi_datatype_create(new_type, size * smpi_datatype_size(old_type),lb,ub,sizeof(s_smpi_mpi_indexed_t), subtype, DT_FLAG_DATA);
828 s_smpi_mpi_contiguous_t* subtype = smpi_datatype_contiguous_create( lb, size, old_type,
829 smpi_datatype_size(old_type));
830 smpi_datatype_create(new_type, size * smpi_datatype_size(old_type), lb, ub, sizeof(s_smpi_mpi_contiguous_t), subtype,
831 DT_FLAG_DATA|DT_FLAG_CONTIGUOUS);
836 /* Hindexed Implementation - Indexed with indices in bytes */
838 /* Copies noncontiguous data into contiguous memory.
839 * @param contiguous_hindexed - output hindexed
840 * @param noncontiguous_hindexed - input hindexed
841 * @param type - pointer contening :
842 * - block_lengths - the width or height of blocked matrix
843 * - block_indices - indices of each data, in bytes
844 * - count - the number of rows of matrix
846 void serialize_hindexed( const void *noncontiguous_hindexed, void *contiguous_hindexed, int count, void *type)
848 s_smpi_mpi_hindexed_t* type_c = (s_smpi_mpi_hindexed_t*)type;
850 char* contiguous_hindexed_char = (char*)contiguous_hindexed;
851 char* noncontiguous_hindexed_char = (char*)noncontiguous_hindexed+ type_c->block_indices[0];
852 for(j=0; j<count;j++){
853 for (i = 0; i < type_c->block_count; i++) {
854 if (type_c->old_type->sizeof_substruct == 0)
855 memcpy(contiguous_hindexed_char, noncontiguous_hindexed_char, type_c->block_lengths[i] * type_c->size_oldtype);
857 ((s_smpi_subtype_t*)type_c->old_type->substruct)->serialize( noncontiguous_hindexed_char,
858 contiguous_hindexed_char,
859 type_c->block_lengths[i],
860 type_c->old_type->substruct);
862 contiguous_hindexed_char += type_c->block_lengths[i]*type_c->size_oldtype;
863 if (i<type_c->block_count-1)
864 noncontiguous_hindexed_char = (char*)noncontiguous_hindexed + type_c->block_indices[i+1];
866 noncontiguous_hindexed_char += type_c->block_lengths[i]*smpi_datatype_get_extent(type_c->old_type);
868 noncontiguous_hindexed=(void*)noncontiguous_hindexed_char;
871 /* Copies contiguous data into noncontiguous memory.
872 * @param noncontiguous_hindexed - output hindexed
873 * @param contiguous_hindexed - input hindexed
874 * @param type - pointer contening :
875 * - block_lengths - the width or height of blocked matrix
876 * - block_indices - indices of each data, in bytes
877 * - count - the number of rows of matrix
879 void unserialize_hindexed( const void *contiguous_hindexed, void *noncontiguous_hindexed, int count, void *type,
882 s_smpi_mpi_hindexed_t* type_c = (s_smpi_mpi_hindexed_t*)type;
885 char* contiguous_hindexed_char = (char*)contiguous_hindexed;
886 char* noncontiguous_hindexed_char = (char*)noncontiguous_hindexed+ type_c->block_indices[0];
887 for(j=0; j<count;j++){
888 for (i = 0; i < type_c->block_count; i++) {
889 if (type_c->old_type->sizeof_substruct == 0)
890 smpi_op_apply(op, contiguous_hindexed_char, noncontiguous_hindexed_char, &type_c->block_lengths[i],
892 /*memcpy(noncontiguous_hindexed_char,contiguous_hindexed_char,type_c->block_lengths[i]*type_c->size_oldtype);*/
894 ((s_smpi_subtype_t*)type_c->old_type->substruct)->unserialize( contiguous_hindexed_char,
895 noncontiguous_hindexed_char,
896 type_c->block_lengths[i],
897 type_c->old_type->substruct, op);
899 contiguous_hindexed_char += type_c->block_lengths[i]*type_c->size_oldtype;
900 if (i<type_c->block_count-1)
901 noncontiguous_hindexed_char = (char*)noncontiguous_hindexed + type_c->block_indices[i+1];
903 noncontiguous_hindexed_char += type_c->block_lengths[i]*smpi_datatype_get_extent(type_c->old_type);
905 noncontiguous_hindexed=(void*)noncontiguous_hindexed_char;
909 void free_hindexed(MPI_Datatype* type){
910 if((*type)->in_use==0){
911 xbt_free(((s_smpi_mpi_hindexed_t *)(*type)->substruct)->block_lengths);
912 xbt_free(((s_smpi_mpi_hindexed_t *)(*type)->substruct)->block_indices);
914 smpi_datatype_unuse(((s_smpi_mpi_indexed_t *)(*type)->substruct)->old_type);
917 void use_hindexed(MPI_Datatype* type){
918 smpi_datatype_use(((s_smpi_mpi_indexed_t *)(*type)->substruct)->old_type);
921 /* Create a Sub type hindexed to be able to serialize and unserialize it the structure s_smpi_mpi_hindexed_t is derived
922 * from s_smpi_subtype which required the functions unserialize and serialize
924 s_smpi_mpi_hindexed_t* smpi_datatype_hindexed_create( int* block_lengths, MPI_Aint* block_indices, int block_count,
925 MPI_Datatype old_type, int size_oldtype){
926 s_smpi_mpi_hindexed_t *new_t= xbt_new(s_smpi_mpi_hindexed_t,1);
927 new_t->base.serialize = &serialize_hindexed;
928 new_t->base.unserialize = &unserialize_hindexed;
929 new_t->base.subtype_free = &free_hindexed;
930 new_t->base.subtype_use = &use_hindexed;
931 //TODO : add a custom function for each time to clean these
932 new_t->block_lengths= xbt_new(int, block_count);
933 new_t->block_indices= xbt_new(MPI_Aint, block_count);
935 for(i=0;i<block_count;i++){
936 new_t->block_lengths[i]=block_lengths[i];
937 new_t->block_indices[i]=block_indices[i];
939 new_t->block_count = block_count;
940 new_t->old_type = old_type;
941 smpi_datatype_use(old_type);
942 new_t->size_oldtype = size_oldtype;
946 int smpi_datatype_hindexed(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype old_type, MPI_Datatype* new_type)
955 lb=indices[0] + smpi_datatype_lb(old_type);
956 ub=indices[0] + blocklens[0]*smpi_datatype_ub(old_type);
958 for(i=0; i< count; i++){
961 size += blocklens[i];
963 if(indices[i]+smpi_datatype_lb(old_type)<lb) lb = indices[i]+smpi_datatype_lb(old_type);
964 if(indices[i]+blocklens[i]*smpi_datatype_ub(old_type)>ub) ub = indices[i]+blocklens[i]*smpi_datatype_ub(old_type);
966 if ( (i< count -1) && (indices[i]+blocklens[i]*static_cast<int>(smpi_datatype_size(old_type)) != indices[i+1]) )
969 if (old_type->sizeof_substruct != 0 || lb!=0)
973 s_smpi_mpi_hindexed_t* subtype = smpi_datatype_hindexed_create( blocklens, indices, count, old_type,
974 smpi_datatype_size(old_type));
975 smpi_datatype_create(new_type, size * smpi_datatype_size(old_type), lb, ub ,sizeof(s_smpi_mpi_hindexed_t), subtype, DT_FLAG_DATA);
977 s_smpi_mpi_contiguous_t* subtype = smpi_datatype_contiguous_create(lb,size, old_type, smpi_datatype_size(old_type));
978 smpi_datatype_create(new_type, size * smpi_datatype_size(old_type), 0,size * smpi_datatype_size(old_type),
979 1, subtype, DT_FLAG_DATA|DT_FLAG_CONTIGUOUS);
985 /* struct Implementation - Indexed with indices in bytes */
987 /* Copies noncontiguous data into contiguous memory.
988 * @param contiguous_struct - output struct
989 * @param noncontiguous_struct - input struct
990 * @param type - pointer contening :
991 * - stride - stride of between noncontiguous data
992 * - block_length - the width or height of blocked matrix
993 * - count - the number of rows of matrix
995 void serialize_struct( const void *noncontiguous_struct, void *contiguous_struct, int count, void *type)
997 s_smpi_mpi_struct_t* type_c = (s_smpi_mpi_struct_t*)type;
999 char* contiguous_struct_char = (char*)contiguous_struct;
1000 char* noncontiguous_struct_char = (char*)noncontiguous_struct+ type_c->block_indices[0];
1001 for(j=0; j<count;j++){
1002 for (i = 0; i < type_c->block_count; i++) {
1003 if (type_c->old_types[i]->sizeof_substruct == 0)
1004 memcpy(contiguous_struct_char, noncontiguous_struct_char,
1005 type_c->block_lengths[i] * smpi_datatype_size(type_c->old_types[i]));
1007 ((s_smpi_subtype_t*)type_c->old_types[i]->substruct)->serialize( noncontiguous_struct_char,
1008 contiguous_struct_char,
1009 type_c->block_lengths[i],
1010 type_c->old_types[i]->substruct);
1013 contiguous_struct_char += type_c->block_lengths[i]*smpi_datatype_size(type_c->old_types[i]);
1014 if (i<type_c->block_count-1)
1015 noncontiguous_struct_char = (char*)noncontiguous_struct + type_c->block_indices[i+1];
1016 else //let's hope this is MPI_UB ?
1017 noncontiguous_struct_char += type_c->block_lengths[i]*smpi_datatype_get_extent(type_c->old_types[i]);
1019 noncontiguous_struct=(void*)noncontiguous_struct_char;
1023 /* Copies contiguous data into noncontiguous memory.
1024 * @param noncontiguous_struct - output struct
1025 * @param contiguous_struct - input struct
1026 * @param type - pointer contening :
1027 * - stride - stride of between noncontiguous data
1028 * - block_length - the width or height of blocked matrix
1029 * - count - the number of rows of matrix
1031 void unserialize_struct( const void *contiguous_struct, void *noncontiguous_struct, int count, void *type, MPI_Op op)
1033 s_smpi_mpi_struct_t* type_c = (s_smpi_mpi_struct_t*)type;
1036 char* contiguous_struct_char = (char*)contiguous_struct;
1037 char* noncontiguous_struct_char = (char*)noncontiguous_struct+ type_c->block_indices[0];
1038 for(j=0; j<count;j++){
1039 for (i = 0; i < type_c->block_count; i++) {
1040 if (type_c->old_types[i]->sizeof_substruct == 0)
1041 smpi_op_apply(op, contiguous_struct_char, noncontiguous_struct_char, &type_c->block_lengths[i],
1042 & type_c->old_types[i]);
1043 /*memcpy(noncontiguous_struct_char,
1044 contiguous_struct_char, type_c->block_lengths[i] * smpi_datatype_size(type_c->old_types[i]));*/
1046 ((s_smpi_subtype_t*)type_c->old_types[i]->substruct)->unserialize( contiguous_struct_char,
1047 noncontiguous_struct_char,
1048 type_c->block_lengths[i],
1049 type_c->old_types[i]->substruct, op);
1051 contiguous_struct_char += type_c->block_lengths[i]*smpi_datatype_size(type_c->old_types[i]);
1052 if (i<type_c->block_count-1)
1053 noncontiguous_struct_char = (char*)noncontiguous_struct + type_c->block_indices[i+1];
1055 noncontiguous_struct_char += type_c->block_lengths[i]*smpi_datatype_get_extent(type_c->old_types[i]);
1057 noncontiguous_struct=(void*)noncontiguous_struct_char;
1061 void free_struct(MPI_Datatype* type){
1063 for (i = 0; i < ((s_smpi_mpi_struct_t *)(*type)->substruct)->block_count; i++)
1064 smpi_datatype_unuse(((s_smpi_mpi_struct_t *)(*type)->substruct)->old_types[i]);
1065 if((*type)->in_use==0){
1066 xbt_free(((s_smpi_mpi_struct_t *)(*type)->substruct)->block_lengths);
1067 xbt_free(((s_smpi_mpi_struct_t *)(*type)->substruct)->block_indices);
1068 xbt_free(((s_smpi_mpi_struct_t *)(*type)->substruct)->old_types);
1072 void use_struct(MPI_Datatype* type){
1074 for (i = 0; i < ((s_smpi_mpi_struct_t *)(*type)->substruct)->block_count; i++)
1075 smpi_datatype_use(((s_smpi_mpi_struct_t *)(*type)->substruct)->old_types[i]);
1078 /* Create a Sub type struct to be able to serialize and unserialize it the structure s_smpi_mpi_struct_t is derived
1079 * from s_smpi_subtype which required the functions unserialize and serialize
1081 s_smpi_mpi_struct_t* smpi_datatype_struct_create( int* block_lengths, MPI_Aint* block_indices, int block_count,
1082 MPI_Datatype* old_types){
1083 s_smpi_mpi_struct_t *new_t= xbt_new(s_smpi_mpi_struct_t,1);
1084 new_t->base.serialize = &serialize_struct;
1085 new_t->base.unserialize = &unserialize_struct;
1086 new_t->base.subtype_free = &free_struct;
1087 new_t->base.subtype_use = &use_struct;
1088 //TODO : add a custom function for each time to clean these
1089 new_t->block_lengths= xbt_new(int, block_count);
1090 new_t->block_indices= xbt_new(MPI_Aint, block_count);
1091 new_t->old_types= xbt_new(MPI_Datatype, block_count);
1093 for(i=0;i<block_count;i++){
1094 new_t->block_lengths[i]=block_lengths[i];
1095 new_t->block_indices[i]=block_indices[i];
1096 new_t->old_types[i]=old_types[i];
1097 smpi_datatype_use(new_t->old_types[i]);
1099 //new_t->block_lengths = block_lengths;
1100 //new_t->block_indices = block_indices;
1101 new_t->block_count = block_count;
1102 //new_t->old_types = old_types;
1106 int smpi_datatype_struct(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype* old_types, MPI_Datatype* new_type)
1115 lb=indices[0] + smpi_datatype_lb(old_types[0]);
1116 ub=indices[0] + blocklens[0]*smpi_datatype_ub(old_types[0]);
1120 for(i=0; i< count; i++){
1123 if (old_types[i]->sizeof_substruct != 0)
1126 size += blocklens[i]*smpi_datatype_size(old_types[i]);
1127 if (old_types[i]==MPI_LB){
1131 if (old_types[i]==MPI_UB){
1136 if(!forced_lb && indices[i]+smpi_datatype_lb(old_types[i])<lb) lb = indices[i];
1137 if(!forced_ub && indices[i]+blocklens[i]*smpi_datatype_ub(old_types[i])>ub)
1138 ub = indices[i]+blocklens[i]*smpi_datatype_ub(old_types[i]);
1140 if ( (i< count -1) && (indices[i]+blocklens[i]*static_cast<int>(smpi_datatype_size(old_types[i])) != indices[i+1]) )
1145 s_smpi_mpi_struct_t* subtype = smpi_datatype_struct_create( blocklens, indices, count, old_types);
1147 smpi_datatype_create(new_type, size, lb, ub,sizeof(s_smpi_mpi_struct_t), subtype, DT_FLAG_DATA);
1149 s_smpi_mpi_contiguous_t* subtype = smpi_datatype_contiguous_create( lb, size, MPI_CHAR, 1);
1150 smpi_datatype_create(new_type, size, lb, ub,1, subtype, DT_FLAG_DATA|DT_FLAG_CONTIGUOUS);
1155 void smpi_datatype_commit(MPI_Datatype *datatype)
1157 (*datatype)->flags= ((*datatype)->flags | DT_FLAG_COMMITED);
1160 typedef struct s_smpi_mpi_op {
1161 MPI_User_function *func;
1165 #define MAX_OP(a, b) (b) = (a) < (b) ? (b) : (a)
1166 #define MIN_OP(a, b) (b) = (a) < (b) ? (a) : (b)
1167 #define SUM_OP(a, b) (b) += (a)
1168 #define PROD_OP(a, b) (b) *= (a)
1169 #define LAND_OP(a, b) (b) = (a) && (b)
1170 #define LOR_OP(a, b) (b) = (a) || (b)
1171 #define LXOR_OP(a, b) (b) = (!(a) && (b)) || ((a) && !(b))
1172 #define BAND_OP(a, b) (b) &= (a)
1173 #define BOR_OP(a, b) (b) |= (a)
1174 #define BXOR_OP(a, b) (b) ^= (a)
1175 #define MAXLOC_OP(a, b) (b) = (a.value) < (b.value) ? (b) : (a)
1176 #define MINLOC_OP(a, b) (b) = (a.value) < (b.value) ? (a) : (b)
1178 #define APPLY_FUNC(a, b, length, type, func) \
1181 type* x = (type*)(a); \
1182 type* y = (type*)(b); \
1183 for(i = 0; i < *(length); i++) { \
1188 static void max_func(void *a, void *b, int *length, MPI_Datatype * datatype)
1190 if (*datatype == MPI_CHAR) {
1191 APPLY_FUNC(a, b, length, char, MAX_OP);
1192 } else if (*datatype == MPI_SHORT) {
1193 APPLY_FUNC(a, b, length, short, MAX_OP);
1194 } else if (*datatype == MPI_INT) {
1195 APPLY_FUNC(a, b, length, int, MAX_OP);
1196 } else if (*datatype == MPI_LONG) {
1197 APPLY_FUNC(a, b, length, long, MAX_OP);
1198 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1199 APPLY_FUNC(a, b, length, unsigned short, MAX_OP);
1200 } else if (*datatype == MPI_UNSIGNED) {
1201 APPLY_FUNC(a, b, length, unsigned int, MAX_OP);
1202 } else if (*datatype == MPI_UNSIGNED_LONG) {
1203 APPLY_FUNC(a, b, length, unsigned long, MAX_OP);
1204 } else if (*datatype == MPI_UNSIGNED_CHAR) {
1205 APPLY_FUNC(a, b, length, unsigned char, MAX_OP);
1206 } else if (*datatype == MPI_FLOAT) {
1207 APPLY_FUNC(a, b, length, float, MAX_OP);
1208 } else if (*datatype == MPI_DOUBLE) {
1209 APPLY_FUNC(a, b, length, double, MAX_OP);
1210 } else if (*datatype == MPI_LONG_DOUBLE) {
1211 APPLY_FUNC(a, b, length, long double, MAX_OP);
1215 static void min_func(void *a, void *b, int *length, MPI_Datatype * datatype)
1217 if (*datatype == MPI_CHAR) {
1218 APPLY_FUNC(a, b, length, char, MIN_OP);
1219 } else if (*datatype == MPI_SHORT) {
1220 APPLY_FUNC(a, b, length, short, MIN_OP);
1221 } else if (*datatype == MPI_INT) {
1222 APPLY_FUNC(a, b, length, int, MIN_OP);
1223 } else if (*datatype == MPI_LONG) {
1224 APPLY_FUNC(a, b, length, long, MIN_OP);
1225 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1226 APPLY_FUNC(a, b, length, unsigned short, MIN_OP);
1227 } else if (*datatype == MPI_UNSIGNED) {
1228 APPLY_FUNC(a, b, length, unsigned int, MIN_OP);
1229 } else if (*datatype == MPI_UNSIGNED_LONG) {
1230 APPLY_FUNC(a, b, length, unsigned long, MIN_OP);
1231 } else if (*datatype == MPI_UNSIGNED_CHAR) {
1232 APPLY_FUNC(a, b, length, unsigned char, MIN_OP);
1233 } else if (*datatype == MPI_FLOAT) {
1234 APPLY_FUNC(a, b, length, float, MIN_OP);
1235 } else if (*datatype == MPI_DOUBLE) {
1236 APPLY_FUNC(a, b, length, double, MIN_OP);
1237 } else if (*datatype == MPI_LONG_DOUBLE) {
1238 APPLY_FUNC(a, b, length, long double, MIN_OP);
1242 static void sum_func(void *a, void *b, int *length, MPI_Datatype * datatype)
1244 if (*datatype == MPI_CHAR) {
1245 APPLY_FUNC(a, b, length, char, SUM_OP);
1246 } else if (*datatype == MPI_SHORT) {
1247 APPLY_FUNC(a, b, length, short, SUM_OP);
1248 } else if (*datatype == MPI_INT) {
1249 APPLY_FUNC(a, b, length, int, SUM_OP);
1250 } else if (*datatype == MPI_LONG) {
1251 APPLY_FUNC(a, b, length, long, SUM_OP);
1252 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1253 APPLY_FUNC(a, b, length, unsigned short, SUM_OP);
1254 } else if (*datatype == MPI_UNSIGNED) {
1255 APPLY_FUNC(a, b, length, unsigned int, SUM_OP);
1256 } else if (*datatype == MPI_UNSIGNED_LONG) {
1257 APPLY_FUNC(a, b, length, unsigned long, SUM_OP);
1258 } else if (*datatype == MPI_UNSIGNED_CHAR) {
1259 APPLY_FUNC(a, b, length, unsigned char, SUM_OP);
1260 } else if (*datatype == MPI_FLOAT) {
1261 APPLY_FUNC(a, b, length, float, SUM_OP);
1262 } else if (*datatype == MPI_DOUBLE) {
1263 APPLY_FUNC(a, b, length, double, SUM_OP);
1264 } else if (*datatype == MPI_LONG_DOUBLE) {
1265 APPLY_FUNC(a, b, length, long double, SUM_OP);
1266 } else if (*datatype == MPI_C_FLOAT_COMPLEX) {
1267 APPLY_FUNC(a, b, length, float _Complex, SUM_OP);
1268 } else if (*datatype == MPI_C_DOUBLE_COMPLEX) {
1269 APPLY_FUNC(a, b, length, double _Complex, SUM_OP);
1270 } else if (*datatype == MPI_C_LONG_DOUBLE_COMPLEX) {
1271 APPLY_FUNC(a, b, length, long double _Complex, SUM_OP);
1275 static void prod_func(void *a, void *b, int *length, MPI_Datatype * datatype)
1277 if (*datatype == MPI_CHAR) {
1278 APPLY_FUNC(a, b, length, char, PROD_OP);
1279 } else if (*datatype == MPI_SHORT) {
1280 APPLY_FUNC(a, b, length, short, PROD_OP);
1281 } else if (*datatype == MPI_INT) {
1282 APPLY_FUNC(a, b, length, int, PROD_OP);
1283 } else if (*datatype == MPI_LONG) {
1284 APPLY_FUNC(a, b, length, long, PROD_OP);
1285 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1286 APPLY_FUNC(a, b, length, unsigned short, PROD_OP);
1287 } else if (*datatype == MPI_UNSIGNED) {
1288 APPLY_FUNC(a, b, length, unsigned int, PROD_OP);
1289 } else if (*datatype == MPI_UNSIGNED_LONG) {
1290 APPLY_FUNC(a, b, length, unsigned long, PROD_OP);
1291 } else if (*datatype == MPI_UNSIGNED_CHAR) {
1292 APPLY_FUNC(a, b, length, unsigned char, PROD_OP);
1293 } else if (*datatype == MPI_FLOAT) {
1294 APPLY_FUNC(a, b, length, float, PROD_OP);
1295 } else if (*datatype == MPI_DOUBLE) {
1296 APPLY_FUNC(a, b, length, double, PROD_OP);
1297 } else if (*datatype == MPI_LONG_DOUBLE) {
1298 APPLY_FUNC(a, b, length, long double, PROD_OP);
1299 } else if (*datatype == MPI_C_FLOAT_COMPLEX) {
1300 APPLY_FUNC(a, b, length, float _Complex, PROD_OP);
1301 } else if (*datatype == MPI_C_DOUBLE_COMPLEX) {
1302 APPLY_FUNC(a, b, length, double _Complex, PROD_OP);
1303 } else if (*datatype == MPI_C_LONG_DOUBLE_COMPLEX) {
1304 APPLY_FUNC(a, b, length, long double _Complex, PROD_OP);
1308 static void land_func(void *a, void *b, int *length, MPI_Datatype * datatype)
1310 if (*datatype == MPI_CHAR) {
1311 APPLY_FUNC(a, b, length, char, LAND_OP);
1312 } else if (*datatype == MPI_SHORT) {
1313 APPLY_FUNC(a, b, length, short, LAND_OP);
1314 } else if (*datatype == MPI_INT) {
1315 APPLY_FUNC(a, b, length, int, LAND_OP);
1316 } else if (*datatype == MPI_LONG) {
1317 APPLY_FUNC(a, b, length, long, LAND_OP);
1318 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1319 APPLY_FUNC(a, b, length, unsigned short, LAND_OP);
1320 } else if (*datatype == MPI_UNSIGNED) {
1321 APPLY_FUNC(a, b, length, unsigned int, LAND_OP);
1322 } else if (*datatype == MPI_UNSIGNED_LONG) {
1323 APPLY_FUNC(a, b, length, unsigned long, LAND_OP);
1324 } else if (*datatype == MPI_UNSIGNED_CHAR) {
1325 APPLY_FUNC(a, b, length, unsigned char, LAND_OP);
1326 } else if (*datatype == MPI_C_BOOL) {
1327 APPLY_FUNC(a, b, length, bool, LAND_OP);
1331 static void lor_func(void *a, void *b, int *length, MPI_Datatype * datatype)
1333 if (*datatype == MPI_CHAR) {
1334 APPLY_FUNC(a, b, length, char, LOR_OP);
1335 } else if (*datatype == MPI_SHORT) {
1336 APPLY_FUNC(a, b, length, short, LOR_OP);
1337 } else if (*datatype == MPI_INT) {
1338 APPLY_FUNC(a, b, length, int, LOR_OP);
1339 } else if (*datatype == MPI_LONG) {
1340 APPLY_FUNC(a, b, length, long, LOR_OP);
1341 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1342 APPLY_FUNC(a, b, length, unsigned short, LOR_OP);
1343 } else if (*datatype == MPI_UNSIGNED) {
1344 APPLY_FUNC(a, b, length, unsigned int, LOR_OP);
1345 } else if (*datatype == MPI_UNSIGNED_LONG) {
1346 APPLY_FUNC(a, b, length, unsigned long, LOR_OP);
1347 } else if (*datatype == MPI_UNSIGNED_CHAR) {
1348 APPLY_FUNC(a, b, length, unsigned char, LOR_OP);
1349 } else if (*datatype == MPI_C_BOOL) {
1350 APPLY_FUNC(a, b, length, bool, LOR_OP);
1354 static void lxor_func(void *a, void *b, int *length, MPI_Datatype * datatype)
1356 if (*datatype == MPI_CHAR) {
1357 APPLY_FUNC(a, b, length, char, LXOR_OP);
1358 } else if (*datatype == MPI_SHORT) {
1359 APPLY_FUNC(a, b, length, short, LXOR_OP);
1360 } else if (*datatype == MPI_INT) {
1361 APPLY_FUNC(a, b, length, int, LXOR_OP);
1362 } else if (*datatype == MPI_LONG) {
1363 APPLY_FUNC(a, b, length, long, LXOR_OP);
1364 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1365 APPLY_FUNC(a, b, length, unsigned short, LXOR_OP);
1366 } else if (*datatype == MPI_UNSIGNED) {
1367 APPLY_FUNC(a, b, length, unsigned int, LXOR_OP);
1368 } else if (*datatype == MPI_UNSIGNED_LONG) {
1369 APPLY_FUNC(a, b, length, unsigned long, LXOR_OP);
1370 } else if (*datatype == MPI_UNSIGNED_CHAR) {
1371 APPLY_FUNC(a, b, length, unsigned char, LXOR_OP);
1372 } else if (*datatype == MPI_C_BOOL) {
1373 APPLY_FUNC(a, b, length, bool, LXOR_OP);
1377 static void band_func(void *a, void *b, int *length, MPI_Datatype * datatype)
1379 if (*datatype == MPI_CHAR) {
1380 APPLY_FUNC(a, b, length, char, BAND_OP);
1381 }else if (*datatype == MPI_SHORT) {
1382 APPLY_FUNC(a, b, length, short, BAND_OP);
1383 } else if (*datatype == MPI_INT) {
1384 APPLY_FUNC(a, b, length, int, BAND_OP);
1385 } else if (*datatype == MPI_LONG) {
1386 APPLY_FUNC(a, b, length, long, BAND_OP);
1387 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1388 APPLY_FUNC(a, b, length, unsigned short, BAND_OP);
1389 } else if (*datatype == MPI_UNSIGNED) {
1390 APPLY_FUNC(a, b, length, unsigned int, BAND_OP);
1391 } else if (*datatype == MPI_UNSIGNED_LONG) {
1392 APPLY_FUNC(a, b, length, unsigned long, BAND_OP);
1393 } else if (*datatype == MPI_UNSIGNED_CHAR) {
1394 APPLY_FUNC(a, b, length, unsigned char, BAND_OP);
1395 } else if (*datatype == MPI_BYTE) {
1396 APPLY_FUNC(a, b, length, uint8_t, BAND_OP);
1400 static void bor_func(void *a, void *b, int *length, MPI_Datatype * datatype)
1402 if (*datatype == MPI_CHAR) {
1403 APPLY_FUNC(a, b, length, char, BOR_OP);
1404 } else if (*datatype == MPI_SHORT) {
1405 APPLY_FUNC(a, b, length, short, BOR_OP);
1406 } else if (*datatype == MPI_INT) {
1407 APPLY_FUNC(a, b, length, int, BOR_OP);
1408 } else if (*datatype == MPI_LONG) {
1409 APPLY_FUNC(a, b, length, long, BOR_OP);
1410 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1411 APPLY_FUNC(a, b, length, unsigned short, BOR_OP);
1412 } else if (*datatype == MPI_UNSIGNED) {
1413 APPLY_FUNC(a, b, length, unsigned int, BOR_OP);
1414 } else if (*datatype == MPI_UNSIGNED_LONG) {
1415 APPLY_FUNC(a, b, length, unsigned long, BOR_OP);
1416 } else if (*datatype == MPI_UNSIGNED_CHAR) {
1417 APPLY_FUNC(a, b, length, unsigned char, BOR_OP);
1418 } else if (*datatype == MPI_BYTE) {
1419 APPLY_FUNC(a, b, length, uint8_t, BOR_OP);
1423 static void bxor_func(void *a, void *b, int *length, MPI_Datatype * datatype)
1425 if (*datatype == MPI_CHAR) {
1426 APPLY_FUNC(a, b, length, char, BXOR_OP);
1427 } else if (*datatype == MPI_SHORT) {
1428 APPLY_FUNC(a, b, length, short, BXOR_OP);
1429 } else if (*datatype == MPI_INT) {
1430 APPLY_FUNC(a, b, length, int, BXOR_OP);
1431 } else if (*datatype == MPI_LONG) {
1432 APPLY_FUNC(a, b, length, long, BXOR_OP);
1433 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1434 APPLY_FUNC(a, b, length, unsigned short, BXOR_OP);
1435 } else if (*datatype == MPI_UNSIGNED) {
1436 APPLY_FUNC(a, b, length, unsigned int, BXOR_OP);
1437 } else if (*datatype == MPI_UNSIGNED_LONG) {
1438 APPLY_FUNC(a, b, length, unsigned long, BXOR_OP);
1439 } else if (*datatype == MPI_UNSIGNED_CHAR) {
1440 APPLY_FUNC(a, b, length, unsigned char, BXOR_OP);
1441 } else if (*datatype == MPI_BYTE) {
1442 APPLY_FUNC(a, b, length, uint8_t, BXOR_OP);
1446 static void minloc_func(void *a, void *b, int *length, MPI_Datatype * datatype)
1448 if (*datatype == MPI_FLOAT_INT) {
1449 APPLY_FUNC(a, b, length, float_int, MINLOC_OP);
1450 } else if (*datatype == MPI_LONG_INT) {
1451 APPLY_FUNC(a, b, length, long_int, MINLOC_OP);
1452 } else if (*datatype == MPI_DOUBLE_INT) {
1453 APPLY_FUNC(a, b, length, double_int, MINLOC_OP);
1454 } else if (*datatype == MPI_SHORT_INT) {
1455 APPLY_FUNC(a, b, length, short_int, MINLOC_OP);
1456 } else if (*datatype == MPI_2LONG) {
1457 APPLY_FUNC(a, b, length, long_long, MINLOC_OP);
1458 } else if (*datatype == MPI_2INT) {
1459 APPLY_FUNC(a, b, length, int_int, MINLOC_OP);
1460 } else if (*datatype == MPI_LONG_DOUBLE_INT) {
1461 APPLY_FUNC(a, b, length, long_double_int, MINLOC_OP);
1462 } else if (*datatype == MPI_2FLOAT) {
1463 APPLY_FUNC(a, b, length, float_float, MINLOC_OP);
1464 } else if (*datatype == MPI_2DOUBLE) {
1465 APPLY_FUNC(a, b, length, double_double, MINLOC_OP);
1469 static void maxloc_func(void *a, void *b, int *length, MPI_Datatype * datatype)
1471 if (*datatype == MPI_FLOAT_INT) {
1472 APPLY_FUNC(a, b, length, float_int, MAXLOC_OP);
1473 } else if (*datatype == MPI_LONG_INT) {
1474 APPLY_FUNC(a, b, length, long_int, MAXLOC_OP);
1475 } else if (*datatype == MPI_DOUBLE_INT) {
1476 APPLY_FUNC(a, b, length, double_int, MAXLOC_OP);
1477 } else if (*datatype == MPI_SHORT_INT) {
1478 APPLY_FUNC(a, b, length, short_int, MAXLOC_OP);
1479 } else if (*datatype == MPI_2LONG) {
1480 APPLY_FUNC(a, b, length, long_long, MAXLOC_OP);
1481 } else if (*datatype == MPI_2INT) {
1482 APPLY_FUNC(a, b, length, int_int, MAXLOC_OP);
1483 } else if (*datatype == MPI_LONG_DOUBLE_INT) {
1484 APPLY_FUNC(a, b, length, long_double_int, MAXLOC_OP);
1485 } else if (*datatype == MPI_2FLOAT) {
1486 APPLY_FUNC(a, b, length, float_float, MAXLOC_OP);
1487 } else if (*datatype == MPI_2DOUBLE) {
1488 APPLY_FUNC(a, b, length, double_double, MAXLOC_OP);
1492 static void replace_func(void *a, void *b, int *length, MPI_Datatype * datatype)
1494 memcpy(b, a, *length * smpi_datatype_size(*datatype));
1497 #define CREATE_MPI_OP(name, func) \
1498 static s_smpi_mpi_op_t mpi_##name = { &(func) /* func */, true }; \
1499 MPI_Op name = &mpi_##name;
1501 CREATE_MPI_OP(MPI_MAX, max_func);
1502 CREATE_MPI_OP(MPI_MIN, min_func);
1503 CREATE_MPI_OP(MPI_SUM, sum_func);
1504 CREATE_MPI_OP(MPI_PROD, prod_func);
1505 CREATE_MPI_OP(MPI_LAND, land_func);
1506 CREATE_MPI_OP(MPI_LOR, lor_func);
1507 CREATE_MPI_OP(MPI_LXOR, lxor_func);
1508 CREATE_MPI_OP(MPI_BAND, band_func);
1509 CREATE_MPI_OP(MPI_BOR, bor_func);
1510 CREATE_MPI_OP(MPI_BXOR, bxor_func);
1511 CREATE_MPI_OP(MPI_MAXLOC, maxloc_func);
1512 CREATE_MPI_OP(MPI_MINLOC, minloc_func);
1513 CREATE_MPI_OP(MPI_REPLACE, replace_func);
1515 MPI_Op smpi_op_new(MPI_User_function * function, int commute)
1518 op = xbt_new(s_smpi_mpi_op_t, 1);
1519 op->func = function;
1520 op-> is_commute = commute;
1524 int smpi_op_is_commute(MPI_Op op)
1526 return (op==MPI_OP_NULL) ? 1 : op-> is_commute;
1529 void smpi_op_destroy(MPI_Op op)
1534 void smpi_op_apply(MPI_Op op, void *invec, void *inoutvec, int *len, MPI_Datatype * datatype)
1539 if(smpi_privatize_global_variables){//we need to switch as the called function may silently touch global variables
1540 XBT_DEBUG("Applying operation, switch to the right data frame ");
1541 smpi_switch_data_segment(smpi_process_index());
1544 if(!smpi_process_get_replaying())
1545 op->func(invec, inoutvec, len, datatype);
1548 int smpi_type_attr_delete(MPI_Datatype type, int keyval){
1549 smpi_type_key_elem elem =
1550 static_cast<smpi_type_key_elem>(xbt_dict_get_or_null_ext(smpi_type_keyvals, (const char*)&keyval, sizeof(int)));
1553 if(elem->delete_fn!=MPI_NULL_DELETE_FN){
1556 if(smpi_type_attr_get(type, keyval, &value, &flag)==MPI_SUCCESS){
1557 int ret = elem->delete_fn(type, keyval, value, &flag);
1558 if(ret!=MPI_SUCCESS) return ret;
1561 if(type->attributes==NULL)
1564 xbt_dict_remove_ext(type->attributes, (const char*)&keyval, sizeof(int));
1568 int smpi_type_attr_get(MPI_Datatype type, int keyval, void* attr_value, int* flag){
1569 smpi_type_key_elem elem =
1570 static_cast<smpi_type_key_elem>(xbt_dict_get_or_null_ext(smpi_type_keyvals, (const char*)&keyval, sizeof(int)));
1574 if(type->attributes==NULL){
1579 *(void**)attr_value = xbt_dict_get_ext(type->attributes, (const char*)&keyval, sizeof(int));
1589 int smpi_type_attr_put(MPI_Datatype type, int keyval, void* attr_value){
1590 if(!smpi_type_keyvals)
1591 smpi_type_keyvals = xbt_dict_new();
1592 smpi_type_key_elem elem =
1593 static_cast<smpi_type_key_elem>(xbt_dict_get_or_null_ext(smpi_type_keyvals, (const char*)&keyval, sizeof(int)));
1598 smpi_type_attr_get(type, keyval, &value, &flag);
1599 if(flag && elem->delete_fn!=MPI_NULL_DELETE_FN){
1600 int ret = elem->delete_fn(type, keyval, value, &flag);
1601 if(ret!=MPI_SUCCESS) return ret;
1603 if(type->attributes==NULL)
1604 type->attributes=xbt_dict_new();
1606 xbt_dict_set_ext(type->attributes, (const char*)&keyval, sizeof(int), attr_value, NULL);
1610 int smpi_type_keyval_create(MPI_Type_copy_attr_function* copy_fn, MPI_Type_delete_attr_function* delete_fn, int* keyval,
1612 if(!smpi_type_keyvals)
1613 smpi_type_keyvals = xbt_dict_new();
1615 smpi_type_key_elem value = (smpi_type_key_elem) xbt_new0(s_smpi_mpi_type_key_elem_t,1);
1617 value->copy_fn=copy_fn;
1618 value->delete_fn=delete_fn;
1620 *keyval = type_keyval_id;
1621 xbt_dict_set_ext(smpi_type_keyvals,(const char*)keyval, sizeof(int),(void*)value, NULL);
1626 int smpi_type_keyval_free(int* keyval){
1627 smpi_type_key_elem elem =
1628 static_cast<smpi_type_key_elem>(xbt_dict_get_or_null_ext(smpi_type_keyvals, (const char*)keyval, sizeof(int)));
1632 xbt_dict_remove_ext(smpi_type_keyvals, (const char*)keyval, sizeof(int));
1637 int smpi_mpi_pack(void* inbuf, int incount, MPI_Datatype type, void* outbuf, int outcount, int* position,MPI_Comm comm){
1638 size_t size = smpi_datatype_size(type);
1639 if (outcount - *position < incount*static_cast<int>(size))
1640 return MPI_ERR_BUFFER;
1641 smpi_datatype_copy(inbuf, incount, type, (char*)outbuf + *position, outcount, MPI_CHAR);
1642 *position += incount * size;
1646 int smpi_mpi_unpack(void* inbuf, int insize, int* position, void* outbuf, int outcount, MPI_Datatype type,MPI_Comm comm){
1647 int size = static_cast<int>(smpi_datatype_size(type));
1648 if (outcount*size> insize)
1649 return MPI_ERR_BUFFER;
1650 smpi_datatype_copy((char*)inbuf + *position, insize, MPI_CHAR, outbuf, outcount, type);
1651 *position += outcount * size;