forked from gtfock-chem/GTMatrix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGTMatrix_Get.c
310 lines (282 loc) · 11.3 KB
/
GTMatrix_Get.c
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <mpi.h>
#include "GTMatrix_Retval.h"
#include "GTMatrix_Typedef.h"
#include "GTMatrix_Get.h"
#include "GTMatrix_Other.h"
#include "utils.h"
// Post the operation of getting a blocking from a process using MPI_Get
// The get operation is not complete when this function returns
// Input parameters:
// gtm : GTMatrix handle
// dst_rank : Target process
// row_start : 1st row of the required block
// row_num : Number of rows the required block has
// col_start : 1st column of the required block
// col_num : Number of columns the required block has
// src_buf_ld : Leading dimension of the received buffer
// Output parameter:
// *src_buf : Receive buffer
int GTM_getBlockFromProcess(
GTMatrix_t gtm, int dst_rank,
int row_start, int row_num,
int col_start, int col_num,
void *src_buf, int src_buf_ld
)
{
if (gtm == NULL) return GTM_NULL_PTR;
int row_end = row_start + row_num;
int col_end = col_start + col_num;
int dst_rowblk = dst_rank / gtm->c_blocks;
int dst_colblk = dst_rank % gtm->c_blocks;
int dst_blk_ld = gtm->ld_local; // gtm->ld_blks[dst_rank];
int dst_row_start = gtm->r_displs[dst_rowblk];
int dst_col_start = gtm->c_displs[dst_colblk];
int dst_row_end = gtm->r_displs[dst_rowblk + 1];
int dst_col_end = gtm->c_displs[dst_colblk + 1];
// Sanity check
if ((row_start < dst_row_start) ||
(col_start < dst_col_start) ||
(row_end > dst_row_end) ||
(col_end > dst_col_end) ||
(row_num * col_num == 0)) return GTM_INVALID_BLOCK;
// Check if the target process is in the shared memory communicator
int shm_rank = getElementIndexInArray(dst_rank, gtm->shm_global_ranks, gtm->shm_size);
void *shm_ptr = (shm_rank == -1) ? NULL : gtm->shm_mat_blocks[shm_rank];
size_t row_msize = (size_t)col_num * (size_t)gtm->unit_size;
int dst_pos = (row_start - dst_row_start) * dst_blk_ld;
dst_pos += col_start - dst_col_start;
if (shm_rank != -1)
{
// Target process and current process is in same node, use memcpy
char *src_ptr = (char*) src_buf;
char *dst_ptr = (char*) shm_ptr + dst_pos * gtm->unit_size;
int src_ptr_ld = src_buf_ld * gtm->unit_size;
int dst_ptr_ld = dst_blk_ld * gtm->unit_size;
for (int irow = 0; irow < row_num; irow++)
{
memcpy(src_ptr, dst_ptr, row_msize);
src_ptr += src_ptr_ld;
dst_ptr += dst_ptr_ld;
}
} else {
// Target process and current process isn't in same node, use MPI_Get
if (row_num <= MPI_DT_SB_DIM_MAX && col_num <= MPI_DT_SB_DIM_MAX)
{
// Block is small, use predefined data type or define a new
// data type to reduce MPI_Get overhead
int block_dt_id = (row_num - 1) * MPI_DT_SB_DIM_MAX + (col_num - 1);
MPI_Datatype *dst_dt = >m->sb_stride[block_dt_id];
if (col_num == src_buf_ld)
{
MPI_Datatype *rcv_dt_ns = >m->sb_nostride[block_dt_id];
MPI_Get(src_buf, 1, *rcv_dt_ns, dst_rank, dst_pos, 1, *dst_dt, gtm->mpi_win);
} else {
if (gtm->ld_local == src_buf_ld)
{
MPI_Get(src_buf, 1, *dst_dt, dst_rank, dst_pos, 1, *dst_dt, gtm->mpi_win);
} else {
MPI_Datatype rcv_dt;
MPI_Type_vector(row_num, col_num, src_buf_ld, gtm->datatype, &rcv_dt);
MPI_Type_commit(&rcv_dt);
MPI_Get(src_buf, 1, rcv_dt, dst_rank, dst_pos, 1, *dst_dt, gtm->mpi_win);
MPI_Type_free(&rcv_dt);
}
}
} else {
// Define a MPI data type to reduce number of request
MPI_Datatype dst_dt, rcv_dt;
MPI_Type_vector(row_num, col_num, dst_blk_ld, gtm->datatype, &dst_dt);
MPI_Type_vector(row_num, col_num, src_buf_ld, gtm->datatype, &rcv_dt);
MPI_Type_commit(&dst_dt);
MPI_Type_commit(&rcv_dt);
MPI_Get(src_buf, 1, rcv_dt, dst_rank, dst_pos, 1, dst_dt, gtm->mpi_win);
MPI_Type_free(&dst_dt);
MPI_Type_free(&rcv_dt);
}
}
return GTM_SUCCESS;
}
// Get a block from all related processes using MPI_Get
// Non-blocking, data may not be ready before synchronization
// This call is not collective, thread-safe
// Input paramaters:
// gtm : GTMatrix handle
// row_start : 1st row of the required block
// row_num : Number of rows the required block has
// col_start : 1st column of the required block
// col_num : Number of columns the required block has
// src_buf_ld : Leading dimension of the received buffer
// access_mode : Access mode, see GTMatrix_Typedef.h
// Output parameter:
// *src_buf : Receive buffer
int GTM_getBlock_(GTM_PARAM, int access_mode)
{
if (gtm == NULL) return GTM_NULL_PTR;
// Sanity check
if ((row_start < 0) || (col_start < 0) ||
(row_start + row_num > gtm->nrows) ||
(col_start + col_num > gtm->ncols) ||
(row_num * col_num == 0)) return GTM_INVALID_BLOCK;
// Find the processes that contain the requested block
// No need to initialize, just to avoid compiler warning
int s_blk_r = 0, e_blk_r = -1, s_blk_c = 0, e_blk_c = -1;
int row_end = row_start + row_num - 1;
int col_end = col_start + col_num - 1;
for (int i = 0; i < gtm->r_blocks; i++)
{
if ((gtm->r_displs[i] <= row_start) &&
(row_start < gtm->r_displs[i+1])) s_blk_r = i;
if ((gtm->r_displs[i] <= row_end) &&
(row_end < gtm->r_displs[i+1])) e_blk_r = i;
}
for (int i = 0; i < gtm->c_blocks; i++)
{
if ((gtm->c_displs[i] <= col_start) &&
(col_start < gtm->c_displs[i+1])) s_blk_c = i;
if ((gtm->c_displs[i] <= col_end) &&
(col_end < gtm->c_displs[i+1])) e_blk_c = i;
}
// Fetch data from each process
int blk_r_s, blk_r_e, blk_c_s, blk_c_e, need_to_fetch;
for (int blk_r = s_blk_r; blk_r <= e_blk_r; blk_r++) // Notice: <=
{
int dst_r_s = gtm->r_displs[blk_r];
int dst_r_e = gtm->r_displs[blk_r + 1] - 1;
for (int blk_c = s_blk_c; blk_c <= e_blk_c; blk_c++) // Notice: <=
{
int dst_c_s = gtm->c_displs[blk_c];
int dst_c_e = gtm->c_displs[blk_c + 1] - 1;
int dst_rank = blk_r * gtm->c_blocks + blk_c;
getRectIntersection(
dst_r_s, dst_r_e, dst_c_s, dst_c_e,
row_start, row_end, col_start, col_end,
&need_to_fetch, &blk_r_s, &blk_r_e, &blk_c_s, &blk_c_e
);
assert(need_to_fetch == 1);
int blk_r_num = blk_r_e - blk_r_s + 1;
int blk_c_num = blk_c_e - blk_c_s + 1;
int row_dist = blk_r_s - row_start;
int col_dist = blk_c_s - col_start;
char *blk_ptr = (char*) src_buf;
blk_ptr += (row_dist * src_buf_ld + col_dist) * gtm->unit_size;
int ret = GTM_SUCCESS;
if (access_mode == BLOCKING_ACCESS)
{
MPI_Win_lock(MPI_LOCK_SHARED, dst_rank, 0, gtm->mpi_win);
ret = GTM_getBlockFromProcess(
gtm, dst_rank, blk_r_s, blk_r_num,
blk_c_s, blk_c_num, blk_ptr, src_buf_ld
);
MPI_Win_unlock(dst_rank, gtm->mpi_win);
}
if (access_mode == NONBLOCKING_ACCESS)
{
if (gtm->nb_op_proc_cnt[dst_rank] == 0)
MPI_Win_lock(MPI_LOCK_SHARED, dst_rank, 0, gtm->mpi_win);
ret = GTM_getBlockFromProcess(
gtm, dst_rank, blk_r_s, blk_r_num,
blk_c_s, blk_c_num, blk_ptr, src_buf_ld
);
gtm->nb_op_proc_cnt[dst_rank]++;
gtm->nb_op_cnt++;
if (gtm->nb_op_cnt >= gtm->max_nb_get)
GTM_waitNB(gtm);
}
if (access_mode == BATCH_ACCESS)
{
GTM_Req_Vector_t req_vec = gtm->req_vec[dst_rank];
ret = GTM_pushToReqVector(
req_vec, MPI_NO_OP, blk_r_s, blk_r_num,
blk_c_s, blk_c_num, blk_ptr, src_buf_ld
);
}
if (ret != GTM_SUCCESS) return ret;
}
}
return GTM_SUCCESS;
}
// Get a block from the global matrix
int GTM_getBlock(GTM_PARAM)
{
if (gtm == NULL) return GTM_NULL_PTR;
return GTM_getBlock_(
gtm,
row_start, row_num,
col_start, col_num,
src_buf, src_buf_ld, BLOCKING_ACCESS
);
}
// Nonblocking get a block from the global matrix
int GTM_getBlockNB(GTM_PARAM)
{
if (gtm == NULL) return GTM_NULL_PTR;
return GTM_getBlock_(
gtm, row_start, row_num, col_start, col_num,
src_buf, src_buf_ld, NONBLOCKING_ACCESS
);
}
// Add a request to get a block from the global matrix
int GTM_addGetBlockRequest(GTM_PARAM)
{
if (gtm == NULL) return GTM_NULL_PTR;
return GTM_getBlock_(
gtm, row_start, row_num, col_start, col_num,
src_buf, src_buf_ld, BATCH_ACCESS
);
}
// Start a batch get epoch and allow to submit get requests
int GTM_startBatchGet(GTMatrix_t gtm)
{
if (gtm == NULL) return GTM_NULL_PTR;
if (gtm->in_batch_put) return GTM_IN_BATCHED_PUT;
if (gtm->in_batch_get) return GTM_IN_BATCHED_GET;
if (gtm->in_batch_acc) return GTM_IN_BATCHED_ACC;
for (int i = 0; i < gtm->comm_size; i++)
GTM_resetReqVector(gtm->req_vec[i]);
gtm->in_batch_get = 1;
return GTM_SUCCESS;
}
// Execute all get requests in the queues
int GTM_execBatchGet(GTMatrix_t gtm)
{
if (gtm == NULL) return GTM_NULL_PTR;
if (gtm->in_batch_get == 0) return GTM_NO_BATCHED_GET;
for (int _dst_rank = gtm->my_rank; _dst_rank < gtm->comm_size + gtm->my_rank; _dst_rank++)
{
int dst_rank = _dst_rank % gtm->comm_size;
GTM_Req_Vector_t req_vec = gtm->req_vec[dst_rank];
if (req_vec->curr_size > 0)
{
MPI_Win_lock(MPI_LOCK_SHARED, dst_rank, 0, gtm->mpi_win);
for (int i = 0; i < req_vec->curr_size; i++)
{
int blk_r_s = req_vec->row_starts[i];
int blk_r_num = req_vec->row_nums[i];
int blk_c_s = req_vec->col_starts[i];
int blk_c_num = req_vec->col_nums[i];
void *blk_ptr = req_vec->src_bufs[i];
int src_buf_ld = req_vec->src_buf_lds[i];
int ret = GTM_getBlockFromProcess(
gtm, dst_rank, blk_r_s, blk_r_num,
blk_c_s, blk_c_num, blk_ptr, src_buf_ld
);
if (ret != GTM_SUCCESS) return ret;
}
MPI_Win_unlock(dst_rank, gtm->mpi_win);
}
GTM_resetReqVector(req_vec);
}
return GTM_SUCCESS;
}
// Stop a batch get epoch and disallow to submit get requests
int GTM_stopBatchGet(GTMatrix_t gtm)
{
if (gtm == NULL) return GTM_NULL_PTR;
if (gtm->in_batch_get == 0) return GTM_NO_BATCHED_GET;
gtm->in_batch_get = 0;
return GTM_SUCCESS;
}