+
+int smpi_type_attr_delete(MPI_Datatype type, int keyval){
+ smpi_type_key_elem elem = xbt_dict_get_or_null_ext(smpi_type_keyvals, (const char*)&keyval, sizeof(int));
+ 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_ext(type->attributes, (const char*)&keyval, sizeof(int));
+ return MPI_SUCCESS;
+}
+
+int smpi_type_attr_get(MPI_Datatype type, int keyval, void* attr_value, int* flag){
+ smpi_type_key_elem elem = xbt_dict_get_or_null_ext(smpi_type_keyvals, (const char*)&keyval, sizeof(int));
+ 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_ext(type->attributes, (const char*)&keyval, sizeof(int));
+ *flag=1;
+ }
+ CATCH(ex) {
+ *flag=0;
+ xbt_ex_free(ex);
+ }
+ 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();
+ smpi_type_key_elem elem = xbt_dict_get_or_null_ext(smpi_type_keyvals, (const char*)&keyval, sizeof(int));
+ 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_ext(type->attributes, (const char*)&keyval, sizeof(int), attr_value, NULL);
+ 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;
+ xbt_dict_set_ext(smpi_type_keyvals,(const char*)keyval, sizeof(int),(void*)value, NULL);
+ type_keyval_id++;
+ return MPI_SUCCESS;
+}
+
+int smpi_type_keyval_free(int* keyval){
+ smpi_type_key_elem elem = xbt_dict_get_or_null_ext(smpi_type_keyvals, (const char*)keyval, sizeof(int));
+ if(!elem){
+ return MPI_ERR_ARG;
+ }
+ xbt_dict_remove_ext(smpi_type_keyvals, (const char*)keyval, sizeof(int));
+ xbt_free(elem);
+ return MPI_SUCCESS;
+}
+
+int smpi_mpi_pack(void* inbuf, int incount, MPI_Datatype type, void* outbuf, int outcount, int* position, MPI_Comm comm){
+ size_t size = smpi_datatype_size(type);
+ if (outcount - *position < incount*size)
+ return MPI_ERR_BUFFER;
+ smpi_datatype_copy(inbuf, incount, type,
+ (char*)outbuf + *position, outcount, MPI_CHAR);
+ *position += incount * size;
+ return MPI_SUCCESS;
+}
+
+int smpi_mpi_unpack(void* inbuf, int insize, int* position, void* outbuf, int outcount, MPI_Datatype type, MPI_Comm comm){
+ size_t size = smpi_datatype_size(type);
+ if (outcount*size> insize)
+ return MPI_ERR_BUFFER;
+ smpi_datatype_copy((char*)inbuf + *position, insize, MPI_CHAR,
+ outbuf, outcount, type);
+ *position += outcount * size;
+ return MPI_SUCCESS;
+}