File: tsubarray.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 (263 lines) | stat: -rw-r--r-- 6,052 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
/*
 * Copyright (c) 2001-2002 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: tsubarray.c,v 1.5 2002/12/11 19:15:14 jsquyres Exp $
 *
 *	Function:	- create local array derived datatype
 *	Accepts:	- number of array dimensions
 *			- array of sizes
 *			- array of sub sizes
 *			- array of starting coordinates
 *			- storage order
 *			- element datatype
 *			- new datatype (out)
 *	Returns:	- MPI_SUCCESS or error code
 *
 *	Notes:		- this code is derived from code in the ROMIO
 *			  distibution authored by Rajeev Thakur.
 */

#include <errno.h>
#include <stdlib.h>

#include <blktype.h>
#include <mpi.h>
#include <mpisys.h>

/*
 * Local function
 */
static void freetype(MPI_Datatype *type);

/*@
   MPI_Type_create_subarray - Create local array derived datatype

Input Parameters:
+ ndims - Number of array dimensions
. sizes - Number of elements of type oldtype in each dimension of the full array
. subsizes - Number of elements of type oldtype in each dimension of the subarray
. starts - Starting coordinates of the subarray in each dimension
. order - array storage order flag 
- oldtype - array element datatype

Output Parameter:
+ newtype - new datatype

.N fortran

.N Errors
.N MPI_SUCCESS
.N MPI_ERR_ARG
.N MPI_ERR_TYPE
.N MPI_ERR_OTHER

.N WEB
@*/

