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-2024 Simons Foundation
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0.txt
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15// Authors: Thomas Hahn, Miguel Morales, Nils Wentzell
16
22#pragma once
23
24#include "address_space.hpp"
25#include "../device.hpp"
26
27#include <cstring>
28
29namespace nda::mem {
30
48 template <AddressSpace AdrSp>
49 void memset(void *p, int value, size_t count) {
51 static_assert(nda::have_device == nda::have_cuda, "Adjust function for new device types");
52
53 if constexpr (AdrSp == Host) {
54 std::memset(p, value, count);
55 } else {
56 device_error_check(cudaMemset(p, value, count), "cudaMemset");
57 }
58 }
59
77 template <AddressSpace AdrSp>
78 void memset2D(void *ptr, size_t pitch, int value, size_t width, size_t height) {
80 static_assert(nda::have_device == nda::have_cuda, "Adjust function for new device types");
81
82 if constexpr (AdrSp == Host) {
83 auto *ptri = static_cast<unsigned char *>(ptr);
84 for (size_t i = 0; i < height; ++i, ptri += pitch) std::memset(ptri, value, width);
85 } else { // Device or Unified
86 device_error_check(cudaMemset2D(ptr, pitch, value, width, height), "cudaMemset2D");
87 }
88 }
89
92} // 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:146
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:78
#define device_error_check(ARG1, ARG2)
Trigger a compilation error every time the nda::device_error_check function is called.
Definition device.hpp:140
static constexpr bool have_device
Constexpr variable that is true if the project is configured with GPU support.
Definition device.hpp:143
void memset(void *p, int value, size_t count)
Call the correct memset function based on the given address space.
Definition memset.hpp:49