Code beautification with uncrustify
[alexxy/gromacs.git] / src / gromacs / 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 tmpi_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         {
73             return TRUE;
74         }
75     }
76     return FALSE;
77 }
78
79 int tMPI_Group_size(tMPI_Group group, int *size)
80 {
81 #ifdef TMPI_TRACE
82     tMPI_Trace_print("tMPI_Group_size(%p, %p)", group, size);
83 #endif
84
85     if (group)
86     {
87         *size = group->N;
88     }
89     else
90     {
91         *size = 0;
92     }
93     return TMPI_SUCCESS;
94 }
95
96 int tMPI_Group_rank(tMPI_Group group, int *rank)
97 {
98     int                 i;
99     struct tmpi_thread *cur;
100
101 #ifdef TMPI_TRACE
102     tMPI_Trace_print("tMPI_Group_rank(%p, %p)", group, rank);
103 #endif
104     if (!group)
105     {
106         return TMPI_UNDEFINED;
107     }
108
109     /* search for my id in the list of peers */
110     cur = tMPI_Get_current();
111     for (i = 0; i < group->N; i++)
112     {
113         if (group->peers[i] == cur)
114         {
115             *rank = i;
116             return TMPI_SUCCESS;
117         }
118     }
119     return TMPI_UNDEFINED;
120 }
121
122
123
124 tMPI_Group tMPI_Group_alloc(void)
125 {
126     struct tmpi_group_ *ret;
127
128     ret        = (struct tmpi_group_*)tMPI_Malloc(sizeof(struct tmpi_group_));
129     ret->peers = (struct tmpi_thread**)tMPI_Malloc(
130                 sizeof(struct tmpi_thread*)*Nthreads);
131     ret->N = 0;
132 #if 0
133     ret->Nrefs = 1;
134 #endif
135
136     return ret;
137 }
138
139 int tMPI_Group_free(tMPI_Group *group)
140 {
141 #ifdef TMPI_TRACE
142     tMPI_Trace_print("tMPI_Group_free(%p)", group);
143 #endif
144     if (group)
145     {
146         free((*group)->peers);
147         free(*group);
148     }
149     return TMPI_SUCCESS;
150 }
151
152 int tMPI_Comm_group(tMPI_Comm comm, tMPI_Group *group)
153 {
154     int                 i;
155     struct tmpi_group_ *ret = tMPI_Group_alloc();
156
157 #ifdef TMPI_TRACE
158     tMPI_Trace_print("tMPI_Comm_group(%p, %p)", comm, group);
159 #endif
160     ret->N = comm->grp.N;
161     for (i = 0; i < comm->grp.N; i++)
162     {
163         ret->peers[i] = comm->grp.peers[i];
164     }
165     *group = ret;
166 #if 0
167     if (comm)
168     {
169         *group = &(comm->grp);
170     }
171     else
172     {
173         *group = NULL;
174     }
175 #endif
176
177     return TMPI_SUCCESS;
178 }
179
180
181 int tMPI_Group_incl(tMPI_Group group, int n, int *ranks, tMPI_Group *newgroup)
182 {
183     int        i;
184     tMPI_Group ng;
185
186 #ifdef TMPI_TRACE
187     tMPI_Trace_print("tMPI_Group_incl(%p, %d, %p, %p, %p)", group, n, ranks,
188                      newgroup);
189 #endif
190     /* just allocate and copy */
191     ng    = tMPI_Group_alloc();
192     ng->N = n;
193     for (i = 0; i < n; i++)
194     {
195         if (ranks[i] < 0 || !group || ranks[i] >= group->N)
196         {
197             return tMPI_Error(TMPI_COMM_WORLD, TMPI_ERR_GROUP_RANK);
198         }
199         ng->peers[i] = group->peers[ranks[i]];
200     }
201     *newgroup = ng;
202     return TMPI_SUCCESS;
203 }