+
+/*
+struct Implementation - Indexed with indices in bytes
+*/
+
+/*
+ * Copies noncontiguous data into contiguous memory.
+ * @param contiguous_struct - output struct
+ * @param noncontiguous_struct - input struct
+ * @param type - pointer contening :
+ * - stride - stride of between noncontiguous data
+ * - block_length - the width or height of blocked matrix
+ * - count - the number of rows of matrix
+ */
+void serialize_struct( const void *noncontiguous_struct,
+ void *contiguous_struct,
+ size_t count,
+ void *type)
+{
+ 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)
+ memcpy(contiguous_struct_char,
+ noncontiguous_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)->serialize( noncontiguous_struct_char,
+ contiguous_struct_char,
+ type_c->block_lengths[i],
+ type_c->old_types[i]->substruct);
+
+
+ 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]);//let's hope this is MPI_UB ?
+ }
+ noncontiguous_struct=(void*)noncontiguous_struct_char;
+ }
+}
+/*
+ * Copies contiguous data into noncontiguous memory.
+ * @param noncontiguous_struct - output struct
+ * @param contiguous_struct - input struct
+ * @param type - pointer contening :
+ * - stride - stride of between noncontiguous data
+ * - block_length - the width or height of blocked matrix
+ * - count - the number of rows of matrix
+ */
+void unserialize_struct( const void *contiguous_struct,
+ void *noncontiguous_struct,
+ size_t count,
+ void *type)
+{
+ 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)
+ 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);
+
+ 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);
+ 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];
+ }
+ //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;
+}
+
+