5 #define MAX_PROCESSES 10
7 int main( int argc, char **argv )
10 int table[MAX_PROCESSES][MAX_PROCESSES];
13 int displs[MAX_PROCESSES];
14 int recv_counts[MAX_PROCESSES];
16 MPI_Init( &argc, &argv );
17 MPI_Comm_rank( MPI_COMM_WORLD, &rank );
18 MPI_Comm_size( MPI_COMM_WORLD, &size );
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",
27 MPI_Abort( MPI_COMM_WORLD, 1 );
29 if ( (rank < participants) ) {
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;
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;
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;
48 /* Everybody gets the gathered data */
49 MPI_Allgatherv(&table[begin_row][0], send_count, MPI_INT,
50 &table[0][0], recv_counts, displs,
51 MPI_INT, MPI_COMM_WORLD);
53 /* Everybody should have the same table now.
56 Table[i][j] = (i/block_size) + 10;
58 for (i=0; i<MAX_PROCESSES;i++)
59 if ( (table[i][0] - table[i][MAX_PROCESSES-1] !=0) )
61 for (i=0; i<MAX_PROCESSES;i++) {
62 for (j=0; j<MAX_PROCESSES;j++) {
63 if (table[i][j] != (i/block_size) + 10) errors++;
67 /* Print out table if there are any errors */
68 for (i=0; i<MAX_PROCESSES;i++) {
70 for (j=0; j<MAX_PROCESSES; j++)
71 printf(" %d",table[i][j]);
80 printf( "[%d] done with ERRORS(%d)!\n", rank, errors );