Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ _CCCL_BEGIN_NAMESPACE_CUDA_STD
template <class _IntType = int>
class binomial_distribution
{
static_assert(__libcpp_random_is_valid_inttype<_IntType>, "IntType must be a supported integer type");
static_assert(__cccl_random_is_valid_inttype<_IntType>, "IntType must be a supported integer type");

public:
// types
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ _CCCL_BEGIN_NAMESPACE_CUDA_STD
template <class _RealType = double>
class cauchy_distribution
{
static_assert(__libcpp_random_is_valid_realtype<_RealType>, "RealType must be a supported floating-point type");
static_assert(__cccl_random_is_valid_realtype<_RealType>, "RealType must be a supported floating-point type");

public:
// types
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ _CCCL_BEGIN_NAMESPACE_CUDA_STD
template <class _RealType = double>
class chi_squared_distribution
{
static_assert(__libcpp_random_is_valid_realtype<_RealType>, "RealType must be a supported floating-point type");
static_assert(__cccl_random_is_valid_realtype<_RealType>, "RealType must be a supported floating-point type");

public:
// types
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ _CCCL_BEGIN_NAMESPACE_CUDA_STD
template <class _RealType = double>
class exponential_distribution
{
static_assert(__libcpp_random_is_valid_realtype<_RealType>, "RealType must be a supported floating-point type");
static_assert(__cccl_random_is_valid_realtype<_RealType>, "RealType must be a supported floating-point type");

public:
// types
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ _CCCL_BEGIN_NAMESPACE_CUDA_STD
template <class _RealType = double>
class extreme_value_distribution
{
static_assert(__libcpp_random_is_valid_realtype<_RealType>, "RealType must be a supported floating-point type");
static_assert(__cccl_random_is_valid_realtype<_RealType>, "RealType must be a supported floating-point type");

public:
// types
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ _CCCL_BEGIN_NAMESPACE_CUDA_STD
template <class _RealType = double>
class fisher_f_distribution
{
static_assert(__libcpp_random_is_valid_realtype<_RealType>, "RealType must be a supported floating-point type");
static_assert(__cccl_random_is_valid_realtype<_RealType>, "RealType must be a supported floating-point type");

public:
// types
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ _CCCL_BEGIN_NAMESPACE_CUDA_STD
template <class _RealType = double>
class gamma_distribution
{
static_assert(__libcpp_random_is_valid_realtype<_RealType>, "RealType must be a supported floating-point type");
static_assert(__cccl_random_is_valid_realtype<_RealType>, "RealType must be a supported floating-point type");

public:
// types
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ _CCCL_BEGIN_NAMESPACE_CUDA_STD
template <class _IntType = int>
class geometric_distribution
{
static_assert(__libcpp_random_is_valid_inttype<_IntType>, "IntType must be a supported integer type");
static_assert(__cccl_random_is_valid_inttype<_IntType>, "IntType must be a supported integer type");

public:
// types
Expand Down
51 changes: 9 additions & 42 deletions libcudacxx/include/cuda/std/__random/is_valid.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@
#endif // no system header

#include <cuda/std/__type_traits/enable_if.h>
#include <cuda/std/__type_traits/integral_constant.h>
#include <cuda/std/__type_traits/is_const.h>
#include <cuda/std/__type_traits/is_floating_point.h>
#include <cuda/std/__type_traits/is_integer.h>
#include <cuda/std/__type_traits/is_same.h>
#include <cuda/std/__type_traits/is_unsigned.h>
#include <cuda/std/__utility/declval.h>
#include <cuda/std/cstdint>

#include <cuda/std/__cccl/prologue.h>

Expand All @@ -36,52 +37,18 @@ _CCCL_BEGIN_NAMESPACE_CUDA_STD
// named RealType is undefined unless the corresponding template argument is
// cv-unqualified and is one of float, double, or long double.

template <class>
inline constexpr bool __libcpp_random_is_valid_realtype = false;

template <>
inline constexpr bool __libcpp_random_is_valid_realtype<float> = true;
template <>
inline constexpr bool __libcpp_random_is_valid_realtype<double> = true;
#if _CCCL_HAS_LONG_DOUBLE()
template <>
inline constexpr bool __libcpp_random_is_valid_realtype<long double> = true;
#endif // _CCCL_HAS_LONG_DOUBLE()
template <class _Type>
inline constexpr bool __cccl_random_is_valid_realtype =
::cuda::std::is_floating_point_v<_Type> && !::cuda::std::is_const_v<_Type>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you wait for #6952, you can just use __cccl_is_floating_point_v, which will be cv uqualified :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here, we can replace __cccl_random_is_valid_realtype with __cccl_is_floating_point_v

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, have updated that.


// [rand.req.genl]/1.5:
// The effect of instantiating a template that has a template type parameter
// named IntType is undefined unless the corresponding template argument is
// cv-unqualified and is one of short, int, long, long long, unsigned short,
// unsigned int, unsigned long, or unsigned long long.

template <class>
inline constexpr bool __libcpp_random_is_valid_inttype = false;
template <> // extension
inline constexpr bool __libcpp_random_is_valid_inttype<int8_t> = true;
template <>
inline constexpr bool __libcpp_random_is_valid_inttype<short> = true;
template <>
inline constexpr bool __libcpp_random_is_valid_inttype<int> = true;
template <>
inline constexpr bool __libcpp_random_is_valid_inttype<long> = true;
template <>
inline constexpr bool __libcpp_random_is_valid_inttype<long long> = true;
template <> // extension
inline constexpr bool __libcpp_random_is_valid_inttype<uint8_t> = true;
template <>
inline constexpr bool __libcpp_random_is_valid_inttype<unsigned short> = true;
template <>
inline constexpr bool __libcpp_random_is_valid_inttype<unsigned int> = true;
template <>
inline constexpr bool __libcpp_random_is_valid_inttype<unsigned long> = true;
template <>
inline constexpr bool __libcpp_random_is_valid_inttype<unsigned long long> = true;
#if _CCCL_HAS_INT128()
template <> // extension
inline constexpr bool __libcpp_random_is_valid_inttype<__int128_t> = true;
template <> // extension
inline constexpr bool __libcpp_random_is_valid_inttype<__uint128_t> = true;
#endif // _CCCL_HAS_INT128()
template <class _Type>
inline constexpr bool __cccl_random_is_valid_inttype =
::cuda::std::__cccl_is_integer_v<_Type> && !::cuda::std::is_const_v<_Type>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

!::cuda::std::is_const_v<_Type> is not necessary, __cccl_is_integer_v already requires the type to be cv-unqualified

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well, at this point, I would use __cccl_is_integer_v directly


// [rand.req.urng]/3:
// A class G meets the uniform random bit generator requirements if G models
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ _CCCL_BEGIN_NAMESPACE_CUDA_STD
template <class _RealType = double>
class lognormal_distribution
{
static_assert(__libcpp_random_is_valid_realtype<_RealType>, "RealType must be a supported floating-point type");
static_assert(__cccl_random_is_valid_realtype<_RealType>, "RealType must be a supported floating-point type");

public:
// types
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ _CCCL_BEGIN_NAMESPACE_CUDA_STD
template <class _IntType = int>
class negative_binomial_distribution
{
static_assert(__libcpp_random_is_valid_inttype<_IntType>, "IntType must be a supported integer type");
static_assert(__cccl_random_is_valid_inttype<_IntType>, "IntType must be a supported integer type");

public:
// types
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ _CCCL_BEGIN_NAMESPACE_CUDA_STD
template <class _RealType = double>
class normal_distribution
{
static_assert(__libcpp_random_is_valid_realtype<_RealType>, "RealType must be a supported floating-point type");
static_assert(__cccl_random_is_valid_realtype<_RealType>, "RealType must be a supported floating-point type");

public:
// types
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ _CCCL_BEGIN_NAMESPACE_CUDA_STD
template <class _IntType = int>
class poisson_distribution
{
static_assert(__libcpp_random_is_valid_inttype<_IntType>, "IntType must be a supported integer type");
static_assert(__cccl_random_is_valid_inttype<_IntType>, "IntType must be a supported integer type");

public:
// types
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ _CCCL_BEGIN_NAMESPACE_CUDA_STD
template <class _RealType = double>
class student_t_distribution
{
static_assert(__libcpp_random_is_valid_realtype<_RealType>, "RealType must be a supported floating-point type");
static_assert(__cccl_random_is_valid_realtype<_RealType>, "RealType must be a supported floating-point type");

public:
// types
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ class __independent_bits_engine
template <class _IntType = int>
class uniform_int_distribution
{
static_assert(__libcpp_random_is_valid_inttype<_IntType>, "IntType must be a supported integer type");
static_assert(__cccl_random_is_valid_inttype<_IntType>, "IntType must be a supported integer type");

public:
// types
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ _CCCL_BEGIN_NAMESPACE_CUDA_STD
template <class _RealType = double>
class uniform_real_distribution
{
static_assert(__libcpp_random_is_valid_realtype<_RealType>, "RealType must be a supported floating-point type");
static_assert(__cccl_random_is_valid_realtype<_RealType>, "RealType must be a supported floating-point type");

public:
// types
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ _CCCL_BEGIN_NAMESPACE_CUDA_STD
template <class _RealType = double>
class weibull_distribution
{
static_assert(__libcpp_random_is_valid_realtype<_RealType>, "RealType must be a supported floating-point type");
static_assert(__cccl_random_is_valid_realtype<_RealType>, "RealType must be a supported floating-point type");

public:
// types
Expand Down
1 change: 1 addition & 0 deletions libcudacxx/include/cuda/std/__random_
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <cuda/std/__random/extreme_value_distribution.h>
#include <cuda/std/__random/fisher_f_distribution.h>
#include <cuda/std/__random/gamma_distribution.h>
#include <cuda/std/__random/generate_canonical.h>
#include <cuda/std/__random/geometric_distribution.h>
#include <cuda/std/__random/linear_congruential_engine.h>
#include <cuda/std/__random/lognormal_distribution.h>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES.
//
//===----------------------------------------------------------------------===//
//

#include <cuda/std/__random_>
#include <cuda/std/array>
#include <cuda/std/cassert>
#include <cuda/std/numeric>

#include "test_macros.h"

template <class T>
__host__ __device__ void test()
{
cuda::std::array<T, 100> data;
cuda::std::philox4x64 g{};
for (auto& v : data)
{
v = cuda::std::generate_canonical<T, cuda::std::numeric_limits<T>::digits>(g);
assert(v >= T(0) && v < T(1));
}
auto mean = cuda::std::accumulate(data.begin(), data.end(), T(0)) / data.size();
assert(mean > T(0.4) && mean < T(0.6));
}

int main(int, char**)
{
test<float>();
test<double>();
return 0;
}
Loading