Skip to content

Commit

Permalink
Add counter for property definition
Browse files Browse the repository at this point in the history
  • Loading branch information
icui committed Feb 24, 2025
1 parent 2176538 commit 10933c2
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 63 deletions.
46 changes: 27 additions & 19 deletions benchmarks/impl_container.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,20 @@
#include "impl_point.hpp"
#include "point/interface.hpp"
#include <Kokkos_SIMD.hpp>
#include <boost/preprocessor/slot/counter.hpp>

namespace specfem {
namespace benchmarks {

#define DEFINE_CONTAINER(prop, i) \
#define DEFINE_CONTAINER(prop) \
constexpr static int i_##prop = __COUNTER__ - _counter; \
KOKKOS_INLINE_FUNCTION type_real &prop(const int &ispec, const int &iz, \
const int &ix) const { \
return Base::data(ispec, iz, ix, i); \
return Base::data(ispec, iz, ix, i_##prop); \
} \
KOKKOS_INLINE_FUNCTION type_real &h_##prop(const int &ispec, const int &iz, \
const int &ix) const { \
return Base::h_data(ispec, iz, ix, i); \
return Base::h_data(ispec, iz, ix, i_##prop); \
}

namespace impl {
Expand Down Expand Up @@ -127,9 +129,10 @@ struct properties_container<specfem::element::medium_tag::acoustic,
specfem::element::property_tag::isotropic,
2>;
using Base::Base;
constexpr static int _counter = __COUNTER__;

DEFINE_CONTAINER(rho_inverse, 0)
DEFINE_CONTAINER(kappa, 1)
DEFINE_CONTAINER(rho_inverse)
DEFINE_CONTAINER(kappa)
};

template <>
Expand All @@ -143,10 +146,11 @@ struct properties_container<specfem::element::medium_tag::elastic,
specfem::element::property_tag::isotropic,
3>;
using Base::Base;
constexpr static int _counter = __COUNTER__;

DEFINE_CONTAINER(lambdaplus2mu, 0)
DEFINE_CONTAINER(mu, 1)
DEFINE_CONTAINER(rho, 2)
DEFINE_CONTAINER(lambdaplus2mu)
DEFINE_CONTAINER(mu)
DEFINE_CONTAINER(rho)
};

template <>
Expand All @@ -159,17 +163,18 @@ struct properties_container<specfem::element::medium_tag::elastic,
specfem::element::medium_tag::elastic,
specfem::element::property_tag::anisotropic, 10>;
using Base::Base;

DEFINE_CONTAINER(c11, 0)
DEFINE_CONTAINER(c13, 1)
DEFINE_CONTAINER(c15, 2)
DEFINE_CONTAINER(c33, 3)
DEFINE_CONTAINER(c35, 4)
DEFINE_CONTAINER(c55, 5)
DEFINE_CONTAINER(c12, 6)
DEFINE_CONTAINER(c23, 7)
DEFINE_CONTAINER(c25, 8)
DEFINE_CONTAINER(rho, 9)
constexpr static int _counter = __COUNTER__;

DEFINE_CONTAINER(c11)
DEFINE_CONTAINER(c13)
DEFINE_CONTAINER(c15)
DEFINE_CONTAINER(c33)
DEFINE_CONTAINER(c35)
DEFINE_CONTAINER(c55)
DEFINE_CONTAINER(c12)
DEFINE_CONTAINER(c23)
DEFINE_CONTAINER(c25)
DEFINE_CONTAINER(rho)
};

template <specfem::element::medium_tag type,
Expand Down Expand Up @@ -318,5 +323,8 @@ struct assembly_properties : public specfem::compute::impl::value_containers<
}
};

