Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix dist
[simgrid.git] / teshsuite / smpi / mpich3-test / coll / alltoallw_zeros.c
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  *  (C) 2009 by Argonne National Laboratory.
4  *      See COPYRIGHT in top-level directory.
5  */
6 /* Based on a test case contributed by Michael Hofmann.
7  *
8  * This test makes sure that zero counts with non-zero-sized types on the
9  * send (recv) side match and don't cause a problem with non-zero counts and
10  * zero-sized types on the recv (send) side when using MPI_Alltoallw and
11  * MPI_Alltoallv.  */
12
13 /* TODO test intercommunicators as well */
14
15
16 #include <stdio.h>
17 #include <stdlib.h>
18
19 #include <mpi.h>
20
21 #include "mpitest.h"
22
23 int main(int argc, char *argv[])
24 {
25     int sendbuf, recvbuf;
26     int *sendcounts;
27     int *recvcounts;
28     int *sdispls;
29     int *rdispls;
30     MPI_Datatype sendtype;
31     MPI_Datatype *sendtypes;
32     MPI_Datatype *recvtypes;
33     int rank = -1;
34     int size = -1;
35     int i;
36
37
38     MPI_Init(&argc, &argv);
39
40     MPI_Comm_size(MPI_COMM_WORLD, &size);
41     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
42
43     sendtypes = malloc(size * sizeof(MPI_Datatype));
44     recvtypes = malloc(size * sizeof(MPI_Datatype));
45     sendcounts = malloc(size * sizeof(int));
46     recvcounts = malloc(size * sizeof(int));
47     sdispls = malloc(size * sizeof(int));
48     rdispls = malloc(size * sizeof(int));
49     if (!sendtypes || !recvtypes || !sendcounts || !recvcounts || !sdispls || !rdispls) {
50         printf("error, unable to allocate memory\n");
51         goto fn_exit;
52     }
53
54     MPI_Type_contiguous(0, MPI_INT, &sendtype);
55     MPI_Type_commit(&sendtype);
56
57     for (i = 0; i < size; ++i) {
58         sendtypes[i] = sendtype;
59         sendcounts[i] = 1;
60         sdispls[i] = 0;
61
62         recvtypes[i] = MPI_INT;
63         recvcounts[i] = 0;
64         rdispls[i] = 0;
65     }
66
67
68     /* try zero-counts on both the send and recv side in case only one direction is broken for some reason */
69     MPI_Alltoallw(&sendbuf, sendcounts, sdispls, sendtypes, &recvbuf, recvcounts, rdispls,
70                   recvtypes, MPI_COMM_WORLD);
71     MPI_Alltoallw(&sendbuf, recvcounts, rdispls, recvtypes, &recvbuf, sendcounts, sdispls,
72                   sendtypes, MPI_COMM_WORLD);
73
74 #if MTEST_HAVE_MIN_MPI_VERSION(2,2)
75     /* pass MPI_IN_PLACE and different but compatible types rank is even/odd */
76     if (rank % 2)
77         MPI_Alltoallw(MPI_IN_PLACE, NULL, NULL, NULL, &recvbuf, recvcounts, rdispls, recvtypes,
78                       MPI_COMM_WORLD);
79     else
80         MPI_Alltoallw(MPI_IN_PLACE, NULL, NULL, NULL, &recvbuf, sendcounts, sdispls, sendtypes,
81                       MPI_COMM_WORLD);
82 #endif
83
84     /* now the same for Alltoallv instead of Alltoallw */
85     MPI_Alltoallv(&sendbuf, sendcounts, sdispls, sendtypes[0], &recvbuf, recvcounts, rdispls,
86                   recvtypes[0], MPI_COMM_WORLD);
87     MPI_Alltoallv(&sendbuf, recvcounts, rdispls, recvtypes[0], &recvbuf, sendcounts, sdispls,
88                   sendtypes[0], MPI_COMM_WORLD);
89
90 #if MTEST_HAVE_MIN_MPI_VERSION(2,2)
91     if (rank % 2)
92         MPI_Alltoallv(MPI_IN_PLACE, NULL, NULL, MPI_DATATYPE_NULL, &recvbuf, recvcounts, rdispls,
93                       recvtypes[0], MPI_COMM_WORLD);
94     else
95         MPI_Alltoallv(MPI_IN_PLACE, NULL, NULL, MPI_DATATYPE_NULL, &recvbuf, sendcounts, sdispls,
96                       sendtypes[0], MPI_COMM_WORLD);
97 #endif
98
99     MPI_Type_free(&sendtype);
100
101     if (rank == 0)
102         printf(" No Errors\n");
103
104   fn_exit:
105     if (rdispls)
106         free(rdispls);
107     if (sdispls)
108         free(sdispls);
109     if (recvcounts)
110         free(recvcounts);
111     if (sendcounts)
112         free(sendcounts);
113     if (recvtypes)
114         free(recvtypes);
115     if (sendtypes)
116         free(sendtypes);
117
118     MPI_Finalize();
119
120     return 0;
121 }