1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
3 * (C) 2001 by Argonne National Laboratory.
4 * See COPYRIGHT in top-level directory.
10 /* This example sends a vector and receives individual elements */
12 int main( int argc, char **argv )
15 double *vecin, *vecout, ivalue;
16 int root, i, n, stride, err = 0;
19 MPI_Init( &argc, &argv );
21 MPI_Comm_size( MPI_COMM_WORLD, &size );
22 MPI_Comm_rank( MPI_COMM_WORLD, &rank );
26 vecin = (double *)malloc( n * stride * size * sizeof(double) );
27 vecout = (double *)malloc( n * sizeof(double) );
29 MPI_Type_vector( n, 1, stride, MPI_DOUBLE, &vec );
30 MPI_Type_commit( &vec );
32 for (i=0; i<n*stride*size; i++) vecin[i] = (double)i;
33 for (root=0; root<size; root++) {
34 for (i=0; i<n; i++) vecout[i] = -1.0;
35 MPI_Scatter( vecin, 1, vec, vecout, n, MPI_DOUBLE, root,
37 ivalue = rank * ((n-1) * stride + 1);
39 if (vecout[i] != ivalue) {
40 printf( "Expected %f but found %f\n",
48 MPI_Allreduce( &i, &err, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD );
50 if (err > 0) printf( "Found %d errors!\n", err );
51 else printf( " No Errors\n" );
53 MPI_Type_free( &vec );