Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid
[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, 
48                     MPI_COMM_WORLD, &win );
49
50     /* Vector of 10 elements, separated by 4 */
51     MPI_Type_vector( 10, 1, 4, MPI_INT, &vectype );
52     MPI_Type_commit( &vectype );
53
54     /* Accumulate with a derived origin type and target predefined type*/
55     if (wrank == 0) {
56         MPI_Barrier( MPI_COMM_WORLD );
57         MPI_Win_lock( MPI_LOCK_EXCLUSIVE, 0, 0, win );
58         for (i=0; i<10; i++) {
59             if (rmabuf[i] != -i + 4*i) {
60                 errs++;
61                 printf( "Acc: expected rmabuf[%d] = %d but saw %d\n", 
62                         i, -i + 4*i, rmabuf[i] );
63             }
64             rmabuf[i] = -i;
65         }
66         for (i=10; i<memsize; i++) {
67             if (rmabuf[i] != -i) {
68                 errs++;
69                 printf( "Acc: expected rmabuf[%d] = %d but saw %d\n", 
70                         i, -i, rmabuf[i] );
71                 rmabuf[i] = -i;
72             }
73         }
74         MPI_Win_unlock( 0, win );
75     }
76     else if (wrank == 1) {
77         MPI_Win_lock( MPI_LOCK_SHARED, 0, 0, win );
78         MPI_Accumulate( srcbuf, 1, vectype, 0, 0, 10, MPI_INT, MPI_SUM, win );
79         MPI_Win_unlock( 0, win );
80         MPI_Barrier( MPI_COMM_WORLD );
81     }
82     else {
83         MPI_Barrier( MPI_COMM_WORLD );
84     }
85
86     MPI_Barrier(MPI_COMM_WORLD);
87
88     /* Put with a derived origin type and target predefined type*/
89     if (wrank == 0) {
90         MPI_Barrier( MPI_COMM_WORLD );
91         MPI_Win_lock( MPI_LOCK_EXCLUSIVE, 0, 0, win );
92         for (i=0; i<10; i++) {
93             if (rmabuf[i] != 4*i) {
94                 errs++;
95                 printf( "Put: expected rmabuf[%d] = %d but saw %d\n", 
96                         i, 4*i, rmabuf[i] );
97             }
98             rmabuf[i] = -i;
99         }
100         for (i=10; i<memsize; i++) {
101             if (rmabuf[i] != -i) {
102                 errs++;
103                 printf( "Put: expected rmabuf[%d] = %d but saw %d\n", 
104                         i, -i, rmabuf[i] );
105                 rmabuf[i] = -i;
106             }
107         }
108         MPI_Win_unlock( 0, win );
109     }
110     else if (wrank == 1) {
111         MPI_Win_lock( MPI_LOCK_SHARED, 0, 0, win );
112         MPI_Put( srcbuf, 1, vectype, 0, 0, 10, MPI_INT, win );
113         MPI_Win_unlock( 0, win );
114         MPI_Barrier( MPI_COMM_WORLD );
115     }
116     else {
117         MPI_Barrier( MPI_COMM_WORLD );
118     }
119
120     MPI_Barrier(MPI_COMM_WORLD);
121
122     /* Put with a derived origin type and target predefined type, with 
123        a get (see the move-to-end optimization) */
124     if (wrank == 0) {
125         MPI_Barrier( MPI_COMM_WORLD );
126         MPI_Win_lock( MPI_LOCK_EXCLUSIVE, 0, 0, win );
127         for (i=0; i<10; i++) {
128             if (rmabuf[i] != 4*i) {
129                 errs++;
130                 printf( "Put: expected rmabuf[%d] = %d but saw %d\n", 
131                         i, 4*i, rmabuf[i] );
132             }
133             rmabuf[i] = -i;
134         }
135         for (i=10; i<memsize; i++) {
136             if (rmabuf[i] != -i) {
137                 errs++;
138                 printf( "Put: expected rmabuf[%d] = %d but saw %d\n", 
139                         i, -i, rmabuf[i] );
140                 rmabuf[i] = -i;
141             }
142         }
143         MPI_Win_unlock( 0, win );
144     }
145     else if (wrank == 1) {
146         int val;
147         MPI_Win_lock( MPI_LOCK_SHARED, 0, 0, win );
148         MPI_Get( &val, 1, MPI_INT, 0, 10, 1, MPI_INT, win );
149         MPI_Put( srcbuf, 1, vectype, 0, 0, 10, MPI_INT, win );
150         MPI_Win_unlock( 0, win );
151         MPI_Barrier( MPI_COMM_WORLD );
152         if (val != -10) {
153             errs++;
154             printf( "Get: Expected -10, got %d\n", val );
155         }
156     }
157     else {
158         MPI_Barrier( MPI_COMM_WORLD );
159     }
160
161     MPI_Barrier(MPI_COMM_WORLD);
162
163     /* Put with a derived origin type and target predefined type, with 
164        a get already at the end (see the move-to-end optimization) */
165     if (wrank == 0) {
166         MPI_Barrier( MPI_COMM_WORLD );
167         MPI_Win_lock( MPI_LOCK_EXCLUSIVE, 0, 0, win );
168         for (i=0; i<10; i++) {
169             if (rmabuf[i] != 4*i) {
170                 errs++;
171                 printf( "Put: expected rmabuf[%d] = %d but saw %d\n", 
172                         i, 4*i, rmabuf[i] );
173             }
174             rmabuf[i] = -i;
175         }
176         for (i=10; i<memsize; i++) {
177             if (rmabuf[i] != -i) {
178                 errs++;
179                 printf( "Put: expected rmabuf[%d] = %d but saw %d\n", 
180                         i, -i, rmabuf[i] );
181                 rmabuf[i] = -i;
182             }
183         }
184         MPI_Win_unlock( 0, win );
185     }
186     else if (wrank == 1) {
187         int val;
188         MPI_Win_lock( MPI_LOCK_SHARED, 0, 0, win );
189         MPI_Put( srcbuf, 1, vectype, 0, 0, 10, MPI_INT, win );
190         MPI_Get( &val, 1, MPI_INT, 0, 10, 1, MPI_INT, win );
191         MPI_Win_unlock( 0, win );
192         MPI_Barrier( MPI_COMM_WORLD );
193         if (val != -10) {
194             errs++;
195             printf( "Get: Expected -10, got %d\n", val );
196         }
197     }
198     else {
199         MPI_Barrier( MPI_COMM_WORLD );
200     }
201
202     MPI_Win_free( &win );
203     MPI_Free_mem( rmabuf );
204     free( srcbuf );
205     MPI_Type_free( &vectype );
206
207     MTest_Finalize(errs);
208     MPI_Finalize(); 
209     return 0; 
210
211