Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
008aa712b13a8a7275707a91a44a339e6a52aca2
[simgrid.git] / teshsuite / smpi / mpich3-test / coll / coll2.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
19     MTest_Init( &argc, &argv );
20     MPI_Comm_rank( MPI_COMM_WORLD, &rank );
21     MPI_Comm_size( MPI_COMM_WORLD, &size );
22
23     /* A maximum of MAX_PROCESSES processes can participate */
24     if ( size > MAX_PROCESSES ) participants = MAX_PROCESSES;
25     else              participants = size;
26
27     if (MAX_PROCESSES % participants) {
28         fprintf( stderr, "Number of processors must divide %d\n",
29                 MAX_PROCESSES );
30         MPI_Abort( MPI_COMM_WORLD, 1 );
31         exit(1);
32         }
33     if ( (rank < participants) ) {
34
35       /* Determine what rows are my responsibility */
36       int block_size = MAX_PROCESSES / participants;
37       int begin_row  = rank * block_size;
38       int end_row    = (rank+1) * block_size;
39       int send_count = block_size * MAX_PROCESSES;
40       int recv_count = send_count;
41
42       /* Paint my rows my color */
43       for (i=begin_row; i<end_row ;i++)
44         for (j=0; j<MAX_PROCESSES; j++)
45           table[i][j] = rank + 10;
46
47       /* Gather everybody's result together - sort of like an */
48       /* inefficient allgather */
49       for (i=0; i<participants; i++) {
50         void *sendbuf = (i == rank ? MPI_IN_PLACE : &table[begin_row][0]);
51         MPI_Gather(sendbuf,              send_count, MPI_INT,
52                    &table[0][0],         recv_count, MPI_INT, i, 
53                    MPI_COMM_WORLD );
54       }
55
56       /* Everybody should have the same table now,  */
57       /* This test does not in any way guarantee there are no errors */
58       /* Print out a table or devise a smart test to make sure it's correct */
59       for (i=0; i<MAX_PROCESSES;i++) {
60         if ( (table[i][0] - table[i][MAX_PROCESSES-1] !=0) ) 
61           errors++;
62       }
63     } 
64
65     MTest_Finalize( errors );
66     MPI_Finalize();
67     return MTestReturnValue( errors );
68 }