Skip to content

Commit

Permalink
#11-make a lot more functions KOKKOS_INLINE_FUNCTION and fix a bug wi…
Browse files Browse the repository at this point in the history
…th computing the bounding box of all contact entities
  • Loading branch information
nmm0 authored and cz4rs committed Sep 18, 2024
1 parent b813c55 commit b48b111
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 66 deletions.
9 changes: 5 additions & 4 deletions src/bvh/collision_object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,16 +103,17 @@ namespace bvh
{
m_clusterer.resize( n );
}
Kokkos::resize( Kokkos::WithoutInitializing, get_split_indices(), n );
auto &ind = get_split_indices();
Kokkos::resize( Kokkos::WithoutInitializing, ind, n );

Kokkos::resize( Kokkos::WithoutInitializing, get_splits(), num_splits );
Kokkos::resize( Kokkos::WithoutInitializing, get_splits_h(), num_splits );

// Initialize our indices
auto split_indices = get_split_indices();
Kokkos::parallel_for( n, KOKKOS_LAMBDA( int _i ) { split_indices( _i ) = _i; } );
Kokkos::parallel_for(
n, KOKKOS_LAMBDA( int _i ) { ind( _i ) = _i; } );

m_clusterer( _data_view, get_split_indices(), get_splits() );
m_clusterer( _data_view, ind, get_splits() );

Kokkos::deep_copy( get_splits_h(), get_splits() );

Expand Down
8 changes: 3 additions & 5 deletions src/bvh/contact_entity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "types.hpp"
#include "hash.hpp"
#include "traits.hpp"
#include <Kokkos_Core.hpp>
#include <limits>
#include "vt/print.hpp"

Expand Down Expand Up @@ -61,7 +62,7 @@ namespace bvh
using size_type = typename view< const Entity * >::size_type;
using traits_type = element_traits< Entity >;

explicit min_diag_bounds_union( const view< const Entity * > &_v )
KOKKOS_INLINE_FUNCTION explicit min_diag_bounds_union( const view< const Entity * > &_v )
: entities( _v )
{}

Expand All @@ -87,7 +88,7 @@ namespace bvh
{
_dst.min[i] = Kokkos::min( _dst.min[i], _src.min[i] );
// Use diag for max right now
_dst.inv_diag[i] = Kokkos::max( _dst.inv_diag[i], _dst.inv_diag[i] );
_dst.inv_diag[i] = Kokkos::max( _dst.inv_diag[i], _src.inv_diag[i] );
}
}

