Redefine the default boolean type to gmx_bool.
[alexxy/gromacs.git] / src / gmxlib / thread_mpi / group.c
1 /*
2 This source code file is part of thread_mpi.  
3 Written by Sander Pronk, Erik Lindahl, and possibly others. 
4
5 Copyright (c) 2009, Sander Pronk, Erik Lindahl.
6 All rights reserved.
7
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
10 1) Redistributions of source code must retain the above copyright
11    notice, this list of conditions and the following disclaimer.
12 2) Redistributions in binary form must reproduce the above copyright
13    notice, this list of conditions and the following disclaimer in the
14    documentation and/or other materials provided with the distribution.
15 3) Neither the name of the copyright holders nor the
16    names of its contributors may be used to endorse or promote products
17    derived from this software without specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY US ''AS IS'' AND ANY
20 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 DISCLAIMED. IN NO EVENT SHALL WE BE LIABLE FOR ANY
23 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 If you want to redistribute modifications, please consider that
31 scientific software is very special. Version control is crucial -
32 bugs must be traceable. We will be happy to consider code for
33 inclusion in the official distribution, but derived work should not
34 be called official thread_mpi. Details are found in the README & COPYING
35 files.
36 */
37
38 #ifdef HAVE_TMPI_CONFIG_H
39 #include "tmpi_config.h"
40 #endif
41
42 #ifdef HAVE_CONFIG_H
43 #include "config.h"
44 #endif
45
46 #ifdef HAVE_UNISTD_H
47 #include <unistd.h>
48 #endif
49
50 #include <errno.h>
51 #include <stdlib.h>
52 #include <stdio.h>
53 #include <stdarg.h>
54 #include <string.h>
55
56
57 #include "impl.h"
58
59
60
61 /* Group query & manipulation functions */
62
63 gmx_bool tMPI_In_group(tMPI_Group group)
64 {
65     int i;
66     struct tmpi_thread *cur;
67
68     cur=tMPI_Get_current();
69     for(i=0;i<group->N;i++)
70     {
71         if (group->peers[i] == cur)
72             return TRUE;
73     }
74     return FALSE;
75 }
76
77 int tMPI_Group_size(tMPI_Group group, int *size)
78 {
79 #ifdef TMPI_TRACE
80     tMPI_Trace_print("tMPI_Group_size(%p, %p)", group, size);
81 #endif
82
83     if (group)
84         *size = group->N;
85     else
86         *size = 0;
87     return TMPI_SUCCESS;
88 }
89
90 int tMPI_Group_rank(tMPI_Group group, int *rank)
91 {    
92     int i;
93     struct tmpi_thread *cur;
94
95 #ifdef TMPI_TRACE
96     tMPI_Trace_print("tMPI_Group_rank(%p, %p)", group, rank);
97 #endif
98     if (!group)
99         return TMPI_UNDEFINED;
100
101     /* search for my id in the list of peers */
102     cur=tMPI_Get_current();
103     for(i=0;i<group->N;i++)
104     {
105         if (group->peers[i] == cur)
106         {
107             *rank=i;
108             return TMPI_SUCCESS;
109         }
110     }
111     return TMPI_UNDEFINED;
112 }
113
114
115
116 tMPI_Group tMPI_Group_alloc(void)
117 {
118     struct tmpi_group_ *ret;
119
120     ret=(struct tmpi_group_*)tMPI_Malloc(sizeof(struct tmpi_group_));
121     ret->peers=(struct tmpi_thread**)tMPI_Malloc(
122                                 sizeof(struct tmpi_thread*)*Nthreads);
123     ret->N=0;
124 #if 0
125     ret->Nrefs=1;
126 #endif
127
128     return ret;
129 }
130
131 int tMPI_Group_free(tMPI_Group *group)
132 {
133 #ifdef TMPI_TRACE
134     tMPI_Trace_print("tMPI_Group_free(%p)", group);
135 #endif
136     if (group)
137     {
138         free((*group)->peers);
139         free(*group);
140     }
141     return TMPI_SUCCESS;
142 }
143
144 int tMPI_Comm_group(tMPI_Comm comm, tMPI_Group *group)
145 {
146     int i;
147     struct tmpi_group_ *ret=tMPI_Group_alloc();
148
149 #ifdef TMPI_TRACE
150     tMPI_Trace_print("tMPI_Comm_group(%p, %p)", comm, group);
151 #endif
152     ret->N=comm->grp.N;
153     for(i=0;i<comm->grp.N;i++)
154     {
155         ret->peers[i]=comm->grp.peers[i];
156     }
157     *group=ret;
158 #if 0
159     if (comm)
160     {
161         *group=&(comm->grp);
162     }
163     else
164     {
165         *group=NULL;
166     }
167 #endif
168
169     return TMPI_SUCCESS;
170 }
171
172
173 int tMPI_Group_incl(tMPI_Group group, int n, int *ranks, tMPI_Group *newgroup)
174 {
175     int i;
176     tMPI_Group ng;
177
178 #ifdef TMPI_TRACE
179     tMPI_Trace_print("tMPI_Group_incl(%p, %d, %p, %p, %p)", group, n, ranks, 
180                        newgroup);
181 #endif
182     /* just allocate and copy */
183     ng=tMPI_Group_alloc();
184     ng->N=n;
185     for(i=0;i<n;i++)
186     {
187         if (ranks[i] < 0 || !group || ranks[i] >= group->N)
188         {
189             return tMPI_Error(TMPI_COMM_WORLD, TMPI_ERR_GROUP_RANK);
190         }
191         ng->peers[i]=group->peers[ranks[i]];
192     }
193     *newgroup=ng;
194     return TMPI_SUCCESS;
195 }
196
197
198