Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Change include order for smpi tests/examples to avoid conflicts
[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 ||
50         !sendcounts || !recvcounts ||
51         !sdispls    || !rdispls)
52     {
53         printf("error, unable to allocate memory\n");
54         goto fn_exit;
55     }
56
57     MPI_Type_contiguous(0, MPI_INT, &sendtype);
58     MPI_Type_commit(&sendtype);
59
60     for (i = 0; i < size; ++i) {
61         sendtypes[i] = sendtype;
62         sendcounts[i] = 1;
63         sdispls[i] = 0;
64
65         recvtypes[i] = MPI_INT;
66         recvcounts[i] = 0;
67         rdispls[i] = 0;
68     }
69
70
71     /* try zero-counts on both the send and recv side in case only one direction is broken for some reason */
72     MPI_Alltoallw(&sendbuf, sendcounts, sdispls, sendtypes, &recvbuf, recvcounts, rdispls, recvtypes, MPI_COMM_WORLD);
73     MPI_Alltoallw(&sendbuf, recvcounts, rdispls, recvtypes, &recvbuf, sendcounts, sdispls, sendtypes, MPI_COMM_WORLD);
74
75 #if MTEST_HAVE_MIN_MPI_VERSION(2,2)
76     /* pass MPI_IN_PLACE and different but compatible types rank is even/odd */
77     if (rank % 2)
78         MPI_Alltoallw(MPI_IN_PLACE, NULL, NULL, NULL, &recvbuf, recvcounts, rdispls, recvtypes, MPI_COMM_WORLD);
79     else
80         MPI_Alltoallw(MPI_IN_PLACE, NULL, NULL, NULL, &recvbuf, sendcounts, sdispls, sendtypes, MPI_COMM_WORLD);
81 #endif
82
83     /* now the same for Alltoallv instead of Alltoallw */
84     MPI_Alltoallv(&sendbuf, sendcounts, sdispls, sendtypes[0], &recvbuf, recvcounts, rdispls, recvtypes[0], MPI_COMM_WORLD);
85     MPI_Alltoallv(&sendbuf, recvcounts, rdispls, recvtypes[0], &recvbuf, sendcounts, sdispls, sendtypes[0], MPI_COMM_WORLD);
86
87 #if MTEST_HAVE_MIN_MPI_VERSION(2,2)
88     if (rank % 2)
89         MPI_Alltoallv(MPI_IN_PLACE, NULL, NULL, MPI_DATATYPE_NULL, &recvbuf, recvcounts, rdispls, recvtypes[0], MPI_COMM_WORLD);
90     else
91         MPI_Alltoallv(MPI_IN_PLACE, NULL, NULL, MPI_DATATYPE_NULL, &recvbuf, sendcounts, sdispls, sendtypes[0], MPI_COMM_WORLD);
92 #endif
93
94     MPI_Type_free(&sendtype);
95
96     if (rank == 0)
97         printf(" No Errors\n");
98
99 fn_exit:
100     if (rdispls)    free(rdispls);
101     if (sdispls)    free(sdispls);
102     if (recvcounts) free(recvcounts);
103     if (sendcounts) free(sendcounts);
104     if (recvtypes)  free(recvtypes);
105     if (sendtypes)  free(sendtypes);
106
107     MPI_Finalize();
108
109     return 0;
110 }
111