Skip to content

Commit

Permalink
Done with math library.
Browse files Browse the repository at this point in the history
  • Loading branch information
martun committed Jan 29, 2024
1 parent f964219 commit 07bf357
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 22 deletions.
7 changes: 4 additions & 3 deletions include/nil/crypto3/math/multithreading/thread_pool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ namespace nil {
std::function<ReturnType(std::size_t begin, std::size_t end)> func) {

std::vector<std::future<ReturnType>> fut;
std::size_t cpu_usage = std::min(elements_count, pool_size);
std::size_t cpu_usage = std::max((size_t)1, std::min(elements_count, pool_size));
std::size_t element_per_cpu = elements_count / cpu_usage;

// Pool #0 will take care of the lowest level of operations, like polynomial operations.
Expand All @@ -86,12 +86,13 @@ namespace nil {
element_per_cpu = elements_count / cpu_usage;
}

std::size_t begin = 0;
for (int i = 0; i < cpu_usage; i++) {
auto begin = element_per_cpu * i;
auto end = (i == cpu_usage - 1) ? elements_count : element_per_cpu * (i + 1);
auto end = begin + (elements_count - begin) / (cpu_usage - i);
fut.emplace_back(post<ReturnType>([begin, end, func]() {
return func(begin, end);
}));
begin = end;
}
return fut;
}
Expand Down
17 changes: 9 additions & 8 deletions include/nil/crypto3/math/polynomial/basic_operations.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include <nil/crypto3/math/algorithms/unity_root.hpp>
#include <nil/crypto3/math/domains/detail/basic_radix2_domain_aux.hpp>
#include <nil/crypto3/math/detail/field_utils.hpp>
#include <nil/crypto3/math/multithreading/parallelization_utils.hpp>

