-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathomp_strassen.hpp
159 lines (143 loc) · 4.53 KB
/
omp_strassen.hpp
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
#include <omp.h>
#include "matrix.hpp"
const int THRESHOLD = 64;
void omp_strassen_util(int** A, int ** B, int** C, int n, int m, int p) {
if (std::max(n, std::max(m, p)) <= THRESHOLD) {
matrix_multiply(A, B, C, n, m, p);
return;
}
int n1 = (n + 1) / 2, m1 = (m + 1) / 2, p1 = (p + 1) / 2;
int **A11 = new_matrix(n1, m1);
init_sub_matrix(A, A11, n, m, 0, 0);
int **A12 = new_matrix(n1, m1);
init_sub_matrix(A, A12, n, m, 0, m1);
int **A21 = new_matrix(n1, m1);
init_sub_matrix(A, A21, n, m, n1, 0);
int **A22 = new_matrix(n1, m1);
init_sub_matrix(A, A22, n, m, n1, m1);
int **B11 = new_matrix(m1, p1);
init_sub_matrix(B, B11, m, p, 0, 0);
int **B12 = new_matrix(m1, p1);
init_sub_matrix(B, B12, m, p, 0, p1);
int **B21 = new_matrix(m1, p1);
init_sub_matrix(B, B21, m, p, m1, 0);
int **B22 = new_matrix(m1, p1);
init_sub_matrix(B, B22, m, p, m1, p1);
int **M1 = new_matrix(n1, p1);
int **M2 = new_matrix(n1, p1);
int **M3 = new_matrix(n1, p1);
int **M4 = new_matrix(n1, p1);
int **M5 = new_matrix(n1, p1);
int **M6 = new_matrix(n1, p1);
int **M7 = new_matrix(n1, p1);
#pragma omp task untied
{
int **M11 = new_matrix(n1, m1);
int **M12 = new_matrix(m1, p1);
// printf("thread num: %d out of %d\n", omp_get_thread_num(), omp_get_max_threads());
matrix_add(A11, A22, M11, n1, m1);
matrix_add(B11, B22, M12, m1, p1);
omp_strassen_util(M11, M12, M1, n1, m1, p1);
delete_matrix(M11);
delete_matrix(M12);
}
#pragma omp task untied
{
int **M21 = new_matrix(n1, m1);
// printf("thread num: %d out of %d\n", omp_get_thread_num(), omp_get_max_threads());
matrix_add(A21, A22, M21, n1, m1);
omp_strassen_util(M21, B11, M2, n1, m1, p1);
delete_matrix(M21);
}
#pragma omp task untied
{
int **M32 = new_matrix(m1, p1);
// printf("thread num: %d out of %d\n", omp_get_thread_num(), omp_get_max_threads());
matrix_sub(B12, B22, M32, m1, p1);
omp_strassen_util(A11, M32, M3, n1, m1, p1);
delete_matrix(M32);
}
#pragma omp task
{
int **M42 = new_matrix(m1, p1);
// printf("thread num: %d out of %d\n", omp_get_thread_num(), omp_get_max_threads());
matrix_sub(B21, B11, M42, m1, p1);
omp_strassen_util(A22, M42, M4, n1, m1, p1);
delete_matrix(M42);
}
#pragma omp task
{
int **M51 = new_matrix(n1, m1);
// printf("thread num: %d out of %d\n", omp_get_thread_num(), omp_get_max_threads());
matrix_add(A11, A12, M51, n1, m1);
omp_strassen_util(M51, B22, M5, n1, m1, p1);
delete_matrix(M51);
}
#pragma omp task
{
int **M61 = new_matrix(n1, m1);
int **M62 = new_matrix(m1, p1);
// printf("thread num: %d out of %d\n", omp_get_thread_num(), omp_get_max_threads());
matrix_sub(A21, A11, M61, n1, m1);
matrix_add(B11, B12, M62, m1, p1);
omp_strassen_util(M61, M62, M6, n1, m1, p1);
delete_matrix(M61);
delete_matrix(M62);
}
#pragma omp task
{
int **M71 = new_matrix(n1, m1);
int **M72 = new_matrix(m1, p1);
// printf("thread num: %d out of %d\n", omp_get_thread_num(), omp_get_max_threads());
matrix_sub(A12, A22, M71, n1, m1);
matrix_add(B21, B22, M72, m1, p1);
omp_strassen_util(M71, M72, M7, n1, m1, p1);
delete_matrix(M71);
delete_matrix(M72);
}
#pragma omp taskwait
// matrix_print(A, n, m);
// matrix_print(B, m, p);
// matrix_print(M1, n1, p1);
// matrix_print(M2, n1, p1);
// matrix_print(M3, n1, p1);
// matrix_print(M4, n1, p1);
// matrix_print(M5, n1, p1);
// matrix_print(M6, n1, p1);
// matrix_print(M7, n1, p1);
// std::cout << '\n';
for (int i = 0; i < n1; ++i) {
for (int j = 0; j < p1; ++j) {
C[i][j] = M1[i][j] + M4[i][j] - M5[i][j] + M7[i][j];
if (p1 + j < p) C[i][p1 + j] = M3[i][j] + M5[i][j];
if (n1 + i < n) {
C[n1 + i][j] = M2[i][j] + M4[i][j];
if (p1 + j < p) C[n1 + i][p1 + j] = M1[i][j] - M2[i][j] + M3[i][j] + M6[i][j];
}
}
}
delete_matrix(A11);
delete_matrix(A12);
delete_matrix(A21);
delete_matrix(A22);
delete_matrix(B11);
delete_matrix(B12);
delete_matrix(B21);
delete_matrix(B22);
delete_matrix(M1);
delete_matrix(M2);
delete_matrix(M3);
delete_matrix(M4);
delete_matrix(M5);
delete_matrix(M6);
delete_matrix(M7);
}
inline void omp_strassen(int** A, int ** B, int** C, int n, int m, int p) {
#pragma omp parallel
{
#pragma omp single
{
omp_strassen_util(A, B, C, n, m, p);
} // omp single
} // omp parallel
}