Skip to content

Commit

Permalink
Support sense caching for future lazy evaluation (#1539)
Browse files Browse the repository at this point in the history
  • Loading branch information
esseivaju authored Jan 6, 2025
1 parent d743f0b commit 0c21e07
Show file tree
Hide file tree
Showing 31 changed files with 371 additions and 127 deletions.
4 changes: 2 additions & 2 deletions src/corecel/math/Algorithms.hh
Original file line number Diff line number Diff line change
Expand Up @@ -739,9 +739,9 @@ CELER_FORCEINLINE_FUNCTION void sincospi(double a, double* s, double* c)
*/
template<class T>
#if defined(_MSC_VER)
inline int popcount(T x) noexcept
CELER_FORCEINLINE_FUNCTION int popcount(T x) noexcept
#else
inline constexpr int popcount(T x) noexcept
CELER_CONSTEXPR_FUNCTION int popcount(T x) noexcept
#endif
{
static_assert(sizeof(T) <= 8,
Expand Down
3 changes: 2 additions & 1 deletion src/orange/OrangeData.hh
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "geocel/BoundingBox.hh"

#include "OrangeTypes.hh"
#include "SenseUtils.hh"
#include "univ/detail/Types.hh"

#include "detail/BIHData.hh"
Expand Down Expand Up @@ -471,7 +472,7 @@ struct OrangeStateData
Items<UniverseId> universe;

// Scratch space with dimensions {track}{max_faces}
Items<Sense> temp_sense;
Items<SenseValue> temp_sense;

// Scratch space with dimensions {track}{max_intersections}
Items<FaceId> temp_face;
Expand Down
6 changes: 3 additions & 3 deletions src/orange/OrangeTrackView.hh
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ class OrangeTrackView
find_next_step_impl(detail::Intersection isect);

// Create local sense reference
inline CELER_FUNCTION Span<Sense> make_temp_sense() const;
inline CELER_FUNCTION Span<SenseValue> make_temp_sense() const;

// Create local distance
inline CELER_FUNCTION detail::TempNextFace make_temp_next() const;
Expand Down Expand Up @@ -1038,11 +1038,11 @@ CELER_FUNCTION real_type OrangeTrackView::find_safety(real_type)
/*!
* Get a reference to the current volume, or to world volume if outside.
*/
CELER_FUNCTION Span<Sense> OrangeTrackView::make_temp_sense() const
CELER_FUNCTION Span<SenseValue> OrangeTrackView::make_temp_sense() const
{
auto const max_faces = params_.scalars.max_faces;
auto offset = track_slot_.get() * max_faces;
return states_.temp_sense[AllItems<Sense, MemSpace::native>{}].subspan(
return states_.temp_sense[AllItems<SenseValue, MemSpace::native>{}].subspan(
offset, max_faces);
}

Expand Down
18 changes: 0 additions & 18 deletions src/orange/OrangeTypes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -144,24 +144,6 @@ char const* to_cstring(TransformType value)
return to_cstring_impl(value);
}

//---------------------------------------------------------------------------//
/*!
* Get a string corresponding to a signed sense.
*/
char const* to_cstring(SignedSense s)
{
switch (s)
{
case SignedSense::inside:
return "inside";
case SignedSense::on:
return "on";
case SignedSense::outside:
return "outside";
}
return "<invalid>";
}

//---------------------------------------------------------------------------//
/*!
* Get a string corresponding to a transform type.
Expand Down
82 changes: 0 additions & 82 deletions src/orange/OrangeTypes.hh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include <cstddef>
#include <functional>
#include <type_traits>
#include <utility>

#include "corecel/Macros.hh"
#include "corecel/OpaqueId.hh"
Expand Down Expand Up @@ -349,34 +348,6 @@ extern template struct Tolerance<double>;

//---------------------------------------------------------------------------//
// HELPER FUNCTIONS (HOST/DEVICE)
//---------------------------------------------------------------------------//
/*!
* Convert a boolean value to a Sense enum.
*/
CELER_CONSTEXPR_FUNCTION Sense to_sense(bool s)
{
return static_cast<Sense>(s);
}

//---------------------------------------------------------------------------//
/*!
* Change the sense across a surface.
*/
[[nodiscard]] CELER_CONSTEXPR_FUNCTION Sense flip_sense(Sense orig)
{
return static_cast<Sense>(!static_cast<bool>(orig));
}

//---------------------------------------------------------------------------//
/*!
* Change the sense across a surface.
*/
[[nodiscard]] CELER_CONSTEXPR_FUNCTION SignedSense flip_sense(SignedSense orig)
{
using IntT = std::underlying_type_t<SignedSense>;
return static_cast<SignedSense>(-static_cast<IntT>(orig));
}

//---------------------------------------------------------------------------//
/*!
* Change whether a boundary crossing is reentrant or exiting.
Expand All @@ -387,50 +358,6 @@ flip_boundary(BoundaryResult orig)
return static_cast<BoundaryResult>(!static_cast<bool>(orig));
}

//---------------------------------------------------------------------------//
/*!
* Evaluate the sense based on the LHS expression of the quadric equation.
*
* This is an optimized jump-free version of:
* \code
return quadric == 0 ? SignedSense::on
: quadric < 0 ? SignedSense::inside
: SignedSense::outside;
* \endcode
* as
* \code
int gz = !(quadric <= 0) ? 1 : 0;
int lz = quadric < 0 ? 1 : 0;
return static_cast<SignedSense>(gz - lz);
* \endcode
* and compressed into a single line.
*
* NaN values are treated as "outside".
*/
[[nodiscard]] CELER_CONSTEXPR_FUNCTION SignedSense
real_to_sense(real_type quadric)
{
return static_cast<SignedSense>(!(quadric <= 0) - (quadric < 0));
}

//---------------------------------------------------------------------------//
/*!
* Convert a signed sense to a Sense enum.
*/
CELER_CONSTEXPR_FUNCTION Sense to_sense(SignedSense s)
{
return Sense(static_cast<int>(s) >= 0);
}

//---------------------------------------------------------------------------//
/*!
* Convert a signed sense to a surface state.
*/
CELER_CONSTEXPR_FUNCTION SurfaceState to_surface_state(SignedSense s)
{
return s == SignedSense::on ? SurfaceState::on : SurfaceState::off;
}

//---------------------------------------------------------------------------//
/*!
* Sentinel value indicating "no intersection".
Expand Down Expand Up @@ -466,21 +393,12 @@ CELER_CONSTEXPR_FUNCTION bool is_operator_token(logic_int lv)
//---------------------------------------------------------------------------//
// HELPER FUNCTIONS (HOST)
//---------------------------------------------------------------------------//
//! Get a printable character corresponding to a sense.
inline constexpr char to_char(Sense s)
{
return s == Sense::inside ? '-' : '+';
}

// Get a string corresponding to a surface type
char const* to_cstring(SurfaceType);

// Get a string corresponding to a transform type
char const* to_cstring(TransformType);

// Get a string corresponding to a signed sense
char const* to_cstring(SignedSense);

// Get a string corresponding to a surface state
inline char const* to_cstring(SurfaceState s)
{
Expand Down
173 changes: 173 additions & 0 deletions src/orange/SenseUtils.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
//------------------------------- -*- C++ -*- -------------------------------//
// Copyright Celeritas contributors: see top-level COPYRIGHT file for details
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
//---------------------------------------------------------------------------//
//! \file orange/SenseUtils.hh
//! \brief Sense helper functions and types.
//---------------------------------------------------------------------------//
#pragma once

#include "corecel/Macros.hh"
#include "corecel/cont/Bitset.hh"

#include "OrangeTypes.hh"

namespace celeritas
{
//---------------------------------------------------------------------------//
// HELPER FUNCTIONS (HOST/DEVICE)
//---------------------------------------------------------------------------//
/*!
* Convert a boolean value to a Sense enum.
*/
CELER_CONSTEXPR_FUNCTION Sense to_sense(bool s)
{
return static_cast<Sense>(s);
}

//---------------------------------------------------------------------------//
/*!
* Change the sense across a surface.
*/
[[nodiscard]] CELER_CONSTEXPR_FUNCTION Sense flip_sense(Sense orig)
{
return static_cast<Sense>(!static_cast<bool>(orig));
}

//---------------------------------------------------------------------------//
/*!
* Change the sense across a surface.
*/
[[nodiscard]] CELER_CONSTEXPR_FUNCTION SignedSense flip_sense(SignedSense orig)
{
using IntT = std::underlying_type_t<SignedSense>;
return static_cast<SignedSense>(-static_cast<IntT>(orig));
}

//---------------------------------------------------------------------------//
/*!
* Evaluate the sense based on the LHS expression of the quadric equation.
*
* This is an optimized jump-free version of:
* \code
return quadric == 0 ? SignedSense::on
: quadric < 0 ? SignedSense::inside
: SignedSense::outside;
* \endcode
* as
* \code
int gz = !(quadric <= 0) ? 1 : 0;
int lz = quadric < 0 ? 1 : 0;
return static_cast<SignedSense>(gz - lz);
* \endcode
* and compressed into a single line.
*
* NaN values are treated as "outside".
*/
[[nodiscard]] CELER_CONSTEXPR_FUNCTION SignedSense
real_to_sense(real_type quadric)
{
return static_cast<SignedSense>(!(quadric <= 0) - (quadric < 0));
}

//---------------------------------------------------------------------------//
/*!
* Convert a signed sense to a Sense enum.
*/
CELER_CONSTEXPR_FUNCTION Sense to_sense(SignedSense s)
{
return Sense(static_cast<int>(s) >= 0);
}

//---------------------------------------------------------------------------//
/*!
* Convert a signed sense to a surface state.
*/
CELER_CONSTEXPR_FUNCTION SurfaceState to_surface_state(SignedSense s)
{
return s == SignedSense::on ? SurfaceState::on : SurfaceState::off;
}

//---------------------------------------------------------------------------//
// HELPER FUNCTIONS (HOST)
//---------------------------------------------------------------------------//
//! Get a printable character corresponding to a sense.
inline constexpr char to_char(Sense s)
{
return s == Sense::inside ? '-' : '+';
}

// Get a string corresponding to a signed sense
inline char const* to_cstring(SignedSense s)
{
switch (s)
{
case SignedSense::inside:
return "inside";
case SignedSense::on:
return "on";
case SignedSense::outside:
return "outside";
}
return "<invalid>";
}

//---------------------------------------------------------------------------//
// CLASSES
//---------------------------------------------------------------------------//
/*!
* Wrapper for a sense value that is optionally set.
*/
class SenseValue
{
private:
enum : char
{
sense_bit,
is_assigned_bit,
};

public:
constexpr SenseValue() = default;

//! Construct with a sense value
CELER_CONSTEXPR_FUNCTION SenseValue(Sense sense)
{
sense_[sense_bit] = static_cast<bool>(sense);
sense_[is_assigned_bit] = true;
}

//! Convert to a sense value
CELER_CONSTEXPR_FUNCTION operator Sense() const
{
return to_sense(sense_[sense_bit]);
}

//! Convert to a boolean value
CELER_CONSTEXPR_FUNCTION explicit operator bool() const
{
return sense_[sense_bit];
}

//! Assign a sense value
CELER_CONSTEXPR_FUNCTION SenseValue& operator=(Sense sense)
{
sense_[sense_bit] = static_cast<bool>(sense);
sense_[is_assigned_bit] = true;
return *this;
}

//! Check wether there is a cached sense value
CELER_CONSTEXPR_FUNCTION bool is_assigned() const
{
return sense_[is_assigned_bit];
}

//! Clear the sense value
CELER_CONSTEXPR_FUNCTION void clear() { sense_.reset(); }

private:
Bitset<2> sense_;
};

} // namespace celeritas
1 change: 1 addition & 0 deletions src/orange/orangeinp/detail/InfixStringBuilder.hh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <ostream>

#include "corecel/cont/VariantUtils.hh"
#include "orange/SenseUtils.hh"

#include "../CsgTree.hh"

Expand Down
1 change: 1 addition & 0 deletions src/orange/surf/ConeAligned.hh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "corecel/cont/Span.hh"
#include "corecel/math/ArrayUtils.hh"
#include "orange/OrangeTypes.hh"
#include "orange/SenseUtils.hh"

#include "detail/QuadraticSolver.hh"

Expand Down
1 change: 1 addition & 0 deletions src/orange/surf/CylAligned.hh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "corecel/cont/Span.hh"
#include "corecel/math/ArrayUtils.hh"
#include "orange/OrangeTypes.hh"
#include "orange/SenseUtils.hh"

#include "detail/QuadraticSolver.hh"

Expand Down
1 change: 1 addition & 0 deletions src/orange/surf/CylCentered.hh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "corecel/cont/Span.hh"
#include "corecel/math/ArrayUtils.hh"
#include "orange/OrangeTypes.hh"
#include "orange/SenseUtils.hh"

#include "detail/QuadraticSolver.hh"

Expand Down
Loading

0 comments on commit 0c21e07

Please sign in to comment.