diff --git a/lab05/matrix b/lab05/matrix new file mode 100644 index 0000000..412228b Binary files /dev/null and b/lab05/matrix differ diff --git a/lab05/matrixVectorProductDistributedIndices b/lab05/matrixVectorProductDistributedIndices new file mode 100644 index 0000000..e5bbfc3 Binary files /dev/null and b/lab05/matrixVectorProductDistributedIndices differ diff --git a/lab05/matrixVectorProductDistributedIndices.cpp b/lab05/matrixVectorProductDistributedIndices.cpp index 9ad1289..4205744 100644 --- a/lab05/matrixVectorProductDistributedIndices.cpp +++ b/lab05/matrixVectorProductDistributedIndices.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -7,7 +8,7 @@ // Create the matrix and vector to be multiplied and fill them // with some sensible initial values. std::pair> createMatrixAndVector() { - const int n = 1e3*9; + const int n = 1e3 * 9; Matrix mat(n, n); for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { @@ -38,20 +39,51 @@ void verifyResult(const std::vector result) { } } -void computeResult(const Matrix& mat, const std::vector& vec, int start, int end, std::vector& result) { +void computeResult(const Matrix& mat, + const std::vector& vec, + int start, + int end, + std::vector& result) { int n = vec.size(); for (int x = start; x < end; x++) { for (int y = 0; y < n; y++) { - result[y] = mat(x,y) * vec[x]; + result[x] += mat(x, y) * vec[y]; } } } -int main() { +void benchmarkComputeResult(benchmark::State& state) { + int threads = state.range(0); auto [mat, vec] = createMatrixAndVector(); - std::vector result(vec.size(), 0); - result = mat * vec; - // TODO: compute result = mat * vec with multiple threads - verifyResult(result); + for (auto _ : state) { + std::vector threadPool; + std::vector result(vec.size(), 0); + for (int j = 0; j < threads; j++) { + int start = vec.size() / threads * j; + int end = vec.size() / threads * (j + 1); + threadPool.emplace_back(computeResult, std::ref(mat), std::ref(vec), + start, end, std::ref(result)); + } + for (int j = 0; j < threads; j++) { + threadPool[j].join(); + } + benchmark::DoNotOptimize(result); + } +} + +int main(int argc, char** argv) { + ::benchmark::Initialize(&argc, argv); + + std::vector threads = {1, 2, 3, 4, 6, 9, 12}; + + for (int i = 0; i < threads.size(); i++) { + benchmark::RegisterBenchmark("idk", benchmarkComputeResult) + ->Arg(threads[i]) + ->Unit(benchmark::kMillisecond); + } + + ::benchmark::RunSpecifiedBenchmarks(); + + return 0; } \ No newline at end of file