namespace nil {
namespace crypto3 {
Expand Down Expand Up @@ -101,12 +102,12 @@ namespace nil {

if (a_size > b_size) {
c.resize(a_size);
std::transform(
nil::crypto3::parallel_transform(
std::begin(b), std::end(b), std::begin(a), std::begin(c), std::plus<value_type>());
std::copy(std::begin(a) + b_size, std::end(a), std::begin(c) + b_size);
} else {
c.resize(b_size);
std::transform(
nil::crypto3::parallel_transform(
std::begin(a), std::end(a), std::begin(b), std::begin(c), std::plus<value_type>());
std::copy(std::begin(b) + a_size, std::end(b), std::begin(c) + a_size);
}
Expand All @@ -129,19 +130,19 @@ namespace nil {
c = a;
} else if (is_zero(a)) {
c.resize(b.size());
std::transform(b.begin(), b.end(), c.begin(), std::negate<value_type>());
nil::crypto3::parallel_transform(b.begin(), b.end(), c.begin(), std::negate<value_type>());
} else {
std::size_t a_size = a.size();
std::size_t b_size = b.size();

if (a_size > b_size) {
c.resize(a_size);
std::transform(a.begin(), a.begin() + b_size, b.begin(), c.begin(), std::minus<value_type>());
nil::crypto3::parallel_transform(a.begin(), a.begin() + b_size, b.begin(), c.begin(), std::minus<value_type>());
std::copy(a.begin() + b_size, a.end(), c.begin() + b_size);
} else {
c.resize(b_size);
std::transform(a.begin(), a.end(), b.begin(), c.begin(), std::minus<value_type>());
std::transform(b.begin() + a_size, b.end(), c.begin() + a_size, std::negate<value_type>());
nil::crypto3::parallel_transform(a.begin(), a.end(), b.begin(), c.begin(), std::minus<value_type>());
nil::crypto3::parallel_transform(b.begin() + a_size, b.end(), c.begin() + a_size, std::negate<value_type>());
}
}

Expand Down Expand Up @@ -239,7 +240,7 @@ namespace nil {
if (d == 0) {
value_type c = b[0].inversed();
q.resize(a.size());
std::transform(
nil::crypto3::parallel_transform(
std::begin(a), std::end(a), std::begin(q), [&c](const value_type& value) {return value * c;});
// We will always have no reminder here.
r.resize(1);
Expand Down Expand Up @@ -279,7 +280,7 @@ namespace nil {
if (b.size() + shift + 1 > r.size())
r.resize(b.size() + shift + 1);
auto glambda = [=](value_type x, value_type y) { return y - (x * lead_coeff); };
std::transform(b.begin(), b.end(), r.begin() + shift, r.begin() + shift, glambda);
nil::crypto3::parallel_transform(b.begin(), b.end(), r.begin() + shift, r.begin() + shift, glambda);

condense(r);
r_deg = r.size() - 1;
Expand Down
15 changes: 11 additions & 4 deletions include/nil/crypto3/math/polynomial/polynomial_dfs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -527,10 +527,15 @@ namespace nil {
if (this->size() > other.size()) {
polynomial_dfs tmp(other);
tmp.resize(this->size());
nil::crypto3::parallel_transform(this->begin(), this->end(), tmp.begin(), this->begin(), std::minus<FieldValueType>());

nil::crypto3::in_place_parallel_transform(this->begin(), this->end(), tmp.begin(),
[](FieldValueType& v1, const FieldValueType& v2){v1-=v2;});

return *this;
}
nil::crypto3::parallel_transform(this->begin(), this->end(), other.begin(), this->begin(), std::minus<FieldValueType>());

nil::crypto3::in_place_parallel_transform(this->begin(), this->end(), other.begin(),
[](FieldValueType& v1, const FieldValueType& v2){v1-=v2;});
return *this;
}

Expand Down Expand Up @@ -590,11 +595,13 @@ namespace nil {
polynomial_dfs tmp(other);
tmp.resize(polynomial_s);

nil::crypto3::parallel_transform(tmp.begin(), tmp.end(), this->begin(), this->begin(), std::multiplies<FieldValueType>());
nil::crypto3::in_place_parallel_transform(this->begin(), this->end(), tmp.begin(),
[](FieldValueType& v1, const FieldValueType& v2){v1*=v2;});
return *this;
}

nil::crypto3::parallel_transform(this->begin(), this->end(), other.begin(), this->begin(), std::multiplies<FieldValueType>());
nil::crypto3::in_place_parallel_transform(this->begin(), this->end(), other.begin(),
[](FieldValueType& v1, const FieldValueType& v2){v1*=v2;});
return *this;
}

Expand Down
7 changes: 4 additions & 3 deletions include/nil/crypto3/math/polynomial/polynomial_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <vector>

#include <nil/crypto3/math/polynomial/basic_operations.hpp>
#include <nil/crypto3/math/multithreading/parallelization_utils.hpp>

namespace nil {
namespace crypto3 {
Expand Down Expand Up @@ -352,7 +353,7 @@ namespace nil {

// polynomial_view operator-() const {
void neg() {
std::transform(this->begin(), this->end(), this->begin(), std::negate<FieldValueType>());
nil::crypto3::parallel_transform(this->begin(), this->end(), this->begin(), std::negate<FieldValueType>());
}

/**
Expand Down Expand Up @@ -400,7 +401,7 @@ namespace nil {
auto glambda = [=](const FieldValueType& x, const FieldValueType& y) {
return y - (x * lead_coeff);
};
std::transform(other.begin(), other.end(), r.begin() + shift, r.begin() + shift, glambda);
nil::crypto3::parallel_transform(other.begin(), other.end(), r.begin() + shift, r.begin() + shift, glambda);
r.condense();

r_deg = r.size() - 1;
Expand Down Expand Up @@ -433,7 +434,7 @@ namespace nil {
auto glambda = [=](const FieldValueType& x, const FieldValueType& y) {
return y - (x * lead_coeff);
};
std::transform(other.begin(), other.end(), this->begin() + shift, this->begin() + shift, glambda);
nil::crypto3::parallel_transform(other.begin(), other.end(), this->begin() + shift, this->begin() + shift, glambda);
this->condense();

r_deg = this->size() - 1;
Expand Down
20 changes: 16 additions & 4 deletions test/polynomial_dfs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1347,12 +1347,24 @@ BOOST_AUTO_TEST_CASE(polynomial_dfs_resize_perf_test, *boost::unit_test::disable
values.push_back(nil::crypto3::algebra::random_element<FieldType>());
}

polynomial_dfs<typename FieldType::value_type> poly = {
size - 1, values};

auto start = std::chrono::high_resolution_clock::now();
for (int i = 0; i < 10; ++i) {
polynomial_dfs<typename FieldType::value_type> poly = {
size - 1, values};
poly.resize(8 * size);
BOOST_CHECK(poly.size() == 8 * size);
auto poly2 = poly;
poly2.resize(8 * size);

BOOST_CHECK(poly2.size() == 8 * size);
}

// Record the end time
auto end = std::chrono::high_resolution_clock::now();

// Calculate the duration
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start);

std::cout << "Resize time: " << duration.count() << " microseconds." << std::endl;
}

BOOST_AUTO_TEST_SUITE_END()

0 comments on commit 07bf357

Please sign in to comment.