1 /* Copyright (c) 2013-2023. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
8 * (C) 2001 by Argonne National Laboratory.
9 * See COPYRIGHT in top-level directory.
16 /* This program tests MPI_Alltoallv by having processor i send different amounts of data to each processor.
18 TODO As there are separate send and receive types to alltoallv, there need to be tests to rearrange data on the fly.
19 The first test sends i items to processor i from all processors.
20 Currently, the test uses only MPI_INT; this is adequate for testing systems that use point-to-point operations
22 Example values for 3 processes:
23 * <0> sbuf: (#9): [0][1][2][3][4][5][6][7][8]
24 <0> scount: (#3): [0][1][2]
25 <0> rcount: (#3): [0][0][0]
26 <0> sdisp: (#3): [0][1][3]
27 <0> rdisp: (#3): [0][0][0]
29 <1> sbuf: (#9): [100][101][102][103][104][105][106][107][108]
30 <1> scount: (#3): [0][1][2]
31 <1> rcount: (#3): [1][1][1]
32 <1> sdisp: (#3): [0][1][3]
33 <1> rdisp: (#3): [0][1][2]
35 <2> sbuf: (#9): [200][201][202][203][204][205][206][207][208]
36 <2> scount: (#3): [0][1][2]
37 <2> rcount: (#3): [2][2][2]
38 <2> sdisp: (#3): [0][1][3]
39 <2> rdisp: (#3): [0][2][4]
41 after MPI_Alltoallvv :
42 <0> rbuf: (#9): [-1][-1][-1][-1][-1][-1][-1][-1][-1]
43 <1> rbuf: (#9): [1][101][201][-1][-1][-1][-1][-1][-1]
44 <2> rbuf: (#9): [3][4][103][104][203][204][-1][-1][-1]
47 static void print_buffer_int(const int* buf, int len, const char* msg, int rank)
49 printf("[%d] %s (#%d): ", rank, msg, len);
50 for (int tmp = 0; tmp < len; tmp++) {
51 printf("[%d]", buf[tmp]);
56 int main(int argc, char **argv)
63 MPI_Init(&argc, &argv);
65 comm = MPI_COMM_WORLD;
67 /* Create the buffer */
68 MPI_Comm_size(comm, &size);
69 MPI_Comm_set_errhandler(MPI_COMM_WORLD, MPI_ERRORS_RETURN);
72 printf("error : comm size <= 0, run with mpirun\n");
75 MPI_Comm_rank(comm, &rank);
76 int size2 = size * size;
77 xbt_assert(size2 > 0);
78 int* sbuf = (int*)xbt_malloc(size2 * sizeof(int));
79 int* rbuf = (int*)xbt_malloc(size2 * sizeof(int));
81 /* Load up the buffers */
82 for (i = 0; i < size2; i++) {
83 sbuf[i] = i + 100 * rank;
87 /* Create and load the arguments to alltoallv */
88 int* sendcounts = (int*)xbt_malloc(size * sizeof(int));
89 int* recvcounts = (int*)xbt_malloc(size * sizeof(int));
90 int* rdispls = (int*)xbt_malloc(size * sizeof(int));
91 int* sdispls = (int*)xbt_malloc(size * sizeof(int));
92 for (i = 0; i < size; i++) {
95 rdispls[i] = i * rank;
96 sdispls[i] = (i * (i + 1)) / 2;
100 status = MPI_Alltoallv(NULL, sendcounts, sdispls, MPI_INT, rbuf, recvcounts, rdispls, MPI_INT, MPI_COMM_WORLD);
101 if(status!=MPI_ERR_BUFFER)
102 printf("MPI_Alltoallv did not return MPI_ERR_BUFFER for empty sendbuf\n");
103 status = MPI_Alltoallv(sbuf, NULL, sdispls, MPI_INT, rbuf, recvcounts, rdispls, MPI_INT, MPI_COMM_WORLD);
104 if(status!=MPI_ERR_COUNT)
105 printf("MPI_Alltoallv did not return MPI_ERR_COUNT for NULL sendcounts\n");
106 status = MPI_Alltoallv(sbuf, sendcounts, NULL, MPI_INT, rbuf, recvcounts, rdispls, MPI_INT, MPI_COMM_WORLD);
107 if(status!=MPI_ERR_ARG)
108 printf("MPI_Alltoallv did not return MPI_ERR_ARG for NULL senddispl\n");
109 status = MPI_Alltoallv(sbuf, sendcounts, sdispls, MPI_DATATYPE_NULL, rbuf, recvcounts, rdispls, MPI_INT, MPI_COMM_WORLD);
110 if(status!=MPI_ERR_TYPE)
111 printf("MPI_Alltoallv did not return MPI_ERR_TYPE for MPI_DATATYPE_NULL sendtype\n");
112 if (rank == 0)//if total recvcounts == 0 the call is not illegal, so be sure to fail for rank 0 to avoid deadlocks
113 status = MPI_Alltoallv(NULL, sendcounts, sdispls, MPI_INT, NULL, recvcounts, rdispls, MPI_INT, MPI_COMM_WORLD);
115 status = MPI_Alltoallv(sbuf, sendcounts, sdispls, MPI_INT, NULL, recvcounts, rdispls, MPI_INT, MPI_COMM_WORLD);
116 if(status!=MPI_ERR_BUFFER)
117 printf("MPI_Alltoallv did not return MPI_ERR_BUFFER for empty recvbuf\n");
118 status = MPI_Alltoallv(sbuf, sendcounts, sdispls, MPI_INT, rbuf, NULL, rdispls, MPI_INT, MPI_COMM_WORLD);
119 if(status!=MPI_ERR_COUNT)
120 printf("MPI_Alltoallv did not return MPI_ERR_COUNT for NULL recvcounts\n");
121 status = MPI_Alltoallv(sbuf, sendcounts, sdispls, MPI_INT, rbuf, recvcounts, NULL, MPI_INT, MPI_COMM_WORLD);
122 if(status!=MPI_ERR_ARG)
123 printf("MPI_Alltoallv did not return MPI_ERR_ARG for NULL recvdispl\n");
124 status = MPI_Alltoallv(sbuf, sendcounts, sdispls, MPI_INT, rbuf, recvcounts, rdispls, MPI_DATATYPE_NULL, MPI_COMM_WORLD);
125 if(status!=MPI_ERR_TYPE)
126 printf("MPI_Alltoallv did not return MPI_ERR_TYPE for MPI_DATATYPE_NULL recvtype\n");
127 status = MPI_Alltoallv(sbuf, sendcounts, sdispls, MPI_INT, rbuf, recvcounts, rdispls, MPI_INT, MPI_COMM_NULL);
128 if(status!=MPI_ERR_COMM)
129 printf("MPI_Alltoallv did not return MPI_ERR_COMM for MPI_COMM_NULL comm\n");
131 print_buffer_int(sbuf, size2, "sbuf:", rank);
132 print_buffer_int(sendcounts, size, "scount:", rank);
133 print_buffer_int(recvcounts, size, "rcount:", rank);
134 print_buffer_int(sdispls, size, "sdisp:", rank);
135 print_buffer_int(rdispls, size, "rdisp:", rank);
137 status = MPI_Alltoallv(sbuf, sendcounts, sdispls, MPI_INT, rbuf, recvcounts, rdispls, MPI_INT, comm);
138 if (status != MPI_SUCCESS)
139 printf("MPI_Alltoallv did not return MPI_SUCCESS\n");
141 print_buffer_int(rbuf, size2, "rbuf:", rank);
143 MPI_Barrier(MPI_COMM_WORLD);
145 printf("Alltoallv TEST COMPLETE.\n");
150 xbt_free(recvcounts);
151 xbt_free(sendcounts);