forked from learning-process/parallel_programming_course
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathops_all.cpp
More file actions
68 lines (56 loc) · 2.02 KB
/
ops_all.cpp
File metadata and controls
68 lines (56 loc) · 2.02 KB
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
#include "all/example/include/ops_all.hpp"
#include <mpi.h>
#include <cmath>
#include <cstddef>
#include <functional>
#include <thread>
#include <vector>
#include "core/util/include/util.hpp"
#include "oneapi/tbb/parallel_for.h"
void nesterov_a_test_task_all::MatMul(const std::vector<int> &in_vec, int rc_size, std::vector<int> &out_vec) {
for (int i = 0; i < rc_size; ++i) {
for (int j = 0; j < rc_size; ++j) {
out_vec[(i * rc_size) + j] = 0;
for (int k = 0; k < rc_size; ++k) {
out_vec[(i * rc_size) + j] += in_vec[(i * rc_size) + k] * in_vec[(k * rc_size) + j];
}
}
}
}
void nesterov_a_test_task_all::MatMulTBB(const std::vector<int> &in_vec, int rc_size, std::vector<int> &out_vec) {
tbb::parallel_for(0, ppc::util::GetNumThreads(), [&](int i) { MatMul(in_vec, rc_size - i, out_vec); });
MatMul(in_vec, rc_size, out_vec);
}
bool nesterov_a_test_task_all::TestTaskALL::ValidationImpl() {
auto sqrt_size = static_cast<int>(std::sqrt(input_.size()));
return sqrt_size * sqrt_size == static_cast<int>(input_.size());
}
bool nesterov_a_test_task_all::TestTaskALL::PreProcessingImpl() {
// Init value for input and output
rc_size_ = static_cast<int>(std::sqrt(input_.size()));
output_ = std::vector<int>(input_.size(), 0);
return true;
}
bool nesterov_a_test_task_all::TestTaskALL::RunImpl() {
int rank = -1;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if (rank == 0) {
#pragma omp parallel default(none)
{
#pragma omp critical
MatMul(input_, rc_size_, output_);
}
} else {
MatMulTBB(input_, rc_size_, output_);
}
const int num_threads = ppc::util::GetNumThreads();
std::vector<std::thread> threads(num_threads);
for (int i = 0; i < num_threads; i++) {
threads[i] = std::thread(MatMul, std::cref(input_), rc_size_, std::ref(output_));
threads[i].join();
}
MPI_Barrier(MPI_COMM_WORLD);
return true;
}
bool nesterov_a_test_task_all::TestTaskALL::PostProcessingImpl() { return true; }
std::vector<int> nesterov_a_test_task_all::TestTaskALL::Get() { return output_; }