Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Change include order for smpi tests/examples to avoid conflicts
[simgrid.git] / teshsuite / smpi / mpich3-test / coll / coll6.c
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
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 #include "mpitest.h"
9
10 #define MAX_PROCESSES 10
11
12 int main( int argc, char **argv )
13 {
14     int              rank, size, i,j;
15     int              table[MAX_PROCESSES][MAX_PROCESSES];
16     int              errors=0;
17     int              participants;
18     int              displs[MAX_PROCESSES];
19     int              recv_counts[MAX_PROCESSES];
20     MPI_Comm         test_comm;
21
22     MTest_Init( &argc, &argv );
23     MPI_Comm_rank( MPI_COMM_WORLD, &rank );
24     MPI_Comm_size( MPI_COMM_WORLD, &size );
25
26     /* A maximum of MAX_PROCESSES processes can participate */
27     participants = ( size > MAX_PROCESSES ) ? MAX_PROCESSES : size;
28
29     if (MAX_PROCESSES % participants) {
30         fprintf( stderr, "Number of processors must divide %d\n",
31                 MAX_PROCESSES );
32         MPI_Abort( MPI_COMM_WORLD, 1 );
33         exit(1);
34         }
35     MPI_Comm_split(MPI_COMM_WORLD, rank<participants, rank, &test_comm);
36
37     if ( rank < participants ) {
38
39       /* Determine what rows are my responsibility */
40       int block_size = MAX_PROCESSES / participants;
41       int begin_row  = rank * block_size;
42       int end_row    = (rank+1) * block_size;
43       int send_count = block_size * MAX_PROCESSES;
44       
45       /* Fill in the displacements and recv_counts */
46       for (i=0; i<participants; i++) {
47         displs[i]      = i * block_size * MAX_PROCESSES;
48         recv_counts[i] = send_count;
49       }
50
51       /* Paint my rows my color */
52       for (i=begin_row; i<end_row ;i++)
53         for (j=0; j<MAX_PROCESSES; j++)
54           table[i][j] = rank + 10;
55       
56       /* Everybody gets the gathered data */
57       if ((char *) &table[begin_row][0] != (char *) table + displs[rank]*sizeof(int))
58           MPI_Allgatherv(&table[begin_row][0], send_count, MPI_INT,
59                          &table[0][0], recv_counts, displs,
60                          MPI_INT, test_comm);
61       else
62           MPI_Allgatherv(MPI_IN_PLACE, send_count, MPI_INT,
63                          &table[0][0], recv_counts, displs,
64                          MPI_INT, test_comm);
65
66       /* Everybody should have the same table now.
67
68          The entries are:
69          Table[i][j] = (i/block_size) + 10;
70        */
71       for (i=0; i<MAX_PROCESSES;i++) 
72         if ( (table[i][0] - table[i][MAX_PROCESSES-1] !=0) ) 
73           errors++;
74       for (i=0; i<MAX_PROCESSES;i++) {
75           for (j=0; j<MAX_PROCESSES;j++) {
76               if (table[i][j] != (i/block_size) + 10) errors++;
77               }
78           }
79       if (errors) {
80           /* Print out table if there are any errors */
81           for (i=0; i<MAX_PROCESSES;i++) {
82               printf("\n");
83               for (j=0; j<MAX_PROCESSES; j++)
84                   printf("  %d",table[i][j]);
85               }
86           printf("\n");
87           }
88     } 
89
90     MTest_Finalize( errors );
91
92     MPI_Comm_free(&test_comm);
93     MPI_Finalize();
94     return MTestReturnValue( errors );
95 }