File: csplit.c

package info (click to toggle)
lam 7.1.4-8
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 56,404 kB
  • sloc: ansic: 156,541; sh: 9,991; cpp: 7,699; makefile: 5,621; perl: 488; fortran: 260; asm: 83
file content (280 lines) | stat: -rw-r--r-- 6,706 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
 * Copyright (c) 2001-2003 The Trustees of Indiana University.  
 *                         All rights reserved.
 * Copyright (c) 1998-2001 University of Notre Dame. 
 *                         All rights reserved.
 * Copyright (c) 1994-1998 The Ohio State University.  
 *                         All rights reserved.
 * 
 * This file is part of the LAM/MPI software package.  For license
 * information, see the LICENSE file in the top level directory of the
 * LAM/MPI source distribution.
 * 
 * $HEADER$
 *
 * $Id: csplit.c,v 6.11 2003/08/20 21:49:32 jsquyres Exp $
 *
 *	Function:	- split communicator into disjoint subsets
 *	Accepts:	- old communicator
 *			- process colour
 *			- process key
 *			- new communicator (out)
 *	Returns:	- MPI_SUCCESS or error code
 */

#include <stdlib.h>

#include <lam_config.h>
#include <blktype.h>
#include <mpi.h>
#include <mpisys.h>
#include <terror.h>
#include <lam-ssi-coll.h>


/*
 * local functions
 */
static int cmpprocs(const void *a, const void *b);


