1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
4 * (C) 2003 by Argonne National Laboratory.
5 * See COPYRIGHT in top-level directory.
13 static char MTEST_Descrip[] = "Test bsend message handling where \
14 different messages are received in different orders";
20 * To ensure that messages remain in the bsend buffer until received,
21 * messages are sent with size MSG_SIZE (ints).
24 #define MSG_SIZE 17000
26 int main(int argc, char *argv[])
29 int b1[MSG_SIZE], b2[MSG_SIZE], b3[MSG_SIZE], b4[MSG_SIZE];
30 int src, dest, size, rank, i;
34 MTest_Init(&argc, &argv);
36 MPI_Errhandler_set(MPI_COMM_WORLD, MPI_ERRORS_RETURN);
38 comm = MPI_COMM_WORLD;
39 MPI_Comm_rank(comm, &rank);
40 MPI_Comm_size(comm, &size);
44 fprintf(stderr, "At least 2 processes required\n");
45 MPI_Abort(MPI_COMM_WORLD, 1);
52 int *buf, bufsize, bsize;
54 bufsize = 4 * (MSG_SIZE * sizeof(int) + MPI_BSEND_OVERHEAD);
55 buf = (int *) malloc(bufsize);
57 fprintf(stderr, "Could not allocate buffer of %d bytes\n", bufsize);
58 MPI_Abort(MPI_COMM_WORLD, 1);
60 MPI_Buffer_attach(buf, bufsize);
63 for (i = 0; i < MSG_SIZE; i++) {
66 b3[i] = 2 * MSG_SIZE + i;
67 b4[i] = 3 * MSG_SIZE + i;
69 /* Send and reset buffers after bsend returns */
70 MPI_Bsend(b1, MSG_SIZE, MPI_INT, dest, 0, comm);
71 for (i = 0; i < MSG_SIZE; i++)
73 MPI_Bsend(b2, MSG_SIZE, MPI_INT, dest, 1, comm);
74 for (i = 0; i < MSG_SIZE; i++)
76 MPI_Bsend(b3, MSG_SIZE, MPI_INT, dest, 2, comm);
77 for (i = 0; i < MSG_SIZE; i++)
79 MPI_Bsend(b4, MSG_SIZE, MPI_INT, dest, 3, comm);
80 for (i = 0; i < MSG_SIZE; i++)
84 /* Detach waits until all messages received */
85 MPI_Buffer_detach(&buf, &bsize);
88 else if (rank == dest) {
91 MPI_Recv(b2, MSG_SIZE, MPI_INT, src, 1, comm, &status);
92 MPI_Recv(b1, MSG_SIZE, MPI_INT, src, 0, comm, &status);
93 MPI_Recv(b4, MSG_SIZE, MPI_INT, src, 3, comm, &status);
94 MPI_Recv(b3, MSG_SIZE, MPI_INT, src, 2, comm, &status);
96 /* Check received data */
97 for (i = 0; i < MSG_SIZE; i++) {
101 printf("b1[%d] is %d\n", i, b1[i]);
103 if (b2[i] != MSG_SIZE + i) {
106 printf("b2[%d] is %d\n", i, b2[i]);
108 if (b3[i] != 2 * MSG_SIZE + i) {
111 printf("b3[%d] is %d\n", i, b3[i]);
113 if (b4[i] != 3 * MSG_SIZE + i) {
116 printf("b4[%d] is %d\n", i, b4[i]);
125 MTest_Finalize(errs);