Skip to content

Commit

Permalink
include SO3 backend implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
skim0119 committed Jun 26, 2024
1 parent d612817 commit fb7f32d
Show file tree
Hide file tree
Showing 13 changed files with 624 additions and 0 deletions.
14 changes: 14 additions & 0 deletions backend/src/Systems/States/Expressions/backends.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once

//******************************************************************************
// Includes
//******************************************************************************
///
#include "Systems/States/Expressions/backends/Declarations.hpp"
///

#define ELASTICA_USE_BLAZE 1 // todo : from CMAKE

#if defined(ELASTICA_USE_BLAZE)
#include "Systems/States/Expressions/backends/blaze.hpp"
#endif // ELASTICA_USE_BLAZE
27 changes: 27 additions & 0 deletions backend/src/Systems/States/Expressions/backends/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Distributed under the MIT License. See LICENSE.txt for details.

set(LIBRARY StateExpressionBackends)

add_elastica_library(${LIBRARY} INTERFACE)

elastica_target_headers(
${LIBRARY}
INCLUDE_DIRECTORY ${CMAKE_SOURCE_DIR}/elastica
blaze.hpp
Declarations.hpp
)

elastica_target_sources(
${LIBRARY}
INTERFACE
blaze.hpp
Declarations.hpp
)

add_subdirectory(blaze)

target_link_libraries(
${LIBRARY}
INTERFACE
SO3PrimitiveAddAssignKernel
)
27 changes: 27 additions & 0 deletions backend/src/Systems/States/Expressions/backends/Declarations.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once

//******************************************************************************
// Includes
//******************************************************************************
#include <cstddef> // std::size_t

namespace elastica {

namespace states {

// assign version doesnt care about vectorization
template <typename LHSMatrix, typename RHSMatrix, typename RHSVector>
auto SO3_primitive_assign(LHSMatrix& lhs, RHSMatrix const& rhs_matrix,
RHSVector const& rhs_vector) noexcept -> void;
// add assign version doesnt care about vectorization
template <typename LHSMatrix, typename RHSVector>
auto SO3_primitive_add_assign(LHSMatrix& lhs,
RHSVector const& rhs_vector) noexcept -> void;
template <typename Type>
inline auto size_backend(Type const&) noexcept -> std::size_t;
template <typename Type>
inline auto resize_backend(Type&, std::size_t) -> void;

} // namespace states

} // namespace elastica
5 changes: 5 additions & 0 deletions backend/src/Systems/States/Expressions/backends/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## backends

To interface the requirements of SO3 and SE3 templates with any custom type that the user provides, we need a couple of
interface functions to connect the frontend (state templates) to the backend data type. This folder contains all such
interface functions for commonly used elastica types.
11 changes: 11 additions & 0 deletions backend/src/Systems/States/Expressions/backends/blaze.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

//******************************************************************************
// Includes
//******************************************************************************
#include "Systems/States/Expressions/backends/Declarations.hpp"
///
#include "Systems/States/Expressions/backends/blaze/Resize.hpp"
#include "Systems/States/Expressions/backends/blaze/SO3PrimitiveAddAssign.hpp"
#include "Systems/States/Expressions/backends/blaze/SO3PrimitiveAssign.hpp"
#include "Systems/States/Expressions/backends/blaze/Size.hpp"
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Distributed under the MIT License. See LICENSE.txt for details.

set(LIBRARY SO3PrimitiveAddAssignKernel)

add_elastica_library(${LIBRARY} INTERFACE)

elastica_target_headers(
${LIBRARY}
INCLUDE_DIRECTORY ${CMAKE_SOURCE_DIR}/elastica
SO3PrimitiveAddAssign/BaseTemplate.hpp
SO3PrimitiveAddAssign/Scalar.hpp
SO3PrimitiveAddAssign/SIMD.hpp
Resize.hpp
Size.hpp
SO3PrimitiveAddAssign.hpp
SO3PrimitiveAssign.hpp
)

