1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
3 * (C) 2001 by Argonne National Laboratory.
4 * See COPYRIGHT in top-level directory.
13 This program tests MPI_Alltoallw by having processor i send different
14 amounts of data to each processor. This is just the MPI_Alltoallv test,
15 but with displacements in bytes rather than units of the datatype.
17 Because there are separate send and receive types to alltoallw,
18 there need to be tests to rearrange data on the fly. Not done yet.
20 The first test sends i items to processor i from all processors.
22 Currently, the test uses only MPI_INT; this is adequate for testing systems
23 that use point-to-point operations
26 int main(int argc, char **argv)
32 int *sendcounts, *recvcounts, *rdispls, *sdispls;
34 MPI_Datatype *sendtypes, *recvtypes;
36 MTest_Init(&argc, &argv);
39 while (MTestGetIntracommGeneral(&comm, 2, 1)) {
40 if (comm == MPI_COMM_NULL)
43 /* Create the buffer */
44 MPI_Comm_size(comm, &size);
45 MPI_Comm_rank(comm, &rank);
46 sbuf = (int *) malloc(size * size * sizeof(int));
47 rbuf = (int *) malloc(size * size * sizeof(int));
49 fprintf(stderr, "Could not allocated buffers!\n");
53 /* Load up the buffers */
54 for (i = 0; i < size * size; i++) {
55 sbuf[i] = i + 100 * rank;
59 /* Create and load the arguments to alltoallv */
60 sendcounts = (int *) malloc(size * sizeof(int));
61 recvcounts = (int *) malloc(size * sizeof(int));
62 rdispls = (int *) malloc(size * sizeof(int));
63 sdispls = (int *) malloc(size * sizeof(int));
64 sendtypes = (MPI_Datatype *) malloc(size * sizeof(MPI_Datatype));
65 recvtypes = (MPI_Datatype *) malloc(size * sizeof(MPI_Datatype));
66 if (!sendcounts || !recvcounts || !rdispls || !sdispls || !sendtypes || !recvtypes) {
67 fprintf(stderr, "Could not allocate arg items!\n");
70 /* Note that process 0 sends no data (sendcounts[0] = 0) */
71 for (i = 0; i < size; i++) {
74 rdispls[i] = i * rank * sizeof(int);
75 sdispls[i] = (((i + 1) * (i)) / 2) * sizeof(int);
76 sendtypes[i] = recvtypes[i] = MPI_INT;
78 MPI_Alltoallw(sbuf, sendcounts, sdispls, sendtypes,
79 rbuf, recvcounts, rdispls, recvtypes, comm);
82 for (i = 0; i < size; i++) {
83 p = rbuf + rdispls[i] / sizeof(int);
84 for (j = 0; j < rank; j++) {
85 if (p[j] != i * 100 + (rank * (rank + 1)) / 2 + j) {
86 fprintf(stderr, "[%d] got %d expected %d for %dth\n",
87 rank, p[j], (i * (i + 1)) / 2 + j, j);
98 #if MTEST_HAVE_MIN_MPI_VERSION(2,2)
99 /* check MPI_IN_PLACE, added in MPI-2.2 */
101 rbuf = (int *) malloc(size * (2 * size) * sizeof(int));
103 fprintf(stderr, "Could not reallocate rbuf!\n");
107 /* Load up the buffers */
108 for (i = 0; i < size; i++) {
109 /* alltoallw displs are in bytes, not in type extents */
110 rdispls[i] = i * (2 * size) * sizeof(int);
111 recvtypes[i] = MPI_INT;
112 recvcounts[i] = i + rank;
114 memset(rbuf, -1, size * (2 * size) * sizeof(int));
115 for (i = 0; i < size; i++) {
116 p = rbuf + (rdispls[i] / sizeof(int));
117 for (j = 0; j < recvcounts[i]; ++j) {
118 p[j] = 100 * rank + 10 * i + j;
122 MPI_Alltoallw(MPI_IN_PLACE, NULL, NULL, NULL, rbuf, recvcounts, rdispls, recvtypes, comm);
125 for (i = 0; i < size; i++) {
126 p = rbuf + (rdispls[i] / sizeof(int));
127 for (j = 0; j < recvcounts[i]; j++) {
128 int expected = 100 * i + 10 * rank + j;
129 if (p[j] != expected) {
130 fprintf(stderr, "[%d] got %d expected %d for block=%d, element=%dth\n",
131 rank, p[j], expected, i, j);
142 MTestFreeComm(&comm);