Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
This used to work by accident
[simgrid.git] / teshsuite / smpi / mpich-test / coll / scattern.c
1 #include "mpi.h"
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include "test.h" 
5
6 /* This example sends a vector and receives individual elements */
7
8 int main( int argc, char **argv )
9 {
10     MPI_Datatype vec;
11     double *vecin, *vecout, ivalue;
12     int    root, i, n, stride, err = 0;
13     int    rank, size;
14
15     MPI_Init( &argc, &argv );
16     
17     MPI_Comm_size( MPI_COMM_WORLD, &size );
18     MPI_Comm_rank( MPI_COMM_WORLD, &rank );
19
20     n = 12;
21     stride = 10;
22     vecin = (double *)malloc( n * stride * size * sizeof(double) );
23     vecout = (double *)malloc( n * sizeof(double) );
24     
25     MPI_Type_vector( n, 1, stride, MPI_DOUBLE, &vec );
26     MPI_Type_commit( &vec );
27
28     for (i=0; i<n*stride*size; i++) vecin[i] = (double)i;
29     for (root=0; root<size; root++) {
30         for (i=0; i<n; i++) vecout[i] = -1.0;
31         MPI_Scatter( vecin, 1, vec, vecout, n, MPI_DOUBLE, root, 
32                      MPI_COMM_WORLD );
33         ivalue = rank * ((n-1) * stride + 1);
34         for (i=0; i<n; i++) {
35             if (vecout[i] != ivalue) {
36                 printf( "Expected %f but found %f\n", 
37                         ivalue, vecout[i] );
38                 err++;
39             }
40             ivalue += stride;
41         }
42     }
43     i = err;
44     MPI_Allreduce( &i, &err, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD );
45     if (rank == 0) {
46         if (err > 0) printf( "Found %d errors!\n", err );
47         else         printf( " No Errors\n" );
48     }
49     MPI_Type_free( &vec );
50     MPI_Finalize();
51     return 0;
52        
53 }
54