elastica_target_sources(
${LIBRARY}
INTERFACE
SO3PrimitiveAddAssign/BaseTemplate.hpp
SO3PrimitiveAddAssign/Scalar.hpp
SO3PrimitiveAddAssign/SIMD.hpp
Resize.hpp
Size.hpp
SO3PrimitiveAddAssign.hpp
SO3PrimitiveAssign.hpp
)


target_link_libraries(
${LIBRARY}
INTERFACE
Utilities
Blaze
Frames
ModuleSettings
)
46 changes: 46 additions & 0 deletions backend/src/Systems/States/Expressions/backends/blaze/Resize.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#pragma once

//******************************************************************************
// Includes
//******************************************************************************
#include <cstdint> // std::size_t

#include <blaze/math/DynamicMatrix.h>
#include <blaze_tensor/math/DynamicTensor.h>

//
// this comes from the simulator module, which seems like an anti-pattern
// should be in Systems instead
#include "Simulator/Frames.hpp"
//
#include "Systems/States/Expressions/backends/Declarations.hpp"

namespace elastica {

namespace states {

//**************************************************************************
/*! \cond ELASTICA_INTERNAL */
// the parts below are repeated from the Block module, but they are almost
// always never used in the states module since the size is always expected
// to be
template <typename T>
inline auto resize_backend(blaze::DynamicTensor<T>& data,
std::size_t new_size) -> void {
return data.resize(Frames::Dimension, Frames::Dimension, new_size, true);
}

template <typename Type, // Data type of the matrix
bool SO, // Storage order
typename Alloc, // Type of the allocator
typename Tag> // Type tag
inline auto resize_backend(blaze::DynamicMatrix<Type, SO, Alloc, Tag>& data,
std::size_t new_size) -> void {
return data.resize(Frames::Dimension, new_size, true);
}
/*! \endcond */
//**************************************************************************

} // namespace states

} // namespace elastica
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#pragma once

//******************************************************************************
// Includes
//******************************************************************************
#include <blaze_tensor/math/DynamicTensor.h>
//
#include "Systems/States/Expressions/backends/Declarations.hpp"
#include "Systems/States/Expressions/backends/blaze/SO3PrimitiveAddAssign/BaseTemplate.hpp"
#include "Systems/States/Expressions/backends/blaze/SO3PrimitiveAddAssign/SIMD.hpp"
#include "Systems/States/Expressions/backends/blaze/SO3PrimitiveAddAssign/Scalar.hpp"
#include "Systems/States/Expressions/backends/blaze/SO3PrimitiveAssign.hpp"
#include "Systems/States/Expressions/backends/blaze/Size.hpp"

namespace elastica {

namespace states {

template <typename T, typename RHSVectorBatch>
auto SO3_primitive_add_assign(blaze::DynamicTensor<T>& lhs_matrix_batch,
RHSVectorBatch const& rhs_vector) noexcept
-> void {
/*
* This version creates memory every time
auto temp_to_synch(lhs_matrix_batch); // TODO: avoid making new memory
// (but not sure how)
// The rotation computation cannot be computed asynchronously during
// add_assign.
SO3_primitive_assign(lhs_matrix_batch, temp_to_synch, rhs_vector);
*/

detail::SO3AddAssign<detail::backend_choice<detail::SO3AddAssignKind>()>::
apply(lhs_matrix_batch, rhs_vector);
}

} // namespace states

} // namespace elastica
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#pragma once

#include "Configuration/Kernels.hpp"
#include "ModuleSettings/Vectorization.hpp"

namespace elastica {

namespace states {

namespace detail {

enum class SO3AddAssignKind { scalar, simd };

template <SO3AddAssignKind Op>
struct SO3AddAssign;

template <typename BackendEnumKind>
constexpr auto backend_choice() -> BackendEnumKind;

template <>
constexpr auto backend_choice<SO3AddAssignKind>() -> SO3AddAssignKind {
return ELASTICA_USE_VECTORIZATION
? SO3AddAssignKind::
ELASTICA_COSSERATROD_LIB_SO3_ADDITION_BACKEND
: SO3AddAssignKind::scalar;
}

} // namespace detail

} // namespace states

} // namespace elastica
Loading

0 comments on commit fb7f32d

Please sign in to comment.