Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix dist
[simgrid.git] / teshsuite / smpi / mpich3-test / coll / coll3.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
21     MTest_Init(&argc, &argv);
22     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
23     MPI_Comm_size(MPI_COMM_WORLD, &size);
24
25     /* A maximum of MAX_PROCESSES processes can participate */
26     if (size > MAX_PROCESSES)
27         participants = MAX_PROCESSES;
28     else
29         participants = size;
30     /* while (MAX_PROCESSES % participants) participants--; */
31     if (MAX_PROCESSES % participants) {
32         fprintf(stderr, "Number of processors must divide %d\n", MAX_PROCESSES);
33         MPI_Abort(MPI_COMM_WORLD, 1);
34     }
35     if ((rank < participants)) {
36
37         /* Determine what rows are my responsibility */
38         int block_size = MAX_PROCESSES / participants;
39         int begin_row = rank * block_size;
40         int end_row = (rank + 1) * block_size;
41         int send_count = block_size * MAX_PROCESSES;
42
43         /* Fill in the displacements and recv_counts */
44         for (i = 0; i < participants; i++) {
45             displs[i] = i * block_size * MAX_PROCESSES;
46             recv_counts[i] = send_count;
47         }
48
49         /* Paint my rows my color */
50         for (i = begin_row; i < end_row; i++)
51             for (j = 0; j < MAX_PROCESSES; j++)
52                 table[i][j] = rank + 10;
53
54         /* Gather everybody's result together - sort of like an */
55         /* inefficient allgather */
56         for (i = 0; i < participants; i++) {
57             void *sendbuf = (i == rank ? MPI_IN_PLACE : &table[begin_row][0]);
58             MPI_Gatherv(sendbuf, send_count, MPI_INT,
59                         &table[0][0], recv_counts, displs, MPI_INT, i, MPI_COMM_WORLD);
60         }
61
62
63         /* Everybody should have the same table now.
64          *
65          * The entries are:
66          * Table[i][j] = (i/block_size) + 10;
67          */
68         for (i = 0; i < MAX_PROCESSES; i++)
69             if ((table[i][0] - table[i][MAX_PROCESSES - 1] != 0))
70                 errors++;
71         for (i = 0; i < MAX_PROCESSES; i++) {
72             for (j = 0; j < MAX_PROCESSES; j++) {
73                 if (table[i][j] != (i / block_size) + 10)
74                     errors++;
75             }
76         }
77         if (errors) {
78             /* Print out table if there are any errors */
79             for (i = 0; i < MAX_PROCESSES; i++) {
80                 printf("\n");
81                 for (j = 0; j < MAX_PROCESSES; j++)
82                     printf("  %d", table[i][j]);
83             }
84             printf("\n");
85         }
86     }
87
88     MTest_Finalize(errors);
89     MPI_Finalize();
90     return MTestReturnValue(errors);
91 }