Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix dist
[simgrid.git] / teshsuite / smpi / mpich3-test / coll / exscan.c
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  *
4  *  (C) 2003 by Argonne National Laboratory.
5  *      See COPYRIGHT in top-level directory.
6  */
7 #include "mpi.h"
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include "mpitest.h"
11
12 /*
13 static char MTEST_Descrip[] = "Test MPI_Exscan";
14 */
15
16 int main(int argc, char *argv[])
17 {
18     int errs = 0;
19     int rank, size;
20     int minsize = 2, count;
21     int *sendbuf, *recvbuf, i;
22     MPI_Comm comm;
23
24     MTest_Init(&argc, &argv);
25
26     /* The following illustrates the use of the routines to
27      * run through a selection of communicators and datatypes.
28      * Use subsets of these for tests that do not involve combinations
29      * of communicators, datatypes, and counts of datatypes */
30     while (MTestGetIntracommGeneral(&comm, minsize, 1)) {
31         if (comm == MPI_COMM_NULL)
32             continue;
33
34         MPI_Comm_rank(comm, &rank);
35         MPI_Comm_size(comm, &size);
36
37         for (count = 1; count < 65000; count = count * 2) {
38
39             sendbuf = (int *) malloc(count * sizeof(int));
40             recvbuf = (int *) malloc(count * sizeof(int));
41
42             for (i = 0; i < count; i++) {
43                 sendbuf[i] = rank + i * size;
44                 recvbuf[i] = -1;
45             }
46
47             MPI_Exscan(sendbuf, recvbuf, count, MPI_INT, MPI_SUM, comm);
48
49             /* Check the results.  rank 0 has no data */
50             if (rank > 0) {
51                 int result;
52                 for (i = 0; i < count; i++) {
53                     result = rank * i * size + ((rank) * (rank - 1)) / 2;
54                     if (recvbuf[i] != result) {
55                         errs++;
56                         if (errs < 10) {
57                             fprintf(stderr, "Error in recvbuf[%d] = %d on %d, expected %d\n",
58                                     i, recvbuf[i], rank, result);
59                         }
60                     }
61                 }
62             }
63
64 #if MTEST_HAVE_MIN_MPI_VERSION(2,2)
65             /* now try the MPI_IN_PLACE flavor */
66             for (i = 0; i < count; i++) {
67                 sendbuf[i] = -1;        /* unused */
68                 recvbuf[i] = rank + i * size;
69             }
70
71             MPI_Exscan(MPI_IN_PLACE, recvbuf, count, MPI_INT, MPI_SUM, comm);
72
73             /* Check the results.  rank 0's data must remain unchanged */
74             for (i = 0; i < count; i++) {
75                 int result;
76                 if (rank == 0)
77                     result = rank + i * size;
78                 else
79                     result = rank * i * size + ((rank) * (rank - 1)) / 2;
80                 if (recvbuf[i] != result) {
81                     errs++;
82                     if (errs < 10) {
83                         fprintf(stderr, "Error in recvbuf[%d] = %d on %d, expected %d\n",
84                                 i, recvbuf[i], rank, result);
85                     }
86                 }
87             }
88 #endif
89
90             free(sendbuf);
91             free(recvbuf);
92         }
93         MTestFreeComm(&comm);
94     }
95
96     MTest_Finalize(errs);
97     MPI_Finalize();
98     return 0;
99 }