#undef DEFINE_CONTAINER_CONSTRUCTORS
#undef DEFINE_CONTAINER

} // namespace benchmarks
} // namespace specfem
112 changes: 68 additions & 44 deletions benchmarks/impl_point.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,15 @@
namespace specfem {
namespace benchmarks {

#define DEFINE_PROP_CONSTRUCTORS \
using simd = specfem::datatype::simd<type_real, UseSIMD>; \
using value_type = typename simd::datatype; \
KOKKOS_FUNCTION \
properties() = default; \
KOKKOS_FUNCTION \
properties(const value_type *value) : Base(value) { compute(); } \
template <typename... Args, \
typename std::enable_if_t<sizeof...(Args) == Base::nprops, int> = \
0> \
KOKKOS_FUNCTION properties(Args... args) : Base(args...) { \
compute(); \
#define DEFINE_PROP(prop) \
constexpr static int i_##prop = __COUNTER__ - _counter; \
KOKKOS_INLINE_FUNCTION value_type prop() const { \
return Base::data[i_##prop]; \
} \
KOKKOS_INLINE_FUNCTION void prop(value_type val) { \
Base::data[i_##prop] = val; \
}

#define DEFINE_PROP(prop, i) \
KOKKOS_INLINE_FUNCTION value_type prop() const { return Base::data[i]; } \
KOKKOS_INLINE_FUNCTION void prop(value_type val) { Base::data[i] = val; }

namespace impl {
template <int N, int N_EX, bool UseSIMD> struct impl_properties {
using simd = specfem::datatype::simd<type_real, UseSIMD>; ///< SIMD type
Expand Down Expand Up @@ -115,17 +106,28 @@ struct properties<specfem::dimension::type::dim2,
constexpr static auto medium_tag = specfem::element::medium_tag::elastic;
constexpr static auto property_tag =
specfem::element::property_tag::isotropic;
///@}

DEFINE_PROP_CONSTRUCTORS
using simd = specfem::datatype::simd<type_real, UseSIMD>;
using value_type = typename simd::datatype;
constexpr static int _counter = __COUNTER__;
///@}
KOKKOS_FUNCTION
properties() = default;
KOKKOS_FUNCTION
properties(const value_type *value) : Base(value) { compute(); }
template <typename... Args,
typename std::enable_if_t<sizeof...(Args) == Base::nprops, int> = 0>
KOKKOS_FUNCTION properties(Args... args) : Base(args...) {
compute();
}

DEFINE_PROP(lambdaplus2mu, 0) ///< Lame's parameter @f$ \lambda + 2\mu @f$
DEFINE_PROP(mu, 1) ///< shear modulus @f$ \mu @f$
DEFINE_PROP(rho, 2) ///< density @f$ \rho @f$
DEFINE_PROP(lambdaplus2mu) ///< Lame's parameter @f$ \lambda + 2\mu @f$
DEFINE_PROP(mu) ///< shear modulus @f$ \mu @f$
DEFINE_PROP(rho) ///< density @f$ \rho @f$

DEFINE_PROP(rho_vp, 3) ///< P-wave velocity @f$ \rho v_p @f$
DEFINE_PROP(rho_vs, 4) ///< S-wave velocity @f$ \rho v_s @f$
DEFINE_PROP(lambda, 5) ///< Lame's parameter @f$ \lambda @f$
DEFINE_PROP(rho_vp) ///< P-wave velocity @f$ \rho v_p @f$
DEFINE_PROP(rho_vs) ///< S-wave velocity @f$ \rho v_s @f$
DEFINE_PROP(lambda) ///< Lame's parameter @f$ \lambda @f$

KOKKOS_INLINE_FUNCTION
void compute() {
Expand All @@ -151,29 +153,40 @@ struct properties<specfem::dimension::type::dim2,
constexpr static auto medium_tag = specfem::element::medium_tag::elastic;
constexpr static auto property_tag =
specfem::element::property_tag::anisotropic;
///@}

DEFINE_PROP_CONSTRUCTORS
using simd = specfem::datatype::simd<type_real, UseSIMD>;
using value_type = typename simd::datatype;
constexpr static int _counter = __COUNTER__;
///@}
KOKKOS_FUNCTION
properties() = default;
KOKKOS_FUNCTION
properties(const value_type *value) : Base(value) { compute(); }
template <typename... Args,
typename std::enable_if_t<sizeof...(Args) == Base::nprops, int> = 0>
KOKKOS_FUNCTION properties(Args... args) : Base(args...) {
compute();
}

/**
* @name Elastic constants
*
*/
///@{
DEFINE_PROP(c11, 0) ///< @f$ c_{11} @f$
DEFINE_PROP(c13, 1) ///< @f$ c_{13} @f$
DEFINE_PROP(c15, 2) ///< @f$ c_{15} @f$
DEFINE_PROP(c33, 3) ///< @f$ c_{33} @f$
DEFINE_PROP(c35, 4) ///< @f$ c_{35} @f$
DEFINE_PROP(c55, 5) ///< @f$ c_{55} @f$
DEFINE_PROP(c12, 6) ///< @f$ c_{12} @f$
DEFINE_PROP(c23, 7) ///< @f$ c_{23} @f$
DEFINE_PROP(c25, 8) ///< @f$ c_{25} @f$
DEFINE_PROP(c11) ///< @f$ c_{11} @f$
DEFINE_PROP(c13) ///< @f$ c_{13} @f$
DEFINE_PROP(c15) ///< @f$ c_{15} @f$
DEFINE_PROP(c33) ///< @f$ c_{33} @f$
DEFINE_PROP(c35) ///< @f$ c_{35} @f$
DEFINE_PROP(c55) ///< @f$ c_{55} @f$
DEFINE_PROP(c12) ///< @f$ c_{12} @f$
DEFINE_PROP(c23) ///< @f$ c_{23} @f$
DEFINE_PROP(c25) ///< @f$ c_{25} @f$
///@}

DEFINE_PROP(rho, 9) ///< Density @f$ \rho @f$
DEFINE_PROP(rho_vp, 10) ///< P-wave velocity @f$ \rho v_p @f$
DEFINE_PROP(rho_vs, 11) ///< S-wave velocity @f$ \rho v_s @f$
DEFINE_PROP(rho) ///< Density @f$ \rho @f$
DEFINE_PROP(rho_vp) ///< P-wave velocity @f$ \rho v_p @f$
DEFINE_PROP(rho_vs) ///< S-wave velocity @f$ \rho v_s @f$

KOKKOS_INLINE_FUNCTION
void compute() {
Expand Down Expand Up @@ -203,14 +216,25 @@ struct properties<specfem::dimension::type::dim2,
constexpr static auto medium_tag = specfem::element::medium_tag::acoustic;
constexpr static auto property_tag =
specfem::element::property_tag::isotropic;
///@}

DEFINE_PROP_CONSTRUCTORS
using simd = specfem::datatype::simd<type_real, UseSIMD>;
using value_type = typename simd::datatype;
constexpr static int _counter = __COUNTER__;
///@}
KOKKOS_FUNCTION
properties() = default;
KOKKOS_FUNCTION
properties(const value_type *value) : Base(value) { compute(); }
template <typename... Args,
typename std::enable_if_t<sizeof...(Args) == Base::nprops, int> = 0>
KOKKOS_FUNCTION properties(Args... args) : Base(args...) {
compute();
}

DEFINE_PROP(rho_inverse, 0) ///< @f$ \frac{1}{\rho} @f$
DEFINE_PROP(kappa, 1) ///< Bulk modulus @f$ \kappa @f$
DEFINE_PROP(kappa_inverse, 2) ///< @f$ \frac{1}{\lambda + 2\mu} @f$
DEFINE_PROP(rho_vpinverse, 3) ///< @f$ \frac{1}{\rho v_p} @f$
DEFINE_PROP(rho_inverse) ///< @f$ \frac{1}{\rho} @f$
DEFINE_PROP(kappa) ///< Bulk modulus @f$ \kappa @f$
DEFINE_PROP(kappa_inverse) ///< @f$ \frac{1}{\lambda + 2\mu} @f$
DEFINE_PROP(rho_vpinverse) ///< @f$ \frac{1}{\rho v_p} @f$

KOKKOS_INLINE_FUNCTION
void compute() {
Expand Down

0 comments on commit 10933c2

Please sign in to comment.