+ s_smpi_mpi_struct_t* type_c = (s_smpi_mpi_struct_t*)type;
+ int i,j;
+
+ char* contiguous_struct_char = (char*)contiguous_struct;
+ char* noncontiguous_struct_char = (char*)noncontiguous_struct+ type_c->block_indices[0];
+ for(j=0; j<count;j++){
+ for (i = 0; i < type_c->block_count; i++) {
+ if (type_c->old_types[i]->has_subtype == 0)
+ smpi_op_apply(op, contiguous_struct_char, noncontiguous_struct_char, &type_c->block_lengths[i],
+ & type_c->old_types[i]);
+ /*memcpy(noncontiguous_struct_char,
+ contiguous_struct_char, type_c->block_lengths[i] * smpi_datatype_size(type_c->old_types[i]));*/
+ else
+ ((s_smpi_subtype_t*)type_c->old_types[i]->substruct)->unserialize( contiguous_struct_char,
+ noncontiguous_struct_char,
+ type_c->block_lengths[i],
+ type_c->old_types[i]->substruct,
+ op);
+
+ contiguous_struct_char += type_c->block_lengths[i]*smpi_datatype_size(type_c->old_types[i]);
+ if (i<type_c->block_count-1)noncontiguous_struct_char = (char*)noncontiguous_struct + type_c->block_indices[i+1];
+ else noncontiguous_struct_char += type_c->block_lengths[i]*smpi_datatype_get_extent(type_c->old_types[i]);
+ }
+ noncontiguous_struct=(void*)noncontiguous_struct_char;
+
+ }
+}
+
+void free_struct(MPI_Datatype* type){
+ xbt_free(((s_smpi_mpi_struct_t *)(*type)->substruct)->block_lengths);
+ xbt_free(((s_smpi_mpi_struct_t *)(*type)->substruct)->block_indices);
+ int i=0;
+ for (i = 0; i < ((s_smpi_mpi_struct_t *)(*type)->substruct)->block_count; i++)
+ smpi_datatype_unuse(((s_smpi_mpi_struct_t *)(*type)->substruct)->old_types[i]);
+ xbt_free(((s_smpi_mpi_struct_t *)(*type)->substruct)->old_types);
+}
+
+/*
+ * Create a Sub type struct to be able to serialize and unserialize it
+ * the structure s_smpi_mpi_struct_t is derived from s_smpi_subtype which
+ * required the functions unserialize and serialize
+ */
+s_smpi_mpi_struct_t* smpi_datatype_struct_create( int* block_lengths,
+ MPI_Aint* block_indices,
+ int block_count,
+ MPI_Datatype* old_types){
+ s_smpi_mpi_struct_t *new_t= xbt_new(s_smpi_mpi_struct_t,1);
+ new_t->base.serialize = &serialize_struct;
+ new_t->base.unserialize = &unserialize_struct;
+ new_t->base.subtype_free = &free_struct;
+ //TODO : add a custom function for each time to clean these
+ new_t->block_lengths= xbt_new(int, block_count);
+ new_t->block_indices= xbt_new(MPI_Aint, block_count);
+ new_t->old_types= xbt_new(MPI_Datatype, block_count);
+ int i;
+ for(i=0;i<block_count;i++){
+ new_t->block_lengths[i]=block_lengths[i];
+ new_t->block_indices[i]=block_indices[i];
+ new_t->old_types[i]=old_types[i];
+ smpi_datatype_use(new_t->old_types[i]);
+ }
+ //new_t->block_lengths = block_lengths;
+ //new_t->block_indices = block_indices;
+ new_t->block_count = block_count;
+ //new_t->old_types = old_types;
+ return new_t;
+}
+
+
+int smpi_datatype_struct(int count, int* blocklens, MPI_Aint* indices, MPI_Datatype* old_types, MPI_Datatype* new_type)
+{
+ int i;
+ size_t size = 0;
+ int contiguous=1;
+ size = 0;
+ MPI_Aint lb = 0;
+ MPI_Aint ub = 0;
+ if(count>0){
+ lb=indices[0] + smpi_datatype_lb(old_types[0]);
+ ub=indices[0] + blocklens[0]*smpi_datatype_ub(old_types[0]);
+ }
+ int forced_lb=0;
+ int forced_ub=0;
+ for(i=0; i< count; i++){
+ if (blocklens[i]<0)
+ return MPI_ERR_ARG;
+ if (old_types[i]->has_subtype == 1)
+ contiguous=0;
+
+ size += blocklens[i]*smpi_datatype_size(old_types[i]);
+ if (old_types[i]==MPI_LB){
+ lb=indices[i];
+ forced_lb=1;
+ }
+ if (old_types[i]==MPI_UB){
+ ub=indices[i];
+ forced_ub=1;
+ }
+
+ if(!forced_lb && indices[i]+smpi_datatype_lb(old_types[i])<lb) lb = indices[i];
+ if(!forced_ub && indices[i]+blocklens[i]*smpi_datatype_ub(old_types[i])>ub) ub = indices[i]+blocklens[i]*smpi_datatype_ub(old_types[i]);
+
+ if ( (i< count -1) && (indices[i]+blocklens[i]*smpi_datatype_size(old_types[i]) != indices[i+1]) )contiguous=0;
+ }
+
+ if(!contiguous){
+ s_smpi_mpi_struct_t* subtype = smpi_datatype_struct_create( blocklens,
+ indices,
+ count,
+ old_types);
+
+ smpi_datatype_create(new_type, size, lb, ub,1, subtype, DT_FLAG_DATA);
+ }else{
+ s_smpi_mpi_contiguous_t* subtype = smpi_datatype_contiguous_create( lb,
+ size,
+ MPI_CHAR,
+ 1);
+ smpi_datatype_create(new_type, size, lb, ub,1, subtype, DT_FLAG_DATA|DT_FLAG_CONTIGUOUS);
+ }
+ return MPI_SUCCESS;
+}
+
+void smpi_datatype_commit(MPI_Datatype *datatype)
+{
+ (*datatype)->flags= ((*datatype)->flags | DT_FLAG_COMMITED);
+}
+
+typedef struct s_smpi_mpi_op {
+ MPI_User_function *func;
+ int is_commute;
+} s_smpi_mpi_op_t;
+
+#define MAX_OP(a, b) (b) = (a) < (b) ? (b) : (a)
+#define MIN_OP(a, b) (b) = (a) < (b) ? (a) : (b)
+#define SUM_OP(a, b) (b) += (a)
+#define PROD_OP(a, b) (b) *= (a)
+#define LAND_OP(a, b) (b) = (a) && (b)
+#define LOR_OP(a, b) (b) = (a) || (b)
+#define LXOR_OP(a, b) (b) = (!(a) && (b)) || ((a) && !(b))
+#define BAND_OP(a, b) (b) &= (a)
+#define BOR_OP(a, b) (b) |= (a)
+#define BXOR_OP(a, b) (b) ^= (a)
+#define MAXLOC_OP(a, b) (b) = (a.value) < (b.value) ? (b) : (a)
+#define MINLOC_OP(a, b) (b) = (a.value) < (b.value) ? (a) : (b)
+
+#define APPLY_FUNC(a, b, length, type, func) \
+{ \
+ int i; \
+ type* x = (type*)(a); \
+ type* y = (type*)(b); \
+ for(i = 0; i < *(length); i++) { \
+ func(x[i], y[i]); \
+ } \
+}
+
+static void max_func(void *a, void *b, int *length,
+ MPI_Datatype * datatype)
+{
+ if (*datatype == MPI_CHAR) {
+ APPLY_FUNC(a, b, length, char, MAX_OP);
+ } else if (*datatype == MPI_SHORT) {
+ APPLY_FUNC(a, b, length, short, MAX_OP);
+ } else if (*datatype == MPI_INT) {
+ APPLY_FUNC(a, b, length, int, MAX_OP);
+ } else if (*datatype == MPI_LONG) {
+ APPLY_FUNC(a, b, length, long, MAX_OP);
+ } else if (*datatype == MPI_UNSIGNED_SHORT) {
+ APPLY_FUNC(a, b, length, unsigned short, MAX_OP);
+ } else if (*datatype == MPI_UNSIGNED) {
+ APPLY_FUNC(a, b, length, unsigned int, MAX_OP);
+ } else if (*datatype == MPI_UNSIGNED_LONG) {
+ APPLY_FUNC(a, b, length, unsigned long, MAX_OP);
+ } else if (*datatype == MPI_UNSIGNED_CHAR) {
+ APPLY_FUNC(a, b, length, unsigned char, MAX_OP);
+ } else if (*datatype == MPI_FLOAT) {
+ APPLY_FUNC(a, b, length, float, MAX_OP);
+ } else if (*datatype == MPI_DOUBLE) {
+ APPLY_FUNC(a, b, length, double, MAX_OP);
+ } else if (*datatype == MPI_LONG_DOUBLE) {
+ APPLY_FUNC(a, b, length, long double, MAX_OP);
+ }
+}
+
+static void min_func(void *a, void *b, int *length,
+ MPI_Datatype * datatype)
+{
+ if (*datatype == MPI_CHAR) {
+ APPLY_FUNC(a, b, length, char, MIN_OP);
+ } else if (*datatype == MPI_SHORT) {
+ APPLY_FUNC(a, b, length, short, MIN_OP);
+ } else if (*datatype == MPI_INT) {
+ APPLY_FUNC(a, b, length, int, MIN_OP);
+ } else if (*datatype == MPI_LONG) {
+ APPLY_FUNC(a, b, length, long, MIN_OP);
+ } else if (*datatype == MPI_UNSIGNED_SHORT) {
+ APPLY_FUNC(a, b, length, unsigned short, MIN_OP);
+ } else if (*datatype == MPI_UNSIGNED) {
+ APPLY_FUNC(a, b, length, unsigned int, MIN_OP);
+ } else if (*datatype == MPI_UNSIGNED_LONG) {
+ APPLY_FUNC(a, b, length, unsigned long, MIN_OP);
+ } else if (*datatype == MPI_UNSIGNED_CHAR) {
+ APPLY_FUNC(a, b, length, unsigned char, MIN_OP);
+ } else if (*datatype == MPI_FLOAT) {
+ APPLY_FUNC(a, b, length, float, MIN_OP);
+ } else if (*datatype == MPI_DOUBLE) {
+ APPLY_FUNC(a, b, length, double, MIN_OP);
+ } else if (*datatype == MPI_LONG_DOUBLE) {
+ APPLY_FUNC(a, b, length, long double, MIN_OP);
+ }
+}
+
+static void sum_func(void *a, void *b, int *length,
+ MPI_Datatype * datatype)
+{
+ if (*datatype == MPI_CHAR) {
+ APPLY_FUNC(a, b, length, char, SUM_OP);
+ } else if (*datatype == MPI_SHORT) {
+ APPLY_FUNC(a, b, length, short, SUM_OP);
+ } else if (*datatype == MPI_INT) {
+ APPLY_FUNC(a, b, length, int, SUM_OP);
+ } else if (*datatype == MPI_LONG) {
+ APPLY_FUNC(a, b, length, long, SUM_OP);
+ } else if (*datatype == MPI_UNSIGNED_SHORT) {
+ APPLY_FUNC(a, b, length, unsigned short, SUM_OP);
+ } else if (*datatype == MPI_UNSIGNED) {
+ APPLY_FUNC(a, b, length, unsigned int, SUM_OP);
+ } else if (*datatype == MPI_UNSIGNED_LONG) {
+ APPLY_FUNC(a, b, length, unsigned long, SUM_OP);
+ } else if (*datatype == MPI_UNSIGNED_CHAR) {
+ APPLY_FUNC(a, b, length, unsigned char, SUM_OP);
+ } else if (*datatype == MPI_FLOAT) {
+ APPLY_FUNC(a, b, length, float, SUM_OP);
+ } else if (*datatype == MPI_DOUBLE) {
+ APPLY_FUNC(a, b, length, double, SUM_OP);
+ } else if (*datatype == MPI_LONG_DOUBLE) {
+ APPLY_FUNC(a, b, length, long double, SUM_OP);
+ } else if (*datatype == MPI_C_FLOAT_COMPLEX) {
+ APPLY_FUNC(a, b, length, float _Complex, SUM_OP);
+ } else if (*datatype == MPI_C_DOUBLE_COMPLEX) {
+ APPLY_FUNC(a, b, length, double _Complex, SUM_OP);
+ } else if (*datatype == MPI_C_LONG_DOUBLE_COMPLEX) {
+ APPLY_FUNC(a, b, length, long double _Complex, SUM_OP);
+ }
+}
+
+static void prod_func(void *a, void *b, int *length,
+ MPI_Datatype * datatype)
+{
+ if (*datatype == MPI_CHAR) {
+ APPLY_FUNC(a, b, length, char, PROD_OP);
+ } else if (*datatype == MPI_SHORT) {
+ APPLY_FUNC(a, b, length, short, PROD_OP);
+ } else if (*datatype == MPI_INT) {
+ APPLY_FUNC(a, b, length, int, PROD_OP);
+ } else if (*datatype == MPI_LONG) {
+ APPLY_FUNC(a, b, length, long, PROD_OP);
+ } else if (*datatype == MPI_UNSIGNED_SHORT) {
+ APPLY_FUNC(a, b, length, unsigned short, PROD_OP);
+ } else if (*datatype == MPI_UNSIGNED) {
+ APPLY_FUNC(a, b, length, unsigned int, PROD_OP);
+ } else if (*datatype == MPI_UNSIGNED_LONG) {
+ APPLY_FUNC(a, b, length, unsigned long, PROD_OP);
+ } else if (*datatype == MPI_UNSIGNED_CHAR) {
+ APPLY_FUNC(a, b, length, unsigned char, PROD_OP);
+ } else if (*datatype == MPI_FLOAT) {
+ APPLY_FUNC(a, b, length, float, PROD_OP);
+ } else if (*datatype == MPI_DOUBLE) {
+ APPLY_FUNC(a, b, length, double, PROD_OP);
+ } else if (*datatype == MPI_LONG_DOUBLE) {
+ APPLY_FUNC(a, b, length, long double, PROD_OP);
+ } else if (*datatype == MPI_C_FLOAT_COMPLEX) {
+ APPLY_FUNC(a, b, length, float _Complex, PROD_OP);
+ } else if (*datatype == MPI_C_DOUBLE_COMPLEX) {
+ APPLY_FUNC(a, b, length, double _Complex, PROD_OP);
+ } else if (*datatype == MPI_C_LONG_DOUBLE_COMPLEX) {
+ APPLY_FUNC(a, b, length, long double _Complex, PROD_OP);
+ }
+}
+
+static void land_func(void *a, void *b, int *length,
+ MPI_Datatype * datatype)
+{
+ if (*datatype == MPI_CHAR) {
+ APPLY_FUNC(a, b, length, char, LAND_OP);
+ } else if (*datatype == MPI_SHORT) {
+ APPLY_FUNC(a, b, length, short, LAND_OP);
+ } else if (*datatype == MPI_INT) {
+ APPLY_FUNC(a, b, length, int, LAND_OP);
+ } else if (*datatype == MPI_LONG) {
+ APPLY_FUNC(a, b, length, long, LAND_OP);
+ } else if (*datatype == MPI_UNSIGNED_SHORT) {
+ APPLY_FUNC(a, b, length, unsigned short, LAND_OP);
+ } else if (*datatype == MPI_UNSIGNED) {
+ APPLY_FUNC(a, b, length, unsigned int, LAND_OP);
+ } else if (*datatype == MPI_UNSIGNED_LONG) {
+ APPLY_FUNC(a, b, length, unsigned long, LAND_OP);
+ } else if (*datatype == MPI_UNSIGNED_CHAR) {
+ APPLY_FUNC(a, b, length, unsigned char, LAND_OP);
+ } else if (*datatype == MPI_C_BOOL) {
+ APPLY_FUNC(a, b, length, _Bool, LAND_OP);
+ }
+}
+
+static void lor_func(void *a, void *b, int *length,
+ MPI_Datatype * datatype)
+{
+ if (*datatype == MPI_CHAR) {
+ APPLY_FUNC(a, b, length, char, LOR_OP);
+ } else if (*datatype == MPI_SHORT) {
+ APPLY_FUNC(a, b, length, short, LOR_OP);
+ } else if (*datatype == MPI_INT) {
+ APPLY_FUNC(a, b, length, int, LOR_OP);
+ } else if (*datatype == MPI_LONG) {
+ APPLY_FUNC(a, b, length, long, LOR_OP);
+ } else if (*datatype == MPI_UNSIGNED_SHORT) {
+ APPLY_FUNC(a, b, length, unsigned short, LOR_OP);
+ } else if (*datatype == MPI_UNSIGNED) {
+ APPLY_FUNC(a, b, length, unsigned int, LOR_OP);
+ } else if (*datatype == MPI_UNSIGNED_LONG) {
+ APPLY_FUNC(a, b, length, unsigned long, LOR_OP);
+ } else if (*datatype == MPI_UNSIGNED_CHAR) {
+ APPLY_FUNC(a, b, length, unsigned char, LOR_OP);
+ } else if (*datatype == MPI_C_BOOL) {
+ APPLY_FUNC(a, b, length, _Bool, LOR_OP);
+ }
+}
+
+static void lxor_func(void *a, void *b, int *length,
+ MPI_Datatype * datatype)
+{
+ if (*datatype == MPI_CHAR) {
+ APPLY_FUNC(a, b, length, char, LXOR_OP);
+ } else if (*datatype == MPI_SHORT) {
+ APPLY_FUNC(a, b, length, short, LXOR_OP);
+ } else if (*datatype == MPI_INT) {
+ APPLY_FUNC(a, b, length, int, LXOR_OP);
+ } else if (*datatype == MPI_LONG) {
+ APPLY_FUNC(a, b, length, long, LXOR_OP);
+ } else if (*datatype == MPI_UNSIGNED_SHORT) {
+ APPLY_FUNC(a, b, length, unsigned short, LXOR_OP);
+ } else if (*datatype == MPI_UNSIGNED) {
+ APPLY_FUNC(a, b, length, unsigned int, LXOR_OP);
+ } else if (*datatype == MPI_UNSIGNED_LONG) {
+ APPLY_FUNC(a, b, length, unsigned long, LXOR_OP);
+ } else if (*datatype == MPI_UNSIGNED_CHAR) {
+ APPLY_FUNC(a, b, length, unsigned char, LXOR_OP);
+ } else if (*datatype == MPI_C_BOOL) {
+ APPLY_FUNC(a, b, length, _Bool, LXOR_OP);
+ }
+}
+
+static void band_func(void *a, void *b, int *length,
+ MPI_Datatype * datatype)
+{
+ if (*datatype == MPI_CHAR) {
+ APPLY_FUNC(a, b, length, char, BAND_OP);
+ }else if (*datatype == MPI_SHORT) {
+ APPLY_FUNC(a, b, length, short, BAND_OP);
+ } else if (*datatype == MPI_INT) {
+ APPLY_FUNC(a, b, length, int, BAND_OP);
+ } else if (*datatype == MPI_LONG) {
+ APPLY_FUNC(a, b, length, long, BAND_OP);
+ } else if (*datatype == MPI_UNSIGNED_SHORT) {
+ APPLY_FUNC(a, b, length, unsigned short, BAND_OP);
+ } else if (*datatype == MPI_UNSIGNED) {
+ APPLY_FUNC(a, b, length, unsigned int, BAND_OP);
+ } else if (*datatype == MPI_UNSIGNED_LONG) {
+ APPLY_FUNC(a, b, length, unsigned long, BAND_OP);
+ } else if (*datatype == MPI_UNSIGNED_CHAR) {
+ APPLY_FUNC(a, b, length, unsigned char, BAND_OP);
+ } else if (*datatype == MPI_BYTE) {
+ APPLY_FUNC(a, b, length, uint8_t, BAND_OP);
+ }
+}
+
+static void bor_func(void *a, void *b, int *length,
+ MPI_Datatype * datatype)
+{
+ if (*datatype == MPI_CHAR) {
+ APPLY_FUNC(a, b, length, char, BOR_OP);
+ } else if (*datatype == MPI_SHORT) {
+ APPLY_FUNC(a, b, length, short, BOR_OP);
+ } else if (*datatype == MPI_INT) {
+ APPLY_FUNC(a, b, length, int, BOR_OP);
+ } else if (*datatype == MPI_LONG) {
+ APPLY_FUNC(a, b, length, long, BOR_OP);
+ } else if (*datatype == MPI_UNSIGNED_SHORT) {
+ APPLY_FUNC(a, b, length, unsigned short, BOR_OP);
+ } else if (*datatype == MPI_UNSIGNED) {
+ APPLY_FUNC(a, b, length, unsigned int, BOR_OP);
+ } else if (*datatype == MPI_UNSIGNED_LONG) {
+ APPLY_FUNC(a, b, length, unsigned long, BOR_OP);
+ } else if (*datatype == MPI_UNSIGNED_CHAR) {
+ APPLY_FUNC(a, b, length, unsigned char, BOR_OP);
+ } else if (*datatype == MPI_BYTE) {
+ APPLY_FUNC(a, b, length, uint8_t, BOR_OP);
+ }
+}
+
+static void bxor_func(void *a, void *b, int *length,
+ MPI_Datatype * datatype)
+{
+ if (*datatype == MPI_CHAR) {
+ APPLY_FUNC(a, b, length, char, BXOR_OP);
+ } else if (*datatype == MPI_SHORT) {
+ APPLY_FUNC(a, b, length, short, BXOR_OP);
+ } else if (*datatype == MPI_INT) {
+ APPLY_FUNC(a, b, length, int, BXOR_OP);
+ } else if (*datatype == MPI_LONG) {
+ APPLY_FUNC(a, b, length, long, BXOR_OP);
+ } else if (*datatype == MPI_UNSIGNED_SHORT) {
+ APPLY_FUNC(a, b, length, unsigned short, BXOR_OP);
+ } else if (*datatype == MPI_UNSIGNED) {
+ APPLY_FUNC(a, b, length, unsigned int, BXOR_OP);
+ } else if (*datatype == MPI_UNSIGNED_LONG) {
+ APPLY_FUNC(a, b, length, unsigned long, BXOR_OP);
+ } else if (*datatype == MPI_UNSIGNED_CHAR) {
+ APPLY_FUNC(a, b, length, unsigned char, BXOR_OP);
+ } else if (*datatype == MPI_BYTE) {
+ APPLY_FUNC(a, b, length, uint8_t, BXOR_OP);
+ }
+}
+
+static void minloc_func(void *a, void *b, int *length,
+ MPI_Datatype * datatype)
+{
+ if (*datatype == MPI_FLOAT_INT) {
+ APPLY_FUNC(a, b, length, float_int, MINLOC_OP);
+ } else if (*datatype == MPI_LONG_INT) {
+ APPLY_FUNC(a, b, length, long_int, MINLOC_OP);
+ } else if (*datatype == MPI_DOUBLE_INT) {
+ APPLY_FUNC(a, b, length, double_int, MINLOC_OP);
+ } else if (*datatype == MPI_SHORT_INT) {
+ APPLY_FUNC(a, b, length, short_int, MINLOC_OP);
+ } else if (*datatype == MPI_2LONG) {
+ APPLY_FUNC(a, b, length, long_long, MINLOC_OP);
+ } else if (*datatype == MPI_2INT) {
+ APPLY_FUNC(a, b, length, int_int, MINLOC_OP);
+ } else if (*datatype == MPI_LONG_DOUBLE_INT) {
+ APPLY_FUNC(a, b, length, long_double_int, MINLOC_OP);
+ } else if (*datatype == MPI_2FLOAT) {
+ APPLY_FUNC(a, b, length, float_float, MINLOC_OP);
+ } else if (*datatype == MPI_2DOUBLE) {
+ APPLY_FUNC(a, b, length, double_double, MINLOC_OP);
+ }
+}
+
+static void maxloc_func(void *a, void *b, int *length,
+ MPI_Datatype * datatype)
+{
+ if (*datatype == MPI_FLOAT_INT) {
+ APPLY_FUNC(a, b, length, float_int, MAXLOC_OP);
+ } else if (*datatype == MPI_LONG_INT) {
+ APPLY_FUNC(a, b, length, long_int, MAXLOC_OP);
+ } else if (*datatype == MPI_DOUBLE_INT) {
+ APPLY_FUNC(a, b, length, double_int, MAXLOC_OP);
+ } else if (*datatype == MPI_SHORT_INT) {
+ APPLY_FUNC(a, b, length, short_int, MAXLOC_OP);
+ } else if (*datatype == MPI_2LONG) {
+ APPLY_FUNC(a, b, length, long_long, MAXLOC_OP);
+ } else if (*datatype == MPI_2INT) {
+ APPLY_FUNC(a, b, length, int_int, MAXLOC_OP);
+ } else if (*datatype == MPI_LONG_DOUBLE_INT) {
+ APPLY_FUNC(a, b, length, long_double_int, MAXLOC_OP);
+ } else if (*datatype == MPI_2FLOAT) {
+ APPLY_FUNC(a, b, length, float_float, MAXLOC_OP);
+ } else if (*datatype == MPI_2DOUBLE) {
+ APPLY_FUNC(a, b, length, double_double, MAXLOC_OP);
+ }
+}
+
+static void replace_func(void *a, void *b, int *length,
+ MPI_Datatype * datatype)
+{
+ memcpy(b, a, *length * smpi_datatype_size(*datatype));
+}
+
+#define CREATE_MPI_OP(name, func) \
+ static s_smpi_mpi_op_t mpi_##name = { &(func) /* func */, TRUE }; \
+MPI_Op name = &mpi_##name;
+
+CREATE_MPI_OP(MPI_MAX, max_func);
+CREATE_MPI_OP(MPI_MIN, min_func);
+CREATE_MPI_OP(MPI_SUM, sum_func);
+CREATE_MPI_OP(MPI_PROD, prod_func);
+CREATE_MPI_OP(MPI_LAND, land_func);
+CREATE_MPI_OP(MPI_LOR, lor_func);
+CREATE_MPI_OP(MPI_LXOR, lxor_func);
+CREATE_MPI_OP(MPI_BAND, band_func);
+CREATE_MPI_OP(MPI_BOR, bor_func);
+CREATE_MPI_OP(MPI_BXOR, bxor_func);
+CREATE_MPI_OP(MPI_MAXLOC, maxloc_func);
+CREATE_MPI_OP(MPI_MINLOC, minloc_func);
+CREATE_MPI_OP(MPI_REPLACE, replace_func);
+
+
+MPI_Op smpi_op_new(MPI_User_function * function, int commute)
+{
+ MPI_Op op;
+ op = xbt_new(s_smpi_mpi_op_t, 1);
+ op->func = function;
+ op-> is_commute = commute;
+ return op;
+}
+
+int smpi_op_is_commute(MPI_Op op)
+{
+ return (op==MPI_OP_NULL) ? 1 : op-> is_commute;
+}
+
+void smpi_op_destroy(MPI_Op op)
+{
+ xbt_free(op);
+}
+
+void smpi_op_apply(MPI_Op op, void *invec, void *inoutvec, int *len,
+ MPI_Datatype * datatype)
+{
+ if(op==MPI_OP_NULL)
+ return;
+
+ if(smpi_privatize_global_variables){ //we need to switch here, as the called function may silently touch global variables
+ XBT_DEBUG("Applying operation, switch to the right data frame ");
+ smpi_switch_data_segment(smpi_process_index());
+ }
+
+ if(!smpi_process_get_replaying())
+ op->func(invec, inoutvec, len, datatype);
+}
+
+int smpi_type_attr_delete(MPI_Datatype type, int keyval){
+ char* tmpkey=xbt_malloc(INTSIZEDCHAR);
+ sprintf(tmpkey, "%d", keyval);
+ smpi_type_key_elem elem = xbt_dict_get_or_null(smpi_type_keyvals, (const char*)tmpkey);
+ if(!elem)
+ return MPI_ERR_ARG;
+ if(elem->delete_fn!=MPI_NULL_DELETE_FN){
+ void * value;
+ int flag;
+ if(smpi_type_attr_get(type, keyval, &value, &flag)==MPI_SUCCESS){
+ int ret = elem->delete_fn(type, keyval, value, &flag);
+ if(ret!=MPI_SUCCESS) return ret;
+ }
+ }
+ if(type->attributes==NULL)
+ return MPI_ERR_ARG;
+
+ xbt_dict_remove(type->attributes, (const char*)tmpkey);
+ xbt_free(tmpkey);
+ return MPI_SUCCESS;
+}
+
+int smpi_type_attr_get(MPI_Datatype type, int keyval, void* attr_value, int* flag){
+ char* tmpkey=xbt_malloc(INTSIZEDCHAR);
+ sprintf(tmpkey, "%d", keyval);
+ smpi_type_key_elem elem = xbt_dict_get_or_null(smpi_type_keyvals, (const char*)tmpkey);
+ if(!elem)
+ return MPI_ERR_ARG;
+ xbt_ex_t ex;
+ if(type->attributes==NULL){
+ *flag=0;
+ return MPI_SUCCESS;
+ }
+ TRY {
+ *(void**)attr_value = xbt_dict_get(type->attributes, (const char*)tmpkey);
+ *flag=1;
+ }
+ CATCH(ex) {
+ *flag=0;
+ xbt_ex_free(ex);
+ }
+ xbt_free(tmpkey);
+ return MPI_SUCCESS;
+}
+
+int smpi_type_attr_put(MPI_Datatype type, int keyval, void* attr_value){
+ if(!smpi_type_keyvals)
+ smpi_type_keyvals = xbt_dict_new();
+ char* tmpkey=xbt_malloc(INTSIZEDCHAR);
+ sprintf(tmpkey, "%d", keyval);
+ smpi_type_key_elem elem = xbt_dict_get_or_null(smpi_type_keyvals, (const char*)tmpkey);
+ if(!elem )
+ return MPI_ERR_ARG;
+ int flag;
+ void* value;
+ smpi_type_attr_get(type, keyval, &value, &flag);
+ if(flag && elem->delete_fn!=MPI_NULL_DELETE_FN){
+ int ret = elem->delete_fn(type, keyval, value, &flag);
+ if(ret!=MPI_SUCCESS) return ret;
+ }
+ if(type->attributes==NULL)
+ type->attributes=xbt_dict_new();
+
+ xbt_dict_set(type->attributes, (const char*)tmpkey, attr_value, NULL);
+ xbt_free(tmpkey);
+ return MPI_SUCCESS;
+}
+
+int smpi_type_keyval_create(MPI_Type_copy_attr_function* copy_fn, MPI_Type_delete_attr_function* delete_fn, int* keyval, void* extra_state){
+
+ if(!smpi_type_keyvals)
+ smpi_type_keyvals = xbt_dict_new();
+
+ smpi_type_key_elem value = (smpi_type_key_elem) xbt_new0(s_smpi_mpi_type_key_elem_t,1);
+
+ value->copy_fn=copy_fn;
+ value->delete_fn=delete_fn;
+
+ *keyval = type_keyval_id;
+ char* tmpkey=xbt_malloc(INTSIZEDCHAR);
+ sprintf(tmpkey, "%d", *keyval);
+ xbt_dict_set(smpi_type_keyvals,(const char*)tmpkey,(void*)value, NULL);
+ type_keyval_id++;
+ xbt_free(tmpkey);
+ return MPI_SUCCESS;
+}
+
+int smpi_type_keyval_free(int* keyval){
+ char* tmpkey=xbt_malloc(INTSIZEDCHAR);
+ sprintf(tmpkey, "%d", *keyval);
+ smpi_type_key_elem elem = xbt_dict_get_or_null(smpi_type_keyvals, (const char*)tmpkey);
+ if(!elem){
+ xbt_free(tmpkey);
+ return MPI_ERR_ARG;
+ }
+ xbt_dict_remove(smpi_type_keyvals, (const char*)tmpkey);
+ xbt_free(elem);
+ xbt_free(tmpkey);
+ return MPI_SUCCESS;