-
Notifications
You must be signed in to change notification settings - Fork 0
/
mpiPart.cc
274 lines (219 loc) · 7.59 KB
/
mpiPart.cc
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#include <map>
#include <unistd.h>
#include <cstdarg>
#include <omp.h>
#include <stdlib.h>
#include <mpi.h>
#include <assert.h>
#include <stdio.h>
#define PRINTF(args,...) printf( args, ##__VA_ARGS__ )
//#define PRINTF(args,...)
static inline int getMyIndex()
{
return omp_get_thread_num();
}
static inline void createComm( MPI_Comm comm, int n, const int ranks[], MPI_Comm* newComm )
{
int rc;
MPI_Group groupWorld, newGroup;
MPI_Comm_group( comm, &groupWorld);
MPI_Group_incl( groupWorld, n, ranks, &newGroup );
rc = MPI_Comm_create( comm, newGroup, newComm );
assert( rc == MPI_SUCCESS );
int rank,size;
int worldRank;
rc = MPI_Comm_rank( comm, &worldRank );
rc = MPI_Comm_rank( *newComm, &rank );
rc = MPI_Comm_size( *newComm, &size );
PRINTF("%s():%d: worldRank=%d rank=%d size=%d\n",__func__,__LINE__,worldRank,rank,size);
}
struct Info {
MPI_Win win;
const void* buf;
int count;
int chunkSize;
int leftover;
MPI_Datatype datatype;
int tag;
int myRank;
int numThreads;
int numSent;
MPI_Request request;
MPI_Comm comm;
int isSend;
};
#ifdef MPICH
std::map<MPI_Request*,Info*> _infoMap;
static inline void setRequest( MPI_Request* req, Info* info ) {
_infoMap[req] = info;
}
static inline Info* getInfo( MPI_Request* req ) {
return _infoMap[req];
}
static inline void clearReq( MPI_Request* req ) {
_infoMap.erase( req );
}
#else
static inline void setRequest( MPI_Request* req, Info* info ) {
*req = (struct ompi_request_t*)info;
}
static inline Info* getInfo( MPI_Request* req ) {
return (Info*)*req;
}
static inline void clearReq( MPI_Request* req ) {
}
#endif
int MPI_Partitioned_Send_create(const void *buf, int count, MPI_Datatype datatype,
int numThreads, int dest, int tag, MPI_Comm comm,
MPI_Request *request)
{
int rc;
int dataTypeSize;
struct Info* info = (struct Info*) malloc( sizeof (struct Info ) );
rc = MPI_Comm_rank( MPI_COMM_WORLD, &info->myRank );
MPI_Type_size(datatype,&dataTypeSize);
info->chunkSize = count/numThreads;
info->leftover = count - numThreads*(info->chunkSize);
PRINTF("%s(): rank=%d numThreads=%d count=%d chunkSize=%d lefotver=%d enter \n",
__func__,info->myRank,numThreads,count,info->chunkSize,info->leftover);
setRequest( request, info );
info->buf = buf;
info->count = count;
info->datatype = datatype;
info->tag = tag;
info->numThreads = numThreads;
info->numSent = 0;
info->isSend = 1;
int ranks[2] = { info->myRank, dest };
createComm( comm, 2, ranks, &info->comm );
rc = MPI_Win_create( NULL, 0, dataTypeSize, MPI_INFO_NULL, info->comm, &info->win );
assert( rc == MPI_SUCCESS );
//assert( 0 == (dataTypeSize * count) % numThreads );
PRINTF("%s():%d: rank=%d leave\n",__func__,__LINE__,info->myRank);
return MPI_SUCCESS;
}
int MPI_Partitioned_free( MPI_Request* request )
{
struct Info* info = getInfo(request);
clearReq(request);
PRINTF("%s():%d: rank=%d leave\n",__func__,__LINE__,info->myRank);
int rc = MPI_Win_free( &info->win );
PRINTF("%s():%d: rank=%d leave\n",__func__,__LINE__,info->myRank);
assert( rc == MPI_SUCCESS );
free( info );
return MPI_SUCCESS;
}
int MPI_Partitioned_Recv_create(void *buf, int count, MPI_Datatype datatype, int src,
int tag, MPI_Comm comm, MPI_Request *request)
{
int rc;
int dataTypeSize;
struct Info* info = (struct Info*) malloc( sizeof (struct Info ) );
MPI_Type_size(datatype,&dataTypeSize);
rc = MPI_Comm_rank( MPI_COMM_WORLD, &info->myRank );
PRINTF("%s():%d: rank=%d count=%d dataTypeSize=%d enter \n",
__func__,__LINE__,info->myRank,count,dataTypeSize);
setRequest( request, info );
info->buf = buf;
info->count = count;
info->datatype = datatype;
info->tag = tag;
info->isSend = 0;
int ranks[2] = { src, info->myRank };
createComm( comm, 2, ranks, &info->comm );
rc = MPI_Win_create( (void*) buf, count* dataTypeSize, dataTypeSize, MPI_INFO_NULL,
info->comm, &info->win );
assert( rc == MPI_SUCCESS );
PRINTF("%s():%d: rank=%d leave\n",__func__,__LINE__,info->myRank);
return MPI_SUCCESS;
}
int MPI_Partitioned_Add_to_buffer( MPI_Request* request, const void* send_buf,
int count, MPI_Datatype datatype )
{
int numSent;
int rc;
struct Info* info = getInfo(request);
int index = getMyIndex();
MPI_Aint displacement = index * info->chunkSize;
PRINTF("%s():%d: rank=%d tid=%d count=%d displacement=%lu\n",
__func__,__LINE__,info->myRank, index, count, displacement);
rc = MPI_Put( send_buf, count, datatype, 1, displacement, count, datatype, info->win );
assert( rc == MPI_SUCCESS );
#pragma omp critical
{
numSent = ++info->numSent;
}
PRINTF("%s():%d: rank=%d tid=%d numSent=%d numThreads=%d\n",
__func__,__LINE__,info->myRank, index, info->numSent, info->numThreads);
if ( info->numThreads == numSent ) {
PRINTF("%s():%d: rank=%d done\n",__func__,__LINE__,info->myRank);
rc = MPI_Win_unlock_all( info->win );
assert( rc == MPI_SUCCESS );
rc = MPI_Send( NULL, 0, MPI_CHAR, 1, info->tag, info->comm );
assert( rc == MPI_SUCCESS );
}
PRINTF("%s():%d: rank=%d return\n",__func__,__LINE__,info->myRank);
return MPI_SUCCESS;
}
int MPI_Partitioned_Add_to_buffer_a( MPI_Request* request, const void* send_buf,
int count, MPI_Datatype datatype )
{
int numSent;
int rc;
struct Info* info = getInfo(request);
int index = getMyIndex();
MPI_Aint displacement = index * info->chunkSize;
PRINTF("%s():%d: rank=%d tid=%d count=%d displacement=%lu\n",
__func__,__LINE__,info->myRank, index, count, displacement);
rc = MPI_Put( send_buf, count, datatype, 1, displacement, count, datatype, info->win );
assert( rc == MPI_SUCCESS );
#pragma omp critical
{
numSent = ++info->numSent;
}
PRINTF("%s():%d: rank=%d tid=%d numSent=%d numThreads=%d\n",
__func__,__LINE__,info->myRank, index, info->numSent, info->numThreads);
if ( info->numThreads == numSent ) {
PRINTF("%s():%d: rank=%d done\n",__func__,__LINE__,info->myRank);
if(info->leftover){
rc = MPI_Put( send_buf, info->leftover, datatype, 1, info->chunkSize*info->numThreads,
info->leftover, datatype, info->win );
assert( rc == MPI_SUCCESS );
PRINTF("tid=%d sent leftover data of size %d to location %d\n",
index,info->leftover,info->chunkSize*info->numThreads);
}
rc = MPI_Win_unlock_all( info->win );
assert( rc == MPI_SUCCESS );
rc = MPI_Send( NULL, 0, MPI_CHAR, 1, info->tag, info->comm );
assert( rc == MPI_SUCCESS );
}
PRINTF("%s():%d: rank=%d return\n",__func__,__LINE__,info->myRank);
return MPI_SUCCESS;
}
int MPI_Start_part( MPI_Request* req )
{
int rc;
struct Info* info = getInfo(req);
PRINTF("%s():%d: rank=%d %d %s\n",
__func__,__LINE__,info->myRank, getMyIndex(), info->isSend ? "Send":"Recv");
if ( info->isSend ) {
rc = MPI_Win_lock_all( 0, info->win );
assert( rc == MPI_SUCCESS );
info->numSent = 0;
}
return MPI_SUCCESS;
}
int MPI_Wait_part( MPI_Request* req, MPI_Status* status )
{
int rc;
struct Info* info = getInfo(req);
PRINTF("%s():%d: rank=%d %d enter %s\n",
__func__,__LINE__,info->myRank, getMyIndex(), info->isSend ? "Send":"Recv" );
if ( ! info->isSend ) {
rc = MPI_Recv( NULL, 0, MPI_CHAR, 0, info->tag, info->comm, MPI_STATUS_IGNORE );
assert( rc == MPI_SUCCESS );
}
PRINTF("%s():%d: rank=%d %d leave %s\n",
__func__,__LINE__,info->myRank, getMyIndex(), info->isSend ? "Send":"Recv" );
return MPI_SUCCESS;
}