Skip to content
This repository was archived by the owner on Nov 29, 2020. It is now read-only.

Commit 845e468

Browse files
committed
use parallel_for
1 parent 5070182 commit 845e468

File tree

1 file changed

+43
-34
lines changed

1 file changed

+43
-34
lines changed

MatVec.cpp

Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@
2929

3030
#include "SquareMatrixMultiply.cpp"
3131

32+
template <typename T, typename F>
33+
void parallel_for(T i1, T i2, F func) {
34+
if (i1 == i2 - 1)
35+
func(i1);
36+
else {
37+
T mid = (i1 + i2) / 2;
38+
std::thread t1(parallel_for<T, F>, i1, mid, func);
39+
parallel_for<T, F>(mid, i2, func);
40+
t1.join();
41+
}
42+
}
43+
3244
template <typename T>
3345
void MatVecMainLoop(Matrix<T>* A, Matrix<T>* x, Matrix<T>* y, size_t n,
3446
size_t i1, size_t i2) {
@@ -44,63 +56,60 @@ void MatVecMainLoop(Matrix<T>* A, Matrix<T>* x, Matrix<T>* y, size_t n,
4456
}
4557

4658
template <typename T>
47-
void MatVec(Matrix<T>& A, Matrix<T>& x, Matrix<T>& y) {
59+
void MatVecRecursive(Matrix<T>& A, Matrix<T>& x, Matrix<T>& y) {
4860
size_t n = A.rows;
4961
MatVecMainLoop(&A, &x, &y, n, 0, n);
5062
}
5163

5264
template <typename T>
53-
void MatVecWrongLoop2(Matrix<T>* A, Matrix<T>* x, Matrix<T>* y, size_t n,
54-
size_t i, size_t j1, size_t j2) {
55-
if (j1 == j2 - 1)
56-
(*y)[i][0] += (*A)[i][j1] * (*x)[j1][0];
57-
else {
58-
size_t mid = (j1 + j2) / 2;
59-
std::thread t1(MatVecWrongLoop2<T>, A, x, y, n, i, j1, mid);
60-
MatVecWrongLoop2(A, x, y, n, i, mid, j2);
61-
t1.join();
62-
}
63-
}
64-
65-
template <typename T>
66-
void MatVecWrongLoop1(Matrix<T>* A, Matrix<T>* x, Matrix<T>* y, size_t n,
67-
size_t i1, size_t i2) {
68-
if (i1 == i2 - 1)
69-
MatVecWrongLoop2(A, x, y, n, i1, 0, n);
70-
else {
71-
size_t mid = (i1 + i2) / 2;
72-
std::thread t1(MatVecWrongLoop1<T>, A, x, y, n, i1, mid);
73-
MatVecWrongLoop1(A, x, y, n, mid, i2);
74-
t1.join();
75-
}
65+
void MatVec(Matrix<T>& A, Matrix<T>& x, Matrix<T>& y) {
66+
size_t n = A.rows;
67+
parallel_for<size_t>(0, n, [&](size_t i){
68+
for (size_t j = 0; j < n; j++)
69+
y[i][0] += A[i][j] * x[j][0];
70+
});
7671
}
7772

7873
template <typename T>
7974
void MatVecWrong(Matrix<T>& A, Matrix<T>& x, Matrix<T>& y) {
8075
size_t n = A.rows;
81-
MatVecWrongLoop1(&A, &x, &y, n, 0, n);
76+
parallel_for<size_t>(0, n, [&](size_t i){
77+
parallel_for<size_t>(0, n, [&](size_t j){
78+
y[i][0] += A[i][j] * x[j][0];
79+
});
80+
});
8281
}
8382
#endif
8483

8584
#ifdef MAIN_MatVec
8685
int main(int argc, char *argv[]) {
8786
size_t n = get_argv(argc, argv, 1, 10);
88-
size_t compute = get_argv(argc, argv, 2, 1);
87+
size_t compute = get_argv(argc, argv, 2, 7);
8988
std::vector<int> buf_A, buf_x;
9089
random_integers(buf_A, 0, n, n * n);
9190
random_integers(buf_x, 0, n, n);
9291
Matrix<int> A(n, n, buf_A);
9392
Matrix<int> x(n, 1, buf_x);
94-
Matrix<int> y(n, 1, 0);
95-
MatVec(A, x, y);
9693
std::cout << A << std::endl;
9794
std::cout << x << std::endl;
98-
std::cout << y << std::endl;
99-
if (compute) {
100-
Matrix<int> yw(n, 1, 0);
101-
MatVecWrong(A, x, yw);
102-
std::cout << yw << std::endl;
103-
std::cout << std::boolalpha << (y == yw) << std::endl;
95+
Matrix<int> y1(n, 1, 0), y2(n, 1, 0), y3(n, 1, 0);
96+
if (compute >> 0 & 1) {
97+
MatVec(A, x, y1);
98+
std::cout << y1 << std::endl;
99+
}
100+
if (compute >> 1 & 1) {
101+
MatVecRecursive(A, x, y2);
102+
std::cout << y2;
103+
if (compute >> 0 & 1)
104+
std::cout << std::boolalpha << (y1 == y2) << std::endl;
105+
std::cout << std::endl;
106+
}
107+
if (compute >> 2 & 1) {
108+
MatVecWrong(A, x, y3);
109+
std::cout << y3;
110+
if (compute >> 0 & 1)
111+
std::cout << std::boolalpha << (y1 == y3) << std::endl;
112+
std::cout << std::endl;
104113
}
105114
return 0;
106115
}

0 commit comments

Comments
 (0)