Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Reduce the size of partial shared malloc tests.
[simgrid.git] / teshsuite / smpi / mpich3-test / rma / lockopts.c
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  *  (C) 2012 by Argonne National Laboratory.
4  *      See COPYRIGHT in top-level directory.
5  */
6 #include "mpi.h"
7 #include "stdio.h"
8 #include "stdlib.h"
9 #include "mpitest.h"
10
11 /* tests passive target RMA on 2 processes. tests the lock-single_op-unlock
12    optimization for less common cases:
13
14    origin datatype derived, target datatype predefined
15
16 */
17 int main(int argc, char *argv[])
18 {
19     int wrank, nprocs, *srcbuf, *rmabuf, i;
20     int memsize;
21     MPI_Datatype vectype;
22     MPI_Win win;
23     int errs = 0;
24
25     MTest_Init(&argc, &argv);
26     MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
27     MPI_Comm_rank(MPI_COMM_WORLD, &wrank);
28
29     if (nprocs < 2) {
30         printf("Run this program with 2 or more processes\n");
31         MPI_Abort(MPI_COMM_WORLD, 1);
32     }
33
34     memsize = 10 * 4 * nprocs;
35     /* Create and initialize data areas */
36     srcbuf = (int *) malloc(sizeof(int) * memsize);
37     MPI_Alloc_mem(sizeof(int) * memsize, MPI_INFO_NULL, &rmabuf);
38     if (!srcbuf || !rmabuf) {
39         printf("Unable to allocate srcbuf and rmabuf of size %d\n", memsize);
40         MPI_Abort(MPI_COMM_WORLD, 1);
41     }
42     for (i = 0; i < memsize; i++) {
43         rmabuf[i] = -i;
44         srcbuf[i] = i;
45     }
46
47     MPI_Win_create(rmabuf, memsize * sizeof(int), sizeof(int), MPI_INFO_NULL, MPI_COMM_WORLD, &win);
48
49     /* Vector of 10 elements, separated by 4 */
50     MPI_Type_vector(10, 1, 4, MPI_INT, &vectype);
51     MPI_Type_commit(&vectype);
52
53     /* Accumulate with a derived origin type and target predefined type */
54     if (wrank == 0) {
55         MPI_Barrier(MPI_COMM_WORLD);
56         MPI_Win_lock(MPI_LOCK_EXCLUSIVE, 0, 0, win);
57         for (i = 0; i < 10; i++) {
58             if (rmabuf[i] != -i + 4 * i) {
59                 errs++;
60                 printf("Acc: expected rmabuf[%d] = %d but saw %d\n", i, -i + 4 * i, rmabuf[i]);
61             }
62             rmabuf[i] = -i;
63         }
64         for (i = 10; i < memsize; i++) {
65             if (rmabuf[i] != -i) {
66                 errs++;
67                 printf("Acc: expected rmabuf[%d] = %d but saw %d\n", i, -i, rmabuf[i]);
68                 rmabuf[i] = -i;
69             }
70         }
71         MPI_Win_unlock(0, win);
72     }
73     else if (wrank == 1) {
74         MPI_Win_lock(MPI_LOCK_SHARED, 0, 0, win);
75         MPI_Accumulate(srcbuf, 1, vectype, 0, 0, 10, MPI_INT, MPI_SUM, win);
76         MPI_Win_unlock(0, win);
77         MPI_Barrier(MPI_COMM_WORLD);
78     }
79     else {
80         MPI_Barrier(MPI_COMM_WORLD);
81     }
82
83     MPI_Barrier(MPI_COMM_WORLD);
84
85     /* Put with a derived origin type and target predefined type */
86     if (wrank == 0) {
87         MPI_Barrier(MPI_COMM_WORLD);
88         MPI_Win_lock(MPI_LOCK_EXCLUSIVE, 0, 0, win);
89         for (i = 0; i < 10; i++) {
90             if (rmabuf[i] != 4 * i) {
91                 errs++;
92                 printf("Put: expected rmabuf[%d] = %d but saw %d\n", i, 4 * i, rmabuf[i]);
93             }
94             rmabuf[i] = -i;
95         }
96         for (i = 10; i < memsize; i++) {
97             if (rmabuf[i] != -i) {
98                 errs++;
99                 printf("Put: expected rmabuf[%d] = %d but saw %d\n", i, -i, rmabuf[i]);
100                 rmabuf[i] = -i;
101             }
102         }
103         MPI_Win_unlock(0, win);
104     }
105     else if (wrank == 1) {
106         MPI_Win_lock(MPI_LOCK_SHARED, 0, 0, win);
107         MPI_Put(srcbuf, 1, vectype, 0, 0, 10, MPI_INT, win);
108         MPI_Win_unlock(0, win);
109         MPI_Barrier(MPI_COMM_WORLD);
110     }
111     else {
112         MPI_Barrier(MPI_COMM_WORLD);
113     }
114
115     MPI_Barrier(MPI_COMM_WORLD);
116
117     /* Put with a derived origin type and target predefined type, with
118      * a get (see the move-to-end optimization) */
119     if (wrank == 0) {
120         MPI_Barrier(MPI_COMM_WORLD);
121         MPI_Win_lock(MPI_LOCK_EXCLUSIVE, 0, 0, win);
122         for (i = 0; i < 10; i++) {
123             if (rmabuf[i] != 4 * i) {
124                 errs++;
125                 printf("Put: expected rmabuf[%d] = %d but saw %d\n", i, 4 * i, rmabuf[i]);
126             }
127             rmabuf[i] = -i;
128         }
129         for (i = 10; i < memsize; i++) {
130             if (rmabuf[i] != -i) {
131                 errs++;
132                 printf("Put: expected rmabuf[%d] = %d but saw %d\n", i, -i, rmabuf[i]);
133                 rmabuf[i] = -i;
134             }
135         }
136         MPI_Win_unlock(0, win);
137     }
138     else if (wrank == 1) {
139         int val;
140         MPI_Win_lock(MPI_LOCK_SHARED, 0, 0, win);
141         MPI_Get(&val, 1, MPI_INT, 0, 10, 1, MPI_INT, win);
142         MPI_Put(srcbuf, 1, vectype, 0, 0, 10, MPI_INT, win);
143         MPI_Win_unlock(0, win);
144         MPI_Barrier(MPI_COMM_WORLD);
145         if (val != -10) {
146             errs++;
147             printf("Get: Expected -10, got %d\n", val);
148         }
149     }
150     else {
151         MPI_Barrier(MPI_COMM_WORLD);
152     }
153
154     MPI_Barrier(MPI_COMM_WORLD);
155
156     /* Put with a derived origin type and target predefined type, with
157      * a get already at the end (see the move-to-end optimization) */
158     if (wrank == 0) {
159         MPI_Barrier(MPI_COMM_WORLD);
160         MPI_Win_lock(MPI_LOCK_EXCLUSIVE, 0, 0, win);
161         for (i = 0; i < 10; i++) {
162             if (rmabuf[i] != 4 * i) {
163                 errs++;
164                 printf("Put: expected rmabuf[%d] = %d but saw %d\n", i, 4 * i, rmabuf[i]);
165             }
166             rmabuf[i] = -i;
167         }
168         for (i = 10; i < memsize; i++) {
169             if (rmabuf[i] != -i) {
170                 errs++;
171                 printf("Put: expected rmabuf[%d] = %d but saw %d\n", i, -i, rmabuf[i]);
172                 rmabuf[i] = -i;
173             }
174         }
175         MPI_Win_unlock(0, win);
176     }
177     else if (wrank == 1) {
178         int val;
179         MPI_Win_lock(MPI_LOCK_SHARED, 0, 0, win);
180         MPI_Put(srcbuf, 1, vectype, 0, 0, 10, MPI_INT, win);
181         MPI_Get(&val, 1, MPI_INT, 0, 10, 1, MPI_INT, win);
182         MPI_Win_unlock(0, win);
183         MPI_Barrier(MPI_COMM_WORLD);
184         if (val != -10) {
185             errs++;
186             printf("Get: Expected -10, got %d\n", val);
187         }
188     }
189     else {
190         MPI_Barrier(MPI_COMM_WORLD);
191     }
192
193     MPI_Win_free(&win);
194     MPI_Free_mem(rmabuf);
195     free(srcbuf);
196     MPI_Type_free(&vectype);
197
198     MTest_Finalize(errs);
199     MPI_Finalize();
200     return 0;
201 }