Logo AND Algorithmique Numérique Distribuée

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