Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Reduce the size of partial shared malloc tests.
[simgrid.git] / teshsuite / smpi / mpich3-test / coll / alltoallv0.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 "mpitest.h"
8 #include <stdlib.h>
9 #include <stdio.h>
10
11 /*
12   This program tests MPI_Alltoallv by having processor each process
13   send data to two neighbors only, using counts of 0 for the other processes.
14   This idiom is sometimes used for halo exchange operations.
15
16   Because there are separate send and receive types to alltoallv,
17   there need to be tests to rearrange data on the fly.  Not done yet.
18
19   Currently, the test uses only MPI_INT; this is adequate for testing systems
20   that use point-to-point operations
21  */
22
23 int main(int argc, char **argv)
24 {
25
26     MPI_Comm comm;
27     int *sbuf, *rbuf;
28     int rank, size;
29     int *sendcounts, *recvcounts, *rdispls, *sdispls;
30     int i, *p, err;
31     int left, right, length;
32
33     MTest_Init(&argc, &argv);
34     err = 0;
35
36     while (MTestGetIntracommGeneral(&comm, 2, 1)) {
37         if (comm == MPI_COMM_NULL)
38             continue;
39
40         MPI_Comm_size(comm, &size);
41         MPI_Comm_rank(comm, &rank);
42
43         if (size < 3)
44             continue;
45
46         /* Create and load the arguments to alltoallv */
47         sendcounts = (int *) malloc(size * sizeof(int));
48         recvcounts = (int *) malloc(size * sizeof(int));
49         rdispls = (int *) malloc(size * sizeof(int));
50         sdispls = (int *) malloc(size * sizeof(int));
51         if (!sendcounts || !recvcounts || !rdispls || !sdispls) {
52             fprintf(stderr, "Could not allocate arg items!\n");
53             MPI_Abort(comm, 1);
54         }
55
56         /* Get the neighbors */
57         left = (rank - 1 + size) % size;
58         right = (rank + 1) % size;
59
60         /* Set the defaults */
61         for (i = 0; i < size; i++) {
62             sendcounts[i] = 0;
63             recvcounts[i] = 0;
64             rdispls[i] = 0;
65             sdispls[i] = 0;
66         }
67
68         for (length = 1; length < 66000; length = length * 2 + 1) {
69             /* Get the buffers */
70             sbuf = (int *) malloc(2 * length * sizeof(int));
71             rbuf = (int *) malloc(2 * length * sizeof(int));
72             if (!sbuf || !rbuf) {
73                 fprintf(stderr, "Could not allocate buffers!\n");
74                 MPI_Abort(comm, 1);
75             }
76
77             /* Load up the buffers */
78             for (i = 0; i < length; i++) {
79                 sbuf[i] = i + 100000 * rank;
80                 sbuf[i + length] = i + 100000 * rank;
81                 rbuf[i] = -i;
82                 rbuf[i + length] = -i - length;
83             }
84             sendcounts[left] = length;
85             sendcounts[right] = length;
86             recvcounts[left] = length;
87             recvcounts[right] = length;
88             rdispls[left] = 0;
89             rdispls[right] = length;
90             sdispls[left] = 0;
91             sdispls[right] = length;
92
93             MPI_Alltoallv(sbuf, sendcounts, sdispls, MPI_INT,
94                           rbuf, recvcounts, rdispls, MPI_INT, comm);
95
96             /* Check rbuf */
97             p = rbuf;   /* left */
98
99             for (i = 0; i < length; i++) {
100                 if (p[i] != i + 100000 * left) {
101                     if (err < 10) {
102                         fprintf(stderr, "[%d from %d] got %d expected %d for %dth\n",
103                                 rank, left, p[i], i + 100000 * left, i);
104                     }
105                     err++;
106                 }
107             }
108
109             p = rbuf + length;  /* right */
110             for (i = 0; i < length; i++) {
111                 if (p[i] != i + 100000 * right) {
112                     if (err < 10) {
113                         fprintf(stderr, "[%d from %d] got %d expected %d for %dth\n",
114                                 rank, right, p[i], i + 100000 * right, i);
115                     }
116                     err++;
117                 }
118             }
119
120             free(rbuf);
121             free(sbuf);
122         }
123
124         free(sdispls);
125         free(rdispls);
126         free(recvcounts);
127         free(sendcounts);
128         MTestFreeComm(&comm);
129     }
130
131     MTest_Finalize(err);
132     MPI_Finalize();
133     return 0;
134 }