int
MPI_Type_create_subarray(int ndims, int *sizes, int *subsizes,
			int *starts, int order, 
			 MPI_Datatype oldtype, MPI_Datatype *newtype)
{
    MPI_Datatype	types[1];
    MPI_Aint		disps[1];
    int			blklens[1];
    MPI_Aint		ub;
    MPI_Datatype	ntype;
    MPI_Datatype	tmptype;
    MPI_Aint		extent;
    int			size;
    int			err;
    int			i;

    lam_initerr_m();
    lam_setfunc_m(BLKMPITCREATESUBARRAY);
/*
 * Check the arguments.
 */
    if (ndims <= 0 || sizes == 0
		|| subsizes == 0 || starts == 0 || newtype == 0) {
	return(lam_errfunc(MPI_COMM_WORLD,
			   BLKMPITCREATESUBARRAY, 
			   lam_mkerr(MPI_ERR_ARG, EINVAL)));
    }

    if (oldtype == MPI_DATATYPE_NULL) {
	return(lam_errfunc(MPI_COMM_WORLD,
			   BLKMPITCREATESUBARRAY, 
			   lam_mkerr(MPI_ERR_TYPE, EINVAL)));
    }

    for (i = 0; i < ndims; i++) {
	if (sizes[i] <= 0 || subsizes[i] <= 0 || starts[i] < 0) {
	    return(lam_errfunc(MPI_COMM_WORLD,
			       BLKMPITCREATESUBARRAY, 
			       lam_mkerr(MPI_ERR_ARG, EINVAL)));
	}
    }

    MPI_Type_extent(oldtype, &extent);

    if (order == MPI_ORDER_FORTRAN) {
/*
 * Dimension 0 changes fastest.
 */
	if (ndims == 1) {
	    err = MPI_Type_contiguous(subsizes[0], oldtype, &ntype);
	    if (err != MPI_SUCCESS) {
		return(err);
	    }
	} else {
	    err = MPI_Type_vector(subsizes[1], subsizes[0],
			    		sizes[0], oldtype, &ntype);
	    if (err != MPI_SUCCESS) {
		return(err);
	    }

	    size = sizes[0] * extent;

	    for (i = 2; i < ndims; i++) {
		size *= sizes[i-1];
		
		err = MPI_Type_create_hvector(subsizes[i], 1, size,
		    		ntype, &tmptype);
		if (err != MPI_SUCCESS) {
		    return(err);
		}

		freetype(&ntype);
		ntype = tmptype;
	    }
	}
/*
 * Add displacement and UB.
 */
	disps[0] = starts[0];
	size = 1;
	for (i = 1; i < ndims; i++) {
	    size *= sizes[i-1];
	    disps[0] += size * starts[i];
	}
    }
    else if (order == MPI_ORDER_C) {
/*
 * Dimension ndims-1 changes fastest.
 */
	if (ndims == 1) {
	    err = MPI_Type_contiguous(subsizes[0], oldtype, &ntype);
	    if (err != MPI_SUCCESS) {
		return(err);
	    }
	} else {
	    err = MPI_Type_vector(subsizes[ndims-2], subsizes[ndims-1],
			    		sizes[ndims-1], oldtype, &ntype);
	    if (err != MPI_SUCCESS) {
		return(err);
	    }

	    size = sizes[ndims-1] * extent;

	    for (i = ndims - 3; i >= 0; i--) {
		size *= sizes[i+1];

		err = MPI_Type_create_hvector(subsizes[i], 1, size,
		    		ntype, &tmptype);
		if (err != MPI_SUCCESS) {
		    return(err);
		}

		freetype(&ntype);
		ntype = tmptype;
	    }
	}
/*
 * Add displacement and UB.
 */
	disps[0] = starts[ndims-1];
	size = 1;
	for (i = ndims - 2; i >= 0; i--) {
	    size *= sizes[i+1];
	    disps[0] += size * starts[i];
	}
    }
    else {
	return(lam_errfunc(MPI_COMM_WORLD,
			   BLKMPITCREATESUBARRAY, 
			   lam_mkerr(MPI_ERR_ARG, EINVAL)));
    }

    for (ub = extent, i = 0; i < ndims; i++) {
	ub *= sizes[i];
    }

    disps[0] *= extent;
    blklens[0] = 1;
    types[0] = ntype;

    err = MPI_Type_create_struct(1, blklens, disps, types, &ntype);
    if (err != MPI_SUCCESS) {
	return(err);
    }

    freetype(&types[0]);

    ntype->dt_flags &= ~(LAM_DTHASLB | LAM_DTHASUB);
    ntype->dt_lower = 0;
    ntype->dt_upper = ub;
/*
 * Set the no extent adjustment flag if the upper and lower bounds match
 * exactly the upper and lower limits of the data.
 */
    if (ntype->dt_upper == ntype->dt_dataup
		&& ntype->dt_lower == ntype->dt_datalow) {
	ntype->dt_flags |= LAM_DTNOXADJ;
    } else {
	ntype->dt_flags &= ~LAM_DTNOXADJ;
    }

    ntype->dt_format = LAM_DTSUBARRAY;
/*
 * Record user arguments.
 */
    ntype->dt_dtype = oldtype;
    oldtype->dt_refcount++;

    ntype->dt_uargs = (int *) malloc((3*ndims + 2) * sizeof(int));
    if (ntype->dt_uargs == 0) {
	return(lam_errfunc(MPI_COMM_WORLD, BLKMPITCREATESUBARRAY,
	    			lam_mkerr(MPI_ERR_OTHER, errno)));
    }

    ntype->dt_uargs[0] = ndims;
    ntype->dt_uargs[3*ndims + 1] = order;

    for (i = 0; i < ndims; i++) {
	ntype->dt_uargs[i + 1] = sizes[i];
	ntype->dt_uargs[ndims + i + 1] = subsizes[i];
	ntype->dt_uargs[2*ndims + i + 1] = starts[i];
    }

    *newtype = ntype;

    lam_resetfunc_m(BLKMPITCREATESUBARRAY);
    return(MPI_SUCCESS);
}

static void
freetype(MPI_Datatype *type)
{
    if (!((*type)->dt_flags & LAM_PREDEF)) {
	MPI_Type_free(type);
    }
}