Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix memory leaks in msg_pmm.
[simgrid.git] / examples / smpi / MM / topo.c
1 /*!
2  * get the information of which thread are on the same node
3  *
4  */
5
6 #include <stdlib.h>
7 #include <string.h>
8 #include <stdio.h>
9 #include "topo.h"
10
11 int split_comm_intra_node(MPI_Comm comm, MPI_Comm* comm_intra, int key) {
12   // collect processor names
13   char name[MPI_MAX_PROCESSOR_NAME];
14   int len;
15   int size;
16   char** names = get_names(comm);
17   int color = -1;
18   int i = 0;
19
20   MPI_Get_processor_name(name, &len);
21   MPI_Comm_size(comm, &size);
22   while (i < size){
23     if (strcmp(name, names[i]) == 0) {
24       break;
25     }
26     i++;
27   }
28   color = i;
29   free(names);
30   // split the communicator
31   return MPI_Comm_split(comm, color, key, comm_intra);
32 }
33
34 char** get_names(MPI_Comm comm){
35   char name[MPI_MAX_PROCESSOR_NAME];
36   int len;
37   int size;
38   int i;
39   char** friendly_names;
40   char*  names;
41
42   MPI_Get_processor_name(name, &len);
43   MPI_Comm_size(comm, &size);
44   friendly_names = malloc(sizeof(char*) * size);
45   names          = malloc(sizeof(char) * MPI_MAX_PROCESSOR_NAME * size);
46
47   MPI_Allgather(name, MPI_MAX_PROCESSOR_NAME, MPI_CHAR, names,
48                 MPI_MAX_PROCESSOR_NAME, MPI_CHAR, comm);
49
50   for( i = 0; i < size;i++) friendly_names[i] = &names[MPI_MAX_PROCESSOR_NAME * i];
51   return friendly_names;
52 }
53
54 int* get_index(MPI_Comm comm, MPI_Comm comm_intra){
55   int rank = 0;
56   int size;
57   int* index;
58
59   MPI_Comm_rank(comm_intra, &rank);
60   MPI_Comm_size(comm, &size);
61   index = (int*)malloc(sizeof(int) * size);
62   MPI_Allgather(&rank, 1, MPI_INT, index, 1, MPI_INT, comm);
63   return index;
64 }
65