File: scatter3.c

package info (click to toggle)
mpich 4.3.0%2Breally4.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 419,120 kB
  • sloc: ansic: 1,215,557; cpp: 74,755; javascript: 40,763; f90: 20,649; sh: 18,463; xml: 14,418; python: 14,397; perl: 13,772; makefile: 9,279; fortran: 8,063; java: 4,553; asm: 324; ruby: 176; lisp: 19; php: 8; sed: 4
file content (84 lines) | stat: -rw-r--r-- 2,838 bytes parent folder | download | duplicates (4)
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
/*
 * Copyright (C) by Argonne National Laboratory
 *     See COPYRIGHT in top-level directory
 */

#include "mpi.h"
#include "mpitest.h"
#include <stdlib.h>
#include <stdio.h>

/* This example sends contiguous data and receives a vector on some nodes
   and contiguous data on others.  There is some evidence that some
   MPI implementations do not check recvcount on the root process; this
   test checks for that case
*/

int main(int argc, char **argv)
{
    MPI_Datatype vec;
    double *vecin, *vecout, ivalue;
    int root, i, n, stride, errs = 0;
    int rank, size;
    MPI_Aint vextent, tmp_lb;

    MTest_Init(&argc, &argv);

    MPI_Comm_size(MPI_COMM_WORLD, &size);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);

    n = 12;
    stride = 10;
    /* Note that vecout really needs to be only (n-1)*stride+1 doubles, but
     * this is easier and allows a little extra room if there is a bug */
    vecout = (double *) malloc(n * stride * sizeof(double));
    vecin = (double *) malloc(n * size * sizeof(double));

    MPI_Type_vector(n, 1, stride, MPI_DOUBLE, &vec);
    MPI_Type_commit(&vec);
    MPI_Type_get_extent(vec, &tmp_lb, &vextent);
    if (vextent != ((n - 1) * (MPI_Aint) stride + 1) * sizeof(double)) {
        errs++;
        printf("Vector extent is %ld, should be %ld\n",
               (long) vextent, (long) (((n - 1) * stride + 1) * sizeof(double)));
    }
    /* Note that the exted of type vector is from the first to the
     * last element, not n*stride.
     * E.g., with n=1, the extent is a single double */

    for (i = 0; i < n * size; i++)
        vecin[i] = (double) i;
    for (root = 0; root < size; root++) {
        for (i = 0; i < n * stride; i++)
            vecout[i] = -1.0;
        if (rank == root) {
            /* Receive into a vector */
            MPI_Scatter(vecin, n, MPI_DOUBLE, vecout, 1, vec, root, MPI_COMM_WORLD);
            for (i = 0; i < n; i++) {
                ivalue = n * root + i;
                if (vecout[i * stride] != ivalue) {
                    errs++;
                    printf("[%d] Expected %f but found %f for vecout[%d] on root\n",
                           rank, ivalue, vecout[i * stride], i * stride);
                }
            }
        } else {
            /* Receive into contiguous data */
            MPI_Scatter(NULL, -1, MPI_DATATYPE_NULL, vecout, n, MPI_DOUBLE, root, MPI_COMM_WORLD);
            for (i = 0; i < n; i++) {
                ivalue = rank * n + i;
                if (vecout[i] != ivalue) {
                    printf("[%d] Expected %f but found %f for vecout[%d]\n",
                           rank, ivalue, vecout[i], i);
                    errs++;
                }
            }
        }
    }

    free(vecin);
    free(vecout);
    MPI_Type_free(&vec);
    MTest_Finalize(errs);
    return MTestReturnValue(errs);
}