/*@

MPI_Comm_split - Creates new communicators based on colors and keys

Input Parameters:
+ comm - communicator (handle) 
. colour - control of subset assignment (nonnegative integer) 
- key - control of rank assigment (integer) 

Output Parameter:
. newcomm - new communicator (handle) 

Notes:

The 'colour' must be non-negative or 'MPI_UNDEFINED'.  This is a
collective call over all ranks in 'comm'.

When a communicator is no longer being used, it should be freed with
'MPI_Comm_free'.

.N fortran

.N Errors
.N MPI_SUCCESS
.N MPI_ERR_COMM
.N MPI_ERR_ARG
.N MPI_ERR_INTERN
.N MPI_ERR_EXHAUSTED

.seealso: MPI_Comm_free

.N ACK
@*/
int MPI_Comm_split(MPI_Comm comm, int colour, int key, 
		   MPI_Comm *newcomm)
{
	MPI_Group	oldgroup;		/* old group */
	MPI_Group	newgroup;		/* new group */
	int		cid;			/* global context ID */
	int		i;			/* favourite index */
	int		err;			/* error code */
	int		myrank;			/* my group rank */
	int		nprocs;			/* # processes */
	int		mynprocs;		/* # procs in new comm */
	int		*procs;			/* procs array */
	int		*myprocs;		/* new comm procs array */
	int		*p, *p2;		/* favourite pointers */

	lam_initerr();
	lam_setfunc(BLKMPICOMMSPLIT);
/*
 * Check the arguments.
 */
	if (comm == MPI_COMM_NULL) {
		return(lam_errfunc(MPI_COMM_WORLD, BLKMPICOMMSPLIT,
				lam_mkerr(MPI_ERR_COMM, EINVAL)));
	}

	if (LAM_IS_INTER(comm)) {
		return(lam_errfunc(comm, BLKMPICOMMSPLIT,
				lam_mkerr(MPI_ERR_COMM, EINVAL)));
	}

	if (newcomm == 0) {
		return(lam_errfunc(comm, BLKMPICOMMSPLIT,
				lam_mkerr(MPI_ERR_ARG, EINVAL)));
	}

#if LAM_WANT_IMPI

	/* Remove this when IMPI collectives are implemented */

        if (LAM_IS_IMPI(comm)) {
	  return lam_err_comm(comm, MPI_ERR_COMM, EINVAL, 
			      "Collectives not yet implemented on IMPI communicators");
	}
#endif

/*
 * Set up tracing.
 */
	LAM_TRACE(lam_tr_cffstart(BLKMPICOMMSPLIT));
	MPI_Comm_rank(comm, &myrank);

	/* Collectively decide on a new CID */

	err = lam_coll_alloc_intra_cid(comm, (colour != MPI_UNDEFINED), 
				       BLKMPICOMMSPLIT, &cid);
	if (err != MPI_SUCCESS) {
		LAM_TRACE(lam_tr_cffend(BLKMPICOMMSPLIT, -1, comm, 0, 0));
		lam_resetfunc(BLKMPICOMMSPLIT);
		return(lam_errfunc(comm, BLKMPICOMMSPLIT, err));
	}
/*
 * Create the array of process information.
 */
	MPI_Comm_size(comm, &nprocs);

	procs = (int *) malloc((unsigned) 3 * (nprocs + 1) * sizeof(int));
	if (procs == 0) {
		return(lam_errfunc(comm, BLKMPICOMMSPLIT,
				lam_mkerr(MPI_ERR_OTHER, errno)));
	}
/*
 * Gather all process information at all processes.
 */
	p = &procs[3 * nprocs];
	p[0] = colour;
	p[1] = key;
	p[2] = myrank;

	err = MPI_Allgather(p, 3, MPI_INT, procs, 3, MPI_INT, comm);
	if (err != MPI_SUCCESS) {
		free((char *) procs);
		LAM_TRACE(lam_tr_cffend(BLKMPICOMMSPLIT, -1, comm, 0, 0));
		lam_resetfunc(BLKMPICOMMSPLIT);
		return(lam_errfunc(comm, BLKMPICOMMSPLIT, err));
	}
/*
 * Processes with undefined colour can stop here.
 */
	if (colour == MPI_UNDEFINED) {
		*newcomm = MPI_COMM_NULL;
		free((char *) procs);
		LAM_TRACE(lam_tr_cffend(BLKMPICOMMSPLIT, -1, comm, 0, 0));
		lam_resetfunc(BLKMPICOMMSPLIT);
		return(MPI_SUCCESS);
	}
/*
 * Sort the process information.
 * Locate and count the # of processes having my colour.
 */
	qsort((char *) procs, nprocs, 3 * sizeof(int), cmpprocs);

	myprocs = 0;

	for (i = 0, p = procs; (i < nprocs) && (*p != colour); ++i, p += 3);

	myprocs = p;
	mynprocs = 1;

	for (++i, p += 3; (i < nprocs) && (*p == colour); ++i, p += 3) {
		++mynprocs;
	}
/*
 * Compact the old ranks of my old group in the array.
 */
	p = myprocs;
	p2 = myprocs + 2;

	for (i = 0; i < mynprocs; ++i, ++p, p2 += 3) {
		*p = *p2;
	}
/*
 * Create the new group.
 */
	MPI_Comm_group(comm, &oldgroup);

	err = MPI_Group_incl(oldgroup, mynprocs, myprocs, &newgroup);
	if (err != MPI_SUCCESS) {
		return(lam_errfunc(comm, BLKMPICOMMSPLIT, err));
	}

	free((char *) procs);
	MPI_Group_free(&oldgroup);
/*
 * Create the new communicator.
 * MPI_Comm_create() is not called because we don't want to communicate
 * globally again, we have the new contextid (and the undefined-colour
 * procs have returned), so we win speed and safety.
 */
	*newcomm = 0;
	if (lam_comm_new(cid, newgroup, MPI_GROUP_NULL, 0, newcomm)) {
		return(lam_errfunc(comm, BLKMPICOMMSPLIT,
				lam_mkerr(MPI_ERR_OTHER, errno)));
	}

	(*newcomm)->c_errhdl = comm->c_errhdl;
	comm->c_errhdl->eh_refcount++;

	if (!al_insert(lam_comms, newcomm)) {
		return(lam_errfunc(comm, BLKMPICOMMSPLIT,
				lam_mkerr(MPI_ERR_INTERN, errno)));
	}
	
	if (lam_tr_comm(*newcomm)) {
		return(lam_errfunc(comm, BLKMPICOMMSPLIT,
				lam_mkerr(MPI_ERR_INTERN, errno)));
	}

	lam_setcid(cid);

	/* Let the collective SSI modules battle over who will do
	   collectives on this new communicator.  It'll likely be the
	   same as the one that's on this communicator, but we still
	   give them the option to do something different anyway... */

	if (lam_ssi_coll_base_init_comm(*newcomm) != 0)
	  return(lam_errfunc(comm, BLKMPICOMMSPLIT,
			     lam_mkerr(MPI_ERR_INTERN, ENOSYS)));

	LAM_TRACE(lam_tr_cffend(BLKMPICOMMSPLIT, -1, comm, 0, 0));

	lam_resetfunc(BLKMPICOMMSPLIT);
	return(MPI_SUCCESS);
}

/*
 *	cmpprocs
 *
 *	Function:	- compare two process entries
 *	Accepts:	- ptr 1st entry
 *			- ptr 2nd entry
 *	Returns:	- -1, 0, +1 (less, equal, greater)
 */
static int
cmpprocs(const void* a, const void *b)
{
        int cmp;
	int *e1 = (int *) a;
	int *e2 = (int *) b;

	if (*e1 < *e2) {
		cmp = -1;
	} else if (*e1 > *e2) {
		cmp = 1;
	} else if (*(++e1) < *(++e2)) {
		cmp = -1;
	} else if (*e1 > *e2) {
		cmp = 1;
	} else if (*(++e1) < *(++e2)) {
		cmp = -1;
	} else if (*e1 > *e2) {
		cmp = 1;
	} else {
		cmp = 0;
	}

	return(cmp);
}