Expand Down Expand Up @@ -140,9 +141,6 @@ namespace bvh
Kokkos::parallel_reduce( "compute_bounds_min_diag", _elements.extent( 0 ),
detail::min_diag_bounds_union< Entity >{ _elements },
_bounds );
if (_bounds.is_hostspace)
::bvh::vt::debug( "bounds: inc_diag {}, min {}\n",
_bounds().inv_diag, _bounds().min );
Kokkos::parallel_for( 1, KOKKOS_LAMBDA( int ) {
auto width = _bounds().inv_diag - _bounds().min;
_bounds().inv_diag = 1.0 / width;
Expand Down
25 changes: 13 additions & 12 deletions src/bvh/hash.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ namespace bvh
return _num;
}

inline std::uint32_t expand32_alt( std::uint32_t _num )
KOKKOS_INLINE_FUNCTION std::uint32_t expand32_alt( std::uint32_t _num )
{
_num = ( _num ^ ( _num << 16 ) ) & 0xff0000ff;
_num = ( _num ^ ( _num << 8 ) ) & 0x0300f00f;
Expand All @@ -69,7 +69,7 @@ namespace bvh



inline std::uint64_t expand64( std::uint64_t _21bit )
KOKKOS_INLINE_FUNCTION std::uint64_t expand64( std::uint64_t _21bit )
{
_21bit = ( _21bit * 0x1000001u ) & 0xfff000000fffu;
_21bit = ( _21bit * 0x1001u ) & 0xfc003f000fc003fu;
Expand All @@ -81,7 +81,7 @@ namespace bvh
}

#ifdef __BMI2__
inline std::uint64_t expand64intrin( std::uint64_t _21bit )
KOKKOS_INLINE_FUNCTION std::uint64_t expand64intrin( std::uint64_t _21bit )
{
#if defined(KOKKOS_COMPILER_NVCC)
return expand64(_21bit);
Expand All @@ -90,14 +90,14 @@ namespace bvh
#endif
}

inline std::uint64_t morton64_intrin( std::uint64_t _x21, std::uint64_t _y21, std::uint64_t _z21 )
KOKKOS_INLINE_FUNCTION std::uint64_t morton64_intrin( std::uint64_t _x21, std::uint64_t _y21, std::uint64_t _z21 )
{
return ( expand64intrin( _z21 ) << 2u ) | ( expand64intrin( _y21 ) << 1u ) | expand64intrin( _x21 );
}
#endif


inline std::uint64_t morton64( std::uint64_t _x21, std::uint64_t _y21, std::uint64_t _z21 )
KOKKOS_INLINE_FUNCTION std::uint64_t morton64( std::uint64_t _x21, std::uint64_t _y21, std::uint64_t _z21 )
{
return ( expand64( _z21 ) << 2u ) | ( expand64( _y21 ) << 1u ) | expand64( _x21 );
}
Expand Down Expand Up @@ -144,7 +144,7 @@ namespace bvh
* @return a vector of 10 bit quantized values
*/
template< typename T >
m::vec3< std::uint32_t >
KOKKOS_INLINE_FUNCTION m::vec3< std::uint32_t >
quantize32( const m::vec3< T > &_p )
{
// Scale up to highest 10 bit value
Expand All @@ -156,7 +156,7 @@ namespace bvh
}

template< typename T >
m::vec3< std::uint32_t >
KOKKOS_INLINE_FUNCTION m::vec3< std::uint32_t >
quantize32( const m::vec3< T > &_p, const m::vec3< T > &_min, const m::vec3< T > &_inv_diagonal )
{
// Normalize
Expand All @@ -175,7 +175,7 @@ namespace bvh
* @return a vector of 21 bit quantized values
*/
template< typename T >
m::vec3< std::uint64_t >
KOKKOS_INLINE_FUNCTION m::vec3< std::uint64_t >
quantize64( const m::vec3< T > &_p )
{
// Scale up to highest 21 bit value
Expand All @@ -187,7 +187,7 @@ namespace bvh
}

template< typename T >
m::vec3< std::uint64_t >
KOKKOS_INLINE_FUNCTION m::vec3< std::uint64_t >
quantize64( const m::vec3< T > &_p, const m::vec3< T > &_min, const m::vec3< T > &_inv_diagonal )
{
// Normalize
Expand All @@ -206,7 +206,7 @@ namespace bvh
struct quantize_impl< std::uint32_t >
{
template< typename... Args >
static auto quantize( Args &&... _args )
static KOKKOS_INLINE_FUNCTION auto quantize( Args &&... _args )
{
return quantize32( std::forward< Args >( _args )... );
}
Expand All @@ -216,15 +216,16 @@ namespace bvh
struct quantize_impl< std::uint64_t >
{
template< typename... Args >
static auto quantize( Args &&... _args )
static KOKKOS_INLINE_FUNCTION auto quantize( Args &&... _args )
{
return quantize64( std::forward< Args >( _args )... );
}
};
}

template< typename B, typename T >
auto quantize( const m::vec3< T > &_p, const m::vec3< T > &_min, const m::vec3< T > &_inv_diagonal )
KOKKOS_INLINE_FUNCTION auto
quantize( const m::vec3< T > &_p, const m::vec3< T > &_min, const m::vec3< T > &_inv_diagonal )
{
return detail::quantize_impl< B >::quantize( _p, _min, _inv_diagonal );
}
Expand Down
20 changes: 10 additions & 10 deletions src/bvh/kdop.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ namespace bvh
template< typename T >
struct extent
{
T min = std::numeric_limits< T >::max(); ///< The lower bound of an extent.
T max = std::numeric_limits< T >::lowest(); ///< The upper bound of an extent.
T min = m::max_double; ///< The lower bound of an extent.
T max = m::lowest_double; ///< The upper bound of an extent.

/**
* The length of an extent.
Expand Down Expand Up @@ -236,7 +236,7 @@ namespace bvh
* \return the constructed k-DOP.
*/
template< typename InputIterator >
static Derived from_vertices( InputIterator _begin, InputIterator _end, T _epsilon = T{ 0 } )
static KOKKOS_INLINE_FUNCTION Derived from_vertices( InputIterator _begin, InputIterator _end, T _epsilon = T{ 0 } )
{
Derived ret;

Expand Down Expand Up @@ -271,7 +271,7 @@ namespace bvh
* \return the constructed k-DOP.
*/
template< typename Vec >
static Derived from_sphere( const Vec &_center, T _radius )
static KOKKOS_INLINE_FUNCTION Derived from_sphere( const Vec &_center, T _radius )
{
Derived ret;

Expand All @@ -294,7 +294,7 @@ namespace bvh
* \param _radius the radius of the sphere.
* \return the constructed \f$k\f$-DOP.
*/
static Derived from_sphere( T _x, T _y, T _z, T _radius )
static KOKKOS_INLINE_FUNCTION Derived from_sphere( T _x, T _y, T _z, T _radius )
{
return from_sphere( m::vec3< T >( _x, _y, _z ), _radius );
}
Expand All @@ -305,7 +305,7 @@ namespace bvh
*
* \param _amount The amount to grow.
*/
void inflate( arithmetic_type _amount )
KOKKOS_INLINE_FUNCTION void inflate( arithmetic_type _amount )
{
for ( int i = 0; i < K / 2; ++i )
{
Expand Down Expand Up @@ -353,12 +353,12 @@ namespace bvh
*/
int longest_axis() const
{
auto iter = std::max_element( extents.begin(), extents.end(),
auto iter = std::max_element( extents.data(), extents.data() + K / 2,
[]( const extent< T > &_lhs, const extent< T > &_rhs ) {
return _lhs.length() < _rhs.length();
});

return static_cast< int >( std::distance( extents.begin(), iter ) );
return static_cast< int >( std::distance( extents.data(), iter ) );
}

/**
Expand Down Expand Up @@ -474,8 +474,8 @@ namespace bvh
friend std::ostream &operator<<( std::ostream &os, const kdop_base &_kdop )
{
os << K << "-dop: ";
for ( auto &&e : _kdop.extents )
os << "[" << e.min << ", " << e.max << "] ";
for ( std::size_t i = 0; i < K / 2; ++i )
os << "[" << _kdop.extents[i].min << ", " << _kdop.extents[i].max << "] ";

return os;
}
Expand Down
8 changes: 8 additions & 0 deletions src/bvh/math/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,22 @@ namespace bvh
constexpr double epsilon = DBL_EPSILON;
constexpr double epsilonf = FLT_EPSILON;

constexpr double lowest_double = -DBL_MAX;
constexpr double max_double = DBL_MAX;

template< typename T >
constexpr T epsilon_value = T{};
template<>
constexpr double epsilon_value<float> = FLT_EPSILON;
template<>
constexpr double epsilon_value<double> = DBL_EPSILON;
#else
constexpr double epsilon = std::numeric_limits< double >::epsilon();
constexpr double epsilonf = std::numeric_limits< float >::epsilon();

constexpr double lowest_double = std::numeric_limits< double >::lowest();
constexpr double max_double = std::numeric_limits< double >::max();

template< typename T >
constexpr T epsilon_value = std::numeric_limits< T >::epsilon();
#endif
Expand Down
2 changes: 1 addition & 1 deletion src/bvh/snapshot.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ namespace bvh
namespace detail
{
template< typename Entity >
auto convert_centroid( const Entity &_ent )
KOKKOS_INLINE_FUNCTION auto convert_centroid( const Entity &_ent )
{
using kdop_type = typename element_traits< Entity >::kdop_type;
using arithmetic_type = typename kdop_type::arithmetic_type;
Expand Down
4 changes: 3 additions & 1 deletion src/bvh/split/split.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@

#include "element_permutations.hpp"

#include <Kokkos_Core_fwd.hpp>
#include <impl/Kokkos_HostThreadTeam.hpp>
#include <iterator>
#include <numeric>
#include <algorithm>
Expand Down Expand Up @@ -296,7 +298,7 @@ namespace bvh
if ( _depth > 0 ) {
std::vector< std::pair< Element, size_t > > combi( _elements.size() );
Kokkos::View< std::pair< Element, size_t >*, Kokkos::HostSpace, Kokkos::MemoryTraits<Kokkos::Unmanaged> > h_combi(combi.data(), _elements.size());
Kokkos::parallel_for("CopyInit", _elements.size(), KOKKOS_LAMBDA (const int& i) {
Kokkos::parallel_for("CopyInit", Kokkos::RangePolicy< Kokkos::DefaultHostExecutionSpace >( 0, static_cast< int >( _elements.size() ) ), KOKKOS_LAMBDA (int i) {
h_combi[i] = std::make_pair( _elements[i], i );
} );
detail::split_permutations_recursive_impl_ml< SplittingMethod, AxisSelector >( _elements, _depth - 1,
Expand Down
4 changes: 2 additions & 2 deletions src/bvh/util/array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@

#include "attributes.hpp"

#include <array>
#include <Kokkos_Array.hpp>

namespace bvh
{
template< typename T, std::size_t N >
using array = std::array< T, N >;
using array = Kokkos::Array< T, N >;
}

#endif // INC_BVH_UTIL_ARRAY_HPP
4 changes: 2 additions & 2 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ if (NOT BVH_DISABLE_TESTS)
#PatchTest.cpp
#PrimitiveTest.cpp
# bits_test.cpp
# hash_test.cpp
# sort_test.cpp
hash_test.cpp
sort_test.cpp
# SplitTest.cpp
# UtilTest.cpp
# math_vec_tests.cpp
Expand Down
Loading

0 comments on commit b48b111

Please sign in to comment.