Skip to content

Commit

Permalink
Move Vc math overloads to algebra-plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
niermann999 committed Nov 29, 2024
1 parent 868c244 commit 0f80d49
Show file tree
Hide file tree
Showing 52 changed files with 663 additions and 400 deletions.
5 changes: 3 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,10 @@ endif()

# Set up the Algebra Plugin libraries.
add_subdirectory( common )
add_subdirectory( storage )
add_subdirectory( math )
add_subdirectory( frontend )
add_subdirectory( math )
add_subdirectory( storage )
add_subdirectory( utils )

# Set up the test(s).
if( BUILD_TESTING AND ALGEBRA_PLUGINS_BUILD_TESTING )
Expand Down
4 changes: 3 additions & 1 deletion benchmarks/array/include/benchmark/array/data_generator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ inline void fill_random_trf(std::vector<transform3_t> &collection) {
1.f);

auto rand_obj = [&]() {
vector_t x_axis, z_axis, t;
vector_t x_axis;
vector_t z_axis;
vector_t t;

x_axis = vector::normalize(vector_t{dist(mt), dist(mt), dist(mt)});
z_axis = {dist(mt), dist(mt), dist(mt)};
Expand Down
20 changes: 9 additions & 11 deletions benchmarks/common/include/benchmark/common/benchmark_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,15 @@ struct benchmark_base {
/// @}

/// Print configuration
friend std::ostream& operator<<(std::ostream& os, const configuration& c);
friend std::ostream& operator<<(std::ostream& os,
const benchmark_base::configuration& cfg) {
os << " -> running:\t " << cfg.n_samples() << " samples" << std::endl;
if (cfg.do_sleep()) {
os << " -> cool down:\t " << cfg.n_sleep() << "s" << std::endl;
}
os << std::endl;
return os;
}
};

/// The benchmark configuration
Expand All @@ -77,14 +85,4 @@ struct benchmark_base {
virtual void operator()(::benchmark::State&) = 0;
};

std::ostream& operator<<(std::ostream& os,
const benchmark_base::configuration& cfg) {
os << " -> running:\t " << cfg.n_samples() << " samples" << std::endl;
if (cfg.do_sleep()) {
os << " -> cool down:\t " << cfg.n_sleep() << "s" << std::endl;
}
os << std::endl;
return os;
}

} // namespace algebra
12 changes: 7 additions & 5 deletions benchmarks/common/include/benchmark/common/benchmark_vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ struct vector_bm : public benchmark_base {
/// Prefix for the benchmark name
inline static const std::string name{"vector"};

std::vector<vector_t> a, b, results;
std::vector<vector_t> a;
std::vector<vector_t> b;
std::vector<vector_t> results;

/// No default construction: Cannot prepare data
vector_bm() = delete;
Expand Down Expand Up @@ -82,8 +84,8 @@ requires std::invocable<unaryOP, vector_t<scalar_t>> struct vector_unaryOP_bm
// Run the benchmark
for (auto _ : state) {
for (std::size_t i{0}; i < n_samples; ++i) {
result_t result = unaryOP{}(this->a[i]);
::benchmark::DoNotOptimize(const_cast<const result_t &>(result));
const result_t result = unaryOP{}(this->a[i]);
::benchmark::DoNotOptimize(result);
}
}
}
Expand Down Expand Up @@ -117,8 +119,8 @@ requires std::invocable<binaryOP, vector_t<scalar_t>,
// Run the benchmark
for (auto _ : state) {
for (std::size_t i{0}; i < n_samples; ++i) {
result_t result = binaryOP{}(this->a[i], this->b[i]);
::benchmark::DoNotOptimize(const_cast<const result_t &>(result));
const result_t result = binaryOP{}(this->a[i], this->b[i]);
::benchmark::DoNotOptimize(result);
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion benchmarks/eigen/include/benchmark/eigen/data_generator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ inline void fill_random_trf(std::vector<transform3_t> &collection) {
using vector_t = typename transform3_t::vector3;

auto rand_obj = []() {
vector_t x_axis, z_axis, t;
vector_t x_axis;
vector_t z_axis;
vector_t t;

x_axis = vector::normalize(vector_t::Random());
z_axis = vector_t::Random();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ inline void fill_random_trf(std::vector<transform3_t> &collection) {
// Generate a random, but valid affine transformation
auto rand_obj = []() {
using vector_t = typename transform3_t::vector3;
vector_t x_axis, z_axis, t;
vector_t x_axis;
vector_t z_axis;
vector_t t;

x_axis = vector_t{vector_t::array_type::Random()};
x_axis = vector::normalize(x_axis);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,13 @@ template <concepts::transform3D transform3_t>
inline void fill_random_trf(std::vector<transform3_t> &collection) {
// Generate a random, but valid affine transformation
auto rand_obj = []() {
using vector_t = typename transform3_t::vector3;
using simd_vector_t = typename transform3_t::scalar_type;
typename transform3_t::vector3 x_axis, z_axis, t;

vector_t x_axis;
vector_t z_axis;
vector_t t;

x_axis[0] = simd_vector_t::Random();
x_axis[1] = simd_vector_t::Random();
x_axis[2] = simd_vector_t::Random();
Expand Down
16 changes: 10 additions & 6 deletions common/include/algebra/concepts.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,15 @@ concept value = algebra::concepts::arithmetic<std::decay_t<T>>;
template <typename T>
concept scalar = !algebra::traits::is_matrix<T> &&
!algebra::traits::is_vector<T> && requires(T a, T b) {
{ a + b } -> std::convertible_to<T>;
{ a - b } -> std::convertible_to<T>;
{ a* b } -> std::convertible_to<T>;
{ a / b } -> std::convertible_to<T>;
};
{ a + b }
->std::convertible_to<T>;
{ a - b }
->std::convertible_to<T>;
{ a* b }
->std::convertible_to<T>;
{ a / b }
->std::convertible_to<T>;
};

/// Check if a scalar is simd
template <typename T>
Expand Down Expand Up @@ -71,7 +75,7 @@ template <typename M>
concept matrix = algebra::traits::is_matrix<M>;

template <typename M>
concept square_matrix = matrix<M> && algebra::traits::is_square<M>;
concept square_matrix = matrix<M>&& algebra::traits::is_square<M>;

template <typename M>
concept row_matrix = matrix<M> && (algebra::traits::rows<M> == 1);
Expand Down
21 changes: 13 additions & 8 deletions common/include/algebra/type_traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ struct dimensions {

/// Specilization for scalar types
template <typename M>
requires std::is_fundamental_v<M>
struct dimensions<M> {
requires std::is_fundamental_v<M> struct dimensions<M> {

using size_type = std::size_t;

Expand Down Expand Up @@ -138,11 +137,11 @@ template <typename T>
struct get_algebra {};

template <typename T>
requires(!std::is_same_v<typename T::point3D, void>)
struct get_algebra<T> {
requires(!std::is_same_v<typename T::point3D, void>) struct get_algebra<T> {
template <typename U>
using simd = typename T::template simd<U>;
using size_type = typename T::size_type;
using boolean = typename T::boolean;
using value = typename T::value_type;
using scalar = typename T::scalar;
using point2D = typename T::point2D;
Expand All @@ -156,11 +155,20 @@ struct get_algebra<T> {
} // namespace traits

template <typename A>
using get_scalar_t = typename traits::get_algebra<A>::scalar;
using get_value_t = typename traits::get_algebra<A>::value;

template <typename A>
using get_boolean_t = typename traits::get_algebra<A>::boolean;

template <typename A, typename T>
using get_simd_t = typename traits::get_algebra<A>::template simd<T>;

template <typename A>
using get_size_t = typename traits::get_algebra<A>::size_type;

template <typename A>
using get_scalar_t = typename traits::get_algebra<A>::scalar;

template <typename A>
using get_point2D_t = typename traits::get_algebra<A>::point2D;

Expand All @@ -173,9 +181,6 @@ using get_vector3D_t = typename traits::get_algebra<A>::vector3D;
template <typename A>
using get_transform3D_t = typename traits::get_algebra<A>::transform3D;

template <typename A>
using get_size_t = typename traits::get_algebra<A>::size_type;

template <typename A, std::size_t R, std::size_t C>
using get_matrix_t = typename traits::get_algebra<A>::template matrix<R, C>;
/// @}
Expand Down
42 changes: 19 additions & 23 deletions frontend/array_cmath/include/algebra/array_cmath.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
// Project include(s).
#include "algebra/math/cmath.hpp"
#include "algebra/math/generic.hpp"
#include "algebra/print.hpp"
#include "algebra/storage/array.hpp"

/// @name Operators on @c algebra::array::storage_type
Expand All @@ -20,9 +19,6 @@ using algebra::cmath::operator*;
using algebra::cmath::operator-;
using algebra::cmath::operator+;

/// Print the linear algebra types of this backend
using algebra::operator<<;

/// @}

namespace algebra {
Expand Down Expand Up @@ -119,27 +115,27 @@ namespace plugin {

/// Define the plugin types
/// @{
template <typename V>
struct cmath {
/// Define scalar type
using value_type = V;

template <typename T>
using simd = T;

using boolean = bool;
using scalar = value_type;
using size_type = algebra::array::size_type;
using transform3D = algebra::array::transform3<value_type>;
using point2D = algebra::array::point2<value_type>;
using point3D = algebra::array::point3<value_type>;
using vector3D = algebra::array::vector3<value_type>;

template <std::size_t ROWS, std::size_t COLS>
using matrix = algebra::array::matrix_type<value_type, ROWS, COLS>;
template <concepts::value V>
struct array {
/// Define scalar type
using value_type = V;

template <concepts::value T>
using simd = T;

using boolean = bool;
using scalar = value_type;
using size_type = algebra::array::size_type;
using transform3D = algebra::array::transform3<value_type>;
using point2D = algebra::array::point2<value_type>;
using point3D = algebra::array::point3<value_type>;
using vector3D = algebra::array::vector3<value_type>;

template <std::size_t ROWS, std::size_t COLS>
using matrix = algebra::array::matrix_type<value_type, ROWS, COLS>;
};
/// @}

} // namespace plugin
} // namespace plugin

} // namespace algebra
40 changes: 18 additions & 22 deletions frontend/eigen_eigen/include/algebra/eigen_eigen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

// Project include(s).
#include "algebra/math/eigen.hpp"
#include "algebra/print.hpp"
#include "algebra/storage/eigen.hpp"

// Eigen include(s).
Expand All @@ -21,9 +20,6 @@
#pragma warning(pop)
#endif // MSVC

/// Print the linear algebra types of this backend
using algebra::operator<<;

namespace algebra {

namespace getter {
Expand Down Expand Up @@ -84,27 +80,27 @@ namespace plugin {

/// Define the plugin types
/// @{
template <typename V>
template <concepts::value V>
struct eigen {
/// Define scalar type
using value_type = V;

template <typename T>
using simd = T;

using boolean = bool;
using scalar = value_type;
using size_type = algebra::eigen::size_type;
using transform3D = algebra::eigen::transform3<value_type>;
using point2D = algebra::eigen::point2<value_type>;
using point3D = algebra::eigen::point3<value_type>;
using vector3D = algebra::eigen::vector3<value_type>;

template <std::size_t ROWS, std::size_t COLS>
using matrix = algebra::eigen::matrix_type<value_type, ROWS, COLS>;
/// Define scalar type
using value_type = V;

template <concepts::value T>
using simd = T;

using boolean = bool;
using scalar = value_type;
using size_type = algebra::eigen::size_type;
using transform3D = algebra::eigen::transform3<value_type>;
using point2D = algebra::eigen::point2<value_type>;
using point3D = algebra::eigen::point3<value_type>;
using vector3D = algebra::eigen::vector3<value_type>;

template <std::size_t ROWS, std::size_t COLS>
using matrix = algebra::eigen::matrix_type<value_type, ROWS, COLS>;
};
/// @}

} // namespace plugin
} // namespace plugin

} // namespace algebra
Loading

0 comments on commit 0f80d49

Please sign in to comment.