Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix copyright headers
[simgrid.git] / src / smpi / smpi_group.c
1 /* Copyright (c) 2010. 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 "private.h"
8
9 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_group, smpi,
10                                 "Logging specific to SMPI (group)");
11
12 typedef struct s_smpi_mpi_group {
13   int size;
14   int* rank_to_index_map;
15   int* index_to_rank_map;
16   int refcount;
17 } s_smpi_mpi_group_t;
18
19 static s_smpi_mpi_group_t mpi_MPI_GROUP_EMPTY = {
20   0,    /* size */
21   NULL, /* rank_to_index_map */
22   NULL, /* index_to_rank_map */
23   1,    /* refcount: start > 0 so that this group never gets freed */
24 };
25 MPI_Group MPI_GROUP_EMPTY = &mpi_MPI_GROUP_EMPTY;
26
27 MPI_Group smpi_group_new(int size) {
28   MPI_Group group;
29   int i, count;
30
31   count = smpi_process_count();
32   group = xbt_new(s_smpi_mpi_group_t, 1);
33   group->size = size;
34   group->rank_to_index_map = xbt_new(int, size);
35   group->index_to_rank_map = xbt_new(int, count);
36   group->refcount = 0;
37   for(i = 0; i < size; i++) {
38     group->rank_to_index_map[i] = MPI_UNDEFINED;
39   }
40   for(i = 0; i < count; i++) {
41     group->index_to_rank_map[i] = MPI_UNDEFINED;
42   }
43   return group;
44 }
45
46 void smpi_group_destroy(MPI_Group group) {
47   if(smpi_group_unuse(group) <= 0) {
48     xbt_free(group->rank_to_index_map);
49     xbt_free(group->index_to_rank_map);
50     xbt_free(group);
51   }
52 }
53
54 void smpi_group_set_mapping(MPI_Group group, int index, int rank) {
55   if(rank < group->size && index < smpi_process_count()) {
56     group->rank_to_index_map[rank] = index;
57     group->index_to_rank_map[index] = rank;
58   }
59 }
60
61 int smpi_group_index(MPI_Group group, int rank) {
62   int index = MPI_UNDEFINED;
63
64   if(rank < group->size) {
65     index = group->rank_to_index_map[rank];
66   }
67   return index;
68 }
69
70 int smpi_group_rank(MPI_Group group, int index) {
71   int rank = MPI_UNDEFINED;
72
73   if(index < smpi_process_count()) {
74     rank = group->index_to_rank_map[index];
75   }
76   return rank;
77 }
78
79 int smpi_group_use(MPI_Group group) {
80   group->refcount++;
81   return group->refcount;
82 }
83
84 int smpi_group_unuse(MPI_Group group) {
85   group->refcount--;
86   return group->refcount;
87 }
88
89 int smpi_group_size(MPI_Group group) {
90   return group->size;
91 }
92
93 int smpi_group_compare(MPI_Group group1, MPI_Group group2) {
94   int result;
95   int i, index, rank, size;
96
97   result = MPI_IDENT;
98   if(smpi_group_size(group1) != smpi_group_size(group2)) {
99     result = MPI_UNEQUAL;
100   } else {
101     size = smpi_group_size(group2);
102     for(i = 0; i < size; i++) {
103       index = smpi_group_index(group1, i);
104       rank = smpi_group_rank(group2, index);
105       if(rank == MPI_UNDEFINED) {
106         result = MPI_UNEQUAL;
107         break;
108       }
109       if(rank != i) {
110         result = MPI_SIMILAR;
111       }
112     }
113   }
114   return result;
115 }