Skip to content

Commit 76dd44a

Browse files
committed
Refactor tasks to simplify matrix operations and input handling
Removed unnecessary matrix multiplication logic, replacing it with a streamlined computation approach. Standardized input/output types and ensured consistent initialization across tasks. Cleaned up unused private variables and redundant functions for better maintainability.
1 parent 38c32e9 commit 76dd44a

15 files changed

Lines changed: 170 additions & 190 deletions

File tree

tasks/example/all/include/ops_all.hpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66

77
namespace nesterov_a_test_task {
88

9-
void MatMul(const std::vector<int> &in_vec, int rc_size, std::vector<int> &out_vec);
10-
void MatMulTBB(const std::vector<int> &in_vec, int rc_size, std::vector<int> &out_vec);
11-
129
class NesterovATestTaskALL : public BaseTask {
1310
public:
1411
static constexpr ppc::core::TypeOfTask GetStaticTypeOfTask() { return ppc::core::TypeOfTask::kALL; }
@@ -17,9 +14,6 @@ class NesterovATestTaskALL : public BaseTask {
1714
bool PreProcessingImpl() override;
1815
bool RunImpl() override;
1916
bool PostProcessingImpl() override;
20-
21-
private:
22-
int rc_size_{};
2317
};
2418

2519
} // namespace nesterov_a_test_task

tasks/example/all/src/ops_all.cpp

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,63 +13,65 @@
1313

1414
namespace nesterov_a_test_task {
1515

16-
void MatMul(const InType &in_vec, int rc_size, OutType &out_vec) {
17-
for (int i = 0; i < rc_size; ++i) {
18-
for (int j = 0; j < rc_size; ++j) {
19-
out_vec[(i * rc_size) + j] = 0;
20-
for (int k = 0; k < rc_size; ++k) {
21-
out_vec[(i * rc_size) + j] += in_vec[(i * rc_size) + k] * in_vec[(k * rc_size) + j];
22-
}
23-
}
24-
}
25-
}
26-
27-
void MatMulTBB(const InType &in_vec, int rc_size, OutType &out_vec) {
28-
tbb::parallel_for(0, ppc::util::GetNumThreads(), [&](int i) { MatMul(in_vec, rc_size - i, out_vec); });
29-
MatMul(in_vec, rc_size, out_vec);
30-
}
31-
3216
NesterovATestTaskALL::NesterovATestTaskALL(const InType &in) {
3317
SetTypeOfTask(GetStaticTypeOfTask());
3418
GetInput() = in;
19+
GetOutput() = 0;
3520
}
3621

37-
bool NesterovATestTaskALL::ValidationImpl() {
38-
auto sqrt_size = static_cast<int>(std::sqrt(GetInput().size()));
39-
return sqrt_size * sqrt_size == static_cast<int>(GetInput().size());
40-
}
22+
bool NesterovATestTaskALL::ValidationImpl() { return (GetInput() > 0) && (GetOutput() == 0); }
4123

4224
bool NesterovATestTaskALL::PreProcessingImpl() {
43-
// Init value for input and output
44-
rc_size_ = static_cast<int>(std::sqrt(GetInput().size()));
45-
GetOutput() = OutType(GetInput().size(), 0);
46-
return true;
25+
GetOutput() = 2 * GetInput();
26+
return GetOutput() > 0;
4727
}
4828

4929
bool NesterovATestTaskALL::RunImpl() {
30+
for (InType i = 0; i < GetInput(); i++) {
31+
for (InType j = 0; j < GetInput(); j++) {
32+
for (InType k = 0; k < GetInput(); k++) {
33+
std::vector<InType> tmp(i + j + k, 1);
34+
GetOutput() += std::accumulate(tmp.begin(), tmp.end(), 0);
35+
GetOutput() -= i + j + k;
36+
}
37+
}
38+
}
39+
40+
const int num_threads = ppc::util::GetNumThreads();
41+
GetOutput() *= num_threads;
42+
5043
int rank = -1;
5144
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
5245
if (rank == 0) {
46+
std::atomic<int> counter(0);
5347
#pragma omp parallel default(none)
5448
{
55-
#pragma omp critical
56-
MatMul(GetInput(), rc_size_, GetOutput());
49+
counter++;
5750
}
51+
GetOutput() /= counter;
5852
} else {
59-
MatMulTBB(GetInput(), rc_size_, GetOutput());
53+
GetOutput() /= num_threads;
6054
}
6155

62-
const int num_threads = ppc::util::GetNumThreads();
56+
GetOutput() *= num_threads;
6357
std::vector<std::thread> threads(num_threads);
58+
std::atomic<int> counter(0);
6459
for (int i = 0; i < num_threads; i++) {
65-
threads[i] = std::thread(MatMul, std::cref(GetInput()), rc_size_, std::ref(GetOutput()));
60+
threads[i] = std::thread([&]() { counter++; });
6661
threads[i].join();
6762
}
63+
GetOutput() /= counter;
64+
65+
tbb::parallel_for(0, ppc::util::GetNumThreads(), [&](int i) { counter--; });
66+
GetOutput() += counter;
6867

6968
MPI_Barrier(MPI_COMM_WORLD);
70-
return true;
69+
return GetOutput() > 0;
7170
}
7271

73-
bool NesterovATestTaskALL::PostProcessingImpl() { return true; }
72+
bool NesterovATestTaskALL::PostProcessingImpl() {
73+
GetOutput() -= GetInput();
74+
return GetOutput() > 0;
75+
}
7476

7577
} // namespace nesterov_a_test_task

tasks/example/common/include/common.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99
namespace nesterov_a_test_task {
1010

11-
using InType = std::vector<int>;
12-
using OutType = std::vector<int>;
11+
using InType = int;
12+
using OutType = int;
1313
using TestType = std::tuple<int, std::string>;
1414
using BaseTask = ppc::core::Task<InType, OutType>;
1515

tasks/example/mpi/include/ops_mpi.hpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77

88
namespace nesterov_a_test_task {
99

10-
void MultiplyRowMajor(const std::vector<int> &in, std::vector<int> &out, int rc_size);
11-
void MultiplyColumnMajor(const std::vector<int> &in, std::vector<int> &out, int rc_size);
12-
1310
class NesterovATestTaskMPI : public BaseTask {
1411
public:
1512
static constexpr ppc::core::TypeOfTask GetStaticTypeOfTask() { return ppc::core::TypeOfTask::kMPI; }
@@ -18,11 +15,6 @@ class NesterovATestTaskMPI : public BaseTask {
1815
bool PreProcessingImpl() override;
1916
bool RunImpl() override;
2017
bool PostProcessingImpl() override;
21-
22-
private:
23-
int rc_size_{};
24-
25-
void MultiplyMatrixBasedOnRank();
2618
};
2719

2820
} // namespace nesterov_a_test_task

tasks/example/mpi/src/ops_mpi.cpp

Lines changed: 29 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,62 +5,56 @@
55
#include <cmath>
66
#include <cstddef>
77
#include <vector>
8-
namespace nesterov_a_test_task {
9-
10-
void MultiplyRowMajor(const InType &in, OutType &out, int rc_size) {
11-
for (int i = 0; i < rc_size; ++i) {
12-
for (int j = 0; j < rc_size; ++j) {
13-
for (int k = 0; k < rc_size; ++k) {
14-
out[(i * rc_size) + j] += in[(i * rc_size) + k] * in[(k * rc_size) + j];
15-
}
16-
}
17-
}
18-
}
198

20-
void MultiplyColumnMajor(const InType &in, OutType &out, int rc_size) {
21-
for (int j = 0; j < rc_size; ++j) {
22-
for (int k = 0; k < rc_size; ++k) {
23-
for (int i = 0; i < rc_size; ++i) {
24-
out[(i * rc_size) + j] += in[(i * rc_size) + k] * in[(k * rc_size) + j];
25-
}
26-
}
27-
}
28-
}
9+
namespace nesterov_a_test_task {
2910

3011
NesterovATestTaskMPI::NesterovATestTaskMPI(const InType &in) {
3112
SetTypeOfTask(GetStaticTypeOfTask());
3213
GetInput() = in;
14+
GetOutput() = 0;
3315
}
3416

35-
bool NesterovATestTaskMPI::ValidationImpl() {
36-
auto sqrt_size = static_cast<int>(std::sqrt(GetInput().size()));
37-
return sqrt_size * sqrt_size == static_cast<int>(GetInput().size());
38-
}
17+
bool NesterovATestTaskMPI::ValidationImpl() { return (GetInput() > 0) && (GetOutput() == 0); }
3918

4019
bool NesterovATestTaskMPI::PreProcessingImpl() {
41-
// Init value for input and output
42-
rc_size_ = static_cast<int>(std::sqrt(GetInput().size()));
43-
GetOutput() = OutType(GetInput().size(), 0);
44-
return true;
20+
GetOutput() = 2 * GetInput();
21+
return GetOutput() > 0;
4522
}
4623

4724
bool NesterovATestTaskMPI::RunImpl() {
48-
MultiplyMatrixBasedOnRank();
49-
return true;
50-
}
25+
for (InType i = 0; i < GetInput(); i++) {
26+
for (InType j = 0; j < GetInput(); j++) {
27+
for (InType k = 0; k < GetInput(); k++) {
28+
std::vector<InType> tmp(i + j + k, 1);
29+
GetOutput() += std::accumulate(tmp.begin(), tmp.end(), 0);
30+
GetOutput() -= i + j + k;
31+
}
32+
}
33+
}
5134

52-
bool NesterovATestTaskMPI::PostProcessingImpl() { return true; }
35+
const int num_threads = ppc::util::GetNumThreads();
36+
GetOutput() *= num_threads;
5337

54-
void NesterovATestTaskMPI::MultiplyMatrixBasedOnRank() {
5538
int rank = -1;
5639
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
5740

5841
if (rank == 0) {
59-
MultiplyRowMajor(GetInput(), GetOutput(), rc_size_);
42+
GetOutput() /= num_threads;
6043
} else {
61-
MultiplyColumnMajor(GetInput(), GetOutput(), rc_size_);
44+
int counter = 0;
45+
for (int i = 0; i < num_threads; i++) {
46+
counter++;
47+
}
48+
GetOutput() /= counter;
6249
}
50+
6351
MPI_Barrier(MPI_COMM_WORLD);
52+
return GetOutput() > 0;
53+
}
54+
55+
bool NesterovATestTaskMPI::PostProcessingImpl() {
56+
GetOutput() -= GetInput();
57+
return GetOutput() > 0;
6458
}
6559

6660
} // namespace nesterov_a_test_task

tasks/example/omp/include/ops_omp.hpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@ class NesterovATestTaskOMP : public BaseTask {
1616
bool PreProcessingImpl() override;
1717
bool RunImpl() override;
1818
bool PostProcessingImpl() override;
19-
20-
private:
21-
int rc_size_{};
2219
};
2320

2421
} // namespace nesterov_a_test_task

tasks/example/omp/src/ops_omp.cpp

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,38 +9,42 @@ namespace nesterov_a_test_task {
99
NesterovATestTaskOMP::NesterovATestTaskOMP(const InType& in) {
1010
SetTypeOfTask(GetStaticTypeOfTask());
1111
GetInput() = in;
12+
GetOutput() = 0;
1213
}
1314

14-
bool NesterovATestTaskOMP::ValidationImpl() {
15-
auto sqrt_size = static_cast<int>(std::sqrt(GetInput().size()));
16-
return sqrt_size * sqrt_size == static_cast<int>(GetInput().size());
17-
}
15+
bool NesterovATestTaskOMP::ValidationImpl() { return (GetInput() > 0) && (GetOutput() == 0); }
1816

1917
bool NesterovATestTaskOMP::PreProcessingImpl() {
20-
rc_size_ = static_cast<int>(std::sqrt(GetInput().size()));
21-
GetOutput() = OutType(GetInput().size(), 0);
22-
return true;
18+
GetOutput() = 2 * GetInput();
19+
return GetOutput() > 0;
2320
}
2421

2522
bool NesterovATestTaskOMP::RunImpl() {
26-
#pragma omp parallel default(none)
27-
{
28-
#pragma omp critical
29-
{
30-
// Multiply matrices
31-
for (int i = 0; i < rc_size_; ++i) {
32-
for (int j = 0; j < rc_size_; ++j) {
33-
GetOutput()[(i * rc_size_) + j] = 0;
34-
for (int k = 0; k < rc_size_; ++k) {
35-
GetOutput()[(i * rc_size_) + j] += GetInput()[(i * rc_size_) + k] * GetInput()[(k * rc_size_) + j];
36-
}
37-
}
23+
for (InType i = 0; i < GetInput(); i++) {
24+
for (InType j = 0; j < GetInput(); j++) {
25+
for (InType k = 0; k < GetInput(); k++) {
26+
std::vector<InType> tmp(i + j + k, 1);
27+
GetOutput() += std::accumulate(tmp.begin(), tmp.end(), 0);
28+
GetOutput() -= i + j + k;
3829
}
3930
}
4031
}
41-
return true;
32+
33+
const int num_threads = ppc::util::GetNumThreads();
34+
GetOutput() *= num_threads;
35+
36+
std::atomic<int> counter(0);
37+
#pragma omp parallel default(none)
38+
{
39+
counter++;
40+
}
41+
GetOutput() /= counter;
42+
return GetOutput() > 0;
4243
}
4344

44-
bool NesterovATestTaskOMP::PostProcessingImpl() { return true; }
45+
bool NesterovATestTaskOMP::PostProcessingImpl() {
46+
GetOutput() -= GetInput();
47+
return GetOutput() > 0;
48+
}
4549

4650
} // namespace nesterov_a_test_task

tasks/example/seq/include/ops_seq.hpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@ class NesterovATestTaskSEQ : public BaseTask {
1616
bool PreProcessingImpl() override;
1717
bool RunImpl() override;
1818
bool PostProcessingImpl() override;
19-
20-
private:
21-
int rc_size_{};
2219
};
2320

2421
} // namespace nesterov_a_test_task

tasks/example/seq/src/ops_seq.cpp

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,42 @@ namespace nesterov_a_test_task {
99
NesterovATestTaskSEQ::NesterovATestTaskSEQ(const InType& in) {
1010
SetTypeOfTask(GetStaticTypeOfTask());
1111
GetInput() = in;
12+
GetOutput() = 0;
1213
}
1314

14-
bool NesterovATestTaskSEQ::ValidationImpl() {
15-
auto sqrt_size = static_cast<int>(std::sqrt(GetInput().size()));
16-
return sqrt_size * sqrt_size == static_cast<int>(GetInput().size());
17-
}
15+
bool NesterovATestTaskSEQ::ValidationImpl() { return (GetInput() > 0) && (GetOutput() == 0); }
1816

1917
bool NesterovATestTaskSEQ::PreProcessingImpl() {
20-
rc_size_ = static_cast<int>(std::sqrt(GetInput().size()));
21-
GetOutput() = OutType(GetInput().size(), 0);
22-
return true;
18+
GetOutput() = 2 * GetInput();
19+
return GetOutput() > 0;
2320
}
2421

2522
bool NesterovATestTaskSEQ::RunImpl() {
26-
// Multiply matrices
27-
for (int i = 0; i < rc_size_; ++i) {
28-
for (int j = 0; j < rc_size_; ++j) {
29-
for (int k = 0; k < rc_size_; ++k) {
30-
GetOutput()[(i * rc_size_) + j] += GetInput()[(i * rc_size_) + k] * GetInput()[(k * rc_size_) + j];
23+
for (InType i = 0; i < GetInput(); i++) {
24+
for (InType j = 0; j < GetInput(); j++) {
25+
for (InType k = 0; k < GetInput(); k++) {
26+
std::vector<InType> tmp(i + j + k, 1);
27+
GetOutput() += std::accumulate(tmp.begin(), tmp.end(), 0);
28+
GetOutput() -= i + j + k;
3129
}
3230
}
3331
}
34-
return true;
32+
33+
const int num_threads = ppc::util::GetNumThreads();
34+
GetOutput() *= num_threads;
35+
36+
int counter = 0;
37+
for (int i = 0; i < num_threads; i++) {
38+
counter++;
39+
}
40+
41+
GetOutput() /= counter;
42+
return GetOutput() > 0;
3543
}
3644

37-
bool NesterovATestTaskSEQ::PostProcessingImpl() { return true; }
45+
bool NesterovATestTaskSEQ::PostProcessingImpl() {
46+
GetOutput() -= GetInput();
47+
return GetOutput() > 0;
48+
}
3849

3950
} // namespace nesterov_a_test_task

tasks/example/stl/include/ops_stl.hpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@ class NesterovATestTaskSTL : public BaseTask {
1616
bool PreProcessingImpl() override;
1717
bool RunImpl() override;
1818
bool PostProcessingImpl() override;
19-
20-
private:
21-
int rc_size_{};
2219
};
2320

2421
} // namespace nesterov_a_test_task

0 commit comments

Comments
 (0)