Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
remove warning with mc
[simgrid.git] / teshsuite / smpi / mpich3-test / coll / scatter2.c
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  *
4  *  (C) 2003 by Argonne National Laboratory.
5  *      See COPYRIGHT in top-level directory.
6  */
7
8 #include "mpi.h"
9 #include "mpitest.h"
10 #include <stdlib.h>
11 #include <stdio.h>
12
13 /* This example sends a vector and receives individual elements, but the
14    root process does not receive any data */
15
16 int main( int argc, char **argv )
17 {
18     MPI_Datatype vec;
19     double *vecin, *vecout, ivalue;
20     int    root, i, n, stride, err = 0;
21     int    rank, size;
22     MPI_Aint vextent;
23
24     MTest_Init( &argc, &argv );
25     
26     MPI_Comm_size( MPI_COMM_WORLD, &size );
27     MPI_Comm_rank( MPI_COMM_WORLD, &rank );
28
29     n = 12;
30     stride = 10;
31     vecin = (double *)malloc( n * stride * size * sizeof(double) );
32     vecout = (double *)malloc( n * sizeof(double) );
33     
34     MPI_Type_vector( n, 1, stride, MPI_DOUBLE, &vec );
35     MPI_Type_commit( &vec );
36     MPI_Type_extent( vec, &vextent );
37     if (vextent != ((n-1)*(MPI_Aint)stride + 1) * sizeof(double) ) {
38         err++;
39         printf( "Vector extent is %ld, should be %ld\n", 
40                  (long) vextent, (long)(((n-1)*stride+1)*sizeof(double)) );
41     }
42     /* Note that the exted of type vector is from the first to the
43        last element, not n*stride.
44        E.g., with n=1, the extent is a single double */
45
46     for (i=0; i<n*stride*size; i++) vecin[i] = (double)i;
47     for (root=0; root<size; root++) {
48         for (i=0; i<n; i++) vecout[i] = -1.0;
49         if (rank == root) {
50             MPI_Scatter( vecin, 1, vec, MPI_IN_PLACE, -1, MPI_DATATYPE_NULL, 
51                          root, MPI_COMM_WORLD );
52         }
53         else {
54             MPI_Scatter( NULL, -1, MPI_DATATYPE_NULL, vecout, n, MPI_DOUBLE,
55                          root, MPI_COMM_WORLD );
56             ivalue = rank * ((n-1) * stride + 1);
57             for (i=0; i<n; i++) {
58                 if (vecout[i] != ivalue) {
59                     printf( "[%d] Expected %f but found %f for vecout[%d]\n", 
60                             rank, ivalue, vecout[i], i );
61                     err++;
62                 }
63                 ivalue += stride;
64             }
65         }
66     }
67
68     MPI_Comm_set_errhandler(MPI_COMM_WORLD, MPI_ERRORS_RETURN);
69     if (rank == 0 && MPI_SUCCESS ==
70             MPI_Scatter(vecin, 1, MPI_DOUBLE, vecin, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD))
71         err++;
72     
73     free(vecin);
74     free(vecout);
75     MTest_Finalize( err );
76     MPI_Type_free( &vec );
77     MPI_Finalize();
78     return 0;
79 }
80