glotzerlab / hoomd-blue

Molecular dynamics and Monte Carlo soft matter simulation on GPUs.

Home Page:http://glotzerlab.engin.umich.edu/hoomd-blue

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Test if Ball-AABB tests can improve performance

joaander opened this issue · comments

Description

Test if this class:

/** Ball data structure for use with AABB.

    When querying the AABB tree for all points in a sphere, use a Ball-AABB check.
**/
struct
#ifndef __HIPCC__
    __attribute__((visibility("default")))
#endif
Ball
    {
    vec3<LongReal> m_center;
    LongReal m_radius;

    DEVICE Ball() :
        m_center(0,0,0),
        m_radius(0)
        {
        }

    DEVICE Ball(const vec3<LongReal>& center, LongReal radius) :
        m_center(center),
        m_radius(radius)
        {
        }

    DEVICE void translate(const vec3<Scalar>& v)
        {
        m_center += v;
        }

/** Check if a Ball and AABB overlap

    @param other AABB
    @returns true when the two shapes overlap, false otherwise
*/
DEVICE inline bool overlaps(const AABB& other) const
    {
    // https://stackoverflow.com/questions/28343716/sphere-intersection-test-of-aabb
    LongReal d_squared_min = 0;

    vec3<LongReal> box_min = other.getLower();
    vec3<LongReal> box_max = other.getUpper();

    if( m_center.x < box_min.x )
        {
        d_squared_min += ( m_center.x - box_min.x ) * ( m_center.x - box_min.x );
        }
    else if( m_center.x > box_max.x)
        {
        d_squared_min += ( m_center.x - box_max.x) * ( m_center.x - box_max.x);
        }

    if( m_center.y < box_min.y )
        {
        d_squared_min += ( m_center.y - box_min.y ) * ( m_center.y - box_min.y );
        }
    else if( m_center.y > box_max.y)
        {
        d_squared_min += ( m_center.y - box_max.y) * ( m_center.y - box_max.y);
        }

    if( m_center.z < box_min.z )
        {
        d_squared_min += ( m_center.z - box_min.z ) * ( m_center.z - box_min.z );
        }
    else if( m_center.z > box_max.z)
        {
        d_squared_min += ( m_center.z - box_max.z) * ( m_center.z - box_max.z);
        }

    return d_squared_min <= m_radius * m_radius;
    }
    };

used as the search query in HPMC leads to faster performance.

In my tests so far, it is ~5% faster for a LJ r_cut=2.5 system, but slower for a moderate density hard polyhedra simulation.

Motivation and context

I need to check if higher density hard particle systems can benefit. The ball-AABB check results in hitting ~50% of the AABB nodes (volume of sphere / volume of cube). But, the ball-AABB check is more expensive than AABB-AABB so there are tradeoffs.