TRIQS/nda 1.3.0
Multi-dimensional array library for C++
Loading...
Searching...
No Matches
memset.hpp
Go to the documentation of this file.
1// Copyright (c) 2022--present, The Simons Foundation
2// This file is part of TRIQS/nda and is licensed under the Apache License, Version 2.0.
3// SPDX-License-Identifier: Apache-2.0
4// See LICENSE in the root of this distribution for details.
5
10
11#pragma once
12
13#include "address_space.hpp"
14#include "../device.hpp"
15
16#include <cstring>
17
18namespace nda::mem {
19
24
37 template <AddressSpace AdrSp>
38 void memset(void *p, int value, size_t count) {
40 static_assert(nda::have_device == nda::have_cuda, "Adjust function for new device types");
41
42 if constexpr (AdrSp == Host) {
43 std::memset(p, value, count);
44 } else {
45 device_error_check(cudaMemset(p, value, count), "cudaMemset");
46 }
47 }
48
66 template <AddressSpace AdrSp>
67 void memset2D(void *ptr, size_t pitch, int value, size_t width, size_t height) {
69 static_assert(nda::have_device == nda::have_cuda, "Adjust function for new device types");
70
71 if constexpr (AdrSp == Host) {
72 auto *ptri = static_cast<unsigned char *>(ptr);
73 for (size_t i = 0; i < height; ++i, ptri += pitch) std::memset(ptri, value, width);
74 } else { // Device or Unified
75 device_error_check(cudaMemset2D(ptr, pitch, value, width, height), "cudaMemset2D");
76 }
77 }
78
80
81} // namespace nda::mem
Provides definitions and type traits involving the different memory address spaces supported by nda.
Provides GPU and non-GPU specific functionality.
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.
Definition device.hpp:135
void memset2D(void *ptr, size_t pitch, int value, size_t width, size_t height)
Call CUDA's cudaMemset2D function or simulate its behavior on the Host based on the given address spa...
Definition memset.hpp:67
#define device_error_check(ARG1, ARG2)
Trigger a compilation error every time the nda::device_error_check function is called.
Definition device.hpp:129
static constexpr bool have_device
Constexpr variable that is true if the project is configured with GPU support.
Definition device.hpp:132
void memset(void *p, int value, size_t count)
Call the correct memset function based on the given address space.
Definition memset.hpp:38