swap.c
References to this file elsewhere.
1 #ifndef MS_SUA
2 # include <stdio.h>
3 #endif
4 #include <fcntl.h>
5
6 #define STANDARD_ERROR 2
7
8 #define STANDARD_OUTPUT 1
9
10 #include "mpi.h"
11 #include "rsl_lite.h"
12
13 #define UP_EVEN(A) ((A)+abs((A)%2))
14 #define DOWN_EVEN(A) ((A) - abs((A)%2))
15 #define UP_ODD(A) ((A) + abs(((A)+1)%2))
16 #define DOWN_ODD(A) ((A) - abs(((A)+1)%2))
17 #define MIN(A,B) ((A)<(B)?(A):(B))
18 #define MAX(A,B) ((A)>(B)?(A):(B))
19
20 static int *y_curs = NULL ;
21 static int *x_curs = NULL ;
22 static int *x_peermask = NULL ;
23 static int *nbytes = NULL ;
24 static MPI_Request *x_recv = NULL , *x_send = NULL ;
25
26 RSL_LITE_INIT_SWAP (
27 int * Fcomm ,
28 int * xy0 ,
29 int * n3dR0, int *n2dR0, int * typesizeR0 ,
30 int * n3dI0, int *n2dI0, int * typesizeI0 ,
31 int * n3dD0, int *n2dD0, int * typesizeD0 ,
32 int * n3dL0, int *n2dL0, int * typesizeL0 ,
33 int * me0, int * np0 , int * np_x0 , int * np_y0 ,
34 int * ids0 , int * ide0 , int * jds0 , int * jde0 , int * kds0 , int * kde0 ,
35 int * ips0 , int * ipe0 , int * jps0 , int * jpe0 , int * kps0 , int * kpe0 )
36 {
37 int n3dR, n2dR, typesizeR ;
38 int n3dI, n2dI, typesizeI ;
39 int n3dD, n2dD, typesizeD ;
40 int n3dL, n2dL, typesizeL ;
41 int xy ;
42 int me, np, np_x, np_y ;
43 int ids , ide , jds , jde , kds , kde ;
44 int ips , ipe , jps , jpe , kps , kpe ;
45 int ips_send , ipe_send ;
46 int npts, i, ii, j, m, n, ps, pe, ops, ope ;
47 int Px, Py, P, coords[2] ;
48 int ips_swap, ipe_swap ;
49 MPI_Comm *comm, dummy_comm ;
50
51 comm = &dummy_comm ;
52 *comm = MPI_Comm_f2c( *Fcomm ) ;
53
54 xy = *xy0 ;
55 n3dR = *n3dR0 ; n2dR = *n2dR0 ; typesizeR = *typesizeR0 ;
56 n3dI = *n3dI0 ; n2dI = *n2dI0 ; typesizeI = *typesizeI0 ;
57 n3dD = *n3dD0 ; n2dD = *n2dD0 ; typesizeD = *typesizeD0 ;
58 n3dL = *n3dL0 ; n2dL = *n2dL0 ; typesizeL = *typesizeL0 ;
59 me = *me0 ; np = *np0 ; np_x = *np_x0 ; np_y = *np_y0 ;
60 ids = *ids0-1 ; ide = *ide0-1 ; jds = *jds0-1 ; jde = *jde0-1 ; kds = *kds0-1 ; kde = *kde0-1 ;
61 ips = *ips0-1 ; ipe = *ipe0-1 ; jps = *jps0-1 ; jpe = *jpe0-1 ; kps = *kps0-1 ; kpe = *kpe0-1 ;
62
63 if ( nbytes == NULL ) nbytes = RSL_MALLOC ( int , np ) ;
64 if ( x_curs == NULL ) x_curs = RSL_MALLOC ( int , np ) ;
65 if ( x_peermask == NULL ) x_peermask = RSL_MALLOC ( int , np ) ;
66 if ( x_recv == NULL ) x_recv = RSL_MALLOC ( MPI_Request , np ) ;
67 if ( x_send == NULL ) x_send = RSL_MALLOC ( MPI_Request , np ) ;
68 for ( i = 0 ; i < np ; i++ ) { nbytes[i] = 0 ; x_curs[i] = 0 ; x_peermask[i] = 0 ; }
69
70 if ( xy == 1 ) { /* xy = 1, swap in X, otherwise Y */
71 n = (ide-ids+1)/4*2 ;
72 m = n*2 ;
73 ps = ips ;
74 pe = ipe ;
75 ops = jps ;
76 ope = jpe ;
77 } else {
78 n = (jde-jds+1)/4*2 ;
79 m = n*2 ;
80 ps = jps ;
81 pe = jpe ;
82 ops = ips ;
83 ope = ipe ;
84 }
85
86 for ( i = UP_ODD( ps ) ; i <= MIN(pe,m) ; i+=2 ) {
87 ii = abs(i+n) % m ;
88 if ( xy == 1 ) {
89 TASK_FOR_POINT ( &ii , &jps , &ids, &ide , &jds, &jde , &np_x , &np_y , &Px, &Py ) ;
90 coords[1] = Px ; coords[0] = Py ;
91 MPI_Cart_rank( *comm, coords, &P ) ;
92 } else {
93 TASK_FOR_POINT ( &ips , &ii , &ids, &ide , &jds, &jde , &np_x , &np_y , &Px, &Py ) ;
94 coords[1] = Px ; coords[0] = Py ;
95 MPI_Cart_rank( *comm, coords, &P ) ;
96 }
97 nbytes[P] += typesizeR*(ope-ops+1)*(n3dR*(kpe-kps+1)+n2dR) +
98 typesizeI*(ope-ops+1)*(n3dI*(kpe-kps+1)+n2dI) +
99 typesizeD*(ope-ops+1)*(n3dD*(kpe-kps+1)+n2dD) +
100 typesizeL*(ope-ops+1)*(n3dL*(kpe-kps+1)+n2dL) ;
101 x_peermask[P] = 1 ;
102 }
103
104 for ( P = 0 ; P < np ; P++ ) {
105 if ( x_peermask[P] ) {
106 buffer_for_proc ( P , nbytes[P], RSL_RECVBUF ) ;
107 buffer_for_proc ( P , nbytes[P], RSL_SENDBUF ) ;
108 }
109 }
110 }
111
112 RSL_LITE_PACK_SWAP ( int * Fcomm , char * buf , int * odd0 , int * typesize0 , int * xy0 , int * pu0 , char * memord , int * xstag0 ,
113 int *me0, int * np0 , int * np_x0 , int * np_y0 ,
114 int * ids0 , int * ide0 , int * jds0 , int * jde0 , int * kds0 , int * kde0 ,
115 int * ims0 , int * ime0 , int * jms0 , int * jme0 , int * kms0 , int * kme0 ,
116 int * ips0 , int * ipe0 , int * jps0 , int * jpe0 , int * kps0 , int * kpe0 )
117 {
118 int me, np, np_x, np_y ;
119 int odd , typesize ;
120 int ids , ide , jds , jde , kds , kde ;
121 int ims , ime , jms , jme , kms , kme ;
122 int ips , ipe , jps , jpe , kps , kpe ;
123 int xstag ; /* 0 not stag, 1 stag */
124 int xy ; /* y = 0 , x = 1 */
125 int pu ; /* pack = 0 , unpack = 1 */
126 int i, ii, j, jj, m, n ;
127 int ps, pe, ops, ope ;
128 register int k, t ;
129 #ifdef crayx1
130 register int i2,i3,i4,i_offset;
131 #endif
132 char *p ;
133 int da_buf ;
134 int Px, Py, P, coords[2] ;
135 int ierr = 0 ;
136 register int *pi, *qi ;
137 float f ;
138 MPI_Comm *comm, dummy_comm ;
139
140 comm = &dummy_comm ;
141 *comm = MPI_Comm_f2c( *Fcomm ) ;
142
143 me = *me0 ; np = *np0 ; np_x = *np_x0 ; np_y = *np_y0 ;
144 xstag = *xstag0 ;
145 odd = *odd0 ; typesize = *typesize0 ;
146 ids = *ids0-1 ; ide = *ide0-1 ; jds = *jds0-1 ; jde = *jde0-1 ; kds = *kds0-1 ; kde = *kde0-1 ;
147 ims = *ims0-1 ; ime = *ime0-1 ; jms = *jms0-1 ; jme = *jme0-1 ; kms = *kms0-1 ; kme = *kme0-1 ;
148 ips = *ips0-1 ; ipe = *ipe0-1 ; jps = *jps0-1 ; jpe = *jpe0-1 ; kps = *kps0-1 ; kpe = *kpe0-1 ;
149 xy = *xy0 ;
150 pu = *pu0 ;
151
152 /* need to adapt for other memory orders */
153 #define RANGE(S1,E1,S2,E2,S3,E3,S4,E4) (((E1)-(S1)+1)*((E2)-(S2)+1)*(((E3)-(S3)+1)/2)*((E4)-(S4)+1))
154 #define IMAX(A) (((A)>ids)?(A):ids)
155 #define IMIN(A) (((A)<ide)?(A):ide)
156 #define JMAX(A) (((A)>jds)?(A):jds)
157 #define JMIN(A) (((A)<jde)?(A):jde)
158
159 da_buf = ( pu == 0 ) ? RSL_SENDBUF : RSL_RECVBUF ;
160
161
162 if ( xy == 1 ) { /* xy = 1, swap in X, otherwise Y */
163 n = (ide-ids+1)/4*2 ;
164 m = n*2 ;
165 } else {
166 n = (jde-jds+1)/4*2 ;
167 m = n*2 ;
168 }
169
170 if ( np_x > 1 && xy == 1 ) {
171
172 for ( i = UP_ODD(ips) ; i <= MIN(ipe,m) ; i+=2 ) {
173 ii = abs(i+n) % m ;
174 TASK_FOR_POINT ( &ii , &jps , &ids, &ide , &jds, &jde , &np_x , &np_y , &Px, &Py ) ;
175 coords[1] = Px ; coords[0] = Py ;
176 MPI_Cart_rank( *comm, coords, &P ) ;
177 p = buffer_for_proc( P , 0 , da_buf ) ;
178 if ( pu == 0 ) {
179 if ( typesize == sizeof(int) ) {
180 for ( j = JMAX(jps) ; j <= JMIN(jpe) ; j++ ) {
181 for ( k = kps ; k <= kpe ; k++ ) {
182 pi = (int *)(p+x_curs[P]) ;
183 qi = (int *)((buf + typesize*( (i-ims) + (ime-ims+1)*(
184 (k-kms) + (j-jms)*(kme-kms+1))))) ;
185 *pi++ = *qi++ ;
186 x_curs[P] += typesize ;
187 }
188 }
189 }
190 else {
191 for ( j = JMAX(jps) ; j <= JMIN(jpe) ; j++ ) {
192 for ( k = kps ; k <= kpe ; k++ ) {
193 for ( t = 0 ; t < typesize ; t++ ) {
194 *(p+x_curs[P]) =
195 *(buf + t + typesize*(
196 (i-ims) + (ime-ims+1)*(
197 (k-kms) + (j-jms)*(kme-kms+1))) ) ;
198 x_curs[P]++ ;
199 }
200 }
201 }
202 }
203 } else {
204 if ( typesize == sizeof(int) ) {
205 for ( j = JMAX(jps) ; j <= JMIN(jpe) ; j++ ) {
206 for ( k = kps ; k <= kpe ; k++ ) {
207 pi = (int *)(p+x_curs[P]) ;
208 qi = (int *)((buf + typesize*( (i-ims) + (ime-ims+1)*(
209 (k-kms) + (j-jms)*(kme-kms+1))))) ;
210 *qi++ = *pi++ ;
211 x_curs[P] += typesize ;
212 }
213 }
214 }
215 else {
216 for ( j = JMAX(jps) ; j <= JMIN(jpe) ; j++ ) {
217 for ( k = kps ; k <= kpe ; k++ ) {
218 for ( t = 0 ; t < typesize ; t++ ) {
219 *(buf + t + typesize*(
220 (i-ims) + (ime-ims+1)*(
221 (k-kms) + (j-jms)*(kme-kms+1))) ) =
222 *(p+x_curs[P]) ;
223 x_curs[P]++ ;
224 }
225 }
226 }
227 }
228 }
229 }
230 } else if ( np_y > 1 && xy == 0 ) {
231 for ( j = UP_ODD(jps) ; j <= MIN(jpe,m) ; j+=2 ) {
232 jj = abs(j+n) % m ;
233 TASK_FOR_POINT ( &ips , &jj , &ids, &ide , &jds, &jde , &np_x , &np_y , &Px, &Py ) ;
234 coords[1] = Px ; coords[0] = Py ;
235 MPI_Cart_rank( *comm, coords, &P ) ;
236 p = buffer_for_proc( P , 0 , da_buf ) ;
237 if ( pu == 0 ) {
238 if ( typesize == sizeof(int) ) {
239 for ( i = IMAX(ips) ; i <= IMIN(ipe) ; i++ ) {
240 for ( k = kps ; k <= kpe ; k++ ) {
241 pi = (int *)(p+x_curs[P]) ;
242 qi = (int *)((buf + typesize*( (i-ims) + (ime-ims+1)*(
243 (k-kms) + (j-jms)*(kme-kms+1))))) ;
244 *pi++ = *qi++ ;
245 x_curs[P] += typesize ;
246 }
247 }
248 }
249 else {
250 for ( i = IMAX(ips) ; i <= IMIN(ipe) ; i++ ) {
251 for ( k = kps ; k <= kpe ; k++ ) {
252 for ( t = 0 ; t < typesize ; t++ ) {
253 *(p+x_curs[P]) =
254 *(buf + t + typesize*(
255 (i-ims) + (ime-ims+1)*(
256 (k-kms) + (j-jms)*(kme-kms+1))) ) ;
257 x_curs[P]++ ;
258 }
259 }
260 }
261 }
262 } else {
263 if ( typesize == sizeof(int) ) {
264 for ( i = IMAX(ips) ; i <= IMIN(ipe) ; i++ ) {
265 for ( k = kps ; k <= kpe ; k++ ) {
266 pi = (int *)(p+x_curs[P]) ;
267 qi = (int *)((buf + typesize*( (i-ims) + (ime-ims+1)*(
268 (k-kms) + (j-jms)*(kme-kms+1))))) ;
269 *qi++ = *pi++ ;
270 x_curs[P] += typesize ;
271 }
272 }
273 }
274 else {
275 for ( i = IMAX(ips) ; i <= IMIN(ipe) ; i++ ) {
276 for ( k = kps ; k <= kpe ; k++ ) {
277 for ( t = 0 ; t < typesize ; t++ ) {
278 *(buf + t + typesize*(
279 (i-ims) + (ime-ims+1)*(
280 (k-kms) + (j-jms)*(kme-kms+1))) ) =
281 *(p+x_curs[P]) ;
282 x_curs[P]++ ;
283 }
284 }
285 }
286 }
287 }
288 }
289 }
290 }
291
292 RSL_LITE_SWAP ( int * Fcomm0, int *me0, int * np0 , int * np_x0 , int * np_y0 )
293 {
294 int me, np, np_x, np_y ;
295 int yp, ym, xp, xm, nb ;
296 MPI_Status stat ;
297 MPI_Comm comm, *comm0, dummy_comm ;
298 int i, P ;
299
300 comm0 = &dummy_comm ;
301 *comm0 = MPI_Comm_f2c( *Fcomm0 ) ;
302 #if 1
303
304 comm = *comm0 ; me = *me0 ; np = *np0 ; np_x = *np_x0 ; np_y = *np_y0 ;
305
306 /* fprintf(stderr,"RSL_LITE_SWAP\n") ; */
307
308 for ( P = 0 ; P < np ; P++ ) {
309 if ( x_peermask[P] ) {
310 nb = buffer_size_for_proc( P, RSL_RECVBUF ) ;
311 /* fprintf(stderr,"posting irecv from %d, nb = %d\n",P,nb) ; */
312 MPI_Irecv ( buffer_for_proc( P, x_curs[P], RSL_RECVBUF ), nb, MPI_CHAR, P, me, comm, &(x_recv[P]) ) ;
313 /* fprintf(stderr,"sending to %d, nb = %d\n",P,x_curs[P]) ; */
314 MPI_Isend ( buffer_for_proc( P, 0, RSL_SENDBUF ), x_curs[P], MPI_CHAR, P, P, comm, &(x_send[P]) ) ;
315 }
316 }
317 for ( P = 0 ; P < np ; P++ ) {
318 if ( x_peermask[P] ) {
319 MPI_Wait( &x_recv[P], &stat ) ;
320 MPI_Wait( &x_send[P], &stat ) ;
321 }
322 }
323 #else
324 # ifndef MS_SUA
325 fprintf(stderr,"RSL_LITE_SWAP disabled\n") ;
326 # endif
327 #endif
328 for ( i = 0 ; i < np ; i++ ) { x_curs[i] = 0 ; }
329 }
330