Commit 7796022f by FritzFlorian

Fix matrix multiplication benchmark for new scheduler.

parent 01596ff3
Pipeline #1393 failed with stages
in 26 seconds
#include "pls/internal/scheduling/scheduler.h" #include "pls/internal/scheduling/scheduler.h"
#include "pls/internal/scheduling/parallel_result.h" #include "pls/internal/scheduling/static_scheduler_memory.h"
#include "pls/internal/scheduling/scheduler_memory.h"
#include "pls/algorithms/for_each.h" #include "pls/algorithms/for_each.h"
using namespace pls::internal::scheduling; using namespace pls::internal::scheduling;
...@@ -15,17 +14,20 @@ class pls_matrix : public matrix::matrix<T, SIZE> { ...@@ -15,17 +14,20 @@ class pls_matrix : public matrix::matrix<T, SIZE> {
public: public:
pls_matrix() : matrix::matrix<T, SIZE>() {} pls_matrix() : matrix::matrix<T, SIZE>() {}
parallel_result<int> pls_multiply(const matrix::matrix<T, SIZE> &a, const matrix::matrix<T, SIZE> &b) { void pls_multiply(const matrix::matrix<T, SIZE> &a, const matrix::matrix<T, SIZE> &b) {
return pls::algorithm::for_each_range(0, SIZE, [this, &a, &b](int i) { pls::algorithm::for_each_range(0, SIZE, [this, &a, &b](int i) {
this->multiply_column(i, a, b); this->multiply_column(i, a, b);
}); });
} }
}; };
constexpr size_t MAX_NUM_THREADS = 8; constexpr int MAX_NUM_THREADS = 8;
constexpr size_t MAX_NUM_TASKS = 32; constexpr int MAX_NUM_TASKS = 32;
constexpr size_t MAX_NUM_CONTS = 32; constexpr int MAX_STACK_SIZE = 1024 * 1;
constexpr size_t MAX_CONT_SIZE = 512;
static_scheduler_memory<MAX_NUM_THREADS,
MAX_NUM_TASKS,
MAX_STACK_SIZE> global_scheduler_memory;
int main(int argc, char **argv) { int main(int argc, char **argv) {
int num_threads; int num_threads;
...@@ -40,40 +42,20 @@ int main(int argc, char **argv) { ...@@ -40,40 +42,20 @@ int main(int argc, char **argv) {
pls_matrix<double, matrix::MATRIX_SIZE> b; pls_matrix<double, matrix::MATRIX_SIZE> b;
pls_matrix<double, matrix::MATRIX_SIZE> result; pls_matrix<double, matrix::MATRIX_SIZE> result;
static_scheduler_memory<MAX_NUM_THREADS, scheduler scheduler{global_scheduler_memory, (unsigned) num_threads};
MAX_NUM_TASKS,
MAX_NUM_CONTS,
MAX_CONT_SIZE> static_scheduler_memory;
scheduler scheduler{static_scheduler_memory, (unsigned int) num_threads};
for (int i = 0; i < matrix::WARMUP_ITERATIONS; i++) {
scheduler.perform_work([&]() { scheduler.perform_work([&]() {
return scheduler::par([&]() { for (int i = 0; i < matrix::WARMUP_ITERATIONS; i++) {
return result.pls_multiply(a, b); result.pls_multiply(a, b);
}, []() {
return parallel_result<int>{0};
}).then([&](int, int) {
return parallel_result<int>{0};
});
});
} }
});
for (int i = 0; i < matrix::NUM_ITERATIONS; i++) {
scheduler.perform_work([&]() { scheduler.perform_work([&]() {
for (int i = 0; i < matrix::NUM_ITERATIONS; i++) {
runner.start_iteration(); runner.start_iteration();
result.pls_multiply(a, b);
return scheduler::par([&]() {
return result.pls_multiply(a, b);
}, []() {
return parallel_result<int>{0};
}).then([&](int, int) {
runner.end_iteration(); runner.end_iteration();
return parallel_result<int>{0};
});
});
} }
});
runner.commit_results(true); runner.commit_results(true);
} }
...@@ -2,8 +2,6 @@ ...@@ -2,8 +2,6 @@
#ifndef PLS_PARALLEL_FOR_H #ifndef PLS_PARALLEL_FOR_H
#define PLS_PARALLEL_FOR_H #define PLS_PARALLEL_FOR_H
#include "pls/internal/scheduling/parallel_result.h"
namespace pls { namespace pls {
namespace algorithm { namespace algorithm {
...@@ -11,24 +9,24 @@ class fixed_strategy; ...@@ -11,24 +9,24 @@ class fixed_strategy;
class dynamic_strategy; class dynamic_strategy;
template<typename Function, typename ExecutionStrategy> template<typename Function, typename ExecutionStrategy>
pls::internal::scheduling::parallel_result<int> for_each_range(unsigned long first, void for_each_range(unsigned long first,
unsigned long last, unsigned long last,
const Function &function, const Function &function,
ExecutionStrategy &execution_strategy); ExecutionStrategy &execution_strategy);
template<typename Function> template<typename Function>
pls::internal::scheduling::parallel_result<int> for_each_range(unsigned long first, void for_each_range(unsigned long first,
unsigned long last, unsigned long last,
const Function &function); const Function &function);
template<typename RandomIt, typename Function, typename ExecutionStrategy> template<typename RandomIt, typename Function, typename ExecutionStrategy>
pls::internal::scheduling::parallel_result<int> for_each(RandomIt first, void for_each(RandomIt first,
RandomIt last, RandomIt last,
const Function &function, const Function &function,
ExecutionStrategy execution_strategy); ExecutionStrategy execution_strategy);
template<typename RandomIt, typename Function> template<typename RandomIt, typename Function>
pls::internal::scheduling::parallel_result<int> for_each(RandomIt first, void for_each(RandomIt first,
RandomIt last, RandomIt last,
const Function &function); const Function &function);
......
...@@ -11,7 +11,7 @@ namespace algorithm { ...@@ -11,7 +11,7 @@ namespace algorithm {
namespace internal { namespace internal {
template<typename RandomIt, typename Function> template<typename RandomIt, typename Function>
pls::internal::scheduling::parallel_result<int> for_each(const RandomIt first, void for_each(const RandomIt first,
const RandomIt last, const RandomIt last,
const Function function, const Function function,
const long min_elements) { const long min_elements) {
...@@ -23,25 +23,23 @@ pls::internal::scheduling::parallel_result<int> for_each(const RandomIt first, ...@@ -23,25 +23,23 @@ pls::internal::scheduling::parallel_result<int> for_each(const RandomIt first,
for (auto current = first; current != last; current++) { for (auto current = first; current != last; current++) {
function(*current); function(*current);
} }
return parallel_result<int>{0};
} else { } else {
// Cut in half recursively // Cut in half recursively
const long middle_index = num_elements / 2; const long middle_index = num_elements / 2;
return scheduler::par([first, middle_index, last, function, min_elements] { scheduler::spawn([first, middle_index, last, &function, min_elements] {
return internal::for_each(first, return internal::for_each(first,
first + middle_index, first + middle_index,
function, function,
min_elements); min_elements);
}, [first, middle_index, last, function, min_elements] { });
scheduler::spawn([first, middle_index, last, &function, min_elements] {
return internal::for_each(first + middle_index, return internal::for_each(first + middle_index,
last, last,
function, function,
min_elements); min_elements);
}).then([](int, int) {
return parallel_result<int>{0};
}); });
scheduler::sync();
} }
} }
...@@ -52,7 +50,7 @@ class dynamic_strategy { ...@@ -52,7 +50,7 @@ class dynamic_strategy {
explicit dynamic_strategy(const unsigned int tasks_per_thread = 4) : tasks_per_thread_{tasks_per_thread} {}; explicit dynamic_strategy(const unsigned int tasks_per_thread = 4) : tasks_per_thread_{tasks_per_thread} {};
long calculate_min_elements(long num_elements) const { long calculate_min_elements(long num_elements) const {
const long num_threads = pls::internal::scheduling::thread_state::get().scheduler_->num_threads(); const long num_threads = pls::internal::scheduling::thread_state::get().get_scheduler().num_threads();
return num_elements / (num_threads * tasks_per_thread_); return num_elements / (num_threads * tasks_per_thread_);
} }
private: private:
...@@ -71,21 +69,27 @@ class fixed_strategy { ...@@ -71,21 +69,27 @@ class fixed_strategy {
}; };
template<typename RandomIt, typename Function, typename ExecutionStrategy> template<typename RandomIt, typename Function, typename ExecutionStrategy>
pls::internal::scheduling::parallel_result<int> for_each(RandomIt first, void for_each(RandomIt
first,
RandomIt last, RandomIt last,
const Function &function, const Function &function,
ExecutionStrategy execution_strategy) { ExecutionStrategy
execution_strategy) {
long num_elements = std::distance(first, last); long num_elements = std::distance(first, last);
return internal::for_each(first, last, function, execution_strategy.calculate_min_elements(num_elements)); return
internal::for_each(first, last, function, execution_strategy
.
calculate_min_elements(num_elements)
);
} }
template<typename RandomIt, typename Function> template<typename RandomIt, typename Function>
pls::internal::scheduling::parallel_result<int> for_each(RandomIt first, RandomIt last, const Function &function) { void for_each(RandomIt first, RandomIt last, const Function &function) {
return for_each(first, last, function, dynamic_strategy{4}); return for_each(first, last, function, dynamic_strategy{4});
} }
template<typename Function, typename ExecutionStrategy> template<typename Function, typename ExecutionStrategy>
pls::internal::scheduling::parallel_result<int> for_each_range(unsigned long first, void for_each_range(unsigned long first,
unsigned long last, unsigned long last,
const Function &function, const Function &function,
ExecutionStrategy execution_strategy) { ExecutionStrategy execution_strategy) {
...@@ -94,7 +98,7 @@ pls::internal::scheduling::parallel_result<int> for_each_range(unsigned long fir ...@@ -94,7 +98,7 @@ pls::internal::scheduling::parallel_result<int> for_each_range(unsigned long fir
} }
template<typename Function> template<typename Function>
pls::internal::scheduling::parallel_result<int> for_each_range(unsigned long first, void for_each_range(unsigned long first,
unsigned long last, unsigned long last,
const Function &function) { const Function &function) {
auto range = pls::internal::helpers::range(first, last); auto range = pls::internal::helpers::range(first, last);
......
...@@ -112,7 +112,7 @@ struct basic_range { ...@@ -112,7 +112,7 @@ struct basic_range {
: r(rhs.r), index(rhs.index) {} : r(rhs.r), index(rhs.index) {}
const_iterator_impl(basic_range<IntegerType> const *p_range, size_type p_index) const_iterator_impl(basic_range<IntegerType> const *p_range, size_type p_index)
: r(*p_range), index(p_index) {} : r(p_range), index(p_index) {}
const_iterator_impl &operator=(const const_iterator_impl &rhs) { const_iterator_impl &operator=(const const_iterator_impl &rhs) {
r = rhs.r; r = rhs.r;
...@@ -121,7 +121,7 @@ struct basic_range { ...@@ -121,7 +121,7 @@ struct basic_range {
} }
bool operator==(const const_iterator_impl &rhs) const { bool operator==(const const_iterator_impl &rhs) const {
return r == rhs.r && index == rhs.index; return *r == *(rhs.r) && index == rhs.index;
} }
bool operator!=(const const_iterator_impl &rhs) const { bool operator!=(const const_iterator_impl &rhs) const {
...@@ -145,7 +145,7 @@ struct basic_range { ...@@ -145,7 +145,7 @@ struct basic_range {
} }
value_type operator*() const { value_type operator*() const {
return r.m_first_element + r.m_step * index; return r->m_first_element + r->m_step * index;
} }
// operator-> // operator->
...@@ -212,11 +212,11 @@ struct basic_range { ...@@ -212,11 +212,11 @@ struct basic_range {
value_type operator[](difference_type offset) const { value_type operator[](difference_type offset) const {
size_type new_index = index + offset; size_type new_index = index + offset;
return r.m_first_element + r.m_step * new_index; return r->m_first_element + r->m_step * new_index;
} }
private: private:
basic_range<IntegerType> r; basic_range<IntegerType> const *r;
size_type index; size_type index;
}; };
...@@ -236,7 +236,7 @@ struct basic_range { ...@@ -236,7 +236,7 @@ struct basic_range {
: r(rhs.r), index(rhs.index) {} : r(rhs.r), index(rhs.index) {}
const_reverse_iterator_impl(basic_range<IntegerType> const *p_range, size_type p_index) const_reverse_iterator_impl(basic_range<IntegerType> const *p_range, size_type p_index)
: r(*p_range), index(p_index) {} : r(p_range), index(p_index) {}
const_reverse_iterator_impl &operator=(const const_reverse_iterator_impl &rhs) { const_reverse_iterator_impl &operator=(const const_reverse_iterator_impl &rhs) {
r = rhs.r; r = rhs.r;
...@@ -245,7 +245,7 @@ struct basic_range { ...@@ -245,7 +245,7 @@ struct basic_range {
} }
bool operator==(const const_reverse_iterator_impl &rhs) const { bool operator==(const const_reverse_iterator_impl &rhs) const {
return r == rhs.r && index == rhs.index; return *r == *(rhs.r) && index == rhs.index;
} }
bool operator!=(const const_reverse_iterator_impl &rhs) const { bool operator!=(const const_reverse_iterator_impl &rhs) const {
...@@ -270,8 +270,8 @@ struct basic_range { ...@@ -270,8 +270,8 @@ struct basic_range {
value_type operator*() const { value_type operator*() const {
size_type reverse_index size_type reverse_index
= (r.m_element_count - 1) - index; = (r->m_element_count - 1) - index;
return r.m_first_element + r.m_step * reverse_index; return r->m_first_element + r->m_step * reverse_index;
} }
// operator-> // operator->
...@@ -338,12 +338,12 @@ struct basic_range { ...@@ -338,12 +338,12 @@ struct basic_range {
value_type operator[](difference_type offset) const { value_type operator[](difference_type offset) const {
size_type new_reverse_index size_type new_reverse_index
= (r.m_element_count - 1) - (index + offset); = (r->m_element_count - 1) - (index + offset);
return r.m_first_element + r.m_step * new_reverse_index; return r->m_first_element + r->m_step * new_reverse_index;
} }
private: private:
basic_range<IntegerType> r; basic_range<IntegerType> const *r;
size_type index; size_type index;
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment