Logo AND Algorithmique Numérique Distribuée

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