Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'v3_8_x'
[simgrid.git] / teshsuite / smpi / mpich-test / coll / coll3.c
1 #include "mpi.h"
2 #include <stdio.h>
3 #include "test.h"
4
5 #define MAX_PROCESSES 10
6
7 int main( int argc, char **argv )
8 {
9     int              rank, size, i,j;
10     int              table[MAX_PROCESSES][MAX_PROCESSES];
11     int              errors=0;
12     int              participants;
13     int              displs[MAX_PROCESSES];
14     int              recv_counts[MAX_PROCESSES];
15
16     MPI_Init( &argc, &argv );
17     MPI_Comm_rank( MPI_COMM_WORLD, &rank );
18     MPI_Comm_size( MPI_COMM_WORLD, &size );
19
20     /* A maximum of MAX_PROCESSES processes can participate */
21     if ( size > MAX_PROCESSES ) participants = MAX_PROCESSES;
22     else              participants = size;
23     /* while (MAX_PROCESSES % participants) participants--; */
24     if (MAX_PROCESSES % participants) {
25         fprintf( stderr, "Number of processors must divide %d\n",
26                 MAX_PROCESSES );
27         MPI_Abort( MPI_COMM_WORLD, 1 );
28         }
29     if ( (rank < participants) ) {
30
31       /* Determine what rows are my responsibility */
32       int block_size = MAX_PROCESSES / participants;
33       int begin_row  = rank * block_size;
34       int end_row    = (rank+1) * block_size;
35       int send_count = block_size * MAX_PROCESSES;
36       
37       /* Fill in the displacements and recv_counts */
38       for (i=0; i<participants; i++) {
39         displs[i]      = i * block_size * MAX_PROCESSES;
40         recv_counts[i] = send_count;
41       }
42
43       /* Paint my rows my color */
44       for (i=begin_row; i<end_row ;i++)
45         for (j=0; j<MAX_PROCESSES; j++)
46           table[i][j] = rank + 10;
47       
48       /* Gather everybody's result together - sort of like an */
49       /* inefficient allgather */
50       for (i=0; i<participants; i++) {
51         MPI_Gatherv(&table[begin_row][0], send_count, MPI_INT, 
52                     &table[0][0], recv_counts, displs, MPI_INT, 
53                     i, MPI_COMM_WORLD);
54       }
55
56
57       /* Everybody should have the same table now.
58
59          The entries are:
60          Table[i][j] = (i/block_size) + 10;
61        */
62       for (i=0; i<MAX_PROCESSES;i++) 
63         if ( (table[i][0] - table[i][MAX_PROCESSES-1] !=0) ) 
64           errors++;
65       for (i=0; i<MAX_PROCESSES;i++) {
66           for (j=0; j<MAX_PROCESSES;j++) {
67               if (table[i][j] != (i/block_size) + 10) errors++;
68               }
69           }
70       if (errors) {
71           /* Print out table if there are any errors */
72           for (i=0; i<MAX_PROCESSES;i++) {
73               printf("\n");
74               for (j=0; j<MAX_PROCESSES; j++)
75                   printf("  %d",table[i][j]);
76               }
77           printf("\n");
78           }
79     } 
80
81     Test_Waitforall( );
82     MPI_Finalize();
83     if (errors)
84       printf( "[%d] done with ERRORS(%d)!\n", rank, errors );
85     return errors;
86 }