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 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
|
/*
* 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: tdarray.c,v 1.6 2003/11/02 21:13:10 brbarret Exp $
*
* Function: - create distributed array derived datatype
* Accepts: - size of process group
* - rank in process group
* - number of array dimensions
* - array of global sizes
* - array of distributions
* - array of distribution arguments
* - size of process grid in each dimension
* - 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 <stdlib.h>
#include <blktype.h>
#include <mpi.h>
#include <mpisys.h>
#include <terror.h>
#include <typical.h>
static int createblock(int *gsizes, int dim, int ndims, int nprocs,
int rank, int darg, int order, MPI_Aint extent,
MPI_Datatype oldtype, MPI_Datatype *newtype,
MPI_Aint *offset);
static int createcyclic(int *gsizes, int dim, int ndims, int nprocs,
int rank, int darg, int order, MPI_Aint extent,
MPI_Datatype oldtype, MPI_Datatype *newtype,
MPI_Aint *offset);
/*@
MPI_Type_create_darray - create distributed array derived datatype
Input Parameters:
+ size - Size of process group (positive integer)
. rank - rank in process group (nonnegative integer)
. ndmis - Number of array dimensions (positive integer)
. gsizes - Array of global sizes (array of positive integers)
. distribs - Array of distributions (array of state)
. dargs - Array of distibution arguments (array of positive integers)
. psizes - Size of process grid in each dimension (array of positive integers)
. order - Storage order (state)
- oldtype - Old datatype (handle)
Output Parameter:
+ newtype - New datatype (handle)
.N fortran
.N Errors
.N MPI_SUCCESS
.N MPI_ERR_TYPE
.N MPI_ERR_ARG
.N MPI_ERR_OTHER
.N WEB
@*/
int
MPI_Type_create_darray(int size, int rank, int ndims, int *gsizes,
int *distribs, int *dargs, int *psizes,
int order, MPI_Datatype oldtype,
MPI_Datatype *newtype)
{
MPI_Datatype types[1];
MPI_Datatype ntype; /* new datatype */
MPI_Datatype intype; /* datatype passed in */
int blklens[1];
MPI_Aint disps[1];
MPI_Aint *offsets;
MPI_Aint extent;
MPI_Aint ub;
int *coords;
int err = MPI_SUCCESS;
int pi;
int n, r, s;
int i;
lam_initerr_m();
lam_setfunc_m(BLKMPITCREATEDARRAY);
/*
* Check the arguments.
*/
if (size <= 0 || rank < 0 || ndims <= 0 || newtype == 0) {
return(lam_errfunc(MPI_COMM_WORLD,
BLKMPITCREATEDARRAY, lam_mkerr(MPI_ERR_ARG, EINVAL)));
}
if (oldtype == MPI_DATATYPE_NULL) {
return(lam_errfunc(MPI_COMM_WORLD,
BLKMPITCREATEDARRAY, lam_mkerr(MPI_ERR_TYPE, EINVAL)));
}
for (i = 0, pi = 1; i < ndims; ++i) {
if (gsizes[i] <= 0 || psizes[i] <= 0
|| distribs[i] < MPI_DISTRIBUTE_BLOCK
|| distribs[i] > MPI_DISTRIBUTE_NONE
|| (dargs[i] <= 0 &&
(dargs[i] != MPI_DISTRIBUTE_DFLT_DARG
&& distribs[i] != MPI_DISTRIBUTE_NONE))) {
return(lam_errfunc(MPI_COMM_WORLD,
BLKMPITCREATEDARRAY, lam_mkerr(MPI_ERR_ARG, EINVAL)));
}
if (distribs[i] == MPI_DISTRIBUTE_BLOCK
&& dargs[i] != MPI_DISTRIBUTE_DFLT_DARG) {
if (dargs[i] * psizes[i] < gsizes[i]) {
return(lam_errfunc(MPI_COMM_WORLD, BLKMPITCREATEDARRAY,
lam_mkerr(MPI_ERR_ARG, EINVAL)));
}
} else if (distribs[i] == MPI_DISTRIBUTE_NONE) {
if (psizes[i] != 1) {
return(lam_errfunc(MPI_COMM_WORLD, BLKMPITCREATEDARRAY,
lam_mkerr(MPI_ERR_ARG, EINVAL)));
}
}
pi *= psizes[i];
}
if (pi != size) {
return(lam_errfunc(MPI_COMM_WORLD, BLKMPITCREATEDARRAY,
lam_mkerr(MPI_ERR_ARG, EINVAL)));
}
/*
* Calculate position in Cartesian grid as MPI would (row-major
* ordering)
*/
coords = (int *) malloc(ndims * sizeof(int));
offsets = (MPI_Aint *) malloc(ndims * sizeof(MPI_Aint));
if (coords == 0 || offsets == 0) {
return(lam_errfunc(MPI_COMM_WORLD,
BLKMPITCREATEDARRAY,
lam_mkerr(MPI_ERR_OTHER, EINVAL)));
}
for (s = size, r = rank, i = 0; i < ndims; i++) {
s /= psizes[i];
coords[i] = r / s;
r = r % s;
}
MPI_Type_extent(oldtype, &extent);
intype = oldtype;
if (order == MPI_ORDER_FORTRAN) {
/*
* Dimension 0 changes fastest.
*/
for (i = 0; i < ndims; i++) {
switch (distribs[i]) {
case MPI_DISTRIBUTE_BLOCK:
err = createblock(gsizes, i, ndims, psizes[i], coords[i],
dargs[i], order, extent, oldtype,
&ntype, offsets + i);
break;
case MPI_DISTRIBUTE_CYCLIC:
err = createcyclic(gsizes, i, ndims, psizes[i], coords[i],
dargs[i], order, extent, oldtype,
&ntype, offsets + i);
break;
case MPI_DISTRIBUTE_NONE:
/*
* Treat it as a block distribution on 1 process.
*/
err = createblock(gsizes, i, ndims, 1, 0,
MPI_DISTRIBUTE_DFLT_DARG,
order, extent, oldtype,
&ntype, offsets + i);
break;
}
if (err != MPI_SUCCESS) {
return(err);
}
if (i > 0 && !(oldtype->dt_flags & LAM_PREDEF)) {
MPI_Type_free(&oldtype);
}
oldtype = ntype;
}
/*
* Add displacement and UB.
*/
disps[0] = offsets[0];
for (n = 1, i = 1; i < ndims; i++) {
n *= gsizes[i-1];
disps[0] += n * offsets[i];
}
}
else if (order == MPI_ORDER_C) {
/*
* Dimension ndims-1 changes fastest.
*/
for (i = ndims - 1; i >= 0; i--) {
switch(distribs[i]) {
case MPI_DISTRIBUTE_BLOCK:
err = createblock(gsizes, i, ndims, psizes[i], coords[i],
dargs[i], order, extent, oldtype,
&ntype, offsets + i);
break;
case MPI_DISTRIBUTE_CYCLIC:
err = createcyclic(gsizes, i, ndims, psizes[i], coords[i],
dargs[i], order, extent, oldtype,
&ntype, offsets + i);
break;
case MPI_DISTRIBUTE_NONE:
/*
* Treat it as a block distribution on 1 process.
*/
err = createblock(gsizes, i, ndims, psizes[i], coords[i],
MPI_DISTRIBUTE_DFLT_DARG, order, extent,
oldtype, &ntype, offsets + i);
break;
}
if (err != MPI_SUCCESS) {
return(err);
}
if (i > 0 && !(oldtype->dt_flags & LAM_PREDEF)) {
MPI_Type_free(&oldtype);
}
oldtype = ntype;
}
/*
* Add displacement and UB.
*/
disps[0] = offsets[ndims-1];
for (n = 1, i = ndims - 2; i >= 0; i--) {
n *= gsizes[i+1];
disps[0] += n * offsets[i];
}
}
else {
/*
* Invalid order.
*/
return(lam_errfunc(MPI_COMM_WORLD,
BLKMPITCREATEDARRAY, lam_mkerr(MPI_ERR_ARG, EINVAL)));
}
for (ub = extent, i = 0; i < ndims; i++) {
ub *= gsizes[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);
}
MPI_Type_free(&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_DTDARRAY;
free(offsets);
free(coords);
/*
* Record user arguments.
*/
ntype->dt_dtype = intype;
intype->dt_refcount++;
ntype->dt_uargs = (int *) malloc((4 * ndims + 4) * sizeof(int));
if (ntype->dt_uargs == 0) {
return(lam_errfunc(MPI_COMM_WORLD, BLKMPITCREATEDARRAY,
lam_mkerr(MPI_ERR_OTHER, errno)));
}
ntype->dt_uargs[0] = size;
ntype->dt_uargs[1] = rank;
ntype->dt_uargs[2] = ndims;
ntype->dt_uargs[4*ndims + 3] = order;
for (i = 0; i < ndims; i++) {
ntype->dt_uargs[i + 3] = gsizes[i];
ntype->dt_uargs[ndims + i + 3] = distribs[i];
ntype->dt_uargs[2*ndims + i + 3] = dargs[i];
ntype->dt_uargs[3*ndims + i + 3] = psizes[i];
}
*newtype = ntype;
lam_resetfunc_m(BLKMPITCREATEDARRAY);
return(MPI_SUCCESS);
}
static int
createblock(int *gsizes, int dim, int ndims, int nprocs, int rank, int darg,
int order, MPI_Aint extent, MPI_Datatype oldtype, MPI_Datatype *newtype,
MPI_Aint *offset)
{
int blksize;
int size;
int err;
int i;
if (darg == MPI_DISTRIBUTE_DFLT_DARG) {
blksize = (gsizes[dim] + nprocs - 1) / nprocs;
} else {
blksize = darg;
}
size = gsizes[dim] - blksize * rank;
if (size > blksize) {
size = blksize;
}
if (size < 0) {
size = 0;
}
if (order == MPI_ORDER_FORTRAN) {
if (dim == 0)
err = MPI_Type_contiguous(size, oldtype, newtype);
else {
for (i = 0; i < dim; i++) {
extent *= gsizes[i];
}
err = MPI_Type_create_hvector(size, 1, extent, oldtype, newtype);
}
}
else {
if (dim == ndims - 1)
err = MPI_Type_contiguous(size, oldtype, newtype);
else {
for (i = ndims - 1; i > dim; i--) {
extent *= gsizes[i];
}
err = MPI_Type_create_hvector(size, 1, extent, oldtype, newtype);
}
}
/*
* In terms of number of elements of type oldtype in this dimension.
*/
if (size == 0) {
*offset = 0;
} else {
*offset = blksize * rank;
}
return(err);
}
static int
createcyclic(int *gsizes, int dim, int ndims, int nprocs, int rank, int darg,
int order, MPI_Aint extent, MPI_Datatype oldtype, MPI_Datatype *newtype,
MPI_Aint *offset)
{
int blksize;
int start;
int end;
int size;
int count;
int rem;
int err = MPI_SUCCESS;
int i;
MPI_Aint extent_save = extent;
MPI_Datatype temp;
if (darg == MPI_DISTRIBUTE_DFLT_DARG) {
blksize = 1;
} else {
blksize = darg;
}
/*
* Calculate the number of blocks of the block-cyclic distribution that
* belong to this process.
*/
start = rank * blksize;
end = gsizes[dim] - 1;
if (end < start) {
size = 0;
} else {
rem = (end - start + 1) % (nprocs * blksize);
size = ((end - start + 1) / (nprocs * blksize)) * blksize;
size += (rem < blksize ? rem : blksize);
}
count = size / blksize;
rem = size % blksize;
extent *= nprocs * blksize;
if (order == MPI_ORDER_FORTRAN) {
for (i = 0; i < dim; i++) {
extent *= gsizes[i];
}
} else {
for (i = ndims - 1; i > dim; i--) {
extent *= gsizes[i];
}
}
err = MPI_Type_create_hvector(count, blksize, extent, oldtype, newtype);
if (err != MPI_SUCCESS) {
return(err);
}
if (rem) {
MPI_Datatype types[2];
MPI_Aint disps[2];
int blklens[2];
/*
* If the last block is of size less than blksize, include it separately
* using MPI_Type_struct.
*/
types[0] = *newtype;
types[1] = oldtype;
disps[0] = 0;
disps[1] = count * extent;
blklens[0] = 1;
blklens[1] = rem;
err = MPI_Type_struct(2, blklens, disps, types, newtype);
if (err != MPI_SUCCESS) {
return(err);
}
MPI_Type_free(&types[0]);
}
/* Need to add an upper bound and lower bound */
extent = extent_save * gsizes[dim];
temp = *newtype;
MPI_Type_create_resized(temp, 0, extent, newtype);
MPI_Type_free(&temp);
/*
* In terms of number of elements of type oldtype in this dimension.
*/
if (size == 0) {
*offset = 0;
} else {
*offset = rank * blksize;
}
return(err);
}
|