1 /* -*- Mode: C; c-basic-offset:4 ; -*- */
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
9 void addem ( int *, int *, int *, MPI_Datatype * );
10 void assoc ( int *, int *, int *, MPI_Datatype * );
11
addem(int * invec,int * inoutvec,int * len,MPI_Datatype * dtype)12 void addem( int *invec, int *inoutvec, int *len, MPI_Datatype *dtype)
13 {
14 int i;
15 for ( i=0; i<*len; i++ )
16 inoutvec[i] += invec[i];
17 }
18
19 #define BAD_ANSWER 100000
20
21 /*
22 The operation is inoutvec[i] = invec[i] op inoutvec[i]
23 (see 4.9.4). The order is important.
24
25 Note that the computation is in process rank (in the communicator)
26 order, independant of the root.
27 */
assoc(int * invec,int * inoutvec,int * len,MPI_Datatype * dtype)28 void assoc( int *invec, int *inoutvec, int *len, MPI_Datatype *dtype)
29 {
30 int i;
31 for ( i=0; i<*len; i++ ) {
32 if (inoutvec[i] <= invec[i] ) {
33 int rank;
34 MPI_Comm_rank( MPI_COMM_WORLD, &rank );
35 fprintf( stderr, "[%d] inout[0] = %d, in[0] = %d\n",
36 rank, inoutvec[0], invec[0] );
37 inoutvec[i] = BAD_ANSWER;
38 }
39 else
40 inoutvec[i] = invec[i];
41 }
42 }
43
main(int argc,char ** argv)44 int main( int argc, char **argv )
45 {
46 int rank, size, i;
47 int data;
48 int errors=0;
49 int result = -100;
50 int correct_result;
51 MPI_Op op_assoc, op_addem;
52 MPI_Comm comm=MPI_COMM_WORLD;
53
54 MPI_Init( &argc, &argv );
55 MPI_Op_create( (MPI_User_function *)assoc, 0, &op_assoc );
56 MPI_Op_create( (MPI_User_function *)addem, 1, &op_addem );
57
58 /* Run this for a variety of communicator sizes */
59
60 MPI_Comm_rank( comm, &rank );
61 MPI_Comm_size( comm, &size );
62
63 data = rank;
64
65 correct_result = 0;
66 for (i=0;i<=rank;i++)
67 correct_result += i;
68
69 MPI_Scan ( &data, &result, 1, MPI_INT, MPI_SUM, comm );
70 if (result != correct_result) {
71 fprintf( stderr, "[%d] Error suming ints with scan\n", rank );
72 errors++;
73 }
74
75 MPI_Scan ( &data, &result, 1, MPI_INT, MPI_SUM, comm );
76 if (result != correct_result) {
77 fprintf( stderr, "[%d] Error summing ints with scan (2)\n", rank );
78 errors++;
79 }
80
81 data = rank;
82 result = -100;
83 MPI_Scan ( &data, &result, 1, MPI_INT, op_addem, comm );
84 if (result != correct_result) {
85 fprintf( stderr, "[%d] Error summing ints with scan (userop)\n",
86 rank );
87 errors++;
88 }
89
90 MPI_Scan ( &data, &result, 1, MPI_INT, op_addem, comm );
91 if (result != correct_result) {
92 fprintf( stderr, "[%d] Error summing ints with scan (userop2)\n",
93 rank );
94 errors++;
95 }
96 result = -100;
97 data = rank;
98 MPI_Scan ( &data, &result, 1, MPI_INT, op_assoc, comm );
99 if (result == BAD_ANSWER) {
100 fprintf( stderr, "[%d] Error scanning with non-commutative op\n",
101 rank );
102 errors++;
103 }
104
105 MPI_Op_free( &op_assoc );
106 MPI_Op_free( &op_addem );
107
108 MPI_Finalize();
109 if (errors)
110 printf( "[%d] done with ERRORS(%d)!\n", rank, errors );
111 else {
112 if (rank == 0)
113 printf(" No Errors\n");
114 }
115
116 return errors;
117 }
118