Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
add045fa1d2474a92ac0ccfeb52594f575ef1bc0
[simgrid.git] / src / smpi / smpi_comm.c
1 /* Copyright (c) 2010-2014. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include <stdlib.h>
8
9 #include "private.h"
10 #include "xbt/dict.h"
11 #include "smpi_mpi_dt_private.h"
12 #include "limits.h"
13 #include "simix/smx_private.h"
14 #include "colls/colls.h"
15 #include "xbt/ex.h"
16
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_comm, smpi,
18                                 "Logging specific to SMPI (comm)");
19
20 //number of bytes to hold an int converted in char*
21 #define INTSIZEDCHAR (sizeof(int)*CHAR_BIT-1)/3 + 3 
22 xbt_dict_t smpi_comm_keyvals = NULL;
23 int comm_keyval_id=MPI_TAG_UB+1;//avoid collisions
24
25 /* Support for cartesian topology was added, but there are 2 other types of
26  * topology, graph et dist graph. In order to support them, we have to add a
27  * field MPIR_Topo_type, and replace the MPI_Topology field by an union. */
28
29 typedef struct s_smpi_mpi_communicator {
30   MPI_Group group;
31   MPIR_Topo_type topoType; 
32   MPI_Topology topo; // to be replaced by an union
33   int refcount;
34   MPI_Comm leaders_comm;//inter-node communicator
35   MPI_Comm intra_comm;//intra-node communicator . For MPI_COMM_WORLD this can't be used, as var is global.
36   //use an intracomm stored in the process data instead
37   int* leaders_map; //who is the leader of each process
38   int is_uniform;
39   int* non_uniform_map; //set if smp nodes have a different number of processes allocated
40   int is_blocked;// are ranks allocated on the same smp node contiguous ?
41   xbt_dict_t attributes;
42 } s_smpi_mpi_communicator_t;
43
44 static int smpi_compare_rankmap(const void *a, const void *b)
45 {
46   const int* x = (const int*)a;
47   const int* y = (const int*)b;
48
49   if (x[1] < y[1]) {
50     return -1;
51   }
52   if (x[1] == y[1]) {
53     if (x[0] < y[0]) {
54       return -1;
55     }
56     if (x[0] == y[0]) {
57       return 0;
58     }
59     return 1;
60   }
61   return 1;
62 }
63
64 MPI_Comm smpi_comm_new(MPI_Group group, MPI_Topology topo)
65 {
66   MPI_Comm comm;
67
68   comm = xbt_new(s_smpi_mpi_communicator_t, 1);
69   comm->group = group;
70   smpi_group_use(comm->group);
71   comm->refcount=1;
72   comm->topoType = -1;
73   comm->topo = topo;
74   comm->intra_comm = MPI_COMM_NULL;
75   comm->leaders_comm = MPI_COMM_NULL;
76   comm->is_uniform=1;
77   comm->non_uniform_map = NULL;
78   comm->leaders_map = NULL;
79   comm->is_blocked=0;
80   comm->attributes=NULL;
81   return comm;
82 }
83
84 void smpi_comm_destroy(MPI_Comm comm)
85 {
86   if (comm == MPI_COMM_UNINITIALIZED)
87     comm = smpi_process_comm_world();
88   smpi_group_unuse(comm->group);
89   smpi_topo_destroy(comm->topo); // there's no use count on topos
90   smpi_comm_unuse(comm);
91 }
92
93 int smpi_comm_dup(MPI_Comm comm, MPI_Comm* newcomm){
94   if(smpi_privatize_global_variables){ //we need to switch here, as the called function may silently touch global variables
95      smpi_switch_data_segment(smpi_process_index());
96    }
97   (*newcomm) = smpi_comm_new(smpi_comm_group(comm), smpi_comm_topo(comm));
98   int ret = MPI_SUCCESS;
99   //todo: faire en sorte que ça fonctionne avec un communicator dupliqué (refaire un init_smp ?)
100   
101  /* MPI_Comm tmp=smpi_comm_get_intra_comm(comm);
102   if( tmp != MPI_COMM_NULL)
103     smpi_comm_set_intra_comm((*newcomm), smpi_comm_dup(tmp));
104   tmp=smpi_comm_get_leaders_comm(comm);
105   if( tmp != MPI_COMM_NULL)
106     smpi_comm_set_leaders_comm((*newcomm), smpi_comm_dup(tmp));
107   if(comm->non_uniform_map !=NULL){
108     (*newcomm)->non_uniform_map= 
109       xbt_malloc(smpi_comm_size(comm->leaders_comm)*sizeof(int));
110     memcpy((*newcomm)->non_uniform_map,
111       comm->non_uniform_map,smpi_comm_size(comm->leaders_comm)*sizeof(int) );
112   }
113   if(comm->leaders_map !=NULL){
114     (*newcomm)->leaders_map=xbt_malloc(smpi_comm_size(comm)*sizeof(int));
115     memcpy((*newcomm)->leaders_map, 
116       comm->leaders_map,smpi_comm_size(comm)*sizeof(int) );
117   }*/
118   if(comm->attributes !=NULL){
119       (*newcomm)->attributes=xbt_dict_new();
120       xbt_dict_cursor_t cursor = NULL;
121       int *key;
122       int flag;
123       void* value_in;
124       void* value_out;
125       xbt_dict_foreach(comm->attributes, cursor, key, value_in){
126         smpi_comm_key_elem elem = xbt_dict_get_or_null(smpi_comm_keyvals, (const char*)key);
127         if(elem && elem->copy_fn!=MPI_NULL_COPY_FN){
128           ret = elem->copy_fn(comm, atoi((const char*)key), NULL, value_in, &value_out, &flag );
129           if(ret!=MPI_SUCCESS){
130             smpi_comm_destroy(*newcomm);
131             *newcomm=MPI_COMM_NULL;
132             return ret;
133           }
134           if(flag)
135             xbt_dict_set((*newcomm)->attributes, (const char*)key,value_out, NULL);
136         }
137       }
138     }
139   return ret;
140 }
141
142
143 MPI_Group smpi_comm_group(MPI_Comm comm)
144 {
145   if (comm == MPI_COMM_UNINITIALIZED)
146     comm = smpi_process_comm_world();
147
148   return comm->group;
149 }
150
151 MPI_Topology smpi_comm_topo(MPI_Comm comm) {
152   if (comm != MPI_COMM_NULL)
153     return comm->topo;
154   return NULL;
155 }
156
157 int smpi_comm_size(MPI_Comm comm)
158 {
159   if (comm == MPI_COMM_UNINITIALIZED)
160     comm = smpi_process_comm_world();
161
162   return smpi_group_size(smpi_comm_group(comm));
163 }
164
165 int smpi_comm_rank(MPI_Comm comm)
166 {
167   if (comm == MPI_COMM_UNINITIALIZED)
168     comm = smpi_process_comm_world();
169   return smpi_group_rank(smpi_comm_group(comm), smpi_process_index());
170 }
171
172 void smpi_comm_get_name (MPI_Comm comm, char* name, int* len)
173 {
174   if (comm == MPI_COMM_UNINITIALIZED)
175     comm = smpi_process_comm_world();
176   if(comm == MPI_COMM_WORLD) {
177     strcpy(name, "WORLD");
178     *len = 5;
179   } else {
180     *len = snprintf(name, MPI_MAX_NAME_STRING, "%p", comm);
181   }
182 }
183
184 void smpi_comm_set_leaders_comm(MPI_Comm comm, MPI_Comm leaders){
185   if (comm == MPI_COMM_UNINITIALIZED)
186     comm = smpi_process_comm_world();
187   comm->leaders_comm=leaders;
188 }
189
190 void smpi_comm_set_intra_comm(MPI_Comm comm, MPI_Comm leaders){
191   comm->intra_comm=leaders;
192 }
193
194 int* smpi_comm_get_non_uniform_map(MPI_Comm comm){
195   if (comm == MPI_COMM_UNINITIALIZED)
196     comm = smpi_process_comm_world();
197   return comm->non_uniform_map;
198 }
199
200 int* smpi_comm_get_leaders_map(MPI_Comm comm){
201   if (comm == MPI_COMM_UNINITIALIZED)
202     comm = smpi_process_comm_world();
203   return comm->leaders_map;
204 }
205
206 MPI_Comm smpi_comm_get_leaders_comm(MPI_Comm comm){
207   if (comm == MPI_COMM_UNINITIALIZED)
208     comm = smpi_process_comm_world();
209   return comm->leaders_comm;
210 }
211
212 MPI_Comm smpi_comm_get_intra_comm(MPI_Comm comm){
213   if (comm == MPI_COMM_UNINITIALIZED || comm==MPI_COMM_WORLD) 
214     return smpi_process_get_comm_intra();
215   else return comm->intra_comm;
216 }
217
218 int smpi_comm_is_uniform(MPI_Comm comm){
219   if (comm == MPI_COMM_UNINITIALIZED)
220     comm = smpi_process_comm_world();
221   return comm->is_uniform;
222 }
223
224 int smpi_comm_is_blocked(MPI_Comm comm){
225   if (comm == MPI_COMM_UNINITIALIZED)
226     comm = smpi_process_comm_world();
227   return comm->is_blocked;
228 }
229
230 MPI_Comm smpi_comm_split(MPI_Comm comm, int color, int key)
231 {
232   if (comm == MPI_COMM_UNINITIALIZED)
233     comm = smpi_process_comm_world();
234   int system_tag = 123;
235   int index, rank, size, i, j, count, reqs;
236   int* sendbuf;
237   int* recvbuf;
238   int* rankmap;
239   MPI_Group group, group_root, group_out;
240   MPI_Request* requests;
241
242   group_root = group_out = NULL;
243   group = smpi_comm_group(comm);
244   rank = smpi_comm_rank(comm);
245   size = smpi_comm_size(comm);
246   /* Gather all colors and keys on rank 0 */
247   sendbuf = xbt_new(int, 2);
248   sendbuf[0] = color;
249   sendbuf[1] = key;
250   if(rank == 0) {
251     recvbuf = xbt_new(int, 2 * size);
252   } else {
253     recvbuf = NULL;
254   }
255   smpi_mpi_gather(sendbuf, 2, MPI_INT, recvbuf, 2, MPI_INT, 0, comm);
256   xbt_free(sendbuf);
257   /* Do the actual job */
258   if(rank == 0) {
259     rankmap = xbt_new(int, 2 * size);
260     for(i = 0; i < size; i++) {
261       if(recvbuf[2 * i] == MPI_UNDEFINED) {
262         continue;
263       }
264       count = 0;
265       for(j = i + 1; j < size; j++)  {
266         if(recvbuf[2 * i] == recvbuf[2 * j]) {
267           recvbuf[2 * j] = MPI_UNDEFINED;
268           rankmap[2 * count] = j;
269           rankmap[2 * count + 1] = recvbuf[2 * j + 1];
270           count++;
271         }
272       }
273       /* Add self in the group */
274       recvbuf[2 * i] = MPI_UNDEFINED;
275       rankmap[2 * count] = i;
276       rankmap[2 * count + 1] = recvbuf[2 * i + 1];
277       count++;
278       qsort(rankmap, count, 2 * sizeof(int), &smpi_compare_rankmap);
279       group_out = smpi_group_new(count);
280       if(i == 0) {
281         group_root = group_out; /* Save root's group */
282       }
283       for(j = 0; j < count; j++) {
284         //increment refcounter in order to avoid freeing the group too quick before copy
285         index = smpi_group_index(group, rankmap[2 * j]);
286         smpi_group_set_mapping(group_out, index, j);
287       }
288       requests = xbt_new(MPI_Request, count);
289       reqs = 0;
290       for(j = 0; j < count; j++) {
291         if(rankmap[2 * j] != 0) {
292           requests[reqs] = smpi_isend_init(&group_out, 1, MPI_PTR, rankmap[2 * j], system_tag, comm);
293           reqs++;
294         }
295       }
296       smpi_mpi_startall(reqs, requests);
297       smpi_mpi_waitall(reqs, requests, MPI_STATUS_IGNORE);
298       xbt_free(requests);
299     }
300     xbt_free(recvbuf);
301     group_out = group_root; /* exit with root's group */
302   } else {
303     if(color != MPI_UNDEFINED) {
304       smpi_mpi_recv(&group_out, 1, MPI_PTR, 0, system_tag, comm, MPI_STATUS_IGNORE);
305       if(group_out){
306         group_out=smpi_group_copy(group_out);
307       }
308     } /* otherwise, exit with group_out == NULL */
309   }
310   return group_out ? smpi_comm_new(group_out, NULL) : MPI_COMM_NULL;
311 }
312
313 void smpi_comm_use(MPI_Comm comm){
314   if (comm == MPI_COMM_UNINITIALIZED)
315     comm = smpi_process_comm_world();
316   comm->refcount++;
317 }
318
319 void smpi_comm_unuse(MPI_Comm comm){
320   if (comm == MPI_COMM_UNINITIALIZED)
321     comm = smpi_process_comm_world();
322   comm->refcount--;
323   if(comm->refcount==0){
324     if(comm->intra_comm != MPI_COMM_NULL)
325       smpi_comm_unuse(comm->intra_comm);
326     if(comm->leaders_comm != MPI_COMM_NULL)
327       smpi_comm_unuse(comm->leaders_comm);
328     if(comm->non_uniform_map !=NULL)
329       xbt_free(comm->non_uniform_map);
330     if(comm->leaders_map !=NULL)
331       xbt_free(comm->leaders_map);
332     if(comm->attributes !=NULL){
333       xbt_dict_cursor_t cursor = NULL;
334       int* key;
335       void * value;
336       int flag;
337       xbt_dict_foreach(comm->attributes, cursor, key, value){
338         smpi_comm_key_elem elem = xbt_dict_get_or_null(smpi_comm_keyvals, (const char*)key);
339         if(elem &&  elem->delete_fn)
340           elem->delete_fn(comm, atoi((const char*)key), value, &flag);
341       }
342     }
343     xbt_free(comm);
344   }
345 }
346
347 static int
348 compare_ints (const void *a, const void *b)
349 {
350   const int *da = (const int *) a;
351   const int *db = (const int *) b;
352
353   return (*da > *db) - (*da < *db);
354 }
355
356 void smpi_comm_init_smp(MPI_Comm comm){
357   int leader = -1;
358
359   if (comm == MPI_COMM_UNINITIALIZED)
360     comm = smpi_process_comm_world();
361
362   int comm_size =smpi_comm_size(comm);
363   
364   // If we are in replay - perform an ugly hack  
365   // say to SimGrid that we are not in replay for a while, because we need 
366   // the buffers to be copied for the following calls
367   int replaying = 0; //cache data to set it back again after
368   if(smpi_process_get_replaying()){
369    replaying=1;
370    smpi_process_set_replaying(0);
371   }
372
373   if(smpi_privatize_global_variables){ //we need to switch here, as the called function may silently touch global variables
374      smpi_switch_data_segment(smpi_process_index());
375    }
376   //identify neighbours in comm
377   //get the indexes of all processes sharing the same simix host
378   xbt_swag_t process_list = simcall_host_get_process_list(SIMIX_host_self());
379   int intra_comm_size = 0;
380   //only one process/node, disable SMP support and return
381 //  if(intra_comm_size==1){
382 //      smpi_comm_set_intra_comm(comm, MPI_COMM_SELF);
383 //      //smpi_comm_set_leaders_comm(comm, comm);
384 //      smpi_process_set_comm_intra(MPI_COMM_SELF);
385 //      return;
386 //  }
387
388
389   int i =0;
390   int min_index=INT_MAX;//the minimum index will be the leader
391   msg_process_t process = NULL;
392   xbt_swag_foreach(process, process_list) {
393     //is_in_comm=0;
394     int index = SIMIX_process_get_PID(process) -1;
395
396     if(smpi_group_rank(smpi_comm_group(comm),  index)!=MPI_UNDEFINED){
397         intra_comm_size++;
398       //the process is in the comm
399       if(index < min_index)
400         min_index=index;
401       i++;
402     }
403   }
404   XBT_DEBUG("number of processes deployed on my node : %d", intra_comm_size);
405   MPI_Group group_intra = smpi_group_new(intra_comm_size);
406   i=0;
407   process = NULL;
408   xbt_swag_foreach(process, process_list) {
409     //is_in_comm=0;
410     int index = SIMIX_process_get_PID(process) -1;
411     if(smpi_group_rank(smpi_comm_group(comm),  index)!=MPI_UNDEFINED){
412       smpi_group_set_mapping(group_intra, index, i);
413       i++;
414     }
415   }
416
417
418   MPI_Comm comm_intra = smpi_comm_new(group_intra, NULL);
419   //MPI_Comm shmem_comm = smpi_process_comm_intra();
420   //int intra_rank = smpi_comm_rank(shmem_comm);
421
422
423   //if(smpi_process_index()==min_index)
424   leader=min_index;
425
426   int * leaders_map= (int*)xbt_malloc0(sizeof(int)*comm_size);
427   int * leader_list= (int*)xbt_malloc0(sizeof(int)*comm_size);
428   for(i=0; i<comm_size; i++){
429       leader_list[i]=-1;
430   }
431
432   smpi_coll_tuned_allgather_mpich(&leader, 1, MPI_INT , leaders_map, 1, MPI_INT, comm);
433
434   if(smpi_privatize_global_variables){ //we need to switch here, as the called function may silently touch global variables
435      smpi_switch_data_segment(smpi_process_index());
436    }
437    
438   if(!comm->leaders_map){
439     comm->leaders_map= leaders_map;
440   }else{
441     xbt_free(leaders_map);
442   }
443   int j=0;
444   int leader_group_size = 0;
445   for(i=0; i<comm_size; i++){
446       int already_done=0;
447       for(j=0;j<leader_group_size; j++){
448         if(comm->leaders_map[i]==leader_list[j]){
449             already_done=1;
450         }
451       }
452       if(!already_done){
453         leader_list[leader_group_size]=comm->leaders_map[i];
454         leader_group_size++;
455       }
456   }
457   qsort(leader_list, leader_group_size, sizeof(int),compare_ints);
458
459   MPI_Group leaders_group = smpi_group_new(leader_group_size);
460
461
462   MPI_Comm leader_comm = MPI_COMM_NULL;
463   if(MPI_COMM_WORLD!=MPI_COMM_UNINITIALIZED && comm!=MPI_COMM_WORLD){
464     //create leader_communicator
465     for (i=0; i< leader_group_size;i++)
466       smpi_group_set_mapping(leaders_group, leader_list[i], i);
467     leader_comm = smpi_comm_new(leaders_group, NULL);
468     smpi_comm_set_leaders_comm(comm, leader_comm);
469     smpi_comm_set_intra_comm(comm, comm_intra);
470
471     //create intracommunicator
472    // smpi_comm_set_intra_comm(comm, smpi_comm_split(comm, *(int*)SIMIX_host_self(), comm_rank));
473   }else{
474     for (i=0; i< leader_group_size;i++)
475       smpi_group_set_mapping(leaders_group, leader_list[i], i);
476
477     leader_comm = smpi_comm_new(leaders_group, NULL);
478     if(smpi_comm_get_leaders_comm(comm)==MPI_COMM_NULL)
479       smpi_comm_set_leaders_comm(comm, leader_comm);
480     smpi_process_set_comm_intra(comm_intra);
481   }
482
483   int is_uniform = 1;
484
485   // Are the nodes uniform ? = same number of process/node
486   int my_local_size=smpi_comm_size(comm_intra);
487   if(smpi_comm_rank(comm_intra)==0) {
488     int* non_uniform_map = xbt_malloc0(sizeof(int)*leader_group_size);
489     smpi_coll_tuned_allgather_mpich(&my_local_size, 1, MPI_INT,
490         non_uniform_map, 1, MPI_INT, leader_comm);
491     for(i=0; i < leader_group_size; i++) {
492       if(non_uniform_map[0] != non_uniform_map[i]) {
493         is_uniform = 0;
494         break;
495       }
496     }
497     if(!is_uniform && smpi_comm_is_uniform(comm)){
498         comm->non_uniform_map= non_uniform_map;
499     }else{
500         xbt_free(non_uniform_map);
501     }
502     comm->is_uniform=is_uniform;
503   }
504   smpi_coll_tuned_bcast_mpich(&(comm->is_uniform),1, MPI_INT, 0, comm_intra );
505
506   if(smpi_privatize_global_variables){ //we need to switch here, as the called function may silently touch global variables
507      smpi_switch_data_segment(smpi_process_index());
508    }
509   // Are the ranks blocked ? = allocated contiguously on the SMP nodes
510   int is_blocked=1;
511   int prev=smpi_group_rank(smpi_comm_group(comm), smpi_group_index(smpi_comm_group(comm_intra), 0));
512     for (i=1; i<my_local_size; i++){
513       int this=smpi_group_rank(smpi_comm_group(comm),smpi_group_index(smpi_comm_group(comm_intra), i));
514       if(this!=prev+1){
515         is_blocked=0;
516         break;
517       }
518       prev = this;
519   }
520
521   int global_blocked;
522   smpi_mpi_allreduce(&is_blocked, &(global_blocked), 1,
523             MPI_INT, MPI_LAND, comm);
524
525   if(MPI_COMM_WORLD==SMPI_UNINITIALIZED || comm==MPI_COMM_WORLD){
526     if(smpi_comm_rank(comm)==0){
527         comm->is_blocked=global_blocked;
528     }
529   }else{
530     comm->is_blocked=global_blocked;
531   }
532   xbt_free(leader_list);
533   
534   if(replaying==1)
535     smpi_process_set_replaying(1); 
536 }
537
538 int smpi_comm_attr_delete(MPI_Comm comm, int keyval){
539   char* tmpkey=xbt_malloc(INTSIZEDCHAR);
540   sprintf(tmpkey, "%d", keyval);
541   smpi_comm_key_elem elem = xbt_dict_get_or_null(smpi_comm_keyvals, (const char*)tmpkey);
542   if(!elem)
543     return MPI_ERR_ARG;
544   if(elem->delete_fn!=MPI_NULL_DELETE_FN){
545     void * value;
546     int flag;
547     if(smpi_comm_attr_get(comm, keyval, &value, &flag)==MPI_SUCCESS){
548       int ret = elem->delete_fn(comm, keyval, &value, &flag);
549       if(ret!=MPI_SUCCESS) return ret;
550     }
551   }  
552   if(comm->attributes==NULL)
553     return MPI_ERR_ARG;
554
555   xbt_dict_remove(comm->attributes, (const char*)tmpkey);
556   xbt_free(tmpkey);
557   return MPI_SUCCESS;
558 }
559
560 int smpi_comm_attr_get(MPI_Comm comm, int keyval, void* attr_value, int* flag){
561   char* tmpkey=xbt_malloc(INTSIZEDCHAR);
562   sprintf(tmpkey, "%d", keyval);
563   smpi_comm_key_elem elem = xbt_dict_get_or_null(smpi_comm_keyvals, (const char*)tmpkey);
564   if(!elem)
565     return MPI_ERR_ARG;
566   xbt_ex_t ex;
567   if(comm->attributes==NULL){
568     *flag=0;
569     return MPI_SUCCESS;
570   }
571   TRY {
572     *(void**)attr_value = xbt_dict_get(comm->attributes, (const char*)tmpkey);
573     *flag=1;
574   }
575   CATCH(ex) {
576     *flag=0;
577     xbt_ex_free(ex);
578   }
579   xbt_free(tmpkey);
580   return MPI_SUCCESS;
581 }
582
583 int smpi_comm_attr_put(MPI_Comm comm, int keyval, void* attr_value){
584   if(!smpi_comm_keyvals)
585   smpi_comm_keyvals = xbt_dict_new();
586   char* tmpkey=xbt_malloc(INTSIZEDCHAR);
587   sprintf(tmpkey, "%d", keyval);
588   smpi_comm_key_elem elem = xbt_dict_get_or_null(smpi_comm_keyvals, (const char*)tmpkey);
589   if(!elem )
590     return MPI_ERR_ARG;
591   int flag;
592   void* value;
593   smpi_comm_attr_get(comm, keyval, &value, &flag);
594   if(flag && elem->delete_fn!=MPI_NULL_DELETE_FN){
595     int ret = elem->delete_fn(comm, keyval, &value, &flag);
596     if(ret!=MPI_SUCCESS) return ret;
597   }
598   if(comm->attributes==NULL)
599     comm->attributes=xbt_dict_new();
600
601   xbt_dict_set(comm->attributes, (const char*)tmpkey, attr_value, NULL);
602   xbt_free(tmpkey);
603   return MPI_SUCCESS;
604 }
605
606 int smpi_comm_keyval_create(MPI_Comm_copy_attr_function* copy_fn, MPI_Comm_delete_attr_function* delete_fn, int* keyval, void* extra_state){
607
608   if(!smpi_comm_keyvals)
609   smpi_comm_keyvals = xbt_dict_new();
610   
611   smpi_comm_key_elem value = (smpi_comm_key_elem) xbt_new0(s_smpi_mpi_comm_key_elem_t,1);
612   
613   value->copy_fn=copy_fn;
614   value->delete_fn=delete_fn;
615   
616   *keyval = comm_keyval_id;
617   char* tmpkey=xbt_malloc(INTSIZEDCHAR);
618   sprintf(tmpkey, "%d", *keyval);
619   xbt_dict_set(smpi_comm_keyvals,(const char*)tmpkey,(void*)value, NULL);
620   comm_keyval_id++;
621   xbt_free(tmpkey);
622   return MPI_SUCCESS;
623 }
624
625 int smpi_comm_keyval_free(int* keyval){
626   smpi_comm_key_elem elem = xbt_dict_get_or_null(smpi_comm_keyvals, (const char*)keyval);
627   if(!elem)
628     return MPI_ERR_ARG;
629   char* tmpkey=xbt_malloc(INTSIZEDCHAR);
630   sprintf(tmpkey, "%d", *keyval);
631   xbt_dict_remove(smpi_comm_keyvals, (const char*)tmpkey);
632   xbt_free(elem);
633   xbt_free(tmpkey);
634   return MPI_SUCCESS;
635 }
636