forked from pc2/SubmatrixMethod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mpi-matrix-inv.c
454 lines (395 loc) · 15.7 KB
/
mpi-matrix-inv.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/*
* MIT License
*
* Copyright (c) 2018 Paderborn Center for Parallel Computing
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <fcntl.h>
#include <mkl.h>
#include <mpi.h>
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <unistd.h>
#define PATHLEN 255
struct properties {
int size;
int density;
int condition;
};
MKL_INT find_elem(MKL_INT needle, MKL_INT *haystack, MKL_INT size) {
MKL_INT l, r, c;
l = 0;
r = size;
do {
c = (l + r) / 2;
if (haystack[c] == needle) {
return c;
}
if (haystack[c] < needle) {
l = c + 1;
} else {
r = c;
}
} while (l != r);
return -1;
}
lapack_int invert_matrix(double *matrix, lapack_int size) {
// First we need to compute the LU factorization using ?getrf
lapack_int *ipiv, ret;
ipiv = (lapack_int *)mkl_calloc(size, sizeof(lapack_int), 64);
ret = LAPACKE_dgetrf(LAPACK_COL_MAJOR, size, size, matrix, size, ipiv);
if (ret) {
mkl_free(ipiv);
return ret;
}
// And now we calculate the inverse using the LU factorization
ret = LAPACKE_dgetri(LAPACK_COL_MAJOR, size, matrix, size, ipiv);
mkl_free(ipiv);
return ret;
}
void print_matrix(double *matrix, MKL_INT size) {
MKL_INT i, j;
for (i = 0; i < size; i++) {
for (j = 0; j < size; j++) {
printf("%.2f\t", matrix[i * size + j]);
}
printf("\n");
}
printf("\n");
}
void invert_submatrix(double *values, MKL_INT *row_ind, MKL_INT *col_ptr,
double *values_inv, int i, double *locDurBuild,
double *locDurCalc) {
MKL_INT nnz, k, l, kcal, lcal, idx;
lapack_int ret;
double *submatrix;
double tStart, tEnd;
// 代表了第i列非零元的数量,同样就等于第i个子矩阵的维数
nnz = col_ptr[i + 1] - col_ptr[i];
submatrix = (double *)mkl_calloc(nnz * nnz, sizeof(double), 64);
tStart = omp_get_wtime();
for (k = 0; k < nnz; k++) {
for (l = 0; l < nnz; l++) {
// kcal、lcal是原始矩阵的行列坐标
kcal = row_ind[col_ptr[i] + k];
lcal = row_ind[col_ptr[i] + l];
// We now have to copy M[kcal][lcal] to submatrix[k][l]
// How to access M[kcal][lcal]? Calculate idx
// find_elem是从原始矩阵的第lcal列中找到第kcal行的非零值,由于只有CSC格式,所以得用二分查找
idx = find_elem(kcal, &(row_ind[col_ptr[lcal]]),
col_ptr[lcal + 1] - col_ptr[lcal]);
if (idx != -1) {
submatrix[k * nnz + l] = values[col_ptr[lcal] + idx];
}
}
}
tEnd = omp_get_wtime();
*locDurBuild = (tEnd - tStart);
tStart = omp_get_wtime();
ret = invert_matrix(submatrix, nnz);
tEnd = omp_get_wtime();
*locDurCalc = (tEnd - tStart);
if (ret) {
fprintf(stderr, "Inverting submatrix failed\n");
}
// 大矩阵和子矩阵都是对称矩阵,所以读取某一行就是在读取对应列
// tStart = omp_get_wtime();
memcpy(values_inv,
&(submatrix[find_elem(i, &(row_ind[col_ptr[i]]), nnz) * nnz]),
nnz * sizeof(double));
// tEnd = omp_get_wtime();
// *locDurBuild += (tEnd - tStart);
mkl_free(submatrix);
}
int main(int argc, char *argv[]) {
int threadsupport;
MPI_Init_thread(NULL, NULL, MPI_THREAD_FUNNELED, &threadsupport);
if (threadsupport < MPI_THREAD_FUNNELED) {
fprintf(stderr, "Could not initialize thread support.");
omp_set_num_threads(1);
}
struct properties prop;
int fd, world_rank, world_size, *displs, *recvcounts, mkl_threads;
char fn_in_val[PATHLEN], fn_in_ri[PATHLEN], fn_in_cp[PATHLEN],
fn_out_val[PATHLEN];
MKL_INT *col_ptr, *row_ind, total_nnz, i, submatrices_per_worker, total_elem,
my_first_col, next_first_col, submatrices_for_me;
double *values, *values_inv, tStart, tEnd;
FILE *fp;
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
MPI_Comm_size(MPI_COMM_WORLD, &world_size);
// printf("%d: I'm alive\n", world_rank);
if (world_rank == 0) {
/**************
* MPI Rank 0 *
**************/
// We are the boot process. Decide on what to do, broadcast and gather.
if (argc != 4) {
fprintf(
stderr,
"%d: Main process needs to be called with parameters size density "
"condition\n",
world_rank);
// printf("%d: Shutting down workers...\n", world_rank);
prop.size = 0;
MPI_Bcast(&prop, 3, MPI_INT, 0, MPI_COMM_WORLD);
exit(EXIT_FAILURE);
}
prop.size = strtol(argv[1], NULL, 10);
prop.density = strtol(argv[2], NULL, 10);
prop.condition = strtol(argv[3], NULL, 10);
submatrices_per_worker = prop.size / (world_size - 1);
printf("%d: Each of the %d workers will solve %d submatrices.\n",
world_rank, (world_size - 1), submatrices_per_worker);
if (prop.size % (world_size - 1) != 0) {
fprintf(stderr,
"%d: WARNING: Load imbalanced. Last worker will have to "
"solve %d additional submatrices\n",
world_rank, prop.size % (world_size - 1));
}
/* Main evaluation loop */
int evalRep, evalChoice;
for (evalRep = 0; evalRep < 5; evalRep++) {
for (evalChoice = 1; evalChoice > 0; evalChoice--) {
snprintf(fn_in_cp, PATHLEN, "sprandsym-s%d-d%d-c%d-n%d.cp", prop.size,
prop.density, prop.condition, evalChoice);
snprintf(fn_out_val, PATHLEN, "sprandsym-s%d-d%d-c%d-n%d.inv.val",
prop.size, prop.density, prop.condition, evalChoice);
snprintf(fn_in_val, PATHLEN, "sprandsym-s%d-d%d-c%d-n%d.val", prop.size,
prop.density, prop.condition, evalChoice);
snprintf(fn_in_ri, PATHLEN, "sprandsym-s%d-d%d-c%d-n%d.ri", prop.size,
prop.density, prop.condition, evalChoice);
snprintf(fn_out_val, PATHLEN, "sprandsym-s%d-d%d-c%d-n%d.out.val",
prop.size, prop.density, prop.condition, evalChoice);
// col_ptr最后一个元素存的是总非0元素数量,这样就能计算出最后一列的非0元数量
col_ptr = (MKL_INT *)calloc(prop.size + 1, sizeof(MKL_INT));
fp = fopen(fn_in_cp, "rb");
fread(col_ptr, sizeof(MKL_INT), prop.size + 1, fp);
fclose(fp);
total_nnz = col_ptr[prop.size];
row_ind = (MKL_INT *)calloc(total_nnz, sizeof(MKL_INT));
fp = fopen(fn_in_ri, "rb");
fread(row_ind, sizeof(MKL_INT), total_nnz, fp);
fclose(fp);
values = (double *)calloc(total_nnz, sizeof(double));
fp = fopen(fn_in_val, "rb");
fread(values, sizeof(double), total_nnz, fp);
fclose(fp);
/*
fp = fopen(fn_out_val, "wb");
fseek(fp, total_nnz*sizeof(double)-1, SEEK_SET);
fputc('\0', fp);
fclose(fp);
fd = open(fn_out_val, O_RDWR);
values_inv = (double*) mmap(NULL, total_nnz*sizeof(double),
PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
close(fd);
*/
values_inv = (double *)calloc(total_nnz, sizeof(double));
displs = (int *)calloc(world_size, sizeof(int));
recvcounts = (int *)calloc(world_size, sizeof(int));
for (i = 1; i < world_size; i++) {
displs[i] = col_ptr[(i - 1) * submatrices_per_worker];
recvcounts[i] = col_ptr[i * submatrices_per_worker] -
col_ptr[(i - 1) * submatrices_per_worker];
}
// Allow last worker to send all remaining results
recvcounts[world_size - 1] =
col_ptr[prop.size] -
col_ptr[(world_size - 2) * submatrices_per_worker];
tStart = MPI_Wtime();
// printf("%d: Broadcasting information to all workers...\n",
// world_rank);
MPI_Bcast(&prop, 3, MPI_INT, 0, MPI_COMM_WORLD);
// printf("%d: ... done\n", world_rank);
#ifndef USE_BEEGFS
// Send data to all workers
MPI_Bcast(col_ptr, prop.size + 1, MPI_INT, 0, MPI_COMM_WORLD);
MPI_Bcast(row_ind, total_nnz, MPI_INT, 0, MPI_COMM_WORLD);
MPI_Bcast(values, total_nnz, MPI_DOUBLE, 0, MPI_COMM_WORLD);
#endif
tEnd = MPI_Wtime();
printf("%d: Wall time elapsed for Bcast: %dms\n", world_rank,
(int)((tEnd - tStart) * 1000));
tStart = MPI_Wtime();
// printf("%d: Waiting for results...\n", world_rank);
MPI_Gatherv(NULL, 0, MPI_DOUBLE, values_inv, recvcounts, displs,
MPI_DOUBLE, 0, MPI_COMM_WORLD);
// printf("%d: ... done\n", world_rank);
tEnd = MPI_Wtime();
printf("%d: Wall time elapsed for Gatherv: %dms\n", world_rank,
(int)((tEnd - tStart) * 1000));
fp = fopen(fn_out_val, "wb");
fwrite(values_inv, sizeof(double), total_nnz, fp);
fclose(fp);
free(row_ind);
free(values);
free(recvcounts);
free(displs);
free(col_ptr);
free(values_inv);
// munmap(values_inv, total_nnz*sizeof(double));
}
}
/* End of main evaluation loop */
// printf("%d: Shutting down workers...\n", world_rank);
prop.size = 0;
MPI_Bcast(&prop, 3, MPI_INT, 0, MPI_COMM_WORLD);
} else {
/***************
* Worker code *
***************/
// We are one of the workers. Run in a loop and wait for jobs.
while (1) {
printf("%d: Waiting for matrix properties...\n", world_rank);
MPI_Bcast(&prop, 3, MPI_INT, 0, MPI_COMM_WORLD);
printf("%d: ... received\n", world_rank);
if (prop.size == 0) {
printf("%d: Received signal to halt.\n", world_rank);
break;
}
snprintf(fn_in_cp, PATHLEN, "sprandsym-s%d-d%d-c%d-n1.cp", prop.size,
prop.density, prop.condition);
snprintf(fn_out_val, PATHLEN, "sprandsym-s%d-d%d-c%d-n1.inv.val",
prop.size, prop.density, prop.condition);
snprintf(fn_in_val, PATHLEN, "sprandsym-s%d-d%d-c%d-n1.val", prop.size,
prop.density, prop.condition);
snprintf(fn_in_ri, PATHLEN, "sprandsym-s%d-d%d-c%d-n1.ri", prop.size,
prop.density, prop.condition);
#ifdef USE_BEEGFS
#ifdef USE_MMAP
fd = open(fn_in_cp, O_RDONLY);
col_ptr = (MKL_INT *)mmap(NULL, (prop.size + 1) * sizeof(MKL_INT),
PROT_READ, MAP_SHARED, fd, 0);
close(fd);
#else
fp = fopen(fn_in_cp, "rb");
col_ptr = (MKL_INT *)calloc(prop.size + 1, sizeof(MKL_INT));
fread(col_ptr, sizeof(MKL_INT), prop.size + 1, fp);
fclose(fp);
#endif
#else
col_ptr = (MKL_INT *)calloc(prop.size + 1, sizeof(MKL_INT));
MPI_Bcast(col_ptr, prop.size + 1, MPI_INT, 0, MPI_COMM_WORLD);
#endif
total_nnz = col_ptr[prop.size];
#ifdef USE_BEEGFS
#ifdef USE_MMAP
fd = open(fn_in_ri, O_RDONLY);
row_ind = (MKL_INT *)mmap(NULL, total_nnz * sizeof(MKL_INT), PROT_READ,
MAP_SHARED, fd, 0);
close(fd);
fd = open(fn_in_val, O_RDONLY);
values = (double *)mmap(NULL, total_nnz * sizeof(double), PROT_READ,
MAP_SHARED, fd, 0);
close(fd);
#else
row_ind = (MKL_INT *)calloc(total_nnz, sizeof(MKL_INT));
values = (double *)calloc(total_nnz, sizeof(double));
fp = fopen(fn_in_ri, "rb");
fread(row_ind, sizeof(MKL_INT), total_nnz, fp);
fclose(fp);
fp = fopen(fn_in_val, "rb");
fread(values, sizeof(double), total_nnz, fp);
fclose(fp);
#endif
#else
row_ind = (MKL_INT *)calloc(total_nnz, sizeof(MKL_INT));
values = (double *)calloc(total_nnz, sizeof(double));
MPI_Bcast(row_ind, total_nnz, MPI_INT, 0, MPI_COMM_WORLD);
MPI_Bcast(values, total_nnz, MPI_DOUBLE, 0, MPI_COMM_WORLD);
#endif
submatrices_per_worker = prop.size / (world_size - 1);
my_first_col = (world_rank - 1) * submatrices_per_worker;
if (world_rank == world_size - 1) {
// We are the last worker. Maybe we have to do additional work...
submatrices_for_me =
submatrices_per_worker + (prop.size % (world_size - 1));
next_first_col = prop.size;
} else {
submatrices_for_me = submatrices_per_worker;
next_first_col = world_rank * submatrices_per_worker;
}
total_elem = col_ptr[next_first_col] - col_ptr[my_first_col];
values_inv = (double *)calloc(total_elem, sizeof(double));
/* Optimize threading: We should do as much submatrices as possible in
* parallel. If threads are left, leave them for MKL's internal
* parallelism. */
mkl_threads = omp_get_max_threads() / submatrices_for_me;
if (mkl_threads < 1) {
mkl_threads = 1;
}
mkl_set_num_threads(mkl_threads);
printf("%d: We have %d thread(s) to solve %d submatrices. Give %d "
"thread(s) to MKL for each submatrix operation.\n",
world_rank, omp_get_max_threads(), submatrices_for_me,
mkl_threads);
double durationBuild = .0;
double durationCalc = .0;
tStart = MPI_Wtime();
// printf("%d: Starting the number crunching\n", world_rank);
#pragma omp parallel for schedule(dynamic) \
reduction(+ : durationBuild, durationCalc)
for (i = 0; i < submatrices_for_me; i++) {
double locDurBuild, locDurCalc;
// printf("%d: Inverting submatrix %d in thread %d.\n", world_rank,
// (world_rank - 1) * submatrices_for_me + i,
// omp_get_thread_num());
invert_submatrix(
values, row_ind, col_ptr,
&(values_inv[col_ptr[my_first_col + i] - col_ptr[my_first_col]]),
my_first_col + i, &locDurBuild, &locDurCalc);
durationBuild += locDurBuild;
durationCalc += locDurCalc;
}
tEnd = MPI_Wtime();
printf("%d: Wall time elapsed: %dms\n", world_rank,
(int)((tEnd - tStart) * 1000));
printf("%d: CPU time sm build: %dms\n", world_rank,
(int)(durationBuild * 1000));
printf("%d: CPU time sm calc: %dms\n", world_rank,
(int)(durationCalc * 1000));
// printf("%d: Send results to root\n", world_rank);
MPI_Gatherv(values_inv, total_elem, MPI_DOUBLE, NULL, NULL, NULL,
MPI_DOUBLE, 0, MPI_COMM_WORLD);
// printf("%d: ... done\n", world_rank);
memset(values_inv, 0, total_elem * sizeof(double));
free(values_inv);
#if defined USE_BEEGFS && defined USE_MMAP
munmap(values, total_nnz * sizeof(double));
munmap(row_ind, total_nnz * sizeof(MKL_INT));
munmap(col_ptr, (prop.size + 1) * sizeof(MKL_INT));
#else
memset(values, 0, total_nnz * sizeof(double));
memset(row_ind, 0, total_nnz * sizeof(MKL_INT));
memset(col_ptr, 0, (prop.size + 1) * sizeof(MKL_INT));
free(values);
free(row_ind);
free(col_ptr);
#endif
}
}
MPI_Barrier(MPI_COMM_WORLD);
MPI_Finalize();
exit(EXIT_SUCCESS);
}