42 template <AddressSpace AdrSp,
typename T>
44 T *fill_n(T *first,
size_t count,
const T &value) {
48 if constexpr (AdrSp == Host) {
49 return std::fill_n(first, count, value);
51 auto value_bytes = std::as_bytes(std::span(&value, 1));
52 bool is_zero = std::ranges::all_of(value_bytes, [](
auto b) {
return b == std::byte{0}; });
56 for (
int n = 0; n <
sizeof(T); ++n) {
57 const int byte_value [[maybe_unused]] =
static_cast<int>(value_bytes[n]);
58 device_error_check(cudaMemset2D((
char *)(first) + n,
sizeof(T), byte_value, 1, count),
"cudaMemset2D");
77 template <AddressSpace AdrSp,
typename T>
79 T *fill(T *first, T *end,
const T &value) {
80 if (std::distance(first, end) > 0)
return fill_n<AdrSp>(first, std::distance(first, end), value);
97 template <AddressSpace AdrSp,
typename T>
99 void fill2D_n(T *first [[maybe_unused]],
size_t pitch [[maybe_unused]],
size_t width,
size_t height,
const T &value) {
102 static_assert(AdrSp == mem::Device or AdrSp == mem::Unified,
"Not implemented for host memory");
104 bool is_zero = std::ranges::all_of(std::as_bytes(std::span(&value, 1)), [](
auto b) {
return b == std::byte{0}; });
106 device_error_check(cudaMemset2D(first, pitch *
sizeof(T), 0, width *
sizeof(T), height),
"cudaMemset2D");
108 std::vector<T> v(width * height, value);
109 device_error_check(cudaMemcpy2D(first, pitch *
sizeof(T), v.data(), width *
sizeof(T), width *
sizeof(T), height, cudaMemcpyDefault),
Provides definitions and type traits involving the different memory address spaces supported by nda.
static const auto check_adr_sp_valid
Check validity of a set of nda::mem::AddressSpace values.
static constexpr bool have_cuda
Constexpr variable that is true if the project is configured with CUDA support.
#define device_error_check(ARG1, ARG2)
Trigger a compilation error every time the nda::device_error_check function is called.
static constexpr bool have_device
Constexpr variable that is true if the project is configured with GPU support.
constexpr bool is_scalar_or_convertible_v
Constexpr variable that is true if type S is a scalar type (see nda::is_scalar_v) or if a std::comple...
Provides type traits for the nda library.