Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include "parlib.h"
/*
* Error codes:
* 0 - success
* 1 - nonpositive number of dimensions
* 2 - wrong communicated dimension
* 3 - negative boundary width
* 4 - nonpositive dimension
* 5 - boundary width exceeds the array block length
*/
int P_BExchange_init ( ndims, stride, blklen, bdim, overlap, datatype,
comm, period, bexchange )
int ndims, *stride, *blklen, bdim, overlap[2], period;
MPI_Datatype datatype;
MPI_Comm comm;
BExchange *bexchange;
{
int nproc, iproc, direct, idim, sendproc[2], recvproc[2];
int count, strd, sbind[2], rbind[2], send[2], recv[2];
MPI_Aint fsize;
MPI_Datatype oldtype, btype[2];
MPI_Request sreq[2], rreq[2];
/*
* Check input parameters
*/
if ( ndims < 1 ) {return 1;}
if ( bdim < 1 || bdim > ndims ) {return 2;}
if ( overlap[0] == 0 && overlap[1] == 0 ) {return 0;} /* success */
for ( idim = 0; idim < ndims; idim++ ) {
if ( stride[idim] <= 0 ) {return 4;}
}
for ( direct = 0; direct < 2; direct++ ) {
if ( overlap[direct] < 0 ) {return 3;}
if ( overlap[direct] > blklen[bdim-1] ) {return 5;}
}
/*
* Define the number of processors in the group and the rank
*/
MPI_Comm_size ( comm, &nproc );
if ( nproc == 0 ) {return 0;} /* success */
MPI_Comm_rank ( comm, &iproc );
if ( iproc == MPI_UNDEFINED ) {return 0;} /* the process does not belong to the group */
sendproc[0] = ( iproc == 0 ? nproc-1 : iproc-1 );
recvproc[0] = ( iproc == nproc-1 ? 0 : iproc+1 );
sendproc[1] = recvproc[0];
recvproc[1] = sendproc[0];
send[0] = iproc > 0 || period;
recv[0] = iproc < nproc-1 || period;
send[1] = recv[0];
recv[1] = send[0];
MPI_Type_extent ( datatype, &fsize );
/*
* Define data types for the boundaries
*/
for ( direct = 0; direct < 2; direct++ ) {
if ( overlap[direct] > 0 ) {
oldtype = datatype;
strd = 1;
for ( idim = 0; idim < ndims; idim++ ) {
if ( idim+1 == bdim ) {
count = overlap[direct];
} else {
count = blklen[idim];
}
MPI_Type_hvector ( count, 1, strd * fsize, oldtype,
&btype[direct] );
if ( idim > 0 ) {
MPI_Type_free ( &oldtype );
}
oldtype = btype[direct];
strd = strd * stride[idim];
}
MPI_Type_commit ( &btype[direct] );
}
}
/*
* Determine the begining of boundaries
*/
strd = 1;
for ( idim = 0; idim < bdim - 1; idim++ ) {
strd = strd * stride[idim];
}
sbind[0] = 0;
rbind[0] = blklen[bdim-1]*strd;
sbind[1] = (blklen[bdim-1]-overlap[1])*strd;
rbind[1] = -overlap[1]*strd;
for ( direct = 0; direct < 2; direct++ ) {
bexchange->overlap[direct] = overlap[direct];
bexchange->send[direct] = send[direct];
bexchange->recv[direct] = recv[direct];
bexchange->btype[direct] = btype[direct];
bexchange->sendproc[direct] = sendproc[direct];
bexchange->recvproc[direct] = recvproc[direct];
bexchange->sbind[direct] = sbind[direct];
bexchange->rbind[direct] = rbind[direct];
}
bexchange->comm = comm;
bexchange->fsize = fsize;
return 0;
}
int P_BExchange_start ( a, bexchange )
void *a;
BExchange *bexchange;
{
int direct, overlap[2], send[2], recv[2], btype[2];
int sendproc[2], recvproc[2], sbind[2], rbind[2];
MPI_Comm comm;
MPI_Request sreq[2], rreq[2];
MPI_Aint fsize;
char *ach = (char *) a;
for ( direct = 0; direct < 2; direct++ ) {
overlap[direct] = bexchange->overlap[direct];
send[direct] = bexchange->send[direct];
recv[direct] = bexchange->recv[direct];
btype[direct] = bexchange->btype[direct];
sendproc[direct] = bexchange->sendproc[direct];
recvproc[direct] = bexchange->recvproc[direct];
sbind[direct] = bexchange->sbind[direct];
rbind[direct] = bexchange->rbind[direct];
}
comm = bexchange->comm;
fsize = bexchange->fsize;
for ( direct = 0; direct < 2; direct++ ) {
if ( overlap[direct] > 0 ) {
if ( send[direct] ) {
MPI_Isend ( ach+sbind[direct]*fsize, 1, btype[direct],
sendproc[direct], 0, comm, &sreq[direct] );
}
if ( recv[direct] ) {
MPI_Irecv ( ach+rbind[direct]*fsize, 1, btype[direct],
recvproc[direct], 0, comm, &rreq[direct] );
}
}
}
for ( direct = 0; direct < 2; direct++ ) {
bexchange->sreq[direct]=sreq[direct];
bexchange->rreq[direct]=rreq[direct];
}
return 0;
}
int P_BExchange_end ( bexchange )
BExchange *bexchange;
{
MPI_Status status;
int direct, overlap[2], send[2], recv[2];
MPI_Request sreq[2], rreq[2];
for ( direct = 0; direct < 2; direct++ ) {
overlap[direct] = bexchange->overlap[direct];
send[direct] = bexchange->send[direct];
recv[direct] = bexchange->recv[direct];
sreq[direct] = bexchange->sreq[direct];
rreq[direct] = bexchange->rreq[direct];
}
for ( direct = 0; direct < 2; direct++ ) {
if ( overlap[direct] > 0 ) {
if ( send[direct] ) {
MPI_Wait ( &sreq[direct], &status );
}
if ( recv[direct] ) {
MPI_Wait ( &rreq[direct], &status );
}
}
}
return 0;
}
int P_BExchange_free ( bexchange )
BExchange *bexchange;
{
int direct, overlap[2];
MPI_Datatype btype[2];
for ( direct = 0; direct < 2; direct++ ) {
overlap[direct] = bexchange->overlap[direct];
btype[direct] = bexchange->btype[direct];
}
for ( direct = 0; direct < 2; direct++ ) {
if ( overlap[direct] > 0 ) {
MPI_Type_free ( &btype[direct] );
}
}
return 0;
}
int P_BExchange ( a, ndims, stride, blklen, bdim, overlap, datatype,
comm, period )
void *a;
MPI_Datatype datatype;
int ndims, *stride, *blklen, bdim, overlap[2];
int period;
MPI_Comm comm;
{
BExchange bexchange;
int ierr;
if ( ierr = P_BExchange_init ( ndims, stride, blklen, bdim, overlap,
datatype, comm, period, &bexchange ) != 0 ) { return ierr; }
P_BExchange_start ( a, &bexchange );
P_BExchange_end ( &bexchange );
P_BExchange_free ( &bexchange );
return 0;
}