Skip to content

Commit

Permalink
Merge pull request #3 from cppalliance/CUDA_2_test
Browse files Browse the repository at this point in the history
Second batch of GPU
  • Loading branch information
mborland authored Jul 25, 2024
2 parents d77a111 + 0d8ae7c commit 9ba05bd
Show file tree
Hide file tree
Showing 44 changed files with 2,271 additions and 233 deletions.
73 changes: 45 additions & 28 deletions include/boost/math/distributions/cauchy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#pragma warning(disable : 4127) // conditional expression is constant
#endif

#include <boost/math/tools/config.hpp>
#include <boost/math/distributions/fwd.hpp>
#include <boost/math/constants/constants.hpp>
#include <boost/math/distributions/complement.hpp>
Expand All @@ -30,7 +31,7 @@ namespace detail
{

template <class RealType, class Policy>
RealType cdf_imp(const cauchy_distribution<RealType, Policy>& dist, const RealType& x, bool complement)
BOOST_MATH_GPU_ENABLED RealType cdf_imp(const cauchy_distribution<RealType, Policy>& dist, const RealType& x, bool complement)
{
//
// This calculates the cdf of the Cauchy distribution and/or its complement.
Expand All @@ -54,7 +55,7 @@ RealType cdf_imp(const cauchy_distribution<RealType, Policy>& dist, const RealTy
// to get the result.
//
BOOST_MATH_STD_USING // for ADL of std functions
static const char* function = "boost::math::cdf(cauchy<%1%>&, %1%)";
constexpr auto function = "boost::math::cdf(cauchy<%1%>&, %1%)";
RealType result = 0;
RealType location = dist.location();
RealType scale = dist.scale();
Expand All @@ -66,6 +67,16 @@ RealType cdf_imp(const cauchy_distribution<RealType, Policy>& dist, const RealTy
{
return result;
}
#ifdef BOOST_MATH_HAS_GPU_SUPPORT
if(x > tools::max_value<RealType>())
{
return static_cast<RealType>((complement) ? 0 : 1);
}
if(x < -tools::max_value<RealType>())
{
return static_cast<RealType>((complement) ? 1 : 0);
}
#else
if(std::numeric_limits<RealType>::has_infinity && x == std::numeric_limits<RealType>::infinity())
{ // cdf +infinity is unity.
return static_cast<RealType>((complement) ? 0 : 1);
Expand All @@ -74,6 +85,7 @@ RealType cdf_imp(const cauchy_distribution<RealType, Policy>& dist, const RealTy
{ // cdf -infinity is zero.
return static_cast<RealType>((complement) ? 1 : 0);
}
#endif
if(false == detail::check_x(function, x, &result, Policy()))
{ // Catches x == NaN
return result;
Expand All @@ -88,7 +100,7 @@ RealType cdf_imp(const cauchy_distribution<RealType, Policy>& dist, const RealTy
} // cdf

template <class RealType, class Policy>
RealType quantile_imp(
BOOST_MATH_GPU_ENABLED RealType quantile_imp(
const cauchy_distribution<RealType, Policy>& dist,
const RealType& p,
bool complement)
Expand All @@ -101,7 +113,7 @@ RealType quantile_imp(
// mid-point of the distribution. This is either added or subtracted
// from the location parameter depending on whether `complement` is true.
//
static const char* function = "boost::math::quantile(cauchy<%1%>&, %1%)";
constexpr auto function = "boost::math::quantile(cauchy<%1%>&, %1%)";
BOOST_MATH_STD_USING // for ADL of std functions

RealType result = 0;
Expand Down Expand Up @@ -151,20 +163,20 @@ class cauchy_distribution
typedef RealType value_type;
typedef Policy policy_type;

cauchy_distribution(RealType l_location = 0, RealType l_scale = 1)
BOOST_MATH_GPU_ENABLED cauchy_distribution(RealType l_location = 0, RealType l_scale = 1)
: m_a(l_location), m_hg(l_scale)
{
static const char* function = "boost::math::cauchy_distribution<%1%>::cauchy_distribution";
constexpr auto function = "boost::math::cauchy_distribution<%1%>::cauchy_distribution";
RealType result;
detail::check_location(function, l_location, &result, Policy());
detail::check_scale(function, l_scale, &result, Policy());
} // cauchy_distribution

RealType location()const
BOOST_MATH_GPU_ENABLED RealType location()const
{
return m_a;
}
RealType scale()const
BOOST_MATH_GPU_ENABLED RealType scale()const
{
return m_hg;
}
Expand All @@ -184,48 +196,52 @@ cauchy_distribution(RealType,RealType)->cauchy_distribution<typename boost::math
#endif

template <class RealType, class Policy>
inline const std::pair<RealType, RealType> range(const cauchy_distribution<RealType, Policy>&)
BOOST_MATH_GPU_ENABLED inline const std::pair<RealType, RealType> range(const cauchy_distribution<RealType, Policy>&)
{ // Range of permissible values for random variable x.
if (std::numeric_limits<RealType>::has_infinity)
#ifndef BOOST_MATH_HAS_GPU_SUPPORT
BOOST_MATH_IF_CONSTEXPR (std::numeric_limits<RealType>::has_infinity)
{
return std::pair<RealType, RealType>(-std::numeric_limits<RealType>::infinity(), std::numeric_limits<RealType>::infinity()); // - to + infinity.
}
else
#endif
{ // Can only use max_value.
using boost::math::tools::max_value;
return std::pair<RealType, RealType>(-max_value<RealType>(), max_value<RealType>()); // - to + max.
}
}

template <class RealType, class Policy>
inline const std::pair<RealType, RealType> support(const cauchy_distribution<RealType, Policy>& )
BOOST_MATH_GPU_ENABLED inline const std::pair<RealType, RealType> support(const cauchy_distribution<RealType, Policy>& )
{ // Range of supported values for random variable x.
// This is range where cdf rises from 0 to 1, and outside it, the pdf is zero.
if (std::numeric_limits<RealType>::has_infinity)
#ifndef BOOST_MATH_HAS_GPU_SUPPORT
BOOST_MATH_IF_CONSTEXPR (std::numeric_limits<RealType>::has_infinity)
{
return std::pair<RealType, RealType>(-std::numeric_limits<RealType>::infinity(), std::numeric_limits<RealType>::infinity()); // - to + infinity.
}
else
#endif
{ // Can only use max_value.
using boost::math::tools::max_value;
return std::pair<RealType, RealType>(-tools::max_value<RealType>(), max_value<RealType>()); // - to + max.
}
}

template <class RealType, class Policy>
inline RealType pdf(const cauchy_distribution<RealType, Policy>& dist, const RealType& x)
BOOST_MATH_GPU_ENABLED inline RealType pdf(const cauchy_distribution<RealType, Policy>& dist, const RealType& x)
{
BOOST_MATH_STD_USING // for ADL of std functions

static const char* function = "boost::math::pdf(cauchy<%1%>&, %1%)";
constexpr auto function = "boost::math::pdf(cauchy<%1%>&, %1%)";
RealType result = 0;
RealType location = dist.location();
RealType scale = dist.scale();
if(false == detail::check_scale("boost::math::pdf(cauchy<%1%>&, %1%)", scale, &result, Policy()))
if(false == detail::check_scale(function, scale, &result, Policy()))
{
return result;
}
if(false == detail::check_location("boost::math::pdf(cauchy<%1%>&, %1%)", location, &result, Policy()))
if(false == detail::check_location(function, location, &result, Policy()))
{
return result;
}
Expand All @@ -250,31 +266,31 @@ inline RealType pdf(const cauchy_distribution<RealType, Policy>& dist, const Rea
} // pdf

template <class RealType, class Policy>
inline RealType cdf(const cauchy_distribution<RealType, Policy>& dist, const RealType& x)
BOOST_MATH_GPU_ENABLED inline RealType cdf(const cauchy_distribution<RealType, Policy>& dist, const RealType& x)
{
return detail::cdf_imp(dist, x, false);
} // cdf

template <class RealType, class Policy>
inline RealType quantile(const cauchy_distribution<RealType, Policy>& dist, const RealType& p)
BOOST_MATH_GPU_ENABLED inline RealType quantile(const cauchy_distribution<RealType, Policy>& dist, const RealType& p)
{
return detail::quantile_imp(dist, p, false);
} // quantile

template <class RealType, class Policy>
inline RealType cdf(const complemented2_type<cauchy_distribution<RealType, Policy>, RealType>& c)
BOOST_MATH_GPU_ENABLED inline RealType cdf(const complemented2_type<cauchy_distribution<RealType, Policy>, RealType>& c)
{
return detail::cdf_imp(c.dist, c.param, true);
} // cdf complement

template <class RealType, class Policy>
inline RealType quantile(const complemented2_type<cauchy_distribution<RealType, Policy>, RealType>& c)
BOOST_MATH_GPU_ENABLED inline RealType quantile(const complemented2_type<cauchy_distribution<RealType, Policy>, RealType>& c)
{
return detail::quantile_imp(c.dist, c.param, true);
} // quantile complement

template <class RealType, class Policy>
inline RealType mean(const cauchy_distribution<RealType, Policy>&)
BOOST_MATH_GPU_ENABLED inline RealType mean(const cauchy_distribution<RealType, Policy>&)
{ // There is no mean:
typedef typename Policy::assert_undefined_type assert_type;
static_assert(assert_type::value == 0, "assert type is undefined");
Expand All @@ -287,7 +303,7 @@ inline RealType mean(const cauchy_distribution<RealType, Policy>&)
}

template <class RealType, class Policy>
inline RealType variance(const cauchy_distribution<RealType, Policy>& /*dist*/)
BOOST_MATH_GPU_ENABLED inline RealType variance(const cauchy_distribution<RealType, Policy>& /*dist*/)
{
// There is no variance:
typedef typename Policy::assert_undefined_type assert_type;
Expand All @@ -301,18 +317,19 @@ inline RealType variance(const cauchy_distribution<RealType, Policy>& /*dist*/)
}

template <class RealType, class Policy>
inline RealType mode(const cauchy_distribution<RealType, Policy>& dist)
BOOST_MATH_GPU_ENABLED inline RealType mode(const cauchy_distribution<RealType, Policy>& dist)
{
return dist.location();
}

template <class RealType, class Policy>
inline RealType median(const cauchy_distribution<RealType, Policy>& dist)
BOOST_MATH_GPU_ENABLED inline RealType median(const cauchy_distribution<RealType, Policy>& dist)
{
return dist.location();
}

template <class RealType, class Policy>
inline RealType skewness(const cauchy_distribution<RealType, Policy>& /*dist*/)
BOOST_MATH_GPU_ENABLED inline RealType skewness(const cauchy_distribution<RealType, Policy>& /*dist*/)
{
// There is no skewness:
typedef typename Policy::assert_undefined_type assert_type;
Expand All @@ -326,7 +343,7 @@ inline RealType skewness(const cauchy_distribution<RealType, Policy>& /*dist*/)
}

template <class RealType, class Policy>
inline RealType kurtosis(const cauchy_distribution<RealType, Policy>& /*dist*/)
BOOST_MATH_GPU_ENABLED inline RealType kurtosis(const cauchy_distribution<RealType, Policy>& /*dist*/)
{
// There is no kurtosis:
typedef typename Policy::assert_undefined_type assert_type;
Expand All @@ -340,7 +357,7 @@ inline RealType kurtosis(const cauchy_distribution<RealType, Policy>& /*dist*/)
}

template <class RealType, class Policy>
inline RealType kurtosis_excess(const cauchy_distribution<RealType, Policy>& /*dist*/)
BOOST_MATH_GPU_ENABLED inline RealType kurtosis_excess(const cauchy_distribution<RealType, Policy>& /*dist*/)
{
// There is no kurtosis excess:
typedef typename Policy::assert_undefined_type assert_type;
Expand All @@ -354,7 +371,7 @@ inline RealType kurtosis_excess(const cauchy_distribution<RealType, Policy>& /*d
}

template <class RealType, class Policy>
inline RealType entropy(const cauchy_distribution<RealType, Policy> & dist)
BOOST_MATH_GPU_ENABLED inline RealType entropy(const cauchy_distribution<RealType, Policy> & dist)
{
using std::log;
return log(2*constants::two_pi<RealType>()*dist.scale());
Expand Down
29 changes: 18 additions & 11 deletions include/boost/math/distributions/detail/common_error_handling.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright John Maddock 2006, 2007.
// Copyright Paul A. Bristow 2006, 2007, 2012.
// Copyright Matt Borland 2024

// Use, modification and distribution are subject to the
// Boost Software License, Version 1.0.
Expand All @@ -9,6 +10,7 @@
#ifndef BOOST_MATH_DISTRIBUTIONS_COMMON_ERROR_HANDLING_HPP
#define BOOST_MATH_DISTRIBUTIONS_COMMON_ERROR_HANDLING_HPP

#include <boost/math/tools/config.hpp>
#include <boost/math/policies/error_handling.hpp>
#include <boost/math/special_functions/fpclassify.hpp>
// using boost::math::isfinite;
Expand All @@ -23,7 +25,7 @@ namespace boost{ namespace math{ namespace detail
{

template <class RealType, class Policy>
inline bool check_probability(const char* function, RealType const& prob, RealType* result, const Policy& pol)
BOOST_MATH_GPU_ENABLED inline bool check_probability(const char* function, RealType const& prob, RealType* result, const Policy& pol)
{
if((prob < 0) || (prob > 1) || !(boost::math::isfinite)(prob))
{
Expand All @@ -36,7 +38,7 @@ inline bool check_probability(const char* function, RealType const& prob, RealTy
}

template <class RealType, class Policy>
inline bool check_df(const char* function, RealType const& df, RealType* result, const Policy& pol)
BOOST_MATH_GPU_ENABLED inline bool check_df(const char* function, RealType const& df, RealType* result, const Policy& pol)
{ // df > 0 but NOT +infinity allowed.
if((df <= 0) || !(boost::math::isfinite)(df))
{
Expand All @@ -49,7 +51,7 @@ inline bool check_df(const char* function, RealType const& df, RealType* result,
}

template <class RealType, class Policy>
inline bool check_df_gt0_to_inf(const char* function, RealType const& df, RealType* result, const Policy& pol)
BOOST_MATH_GPU_ENABLED inline bool check_df_gt0_to_inf(const char* function, RealType const& df, RealType* result, const Policy& pol)
{ // df > 0 or +infinity are allowed.
if( (df <= 0) || (boost::math::isnan)(df) )
{ // is bad df <= 0 or NaN or -infinity.
Expand All @@ -63,7 +65,7 @@ inline bool check_df_gt0_to_inf(const char* function, RealType const& df, RealTy


template <class RealType, class Policy>
inline bool check_scale(
BOOST_MATH_GPU_ENABLED inline bool check_scale(
const char* function,
RealType scale,
RealType* result,
Expand All @@ -80,7 +82,7 @@ inline bool check_scale(
}

template <class RealType, class Policy>
inline bool check_location(
BOOST_MATH_GPU_ENABLED inline bool check_location(
const char* function,
RealType location,
RealType* result,
Expand All @@ -97,7 +99,7 @@ inline bool check_location(
}

template <class RealType, class Policy>
inline bool check_x(
BOOST_MATH_GPU_ENABLED inline bool check_x(
const char* function,
RealType x,
RealType* result,
Expand All @@ -118,7 +120,7 @@ inline bool check_x(
} // bool check_x

template <class RealType, class Policy>
inline bool check_x_not_NaN(
BOOST_MATH_GPU_ENABLED inline bool check_x_not_NaN(
const char* function,
RealType x,
RealType* result,
Expand All @@ -138,7 +140,7 @@ inline bool check_x_not_NaN(
} // bool check_x_not_NaN

template <class RealType, class Policy>
inline bool check_x_gt0(
BOOST_MATH_GPU_ENABLED inline bool check_x_gt0(
const char* function,
RealType x,
RealType* result,
Expand All @@ -159,7 +161,7 @@ inline bool check_x_gt0(
} // bool check_x_gt0

template <class RealType, class Policy>
inline bool check_positive_x(
BOOST_MATH_GPU_ENABLED inline bool check_positive_x(
const char* function,
RealType x,
RealType* result,
Expand All @@ -179,13 +181,18 @@ inline bool check_positive_x(
}

template <class RealType, class Policy>
inline bool check_non_centrality(
BOOST_MATH_GPU_ENABLED inline bool check_non_centrality(
const char* function,
RealType ncp,
RealType* result,
const Policy& pol)
{
#ifndef BOOST_MATH_HAS_GPU_SUPPORT
static const RealType upper_limit = static_cast<RealType>((std::numeric_limits<long long>::max)()) - boost::math::policies::get_max_root_iterations<Policy>();
#else
constexpr RealType upper_limit = static_cast<RealType>(LONG_LONG_MAX) - boost::math::policies::get_max_root_iterations<Policy>();
#endif

if((ncp < 0) || !(boost::math::isfinite)(ncp) || ncp > upper_limit)
{
*result = policies::raise_domain_error<RealType>(
Expand All @@ -197,7 +204,7 @@ inline bool check_non_centrality(
}

template <class RealType, class Policy>
inline bool check_finite(
BOOST_MATH_GPU_ENABLED inline bool check_finite(
const char* function,
RealType x,
RealType* result,
Expand Down
Loading

0 comments on commit 9ba05bd

Please sign in to comment.