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
|
/*
* Copyright (C) by Argonne National Laboratory
* See COPYRIGHT in top-level directory
*/
#include "mpi.h"
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include "mpitest.h"
#include "mpitestconf.h"
#ifdef HAVE_STRING_H
#include <string.h>
#endif
static int verbose = 0;
int a[100][100][100], e[9][9][9];
int main(int argc, char *argv[]);
/* helper functions */
static int parse_args(int argc, char **argv);
int main(int argc, char *argv[])
{
/* Variable declarations */
MPI_Datatype oneslice, twoslice, threeslice;
int errs = 0;
MPI_Aint sizeofint, tmp_lb;
int bufsize, position;
void *buffer;
int i, j, k;
/* Initialize a to some known values. */
for (i = 0; i < 100; i++) {
for (j = 0; j < 100; j++) {
for (k = 0; k < 100; k++) {
a[i][j][k] = i * 1000000 + j * 1000 + k;
}
}
}
/* Initialize MPI */
MTest_Init(&argc, &argv);
MPI_Type_get_extent(MPI_INT, &tmp_lb, &sizeofint);
parse_args(argc, argv);
/* Create data types. */
/* NOTE: This differs from the way that it's done on the sheet. */
/* On the sheet, the slice is a[0, 2, 4, ..., 16][2-10][1-9]. */
/* Below, the slice is a[0-8][2-10][1, 3, 5, ..., 17]. */
MPI_Type_vector(9, 1, 2, MPI_INT, &oneslice);
MPI_Type_create_hvector(9, 1, 100 * sizeofint, oneslice, &twoslice);
MPI_Type_create_hvector(9, 1, 100 * 100 * sizeofint, twoslice, &threeslice);
MPI_Type_commit(&threeslice);
/* Pack it into a buffer. */
position = 0;
MPI_Pack_size(1, threeslice, MPI_COMM_WORLD, &bufsize);
buffer = (void *) malloc((unsigned) bufsize);
/* -1 to indices on sheet to compensate for Fortran --> C */
MPI_Pack(&(a[0][2][1]), 1, threeslice, buffer, bufsize, &position, MPI_COMM_WORLD);
/* Unpack the buffer into e. */
position = 0;
MPI_Unpack(buffer, bufsize, &position, e, 9 * 9 * 9, MPI_INT, MPI_COMM_WORLD);
/* Display errors, if any. */
for (i = 0; i < 9; i++) {
for (j = 0; j < 9; j++) {
for (k = 0; k < 9; k++) {
/* The truncation in integer division makes this safe. */
if (e[i][j][k] != a[i][j + 2][k * 2 + 1]) {
errs++;
if (verbose) {
printf("Error in location %d x %d x %d: %d, should be %d.\n",
i, j, k, e[i][j][k], a[i][j + 2][k * 2 + 1]);
}
}
}
}
}
/* Release memory. */
free(buffer);
MPI_Type_free(&oneslice);
MPI_Type_free(&twoslice);
MPI_Type_free(&threeslice);
MTest_Finalize(errs);
return MTestReturnValue(errs);
}
/* parse_args()
*/
static int parse_args(int argc, char **argv)
{
/*
* int ret;
*
* while ((ret = getopt(argc, argv, "v")) >= 0)
* {
* switch (ret) {
* case 'v':
* verbose = 1;
* break;
* }
* }
*/
if (argc > 1 && strcmp(argv[1], "-v") == 0)
verbose = 1;
return 0;
}
|