5 MPI_Comm GetNextComm( void );
6 void addem ( int *, int *, int *, MPI_Datatype * );
7 void assoc ( int *, int *, int *, MPI_Datatype * );
9 void addem( int *invec, int *inoutvec, int *len, MPI_Datatype *dtype)
12 for ( i=0; i<*len; i++ )
13 inoutvec[i] += invec[i];
16 #define BAD_ANSWER 100000
19 The operation is inoutvec[i] = invec[i] op inoutvec[i]
20 (see 4.9.4). The order is important.
22 Note that the computation is in process rank (in the communicator)
23 order, independant of the root.
25 void assoc( int *invec, int *inoutvec, int *len, MPI_Datatype *dtype)
28 for ( i=0; i<*len; i++ ) {
29 if (inoutvec[i] <= invec[i] ) {
31 MPI_Comm_rank( MPI_COMM_WORLD, &rank );
32 fprintf( stderr, "[%d] inout[0] = %d, in[0] = %d\n",
33 rank, inoutvec[0], invec[0] );
34 inoutvec[i] = BAD_ANSWER;
37 inoutvec[i] = invec[i];
41 MPI_Comm GetNextComm( void )
43 MPI_Comm comm = MPI_COMM_NULL;
46 MPI_Comm_size( MPI_COMM_WORLD, &size );
47 MPI_Comm_rank( MPI_COMM_WORLD, &rank );
51 MPI_Comm_dup( MPI_COMM_WORLD, &comm );
54 /* invert the rank order */
55 MPI_Comm_split( MPI_COMM_WORLD, 0, size - rank, &comm );
58 /* Divide into subsets */
59 MPI_Comm_split( MPI_COMM_WORLD, rank < (size/2), rank, &comm );
62 /* Another division */
63 MPI_Comm_split( MPI_COMM_WORLD, rank < (size/3), size-rank, &comm );
67 MPI_Comm_split( MPI_COMM_WORLD, (rank % 2) == 0, rank, &comm );
70 /* Last case: startover */
78 int main( int argc, char **argv )
85 MPI_Op op_assoc, op_addem;
88 MPI_Init( &argc, &argv );
89 MPI_Op_create( (MPI_User_function *)assoc, 0, &op_assoc );
90 MPI_Op_create( (MPI_User_function *)addem, 1, &op_addem );
92 /* Run this for a variety of communicator sizes */
93 while ((comm = GetNextComm()) != MPI_COMM_NULL) {
94 MPI_Comm_rank( comm, &rank );
95 MPI_Comm_size( comm, &size );
100 for (i=0;i<=rank;i++)
103 MPI_Scan ( &data, &result, 1, MPI_INT, MPI_SUM, comm );
104 if (result != correct_result) {
105 fprintf( stderr, "[%d] Error suming ints with scan\n", rank );
109 MPI_Scan ( &data, &result, 1, MPI_INT, MPI_SUM, comm );
110 if (result != correct_result) {
111 fprintf( stderr, "[%d] Error summing ints with scan (2)\n", rank );
117 MPI_Scan ( &data, &result, 1, MPI_INT, op_addem, comm );
118 if (result != correct_result) {
119 fprintf( stderr, "[%d] Error summing ints with scan (userop)\n",
124 MPI_Scan ( &data, &result, 1, MPI_INT, op_addem, comm );
125 if (result != correct_result) {
126 fprintf( stderr, "[%d] Error summing ints with scan (userop2)\n",
132 /* MPI_Scan ( &data, &result, 1, MPI_INT, op_assoc, comm );*/
133 /* if (result == BAD_ANSWER) {*/
134 /* fprintf( stderr, "[%d] Error scanning with non-commutative op\n",*/
138 MPI_Comm_free( &comm );
141 MPI_Op_free( &op_assoc );
142 MPI_Op_free( &op_addem );
145 MPI_Comm_rank( MPI_COMM_WORLD, &rank );
146 printf( "[%d] done with ERRORS(%d)!\n", rank, errors );