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

Commit 5070182

Browse files
committed
MatVec.cpp
1 parent 17e2eb7 commit 5070182

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

MatVec.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,44 @@ void MatVec(Matrix<T>& A, Matrix<T>& x, Matrix<T>& y) {
4848
size_t n = A.rows;
4949
MatVecMainLoop(&A, &x, &y, n, 0, n);
5050
}
51+
52+
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+
}
76+
}
77+
78+
template <typename T>
79+
void MatVecWrong(Matrix<T>& A, Matrix<T>& x, Matrix<T>& y) {
80+
size_t n = A.rows;
81+
MatVecWrongLoop1(&A, &x, &y, n, 0, n);
82+
}
5183
#endif
5284

5385
#ifdef MAIN_MatVec
5486
int main(int argc, char *argv[]) {
5587
size_t n = get_argv(argc, argv, 1, 10);
88+
size_t compute = get_argv(argc, argv, 2, 1);
5689
std::vector<int> buf_A, buf_x;
5790
random_integers(buf_A, 0, n, n * n);
5891
random_integers(buf_x, 0, n, n);
@@ -63,6 +96,12 @@ int main(int argc, char *argv[]) {
6396
std::cout << A << std::endl;
6497
std::cout << x << std::endl;
6598
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;
104+
}
66105
return 0;
67106
}
68107
#endif

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@
176176
| 27 | MatVec.cpp | Mat Vec | 785 |
177177
| 27 | MatVec.cpp | Mat Vec Main Loop | 785 |
178178
| 27 | RaceExample.cpp | Race Example | 788 |
179+
| 27 | MatVec.cpp | Mat Vec Wrong | 790 |
179180

180181
# Supplementary Files
181182
* `utils.h`: Utils

0 commit comments

Comments
 (0)