Logo AND Algorithmique Numérique Distribuée

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