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 has_subtype*/ \
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 has_subtype*/ \
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->has_subtype){
182 //FIXME: may copy too much information.
183 (*new_t)->substruct=xbt_new(s_smpi_mpi_struct_t,1);
184 memcpy((*new_t)->substruct, datatype->substruct, sizeof(s_smpi_mpi_struct_t));
187 (*new_t)->name = xbt_strdup(datatype->name);
188 if(datatype->attributes !=NULL){
189 (*new_t)->attributes=xbt_dict_new();
190 xbt_dict_cursor_t cursor = NULL;
195 xbt_dict_foreach(datatype->attributes, cursor, key, value_in){
196 smpi_type_key_elem elem =
197 static_cast<smpi_type_key_elem>(xbt_dict_get_or_null_ext(smpi_type_keyvals, (const char*)key, sizeof(int)));
198 if(elem && elem->copy_fn!=MPI_NULL_COPY_FN){
199 ret = elem->copy_fn(datatype, *key, NULL, value_in, &value_out, &flag );
200 if(ret!=MPI_SUCCESS){
201 *new_t=MPI_DATATYPE_NULL;
205 xbt_dict_set_ext((*new_t)->attributes, (const char*)key, sizeof(int),value_out, NULL);
212 int smpi_datatype_extent(MPI_Datatype datatype, MPI_Aint * lb, MPI_Aint * extent)
214 if(datatype == MPI_DATATYPE_NULL){
220 *extent = datatype->ub - datatype->lb;
224 MPI_Aint smpi_datatype_get_extent(MPI_Datatype datatype){
225 if(datatype == MPI_DATATYPE_NULL){
228 return datatype->ub - datatype->lb;
231 void smpi_datatype_get_name(MPI_Datatype datatype, char* name, int* length){
232 *length = strlen(datatype->name);
233 strcpy(name, datatype->name);
236 void smpi_datatype_set_name(MPI_Datatype datatype, char* name){
237 datatype->name = xbt_strdup(name);;
240 int smpi_datatype_copy(void *sendbuf, int sendcount, MPI_Datatype sendtype,
241 void *recvbuf, int recvcount, MPI_Datatype recvtype)
244 if(smpi_privatize_global_variables){
245 smpi_switch_data_segment(smpi_process_index());
247 /* First check if we really have something to do */
248 if (recvcount > 0 && recvbuf != sendbuf) {
249 /* FIXME: treat packed cases */
250 sendcount *= smpi_datatype_size(sendtype);
251 recvcount *= smpi_datatype_size(recvtype);
252 count = sendcount < recvcount ? sendcount : recvcount;
254 if(sendtype->has_subtype == 0 && recvtype->has_subtype == 0) {
255 if(!smpi_process_get_replaying()) memcpy(recvbuf, sendbuf, count);
257 else if (sendtype->has_subtype == 0)
259 s_smpi_subtype_t *subtype = static_cast<s_smpi_subtype_t*>(recvtype->substruct);
260 subtype->unserialize( sendbuf, recvbuf, recvcount/smpi_datatype_size(recvtype), subtype, MPI_REPLACE);
262 else if (recvtype->has_subtype == 0)
264 s_smpi_subtype_t *subtype = static_cast<s_smpi_subtype_t*>(sendtype->substruct);
265 subtype->serialize(sendbuf, recvbuf, sendcount/smpi_datatype_size(sendtype), subtype);
267 s_smpi_subtype_t *subtype = static_cast<s_smpi_subtype_t*>(sendtype->substruct);
269 void * buf_tmp = xbt_malloc(count);
271 subtype->serialize( sendbuf, buf_tmp,count/smpi_datatype_size(sendtype), subtype);
272 subtype = static_cast<s_smpi_subtype_t*>(recvtype->substruct);
273 subtype->unserialize( buf_tmp, recvbuf,count/smpi_datatype_size(recvtype), subtype, MPI_REPLACE);
279 return sendcount > recvcount ? MPI_ERR_TRUNCATE : MPI_SUCCESS;
283 * Copies noncontiguous data into contiguous memory.
284 * @param contiguous_vector - output vector
285 * @param noncontiguous_vector - input vector
286 * @param type - pointer contening :
287 * - stride - stride of between noncontiguous data
288 * - block_length - the width or height of blocked matrix
289 * - count - the number of rows of matrix
291 void serialize_vector( const void *noncontiguous_vector, void *contiguous_vector, int count, void *type)
293 s_smpi_mpi_vector_t* type_c = (s_smpi_mpi_vector_t*)type;
295 char* contiguous_vector_char = (char*)contiguous_vector;
296 char* noncontiguous_vector_char = (char*)noncontiguous_vector;
298 for (i = 0; i < type_c->block_count * count; i++) {
299 if (type_c->old_type->has_subtype == 0)
300 memcpy(contiguous_vector_char, noncontiguous_vector_char, type_c->block_length * type_c->size_oldtype);
302 ((s_smpi_subtype_t*)type_c->old_type->substruct)->serialize( noncontiguous_vector_char,
303 contiguous_vector_char,
304 type_c->block_length, type_c->old_type->substruct);
306 contiguous_vector_char += type_c->block_length*type_c->size_oldtype;
307 if((i+1)%type_c->block_count ==0)
308 noncontiguous_vector_char += type_c->block_length*smpi_datatype_get_extent(type_c->old_type);
310 noncontiguous_vector_char += type_c->block_stride*smpi_datatype_get_extent(type_c->old_type);
315 * Copies contiguous data into noncontiguous memory.
316 * @param noncontiguous_vector - output vector
317 * @param contiguous_vector - input vector
318 * @param type - pointer contening :
319 * - stride - stride of between noncontiguous data
320 * - block_length - the width or height of blocked matrix
321 * - count - the number of rows of matrix
323 void unserialize_vector( const void *contiguous_vector, void *noncontiguous_vector, int count, void *type, MPI_Op op)
325 s_smpi_mpi_vector_t* type_c = (s_smpi_mpi_vector_t*)type;
328 char* contiguous_vector_char = (char*)contiguous_vector;
329 char* noncontiguous_vector_char = (char*)noncontiguous_vector;
331 for (i = 0; i < type_c->block_count * count; i++) {
332 if (type_c->old_type->has_subtype == 0)
333 smpi_op_apply(op, contiguous_vector_char, noncontiguous_vector_char, &type_c->block_length,
335 /* memcpy(noncontiguous_vector_char,
336 contiguous_vector_char, type_c->block_length * type_c->size_oldtype);*/
338 ((s_smpi_subtype_t*)type_c->old_type->substruct)->unserialize(contiguous_vector_char, noncontiguous_vector_char,
339 type_c->block_length,type_c->old_type->substruct,
341 contiguous_vector_char += type_c->block_length*type_c->size_oldtype;
342 if((i+1)%type_c->block_count ==0)
343 noncontiguous_vector_char += type_c->block_length*smpi_datatype_get_extent(type_c->old_type);
345 noncontiguous_vector_char += type_c->block_stride*smpi_datatype_get_extent(type_c->old_type);
349 /* Create a Sub type vector to be able to serialize and unserialize it the structure s_smpi_mpi_vector_t is derived
350 * from s_smpi_subtype which required the functions unserialize and serialize */
351 s_smpi_mpi_vector_t* smpi_datatype_vector_create( int block_stride, int block_length, int block_count,
352 MPI_Datatype old_type, int size_oldtype){
353 s_smpi_mpi_vector_t *new_t= xbt_new(s_smpi_mpi_vector_t,1);
354 new_t->base.serialize = &serialize_vector;
355 new_t->base.unserialize = &unserialize_vector;
356 new_t->base.subtype_free = &free_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 has_subtype, void *struct_type,
368 MPI_Datatype new_t= xbt_new(s_smpi_mpi_datatype_t,1);
371 new_t->has_subtype = size>0? has_subtype: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)->has_subtype == 1){
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);
417 *type = MPI_DATATYPE_NULL;
420 void smpi_datatype_use(MPI_Datatype type){
421 if(type)type->in_use++;
425 MC_ignore(&(type->in_use), sizeof(type->in_use));
429 void smpi_datatype_unuse(MPI_Datatype type){
430 if (type->in_use > 0)
433 if(type && type->in_use == 0 && (type->flags & DT_FLAG_DESTROYED))
434 smpi_datatype_free(&type);
438 MC_ignore(&(type->in_use), sizeof(type->in_use));
442 /*Contiguous Implementation*/
444 /* Copies noncontiguous data into contiguous memory.
445 * @param contiguous_hvector - output hvector
446 * @param noncontiguous_hvector - input hvector
447 * @param type - pointer contening :
448 * - stride - stride of between noncontiguous data, in bytes
449 * - block_length - the width or height of blocked matrix
450 * - count - the number of rows of matrix
452 void serialize_contiguous( const void *noncontiguous_hvector, void *contiguous_hvector, int count, void *type)
454 s_smpi_mpi_contiguous_t* type_c = (s_smpi_mpi_contiguous_t*)type;
455 char* contiguous_vector_char = (char*)contiguous_hvector;
456 char* noncontiguous_vector_char = (char*)noncontiguous_hvector+type_c->lb;
457 memcpy(contiguous_vector_char, noncontiguous_vector_char, count* type_c->block_count * type_c->size_oldtype);
459 /* Copies contiguous data into noncontiguous memory.
460 * @param noncontiguous_vector - output hvector
461 * @param contiguous_vector - input hvector
462 * @param type - pointer contening :
463 * - stride - stride of between noncontiguous data, in bytes
464 * - block_length - the width or height of blocked matrix
465 * - count - the number of rows of matrix
467 void unserialize_contiguous(const void *contiguous_vector, void *noncontiguous_vector, int count, void *type, MPI_Op op)
469 s_smpi_mpi_contiguous_t* type_c = (s_smpi_mpi_contiguous_t*)type;
470 char* contiguous_vector_char = (char*)contiguous_vector;
471 char* noncontiguous_vector_char = (char*)noncontiguous_vector+type_c->lb;
472 int n= count* type_c->block_count;
473 smpi_op_apply(op, contiguous_vector_char, noncontiguous_vector_char, &n, &type_c->old_type);
474 /*memcpy(noncontiguous_vector_char, contiguous_vector_char, count* type_c->block_count * type_c->size_oldtype);*/
477 void free_contiguous(MPI_Datatype* d){
478 smpi_datatype_unuse(((s_smpi_mpi_indexed_t *)(*d)->substruct)->old_type);
481 /* Create a Sub type contiguous to be able to serialize and unserialize it the structure s_smpi_mpi_contiguous_t is
482 * erived from s_smpi_subtype which required the functions unserialize and serialize */
483 s_smpi_mpi_contiguous_t* smpi_datatype_contiguous_create( MPI_Aint lb, int block_count, MPI_Datatype old_type,
485 s_smpi_mpi_contiguous_t *new_t= xbt_new(s_smpi_mpi_contiguous_t,1);
486 new_t->base.serialize = &serialize_contiguous;
487 new_t->base.unserialize = &unserialize_contiguous;
488 new_t->base.subtype_free = &free_contiguous;
490 new_t->block_count = block_count;
491 new_t->old_type = old_type;
492 new_t->size_oldtype = size_oldtype;
493 smpi_datatype_use(old_type);
497 int smpi_datatype_contiguous(int count, MPI_Datatype old_type, MPI_Datatype* new_type, MPI_Aint lb)
500 if(old_type->has_subtype){
501 //handle this case as a hvector with stride equals to the extent of the datatype
502 return smpi_datatype_hvector(count, 1, smpi_datatype_get_extent(old_type), old_type, new_type);
505 s_smpi_mpi_contiguous_t* subtype = smpi_datatype_contiguous_create( lb, count, old_type,smpi_datatype_size(old_type));
507 smpi_datatype_create(new_type, count * smpi_datatype_size(old_type),lb,lb + count * smpi_datatype_size(old_type),
508 1,subtype, DT_FLAG_CONTIGUOUS);
513 int smpi_datatype_vector(int count, int blocklen, int stride, MPI_Datatype old_type, MPI_Datatype* new_type)
516 if (blocklen<0) return MPI_ERR_ARG;
520 lb=smpi_datatype_lb(old_type);
521 ub=((count-1)*stride+blocklen-1)*smpi_datatype_get_extent(old_type)+smpi_datatype_ub(old_type);
523 if(old_type->has_subtype || stride != blocklen){
525 s_smpi_mpi_vector_t* subtype = smpi_datatype_vector_create(stride, blocklen, count, old_type,
526 smpi_datatype_size(old_type));
527 smpi_datatype_create(new_type, count * (blocklen) * smpi_datatype_size(old_type), lb, ub, 1, subtype,
531 /* in this situation the data are contignous thus it's not required to serialize and unserialize it*/
532 smpi_datatype_create(new_type, count * blocklen * smpi_datatype_size(old_type), 0, ((count -1) * stride + blocklen)*
533 smpi_datatype_size(old_type), 0, NULL, DT_FLAG_VECTOR|DT_FLAG_CONTIGUOUS);
539 void free_vector(MPI_Datatype* d){
540 smpi_datatype_unuse(((s_smpi_mpi_indexed_t *)(*d)->substruct)->old_type);
543 /* Hvector Implementation - Vector with stride in bytes */
545 /* Copies noncontiguous data into contiguous memory.
546 * @param contiguous_hvector - output hvector
547 * @param noncontiguous_hvector - input hvector
548 * @param type - pointer contening :
549 * - stride - stride of between noncontiguous data, in bytes
550 * - block_length - the width or height of blocked matrix
551 * - count - the number of rows of matrix
553 void serialize_hvector( const void *noncontiguous_hvector, void *contiguous_hvector, int count, void *type)
555 s_smpi_mpi_hvector_t* type_c = (s_smpi_mpi_hvector_t*)type;
557 char* contiguous_vector_char = (char*)contiguous_hvector;
558 char* noncontiguous_vector_char = (char*)noncontiguous_hvector;
560 for (i = 0; i < type_c->block_count * count; i++) {
561 if (type_c->old_type->has_subtype == 0)
562 memcpy(contiguous_vector_char, noncontiguous_vector_char, type_c->block_length * type_c->size_oldtype);
564 ((s_smpi_subtype_t*)type_c->old_type->substruct)->serialize( noncontiguous_vector_char,
565 contiguous_vector_char,
566 type_c->block_length, type_c->old_type->substruct);
568 contiguous_vector_char += type_c->block_length*type_c->size_oldtype;
569 if((i+1)%type_c->block_count ==0)
570 noncontiguous_vector_char += type_c->block_length*type_c->size_oldtype;
572 noncontiguous_vector_char += type_c->block_stride;
575 /* Copies contiguous data into noncontiguous memory.
576 * @param noncontiguous_vector - output hvector
577 * @param contiguous_vector - input hvector
578 * @param type - pointer contening :
579 * - stride - stride of between noncontiguous data, in bytes
580 * - block_length - the width or height of blocked matrix
581 * - count - the number of rows of matrix
583 void unserialize_hvector( const void *contiguous_vector, void *noncontiguous_vector, int count, void *type, MPI_Op op)
585 s_smpi_mpi_hvector_t* type_c = (s_smpi_mpi_hvector_t*)type;
588 char* contiguous_vector_char = (char*)contiguous_vector;
589 char* noncontiguous_vector_char = (char*)noncontiguous_vector;
591 for (i = 0; i < type_c->block_count * count; i++) {
592 if (type_c->old_type->has_subtype == 0)
593 smpi_op_apply(op, contiguous_vector_char, noncontiguous_vector_char, &type_c->block_length, &type_c->old_type);
594 /*memcpy(noncontiguous_vector_char,
595 contiguous_vector_char, type_c->block_length * type_c->size_oldtype);*/
597 ((s_smpi_subtype_t*)type_c->old_type->substruct)->unserialize( contiguous_vector_char, noncontiguous_vector_char,
598 type_c->block_length, type_c->old_type->substruct,
600 contiguous_vector_char += type_c->block_length*type_c->size_oldtype;
601 if((i+1)%type_c->block_count ==0)
602 noncontiguous_vector_char += type_c->block_length*type_c->size_oldtype;
604 noncontiguous_vector_char += type_c->block_stride;
608 /* Create a Sub type vector to be able to serialize and unserialize it the structure s_smpi_mpi_vector_t is derived
609 * from s_smpi_subtype which required the functions unserialize and serialize
612 s_smpi_mpi_hvector_t* smpi_datatype_hvector_create( MPI_Aint block_stride, int block_length, int block_count,
613 MPI_Datatype old_type, int size_oldtype){
614 s_smpi_mpi_hvector_t *new_t= xbt_new(s_smpi_mpi_hvector_t,1);
615 new_t->base.serialize = &serialize_hvector;
616 new_t->base.unserialize = &unserialize_hvector;
617 new_t->base.subtype_free = &free_hvector;
618 new_t->block_stride = block_stride;
619 new_t->block_length = block_length;
620 new_t->block_count = block_count;
621 new_t->old_type = old_type;
622 new_t->size_oldtype = size_oldtype;
623 smpi_datatype_use(old_type);
627 //do nothing for vector types
628 void free_hvector(MPI_Datatype* d){
629 smpi_datatype_unuse(((s_smpi_mpi_indexed_t *)(*d)->substruct)->old_type);
632 int smpi_datatype_hvector(int count, int blocklen, MPI_Aint stride, MPI_Datatype old_type, MPI_Datatype* new_type)
635 if (blocklen<0) return MPI_ERR_ARG;
639 lb=smpi_datatype_lb(old_type);
640 ub=((count-1)*stride)+(blocklen-1)*smpi_datatype_get_extent(old_type)+smpi_datatype_ub(old_type);
642 if(old_type->has_subtype || stride != blocklen*smpi_datatype_get_extent(old_type)){
643 s_smpi_mpi_hvector_t* subtype = smpi_datatype_hvector_create( stride, blocklen, count, old_type,
644 smpi_datatype_size(old_type));
646 smpi_datatype_create(new_type, count * blocklen * smpi_datatype_size(old_type), lb,ub, 1, subtype, DT_FLAG_VECTOR);
649 smpi_datatype_create(new_type, count * blocklen * smpi_datatype_size(old_type),0,count * blocklen *
650 smpi_datatype_size(old_type), 0, NULL, DT_FLAG_VECTOR|DT_FLAG_CONTIGUOUS);
656 /* Indexed Implementation */
658 /* Copies noncontiguous data into contiguous memory.
659 * @param contiguous_indexed - output indexed
660 * @param noncontiguous_indexed - input indexed
661 * @param type - pointer contening :
662 * - block_lengths - the width or height of blocked matrix
663 * - block_indices - indices of each data, in element
664 * - count - the number of rows of matrix
666 void serialize_indexed( const void *noncontiguous_indexed, void *contiguous_indexed, int count, void *type)
668 s_smpi_mpi_indexed_t* type_c = (s_smpi_mpi_indexed_t*)type;
670 char* contiguous_indexed_char = (char*)contiguous_indexed;
671 char* noncontiguous_indexed_char = (char*)noncontiguous_indexed+type_c->block_indices[0] * type_c->size_oldtype;
672 for(j=0; j<count;j++){
673 for (i = 0; i < type_c->block_count; i++) {
674 if (type_c->old_type->has_subtype == 0)
675 memcpy(contiguous_indexed_char, noncontiguous_indexed_char, type_c->block_lengths[i] * type_c->size_oldtype);
677 ((s_smpi_subtype_t*)type_c->old_type->substruct)->serialize( noncontiguous_indexed_char,
678 contiguous_indexed_char,
679 type_c->block_lengths[i],
680 type_c->old_type->substruct);
682 contiguous_indexed_char += type_c->block_lengths[i]*type_c->size_oldtype;
683 if (i<type_c->block_count-1)
684 noncontiguous_indexed_char =
685 (char*)noncontiguous_indexed + type_c->block_indices[i+1]*smpi_datatype_get_extent(type_c->old_type);
687 noncontiguous_indexed_char += type_c->block_lengths[i]*smpi_datatype_get_extent(type_c->old_type);
689 noncontiguous_indexed=(void*)noncontiguous_indexed_char;
692 /* Copies contiguous data into noncontiguous memory.
693 * @param noncontiguous_indexed - output indexed
694 * @param contiguous_indexed - input indexed
695 * @param type - pointer contening :
696 * - block_lengths - the width or height of blocked matrix
697 * - block_indices - indices of each data, in element
698 * - count - the number of rows of matrix
700 void unserialize_indexed( const void *contiguous_indexed, void *noncontiguous_indexed, int count, void *type, MPI_Op op)
702 s_smpi_mpi_indexed_t* type_c = (s_smpi_mpi_indexed_t*)type;
704 char* contiguous_indexed_char = (char*)contiguous_indexed;
705 char* noncontiguous_indexed_char =
706 (char*)noncontiguous_indexed+type_c->block_indices[0]*smpi_datatype_get_extent(type_c->old_type);
707 for(j=0; j<count;j++){
708 for (i = 0; i < type_c->block_count; i++) {
709 if (type_c->old_type->has_subtype == 0)
710 smpi_op_apply(op, contiguous_indexed_char, noncontiguous_indexed_char, &type_c->block_lengths[i],
712 /*memcpy(noncontiguous_indexed_char ,
713 contiguous_indexed_char, type_c->block_lengths[i] * type_c->size_oldtype);*/
715 ((s_smpi_subtype_t*)type_c->old_type->substruct)->unserialize( contiguous_indexed_char,
716 noncontiguous_indexed_char,
717 type_c->block_lengths[i],
718 type_c->old_type->substruct, op);
720 contiguous_indexed_char += type_c->block_lengths[i]*type_c->size_oldtype;
721 if (i<type_c->block_count-1)
722 noncontiguous_indexed_char =
723 (char*)noncontiguous_indexed + type_c->block_indices[i+1]*smpi_datatype_get_extent(type_c->old_type);
725 noncontiguous_indexed_char += type_c->block_lengths[i]*smpi_datatype_get_extent(type_c->old_type);
727 noncontiguous_indexed=(void*)noncontiguous_indexed_char;
731 void free_indexed(MPI_Datatype* type){
732 xbt_free(((s_smpi_mpi_indexed_t *)(*type)->substruct)->block_lengths);
733 xbt_free(((s_smpi_mpi_indexed_t *)(*type)->substruct)->block_indices);
734 smpi_datatype_unuse(((s_smpi_mpi_indexed_t *)(*type)->substruct)->old_type);
737 /* Create a Sub type indexed to be able to serialize and unserialize it the structure s_smpi_mpi_indexed_t is derived
738 * from s_smpi_subtype which required the functions unserialize and serialize */
739 s_smpi_mpi_indexed_t* smpi_datatype_indexed_create( int* block_lengths, int* block_indices, int block_count,
740 MPI_Datatype old_type, int size_oldtype){
741 s_smpi_mpi_indexed_t *new_t= xbt_new(s_smpi_mpi_indexed_t,1);
742 new_t->base.serialize = &serialize_indexed;
743 new_t->base.unserialize = &unserialize_indexed;
744 new_t->base.subtype_free = &free_indexed;
745 //TODO : add a custom function for each time to clean these
746 new_t->block_lengths= xbt_new(int, block_count);
747 new_t->block_indices= xbt_new(int, block_count);
749 for(i=0;i<block_count;i++){
750 new_t->block_lengths[i]=block_lengths[i];
751 new_t->block_indices[i]=block_indices[i];
753 new_t->block_count = block_count;
754 smpi_datatype_use(old_type);
755 new_t->old_type = old_type;
756 new_t->size_oldtype = size_oldtype;
760 int smpi_datatype_indexed(int count, int* blocklens, int* indices, MPI_Datatype old_type, MPI_Datatype* new_type)
769 lb=indices[0]*smpi_datatype_get_extent(old_type);
770 ub=indices[0]*smpi_datatype_get_extent(old_type) + blocklens[0]*smpi_datatype_ub(old_type);
773 for(i=0; i< count; i++){
776 size += blocklens[i];
778 if(indices[i]*smpi_datatype_get_extent(old_type)+smpi_datatype_lb(old_type)<lb)
779 lb = indices[i]*smpi_datatype_get_extent(old_type)+smpi_datatype_lb(old_type);
780 if(indices[i]*smpi_datatype_get_extent(old_type)+blocklens[i]*smpi_datatype_ub(old_type)>ub)
781 ub = indices[i]*smpi_datatype_get_extent(old_type)+blocklens[i]*smpi_datatype_ub(old_type);
783 if ( (i< count -1) && (indices[i]+blocklens[i] != indices[i+1]) )contiguous=0;
785 if (old_type->has_subtype == 1)
789 s_smpi_mpi_indexed_t* subtype = smpi_datatype_indexed_create( blocklens, indices, count, old_type,
790 smpi_datatype_size(old_type));
791 smpi_datatype_create(new_type, size * smpi_datatype_size(old_type),lb,ub,1, subtype, DT_FLAG_DATA);
793 s_smpi_mpi_contiguous_t* subtype = smpi_datatype_contiguous_create( lb, size, old_type,
794 smpi_datatype_size(old_type));
795 smpi_datatype_create(new_type, size * smpi_datatype_size(old_type), lb, ub, 1, subtype,
796 DT_FLAG_DATA|DT_FLAG_CONTIGUOUS);
801 /* Hindexed Implementation - Indexed with indices in bytes */
803 /* Copies noncontiguous data into contiguous memory.
804 * @param contiguous_hindexed - output hindexed
805 * @param noncontiguous_hindexed - input hindexed
806 * @param type - pointer contening :
807 * - block_lengths - the width or height of blocked matrix
808 * - block_indices - indices of each data, in bytes
809 * - count - the number of rows of matrix
811 void serialize_hindexed( const void *noncontiguous_hindexed, void *contiguous_hindexed, int count, void *type)
813 s_smpi_mpi_hindexed_t* type_c = (s_smpi_mpi_hindexed_t*)type;
815 char* contiguous_hindexed_char = (char*)contiguous_hindexed;
816 char* noncontiguous_hindexed_char = (char*)noncontiguous_hindexed+ type_c->block_indices[0];
817 for(j=0; j<count;j++){
818 for (i = 0; i < type_c->block_count; i++) {
819 if (type_c->old_type->has_subtype == 0)
820 memcpy(contiguous_hindexed_char, noncontiguous_hindexed_char, type_c->block_lengths[i] * type_c->size_oldtype);
822 ((s_smpi_subtype_t*)type_c->old_type->substruct)->serialize( noncontiguous_hindexed_char,
823 contiguous_hindexed_char,
824 type_c->block_lengths[i],
825 type_c->old_type->substruct);
827 contiguous_hindexed_char += type_c->block_lengths[i]*type_c->size_oldtype;
828 if (i<type_c->block_count-1)
829 noncontiguous_hindexed_char = (char*)noncontiguous_hindexed + type_c->block_indices[i+1];
831 noncontiguous_hindexed_char += type_c->block_lengths[i]*smpi_datatype_get_extent(type_c->old_type);
833 noncontiguous_hindexed=(void*)noncontiguous_hindexed_char;
836 /* Copies contiguous data into noncontiguous memory.
837 * @param noncontiguous_hindexed - output hindexed
838 * @param contiguous_hindexed - input hindexed
839 * @param type - pointer contening :
840 * - block_lengths - the width or height of blocked matrix
841 * - block_indices - indices of each data, in bytes
842 * - count - the number of rows of matrix
844 void unserialize_hindexed( const void *contiguous_hindexed, void *noncontiguous_hindexed, int count, void *type,
847 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->has_subtype == 0)
855 smpi_op_apply(op, contiguous_hindexed_char, noncontiguous_hindexed_char, &type_c->block_lengths[i],
857 /*memcpy(noncontiguous_hindexed_char,contiguous_hindexed_char,type_c->block_lengths[i]*type_c->size_oldtype);*/
859 ((s_smpi_subtype_t*)type_c->old_type->substruct)->unserialize( contiguous_hindexed_char,
860 noncontiguous_hindexed_char,
861 type_c->block_lengths[i],
862 type_c->old_type->substruct, op);
864 contiguous_hindexed_char += type_c->block_lengths[i]*type_c->size_oldtype;
865 if (i<type_c->block_count-1)
866 noncontiguous_hindexed_char = (char*)noncontiguous_hindexed + type_c->block_indices[i+1];
868 noncontiguous_hindexed_char += type_c->block_lengths[i]*smpi_datatype_get_extent(type_c->old_type);
870 noncontiguous_hindexed=(void*)noncontiguous_hindexed_char;
874 void free_hindexed(MPI_Datatype* type){
875 xbt_free(((s_smpi_mpi_hindexed_t *)(*type)->substruct)->block_lengths);
876 xbt_free(((s_smpi_mpi_hindexed_t *)(*type)->substruct)->block_indices);
877 smpi_datatype_unuse(((s_smpi_mpi_indexed_t *)(*type)->substruct)->old_type);
880 /* Create a Sub type hindexed to be able to serialize and unserialize it the structure s_smpi_mpi_hindexed_t is derived
881 * from s_smpi_subtype which required the functions unserialize and serialize
883 s_smpi_mpi_hindexed_t* smpi_datatype_hindexed_create( int* block_lengths, MPI_Aint* block_indices, int block_count,
884 MPI_Datatype old_type, int size_oldtype){
885 s_smpi_mpi_hindexed_t *new_t= xbt_new(s_smpi_mpi_hindexed_t,1);
886 new_t->base.serialize = &serialize_hindexed;
887 new_t->base.unserialize = &unserialize_hindexed;
888 new_t->base.subtype_free = &free_hindexed;
889 //TODO : add a custom function for each time to clean these
890 new_t->block_lengths= xbt_new(int, block_count);
891 new_t->block_indices= xbt_new(MPI_Aint, block_count);
893 for(i=0;i<block_count;i++){
894 new_t->block_lengths[i]=block_lengths[i];
895 new_t->block_indices[i]=block_indices[i];
897 new_t->block_count = block_count;
898 new_t->old_type = old_type;
899 new_t->size_oldtype = size_oldtype;
903 int smpi_datatype_hindexed(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype old_type, MPI_Datatype* new_type)
912 lb=indices[0] + smpi_datatype_lb(old_type);
913 ub=indices[0] + blocklens[0]*smpi_datatype_ub(old_type);
915 for(i=0; i< count; i++){
918 size += blocklens[i];
920 if(indices[i]+smpi_datatype_lb(old_type)<lb) lb = indices[i]+smpi_datatype_lb(old_type);
921 if(indices[i]+blocklens[i]*smpi_datatype_ub(old_type)>ub) ub = indices[i]+blocklens[i]*smpi_datatype_ub(old_type);
923 if ( (i< count -1) && (indices[i]+blocklens[i]*static_cast<int>(smpi_datatype_size(old_type)) != indices[i+1]) )
926 if (old_type->has_subtype == 1 || lb!=0)
930 s_smpi_mpi_hindexed_t* subtype = smpi_datatype_hindexed_create( blocklens, indices, count, old_type,
931 smpi_datatype_size(old_type));
932 smpi_datatype_create(new_type, size * smpi_datatype_size(old_type), lb, ub ,1, subtype, DT_FLAG_DATA);
934 s_smpi_mpi_contiguous_t* subtype = smpi_datatype_contiguous_create(lb,size, old_type, smpi_datatype_size(old_type));
935 smpi_datatype_create(new_type, size * smpi_datatype_size(old_type), 0,size * smpi_datatype_size(old_type),
936 1, subtype, DT_FLAG_DATA|DT_FLAG_CONTIGUOUS);
942 /* struct Implementation - Indexed with indices in bytes */
944 /* Copies noncontiguous data into contiguous memory.
945 * @param contiguous_struct - output struct
946 * @param noncontiguous_struct - input struct
947 * @param type - pointer contening :
948 * - stride - stride of between noncontiguous data
949 * - block_length - the width or height of blocked matrix
950 * - count - the number of rows of matrix
952 void serialize_struct( const void *noncontiguous_struct, void *contiguous_struct, int count, void *type)
954 s_smpi_mpi_struct_t* type_c = (s_smpi_mpi_struct_t*)type;
956 char* contiguous_struct_char = (char*)contiguous_struct;
957 char* noncontiguous_struct_char = (char*)noncontiguous_struct+ type_c->block_indices[0];
958 for(j=0; j<count;j++){
959 for (i = 0; i < type_c->block_count; i++) {
960 if (type_c->old_types[i]->has_subtype == 0)
961 memcpy(contiguous_struct_char, noncontiguous_struct_char,
962 type_c->block_lengths[i] * smpi_datatype_size(type_c->old_types[i]));
964 ((s_smpi_subtype_t*)type_c->old_types[i]->substruct)->serialize( noncontiguous_struct_char,
965 contiguous_struct_char,
966 type_c->block_lengths[i],
967 type_c->old_types[i]->substruct);
970 contiguous_struct_char += type_c->block_lengths[i]*smpi_datatype_size(type_c->old_types[i]);
971 if (i<type_c->block_count-1)
972 noncontiguous_struct_char = (char*)noncontiguous_struct + type_c->block_indices[i+1];
973 else //let's hope this is MPI_UB ?
974 noncontiguous_struct_char += type_c->block_lengths[i]*smpi_datatype_get_extent(type_c->old_types[i]);
976 noncontiguous_struct=(void*)noncontiguous_struct_char;
980 /* Copies contiguous data into noncontiguous memory.
981 * @param noncontiguous_struct - output struct
982 * @param contiguous_struct - input struct
983 * @param type - pointer contening :
984 * - stride - stride of between noncontiguous data
985 * - block_length - the width or height of blocked matrix
986 * - count - the number of rows of matrix
988 void unserialize_struct( const void *contiguous_struct, void *noncontiguous_struct, int count, void *type, MPI_Op op)
990 s_smpi_mpi_struct_t* type_c = (s_smpi_mpi_struct_t*)type;
993 char* contiguous_struct_char = (char*)contiguous_struct;
994 char* noncontiguous_struct_char = (char*)noncontiguous_struct+ type_c->block_indices[0];
995 for(j=0; j<count;j++){
996 for (i = 0; i < type_c->block_count; i++) {
997 if (type_c->old_types[i]->has_subtype == 0)
998 smpi_op_apply(op, contiguous_struct_char, noncontiguous_struct_char, &type_c->block_lengths[i],
999 & type_c->old_types[i]);
1000 /*memcpy(noncontiguous_struct_char,
1001 contiguous_struct_char, type_c->block_lengths[i] * smpi_datatype_size(type_c->old_types[i]));*/
1003 ((s_smpi_subtype_t*)type_c->old_types[i]->substruct)->unserialize( contiguous_struct_char,
1004 noncontiguous_struct_char,
1005 type_c->block_lengths[i],
1006 type_c->old_types[i]->substruct, op);
1008 contiguous_struct_char += type_c->block_lengths[i]*smpi_datatype_size(type_c->old_types[i]);
1009 if (i<type_c->block_count-1)
1010 noncontiguous_struct_char = (char*)noncontiguous_struct + type_c->block_indices[i+1];
1012 noncontiguous_struct_char += type_c->block_lengths[i]*smpi_datatype_get_extent(type_c->old_types[i]);
1014 noncontiguous_struct=(void*)noncontiguous_struct_char;
1018 void free_struct(MPI_Datatype* type){
1019 xbt_free(((s_smpi_mpi_struct_t *)(*type)->substruct)->block_lengths);
1020 xbt_free(((s_smpi_mpi_struct_t *)(*type)->substruct)->block_indices);
1022 for (i = 0; i < ((s_smpi_mpi_struct_t *)(*type)->substruct)->block_count; i++)
1023 smpi_datatype_unuse(((s_smpi_mpi_struct_t *)(*type)->substruct)->old_types[i]);
1024 xbt_free(((s_smpi_mpi_struct_t *)(*type)->substruct)->old_types);
1027 /* Create a Sub type struct to be able to serialize and unserialize it the structure s_smpi_mpi_struct_t is derived
1028 * from s_smpi_subtype which required the functions unserialize and serialize
1030 s_smpi_mpi_struct_t* smpi_datatype_struct_create( int* block_lengths, MPI_Aint* block_indices, int block_count,
1031 MPI_Datatype* old_types){
1032 s_smpi_mpi_struct_t *new_t= xbt_new(s_smpi_mpi_struct_t,1);
1033 new_t->base.serialize = &serialize_struct;
1034 new_t->base.unserialize = &unserialize_struct;
1035 new_t->base.subtype_free = &free_struct;
1036 //TODO : add a custom function for each time to clean these
1037 new_t->block_lengths= xbt_new(int, block_count);
1038 new_t->block_indices= xbt_new(MPI_Aint, block_count);
1039 new_t->old_types= xbt_new(MPI_Datatype, block_count);
1041 for(i=0;i<block_count;i++){
1042 new_t->block_lengths[i]=block_lengths[i];
1043 new_t->block_indices[i]=block_indices[i];
1044 new_t->old_types[i]=old_types[i];
1045 smpi_datatype_use(new_t->old_types[i]);
1047 //new_t->block_lengths = block_lengths;
1048 //new_t->block_indices = block_indices;
1049 new_t->block_count = block_count;
1050 //new_t->old_types = old_types;
1054 int smpi_datatype_struct(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype* old_types, MPI_Datatype* new_type)
1063 lb=indices[0] + smpi_datatype_lb(old_types[0]);
1064 ub=indices[0] + blocklens[0]*smpi_datatype_ub(old_types[0]);
1068 for(i=0; i< count; i++){
1071 if (old_types[i]->has_subtype == 1)
1074 size += blocklens[i]*smpi_datatype_size(old_types[i]);
1075 if (old_types[i]==MPI_LB){
1079 if (old_types[i]==MPI_UB){
1084 if(!forced_lb && indices[i]+smpi_datatype_lb(old_types[i])<lb) lb = indices[i];
1085 if(!forced_ub && indices[i]+blocklens[i]*smpi_datatype_ub(old_types[i])>ub)
1086 ub = indices[i]+blocklens[i]*smpi_datatype_ub(old_types[i]);
1088 if ( (i< count -1) && (indices[i]+blocklens[i]*static_cast<int>(smpi_datatype_size(old_types[i])) != indices[i+1]) )
1093 s_smpi_mpi_struct_t* subtype = smpi_datatype_struct_create( blocklens, indices, count, old_types);
1095 smpi_datatype_create(new_type, size, lb, ub,1, subtype, DT_FLAG_DATA);
1097 s_smpi_mpi_contiguous_t* subtype = smpi_datatype_contiguous_create( lb, size, MPI_CHAR, 1);
1098 smpi_datatype_create(new_type, size, lb, ub,1, subtype, DT_FLAG_DATA|DT_FLAG_CONTIGUOUS);
1103 void smpi_datatype_commit(MPI_Datatype *datatype)
1105 (*datatype)->flags= ((*datatype)->flags | DT_FLAG_COMMITED);
1108 typedef struct s_smpi_mpi_op {
1109 MPI_User_function *func;
1113 #define MAX_OP(a, b) (b) = (a) < (b) ? (b) : (a)
1114 #define MIN_OP(a, b) (b) = (a) < (b) ? (a) : (b)
1115 #define SUM_OP(a, b) (b) += (a)
1116 #define PROD_OP(a, b) (b) *= (a)
1117 #define LAND_OP(a, b) (b) = (a) && (b)
1118 #define LOR_OP(a, b) (b) = (a) || (b)
1119 #define LXOR_OP(a, b) (b) = (!(a) && (b)) || ((a) && !(b))
1120 #define BAND_OP(a, b) (b) &= (a)
1121 #define BOR_OP(a, b) (b) |= (a)
1122 #define BXOR_OP(a, b) (b) ^= (a)
1123 #define MAXLOC_OP(a, b) (b) = (a.value) < (b.value) ? (b) : (a)
1124 #define MINLOC_OP(a, b) (b) = (a.value) < (b.value) ? (a) : (b)
1126 #define APPLY_FUNC(a, b, length, type, func) \
1129 type* x = (type*)(a); \
1130 type* y = (type*)(b); \
1131 for(i = 0; i < *(length); i++) { \
1136 static void max_func(void *a, void *b, int *length, MPI_Datatype * datatype)
1138 if (*datatype == MPI_CHAR) {
1139 APPLY_FUNC(a, b, length, char, MAX_OP);
1140 } else if (*datatype == MPI_SHORT) {
1141 APPLY_FUNC(a, b, length, short, MAX_OP);
1142 } else if (*datatype == MPI_INT) {
1143 APPLY_FUNC(a, b, length, int, MAX_OP);
1144 } else if (*datatype == MPI_LONG) {
1145 APPLY_FUNC(a, b, length, long, MAX_OP);
1146 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1147 APPLY_FUNC(a, b, length, unsigned short, MAX_OP);
1148 } else if (*datatype == MPI_UNSIGNED) {
1149 APPLY_FUNC(a, b, length, unsigned int, MAX_OP);
1150 } else if (*datatype == MPI_UNSIGNED_LONG) {
1151 APPLY_FUNC(a, b, length, unsigned long, MAX_OP);
1152 } else if (*datatype == MPI_UNSIGNED_CHAR) {
1153 APPLY_FUNC(a, b, length, unsigned char, MAX_OP);
1154 } else if (*datatype == MPI_FLOAT) {
1155 APPLY_FUNC(a, b, length, float, MAX_OP);
1156 } else if (*datatype == MPI_DOUBLE) {
1157 APPLY_FUNC(a, b, length, double, MAX_OP);
1158 } else if (*datatype == MPI_LONG_DOUBLE) {
1159 APPLY_FUNC(a, b, length, long double, MAX_OP);
1163 static void min_func(void *a, void *b, int *length, MPI_Datatype * datatype)
1165 if (*datatype == MPI_CHAR) {
1166 APPLY_FUNC(a, b, length, char, MIN_OP);
1167 } else if (*datatype == MPI_SHORT) {
1168 APPLY_FUNC(a, b, length, short, MIN_OP);
1169 } else if (*datatype == MPI_INT) {
1170 APPLY_FUNC(a, b, length, int, MIN_OP);
1171 } else if (*datatype == MPI_LONG) {
1172 APPLY_FUNC(a, b, length, long, MIN_OP);
1173 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1174 APPLY_FUNC(a, b, length, unsigned short, MIN_OP);
1175 } else if (*datatype == MPI_UNSIGNED) {
1176 APPLY_FUNC(a, b, length, unsigned int, MIN_OP);
1177 } else if (*datatype == MPI_UNSIGNED_LONG) {
1178 APPLY_FUNC(a, b, length, unsigned long, MIN_OP);
1179 } else if (*datatype == MPI_UNSIGNED_CHAR) {
1180 APPLY_FUNC(a, b, length, unsigned char, MIN_OP);
1181 } else if (*datatype == MPI_FLOAT) {
1182 APPLY_FUNC(a, b, length, float, MIN_OP);
1183 } else if (*datatype == MPI_DOUBLE) {
1184 APPLY_FUNC(a, b, length, double, MIN_OP);
1185 } else if (*datatype == MPI_LONG_DOUBLE) {
1186 APPLY_FUNC(a, b, length, long double, MIN_OP);
1190 static void sum_func(void *a, void *b, int *length, MPI_Datatype * datatype)
1192 if (*datatype == MPI_CHAR) {
1193 APPLY_FUNC(a, b, length, char, SUM_OP);
1194 } else if (*datatype == MPI_SHORT) {
1195 APPLY_FUNC(a, b, length, short, SUM_OP);
1196 } else if (*datatype == MPI_INT) {
1197 APPLY_FUNC(a, b, length, int, SUM_OP);
1198 } else if (*datatype == MPI_LONG) {
1199 APPLY_FUNC(a, b, length, long, SUM_OP);
1200 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1201 APPLY_FUNC(a, b, length, unsigned short, SUM_OP);
1202 } else if (*datatype == MPI_UNSIGNED) {
1203 APPLY_FUNC(a, b, length, unsigned int, SUM_OP);
1204 } else if (*datatype == MPI_UNSIGNED_LONG) {
1205 APPLY_FUNC(a, b, length, unsigned long, SUM_OP);
1206 } else if (*datatype == MPI_UNSIGNED_CHAR) {
1207 APPLY_FUNC(a, b, length, unsigned char, SUM_OP);
1208 } else if (*datatype == MPI_FLOAT) {
1209 APPLY_FUNC(a, b, length, float, SUM_OP);
1210 } else if (*datatype == MPI_DOUBLE) {
1211 APPLY_FUNC(a, b, length, double, SUM_OP);
1212 } else if (*datatype == MPI_LONG_DOUBLE) {
1213 APPLY_FUNC(a, b, length, long double, SUM_OP);
1214 } else if (*datatype == MPI_C_FLOAT_COMPLEX) {
1215 APPLY_FUNC(a, b, length, float _Complex, SUM_OP);
1216 } else if (*datatype == MPI_C_DOUBLE_COMPLEX) {
1217 APPLY_FUNC(a, b, length, double _Complex, SUM_OP);
1218 } else if (*datatype == MPI_C_LONG_DOUBLE_COMPLEX) {
1219 APPLY_FUNC(a, b, length, long double _Complex, SUM_OP);
1223 static void prod_func(void *a, void *b, int *length, MPI_Datatype * datatype)
1225 if (*datatype == MPI_CHAR) {
1226 APPLY_FUNC(a, b, length, char, PROD_OP);
1227 } else if (*datatype == MPI_SHORT) {
1228 APPLY_FUNC(a, b, length, short, PROD_OP);
1229 } else if (*datatype == MPI_INT) {
1230 APPLY_FUNC(a, b, length, int, PROD_OP);
1231 } else if (*datatype == MPI_LONG) {
1232 APPLY_FUNC(a, b, length, long, PROD_OP);
1233 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1234 APPLY_FUNC(a, b, length, unsigned short, PROD_OP);
1235 } else if (*datatype == MPI_UNSIGNED) {
1236 APPLY_FUNC(a, b, length, unsigned int, PROD_OP);
1237 } else if (*datatype == MPI_UNSIGNED_LONG) {
1238 APPLY_FUNC(a, b, length, unsigned long, PROD_OP);
1239 } else if (*datatype == MPI_UNSIGNED_CHAR) {
1240 APPLY_FUNC(a, b, length, unsigned char, PROD_OP);
1241 } else if (*datatype == MPI_FLOAT) {
1242 APPLY_FUNC(a, b, length, float, PROD_OP);
1243 } else if (*datatype == MPI_DOUBLE) {
1244 APPLY_FUNC(a, b, length, double, PROD_OP);
1245 } else if (*datatype == MPI_LONG_DOUBLE) {
1246 APPLY_FUNC(a, b, length, long double, PROD_OP);
1247 } else if (*datatype == MPI_C_FLOAT_COMPLEX) {
1248 APPLY_FUNC(a, b, length, float _Complex, PROD_OP);
1249 } else if (*datatype == MPI_C_DOUBLE_COMPLEX) {
1250 APPLY_FUNC(a, b, length, double _Complex, PROD_OP);
1251 } else if (*datatype == MPI_C_LONG_DOUBLE_COMPLEX) {
1252 APPLY_FUNC(a, b, length, long double _Complex, PROD_OP);
1256 static void land_func(void *a, void *b, int *length, MPI_Datatype * datatype)
1258 if (*datatype == MPI_CHAR) {
1259 APPLY_FUNC(a, b, length, char, LAND_OP);
1260 } else if (*datatype == MPI_SHORT) {
1261 APPLY_FUNC(a, b, length, short, LAND_OP);
1262 } else if (*datatype == MPI_INT) {
1263 APPLY_FUNC(a, b, length, int, LAND_OP);
1264 } else if (*datatype == MPI_LONG) {
1265 APPLY_FUNC(a, b, length, long, LAND_OP);
1266 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1267 APPLY_FUNC(a, b, length, unsigned short, LAND_OP);
1268 } else if (*datatype == MPI_UNSIGNED) {
1269 APPLY_FUNC(a, b, length, unsigned int, LAND_OP);
1270 } else if (*datatype == MPI_UNSIGNED_LONG) {
1271 APPLY_FUNC(a, b, length, unsigned long, LAND_OP);
1272 } else if (*datatype == MPI_UNSIGNED_CHAR) {
1273 APPLY_FUNC(a, b, length, unsigned char, LAND_OP);
1274 } else if (*datatype == MPI_C_BOOL) {
1275 APPLY_FUNC(a, b, length, bool, LAND_OP);
1279 static void lor_func(void *a, void *b, int *length, MPI_Datatype * datatype)
1281 if (*datatype == MPI_CHAR) {
1282 APPLY_FUNC(a, b, length, char, LOR_OP);
1283 } else if (*datatype == MPI_SHORT) {
1284 APPLY_FUNC(a, b, length, short, LOR_OP);
1285 } else if (*datatype == MPI_INT) {
1286 APPLY_FUNC(a, b, length, int, LOR_OP);
1287 } else if (*datatype == MPI_LONG) {
1288 APPLY_FUNC(a, b, length, long, LOR_OP);
1289 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1290 APPLY_FUNC(a, b, length, unsigned short, LOR_OP);
1291 } else if (*datatype == MPI_UNSIGNED) {
1292 APPLY_FUNC(a, b, length, unsigned int, LOR_OP);
1293 } else if (*datatype == MPI_UNSIGNED_LONG) {
1294 APPLY_FUNC(a, b, length, unsigned long, LOR_OP);
1295 } else if (*datatype == MPI_UNSIGNED_CHAR) {
1296 APPLY_FUNC(a, b, length, unsigned char, LOR_OP);
1297 } else if (*datatype == MPI_C_BOOL) {
1298 APPLY_FUNC(a, b, length, bool, LOR_OP);
1302 static void lxor_func(void *a, void *b, int *length, MPI_Datatype * datatype)
1304 if (*datatype == MPI_CHAR) {
1305 APPLY_FUNC(a, b, length, char, LXOR_OP);
1306 } else if (*datatype == MPI_SHORT) {
1307 APPLY_FUNC(a, b, length, short, LXOR_OP);
1308 } else if (*datatype == MPI_INT) {
1309 APPLY_FUNC(a, b, length, int, LXOR_OP);
1310 } else if (*datatype == MPI_LONG) {
1311 APPLY_FUNC(a, b, length, long, LXOR_OP);
1312 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1313 APPLY_FUNC(a, b, length, unsigned short, LXOR_OP);
1314 } else if (*datatype == MPI_UNSIGNED) {
1315 APPLY_FUNC(a, b, length, unsigned int, LXOR_OP);
1316 } else if (*datatype == MPI_UNSIGNED_LONG) {
1317 APPLY_FUNC(a, b, length, unsigned long, LXOR_OP);
1318 } else if (*datatype == MPI_UNSIGNED_CHAR) {
1319 APPLY_FUNC(a, b, length, unsigned char, LXOR_OP);
1320 } else if (*datatype == MPI_C_BOOL) {
1321 APPLY_FUNC(a, b, length, bool, LXOR_OP);
1325 static void band_func(void *a, void *b, int *length, MPI_Datatype * datatype)
1327 if (*datatype == MPI_CHAR) {
1328 APPLY_FUNC(a, b, length, char, BAND_OP);
1329 }else if (*datatype == MPI_SHORT) {
1330 APPLY_FUNC(a, b, length, short, BAND_OP);
1331 } else if (*datatype == MPI_INT) {
1332 APPLY_FUNC(a, b, length, int, BAND_OP);
1333 } else if (*datatype == MPI_LONG) {
1334 APPLY_FUNC(a, b, length, long, BAND_OP);
1335 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1336 APPLY_FUNC(a, b, length, unsigned short, BAND_OP);
1337 } else if (*datatype == MPI_UNSIGNED) {
1338 APPLY_FUNC(a, b, length, unsigned int, BAND_OP);
1339 } else if (*datatype == MPI_UNSIGNED_LONG) {
1340 APPLY_FUNC(a, b, length, unsigned long, BAND_OP);
1341 } else if (*datatype == MPI_UNSIGNED_CHAR) {
1342 APPLY_FUNC(a, b, length, unsigned char, BAND_OP);
1343 } else if (*datatype == MPI_BYTE) {
1344 APPLY_FUNC(a, b, length, uint8_t, BAND_OP);
1348 static void bor_func(void *a, void *b, int *length, MPI_Datatype * datatype)
1350 if (*datatype == MPI_CHAR) {
1351 APPLY_FUNC(a, b, length, char, BOR_OP);
1352 } else if (*datatype == MPI_SHORT) {
1353 APPLY_FUNC(a, b, length, short, BOR_OP);
1354 } else if (*datatype == MPI_INT) {
1355 APPLY_FUNC(a, b, length, int, BOR_OP);
1356 } else if (*datatype == MPI_LONG) {
1357 APPLY_FUNC(a, b, length, long, BOR_OP);
1358 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1359 APPLY_FUNC(a, b, length, unsigned short, BOR_OP);
1360 } else if (*datatype == MPI_UNSIGNED) {
1361 APPLY_FUNC(a, b, length, unsigned int, BOR_OP);
1362 } else if (*datatype == MPI_UNSIGNED_LONG) {
1363 APPLY_FUNC(a, b, length, unsigned long, BOR_OP);
1364 } else if (*datatype == MPI_UNSIGNED_CHAR) {
1365 APPLY_FUNC(a, b, length, unsigned char, BOR_OP);
1366 } else if (*datatype == MPI_BYTE) {
1367 APPLY_FUNC(a, b, length, uint8_t, BOR_OP);
1371 static void bxor_func(void *a, void *b, int *length, MPI_Datatype * datatype)
1373 if (*datatype == MPI_CHAR) {
1374 APPLY_FUNC(a, b, length, char, BXOR_OP);
1375 } else if (*datatype == MPI_SHORT) {
1376 APPLY_FUNC(a, b, length, short, BXOR_OP);
1377 } else if (*datatype == MPI_INT) {
1378 APPLY_FUNC(a, b, length, int, BXOR_OP);
1379 } else if (*datatype == MPI_LONG) {
1380 APPLY_FUNC(a, b, length, long, BXOR_OP);
1381 } else if (*datatype == MPI_UNSIGNED_SHORT) {
1382 APPLY_FUNC(a, b, length, unsigned short, BXOR_OP);
1383 } else if (*datatype == MPI_UNSIGNED) {
1384 APPLY_FUNC(a, b, length, unsigned int, BXOR_OP);
1385 } else if (*datatype == MPI_UNSIGNED_LONG) {
1386 APPLY_FUNC(a, b, length, unsigned long, BXOR_OP);
1387 } else if (*datatype == MPI_UNSIGNED_CHAR) {
1388 APPLY_FUNC(a, b, length, unsigned char, BXOR_OP);
1389 } else if (*datatype == MPI_BYTE) {
1390 APPLY_FUNC(a, b, length, uint8_t, BXOR_OP);
1394 static void minloc_func(void *a, void *b, int *length, MPI_Datatype * datatype)
1396 if (*datatype == MPI_FLOAT_INT) {
1397 APPLY_FUNC(a, b, length, float_int, MINLOC_OP);
1398 } else if (*datatype == MPI_LONG_INT) {
1399 APPLY_FUNC(a, b, length, long_int, MINLOC_OP);
1400 } else if (*datatype == MPI_DOUBLE_INT) {
1401 APPLY_FUNC(a, b, length, double_int, MINLOC_OP);
1402 } else if (*datatype == MPI_SHORT_INT) {
1403 APPLY_FUNC(a, b, length, short_int, MINLOC_OP);
1404 } else if (*datatype == MPI_2LONG) {
1405 APPLY_FUNC(a, b, length, long_long, MINLOC_OP);
1406 } else if (*datatype == MPI_2INT) {
1407 APPLY_FUNC(a, b, length, int_int, MINLOC_OP);
1408 } else if (*datatype == MPI_LONG_DOUBLE_INT) {
1409 APPLY_FUNC(a, b, length, long_double_int, MINLOC_OP);
1410 } else if (*datatype == MPI_2FLOAT) {
1411 APPLY_FUNC(a, b, length, float_float, MINLOC_OP);
1412 } else if (*datatype == MPI_2DOUBLE) {
1413 APPLY_FUNC(a, b, length, double_double, MINLOC_OP);
1417 static void maxloc_func(void *a, void *b, int *length, MPI_Datatype * datatype)
1419 if (*datatype == MPI_FLOAT_INT) {
1420 APPLY_FUNC(a, b, length, float_int, MAXLOC_OP);
1421 } else if (*datatype == MPI_LONG_INT) {
1422 APPLY_FUNC(a, b, length, long_int, MAXLOC_OP);
1423 } else if (*datatype == MPI_DOUBLE_INT) {
1424 APPLY_FUNC(a, b, length, double_int, MAXLOC_OP);
1425 } else if (*datatype == MPI_SHORT_INT) {
1426 APPLY_FUNC(a, b, length, short_int, MAXLOC_OP);
1427 } else if (*datatype == MPI_2LONG) {
1428 APPLY_FUNC(a, b, length, long_long, MAXLOC_OP);
1429 } else if (*datatype == MPI_2INT) {
1430 APPLY_FUNC(a, b, length, int_int, MAXLOC_OP);
1431 } else if (*datatype == MPI_LONG_DOUBLE_INT) {
1432 APPLY_FUNC(a, b, length, long_double_int, MAXLOC_OP);
1433 } else if (*datatype == MPI_2FLOAT) {
1434 APPLY_FUNC(a, b, length, float_float, MAXLOC_OP);
1435 } else if (*datatype == MPI_2DOUBLE) {
1436 APPLY_FUNC(a, b, length, double_double, MAXLOC_OP);
1440 static void replace_func(void *a, void *b, int *length, MPI_Datatype * datatype)
1442 memcpy(b, a, *length * smpi_datatype_size(*datatype));
1445 #define CREATE_MPI_OP(name, func) \
1446 static s_smpi_mpi_op_t mpi_##name = { &(func) /* func */, true }; \
1447 MPI_Op name = &mpi_##name;
1449 CREATE_MPI_OP(MPI_MAX, max_func);
1450 CREATE_MPI_OP(MPI_MIN, min_func);
1451 CREATE_MPI_OP(MPI_SUM, sum_func);
1452 CREATE_MPI_OP(MPI_PROD, prod_func);
1453 CREATE_MPI_OP(MPI_LAND, land_func);
1454 CREATE_MPI_OP(MPI_LOR, lor_func);
1455 CREATE_MPI_OP(MPI_LXOR, lxor_func);
1456 CREATE_MPI_OP(MPI_BAND, band_func);
1457 CREATE_MPI_OP(MPI_BOR, bor_func);
1458 CREATE_MPI_OP(MPI_BXOR, bxor_func);
1459 CREATE_MPI_OP(MPI_MAXLOC, maxloc_func);
1460 CREATE_MPI_OP(MPI_MINLOC, minloc_func);
1461 CREATE_MPI_OP(MPI_REPLACE, replace_func);
1463 MPI_Op smpi_op_new(MPI_User_function * function, int commute)
1466 op = xbt_new(s_smpi_mpi_op_t, 1);
1467 op->func = function;
1468 op-> is_commute = commute;
1472 int smpi_op_is_commute(MPI_Op op)
1474 return (op==MPI_OP_NULL) ? 1 : op-> is_commute;
1477 void smpi_op_destroy(MPI_Op op)
1482 void smpi_op_apply(MPI_Op op, void *invec, void *inoutvec, int *len, MPI_Datatype * datatype)
1487 if(smpi_privatize_global_variables){//we need to switch as the called function may silently touch global variables
1488 XBT_DEBUG("Applying operation, switch to the right data frame ");
1489 smpi_switch_data_segment(smpi_process_index());
1492 if(!smpi_process_get_replaying())
1493 op->func(invec, inoutvec, len, datatype);
1496 int smpi_type_attr_delete(MPI_Datatype type, int keyval){
1497 smpi_type_key_elem elem =
1498 static_cast<smpi_type_key_elem>(xbt_dict_get_or_null_ext(smpi_type_keyvals, (const char*)&keyval, sizeof(int)));
1501 if(elem->delete_fn!=MPI_NULL_DELETE_FN){
1504 if(smpi_type_attr_get(type, keyval, &value, &flag)==MPI_SUCCESS){
1505 int ret = elem->delete_fn(type, keyval, value, &flag);
1506 if(ret!=MPI_SUCCESS) return ret;
1509 if(type->attributes==NULL)
1512 xbt_dict_remove_ext(type->attributes, (const char*)&keyval, sizeof(int));
1516 int smpi_type_attr_get(MPI_Datatype type, int keyval, void* attr_value, int* flag){
1517 smpi_type_key_elem elem =
1518 static_cast<smpi_type_key_elem>(xbt_dict_get_or_null_ext(smpi_type_keyvals, (const char*)&keyval, sizeof(int)));
1522 if(type->attributes==NULL){
1527 *(void**)attr_value = xbt_dict_get_ext(type->attributes, (const char*)&keyval, sizeof(int));
1537 int smpi_type_attr_put(MPI_Datatype type, int keyval, void* attr_value){
1538 if(!smpi_type_keyvals)
1539 smpi_type_keyvals = xbt_dict_new();
1540 smpi_type_key_elem elem =
1541 static_cast<smpi_type_key_elem>(xbt_dict_get_or_null_ext(smpi_type_keyvals, (const char*)&keyval, sizeof(int)));
1546 smpi_type_attr_get(type, keyval, &value, &flag);
1547 if(flag && elem->delete_fn!=MPI_NULL_DELETE_FN){
1548 int ret = elem->delete_fn(type, keyval, value, &flag);
1549 if(ret!=MPI_SUCCESS) return ret;
1551 if(type->attributes==NULL)
1552 type->attributes=xbt_dict_new();
1554 xbt_dict_set_ext(type->attributes, (const char*)&keyval, sizeof(int), attr_value, NULL);
1558 int smpi_type_keyval_create(MPI_Type_copy_attr_function* copy_fn, MPI_Type_delete_attr_function* delete_fn, int* keyval,
1560 if(!smpi_type_keyvals)
1561 smpi_type_keyvals = xbt_dict_new();
1563 smpi_type_key_elem value = (smpi_type_key_elem) xbt_new0(s_smpi_mpi_type_key_elem_t,1);
1565 value->copy_fn=copy_fn;
1566 value->delete_fn=delete_fn;
1568 *keyval = type_keyval_id;
1569 xbt_dict_set_ext(smpi_type_keyvals,(const char*)keyval, sizeof(int),(void*)value, NULL);
1574 int smpi_type_keyval_free(int* keyval){
1575 smpi_type_key_elem elem =
1576 static_cast<smpi_type_key_elem>(xbt_dict_get_or_null_ext(smpi_type_keyvals, (const char*)keyval, sizeof(int)));
1580 xbt_dict_remove_ext(smpi_type_keyvals, (const char*)keyval, sizeof(int));
1585 int smpi_mpi_pack(void* inbuf, int incount, MPI_Datatype type, void* outbuf, int outcount, int* position,MPI_Comm comm){
1586 size_t size = smpi_datatype_size(type);
1587 if (outcount - *position < incount*static_cast<int>(size))
1588 return MPI_ERR_BUFFER;
1589 smpi_datatype_copy(inbuf, incount, type, (char*)outbuf + *position, outcount, MPI_CHAR);
1590 *position += incount * size;
1594 int smpi_mpi_unpack(void* inbuf, int insize, int* position, void* outbuf, int outcount, MPI_Datatype type,MPI_Comm comm){
1595 int size = static_cast<int>(smpi_datatype_size(type));
1596 if (outcount*size> insize)
1597 return MPI_ERR_BUFFER;
1598 smpi_datatype_copy((char*)inbuf + *position, insize, MPI_CHAR, outbuf, outcount, type);
1599 *position += outcount * size;