Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Change include order for smpi tests/examples to avoid conflicts
[simgrid.git] / teshsuite / smpi / mpich3-test / coll / longuser.c
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  *  (C) 2001 by Argonne National Laboratory.
4  *      See COPYRIGHT in top-level directory.
5  */
6 #include "mpi.h"
7 #include <stdio.h>
8 #include <stdlib.h>
9
10 int add ( double *, double *, int *, MPI_Datatype * );
11 /*
12  * User-defined operation on a long value (tests proper handling of
13  * possible pipelining in the implementation of reductions with user-defined
14  * operations).
15  */
16 int add( double *invec, double *inoutvec, int *len, MPI_Datatype *dtype )
17 {
18     int i, n = *len;
19     for (i=0; i<n; i++) {
20         inoutvec[i] = invec[i] + inoutvec[i];
21     }
22     return 0;
23 }
24
25 int main( int argc, char **argv )
26 {
27     MPI_Op op;
28     int    i, rank, size, bufsize, errcnt = 0, toterr;
29     double *inbuf, *outbuf, value;
30     
31     MPI_Init( &argc, &argv );
32     MPI_Comm_rank( MPI_COMM_WORLD, &rank );
33     MPI_Comm_size( MPI_COMM_WORLD, &size );
34     MPI_Op_create( (MPI_User_function *)add, 1, &op );
35     
36     bufsize = 1;
37     while (bufsize < 100000) {
38         inbuf  = (double *)malloc( bufsize * sizeof(double) );
39         outbuf = (double *)malloc( bufsize * sizeof(double) );
40         if (! inbuf || ! outbuf) {
41             fprintf( stderr, "Could not allocate buffers for size %d\n",
42                      bufsize );
43             errcnt++;
44             break;
45         }
46
47         value = (rank & 0x1) ? 1.0 : -1.0;
48         for (i=0; i<bufsize; i++) {
49             inbuf[i]  = value;
50             outbuf[i] = 100.0;
51         }
52         MPI_Allreduce( inbuf, outbuf, bufsize, MPI_DOUBLE, op, 
53                        MPI_COMM_WORLD );
54         /* Check values */
55         value = (size & 0x1) ? -1.0 : 0.0;
56         for (i=0; i<bufsize; i++) {
57             if (outbuf[i] != value) {
58                 if (errcnt < 10) 
59                     printf( "outbuf[%d] = %f, should = %f\n", i, outbuf[i],
60                             value );
61                 errcnt ++;
62             }
63         }
64         free( inbuf );
65         free( outbuf );
66         bufsize *= 2;
67     }
68     
69     MPI_Allreduce( &errcnt, &toterr, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD );
70     MPI_Comm_rank( MPI_COMM_WORLD, &rank );
71     if (rank == 0) {
72         if (toterr == 0) 
73             printf( " No Errors\n" );
74         else 
75             printf( "*! %d errors!\n", toterr );
76     }
77
78     MPI_Op_free( &op );
79     MPI_Finalize( );
80     return 0;
81 }
82