minor fixes in ditribution files
[gromacs/qmmm-gamess-us.git] / src / gmxlib / thread_mpi / group.c
blobf7b17ee0b1c128a64d9f127eab561b5a9b81ae9f
1 /*
2 This source code file is part of thread_mpi.
3 Written by Sander Pronk, Erik Lindahl, and possibly others.
5 Copyright (c) 2009, Sander Pronk, Erik Lindahl.
6 All rights reserved.
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.
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.
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.
38 #ifdef HAVE_TMPI_CONFIG_H
39 #include "tmpi_config.h"
40 #endif
42 #ifdef HAVE_CONFIG_H
43 #include "config.h"
44 #endif
46 #ifdef HAVE_UNISTD_H
47 #include <unistd.h>
48 #endif
50 #include <errno.h>
51 #include <stdlib.h>
52 #include <stdio.h>
53 #include <stdarg.h>
54 #include <string.h>
57 #include "impl.h"
61 /* Group query & manipulation functions */
63 bool tMPI_In_group(tMPI_Group group)
65 int i;
66 struct tmpi_thread *cur;
68 cur=tMPI_Get_current();
69 for(i=0;i<group->N;i++)
71 if (group->peers[i] == cur)
72 return TRUE;
74 return FALSE;
77 int tMPI_Group_size(tMPI_Group group, int *size)
79 #ifdef TMPI_TRACE
80 tMPI_Trace_print("tMPI_Group_size(%p, %p)", group, size);
81 #endif
83 if (group)
84 *size = group->N;
85 else
86 *size = 0;
87 return TMPI_SUCCESS;
90 int tMPI_Group_rank(tMPI_Group group, int *rank)
92 int i;
93 struct tmpi_thread *cur;
95 #ifdef TMPI_TRACE
96 tMPI_Trace_print("tMPI_Group_rank(%p, %p)", group, rank);
97 #endif
98 if (!group)
99 return TMPI_UNDEFINED;
101 /* search for my id in the list of peers */
102 cur=tMPI_Get_current();
103 for(i=0;i<group->N;i++)
105 if (group->peers[i] == cur)
107 *rank=i;
108 return TMPI_SUCCESS;
111 return TMPI_UNDEFINED;
116 tMPI_Group tMPI_Group_alloc(void)
118 struct tmpi_group_ *ret;
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
128 return ret;
131 int tMPI_Group_free(tMPI_Group *group)
133 #ifdef TMPI_TRACE
134 tMPI_Trace_print("tMPI_Group_free(%p)", group);
135 #endif
136 if (group)
138 free((*group)->peers);
139 free(*group);
141 return TMPI_SUCCESS;
144 int tMPI_Comm_group(tMPI_Comm comm, tMPI_Group *group)
146 int i;
147 struct tmpi_group_ *ret=tMPI_Group_alloc();
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++)
155 ret->peers[i]=comm->grp.peers[i];
157 *group=ret;
158 #if 0
159 if (comm)
161 *group=&(comm->grp);
163 else
165 *group=NULL;
167 #endif
169 return TMPI_SUCCESS;
173 int tMPI_Group_incl(tMPI_Group group, int n, int *ranks, tMPI_Group *newgroup)
175 int i;
176 tMPI_Group ng;
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++)
187 if (ranks[i] < 0 || !group || ranks[i] >= group->N)
189 return tMPI_Error(TMPI_COMM_WORLD, TMPI_ERR_GROUP_RANK);
191 ng->peers[i]=group->peers[ranks[i]];
193 *newgroup=ng;
194 return TMPI_SUCCESS;