Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add new entry in Release_Notes.
[simgrid.git] / teshsuite / smpi / mpich3-test / rma / putfidx.c
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  *
4  *  (C) 2003 by Argonne National Laboratory.
5  *      See COPYRIGHT in top-level directory.
6  */
7 #include "mpi.h"
8 #include <stdio.h>
9 #include "mpitest.h"
10 #ifdef HAVE_STDLIB_H
11 #include <stdlib.h>
12 #endif
13
14 /*
15 static char MTEST_Descrip[] = "Put with Fence for an indexed datatype";
16 */
17
18 int CheckMPIErr(int err);
19
20 int main(int argc, char *argv[])
21 {
22     int errs = 0, err;
23     int i, rank, size, source, dest;
24     int blksize, totsize;
25     int *recvBuf = 0, *srcBuf = 0;
26     MPI_Comm comm;
27     MPI_Win win;
28     MPI_Aint extent;
29     MPI_Datatype originType;
30     int counts[2];
31     int displs[2];
32
33     MTest_Init(&argc, &argv);
34
35     /* Select the communicator and datatypes */
36     comm = MPI_COMM_WORLD;
37
38     /* Create the datatype */
39     /* One MPI Implementation fails this test with sufficiently large
40      * values of blksize - it appears to convert this type to an
41      * incorrect contiguous move */
42     blksize = 2048;
43     counts[0] = blksize;
44     counts[1] = blksize;
45     displs[0] = 0;
46     displs[1] = blksize + 1;
47     MPI_Type_indexed(2, counts, displs, MPI_INT, &originType);
48     MPI_Type_commit(&originType);
49
50     totsize = 2 * blksize;
51
52     /* Determine the sender and receiver */
53     MPI_Comm_rank(comm, &rank);
54     MPI_Comm_size(comm, &size);
55     source = 0;
56     dest = size - 1;
57
58     recvBuf = (int *) malloc(totsize * sizeof(int));
59     srcBuf = (int *) malloc((totsize + 1) * sizeof(int));
60
61     if (!recvBuf || !srcBuf) {
62         fprintf(stderr, "Could not allocate buffers\n");
63         MPI_Abort(MPI_COMM_WORLD, 1);
64     }
65
66     /* Initialize the send and recv buffers */
67     for (i = 0; i < totsize; i++) {
68         recvBuf[i] = -1;
69     }
70     for (i = 0; i < blksize; i++) {
71         srcBuf[i] = i;
72         srcBuf[blksize + 1 + i] = blksize + i;
73     }
74     srcBuf[blksize] = -1;
75
76     MPI_Type_extent(MPI_INT, &extent);
77     MPI_Win_create(recvBuf, totsize * extent, extent, MPI_INFO_NULL, comm, &win);
78     MPI_Win_fence(0, win);
79     if (rank == source) {
80         /* To improve reporting of problems about operations, we
81          * change the error handler to errors return */
82         MPI_Win_set_errhandler(win, MPI_ERRORS_RETURN);
83
84         err = MPI_Put(srcBuf, 1, originType, dest, 0, totsize, MPI_INT, win);
85         errs += CheckMPIErr(err);
86         err = MPI_Win_fence(0, win);
87         errs += CheckMPIErr(err);
88     }
89     else if (rank == dest) {
90         MPI_Win_fence(0, win);
91         for (i = 0; i < totsize; i++) {
92             if (recvBuf[i] != i) {
93                 errs++;
94                 if (errs < 10) {
95                     printf("recvBuf[%d] = %d should = %d\n", i, recvBuf[i], i);
96                 }
97             }
98         }
99     }
100     else {
101         MPI_Win_fence(0, win);
102     }
103
104     MPI_Type_free(&originType);
105     MPI_Win_free(&win);
106     free(recvBuf);
107     free(srcBuf);
108
109     MTest_Finalize(errs);
110     MPI_Finalize();
111     return 0;
112 }
113
114 int CheckMPIErr(int err)
115 {
116     int rc = 0;
117     if (err != MPI_SUCCESS) {
118         MTestPrintError(err);
119         rc = 1;
120     }
121     return rc;
122 }