Logo AND Algorithmique Numérique Distribuée

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