TRIQS/nda 1.3.0
Multi-dimensional array library for C++
Loading...
Searching...
No Matches
memcpy.hpp
Go to the documentation of this file.
1// Copyright (c) 2022-2023 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: Miguel Morales, Nils Wentzell
16
22#pragma once
23
24#include "./address_space.hpp"
25#include "../device.hpp"
26#include "../macros.hpp"
27
28#include <cstring>
29
30namespace nda::mem {
31
50 template <AddressSpace DestAdrSp, AddressSpace SrcAdrSp>
51 void memcpy(void *dest, void const *src, size_t count) {
53 static_assert(nda::have_device == nda::have_cuda, "Adjust function for new device types");
54
55 if constexpr (DestAdrSp == Host && SrcAdrSp == Host) {
56 std::memcpy(dest, src, count);
57 } else {
58 device_error_check(cudaMemcpy(dest, src, count, cudaMemcpyDefault), "cudaMemcpy");
59 }
60 }
61
83 template <AddressSpace DestAdrSp, AddressSpace SrcAdrSp>
84 void memcpy2D(void *dest, size_t dpitch, const void *src, size_t spitch, size_t width, size_t height) {
85 EXPECTS(width <= dpitch && width <= spitch);
87 static_assert(nda::have_device == nda::have_cuda, "Adjust function for new device types");
88
89 if constexpr (DestAdrSp == Host && SrcAdrSp == Host) {
90 auto *desti = static_cast<unsigned char *>(dest);
91 auto *srci = static_cast<const unsigned char *>(src);
92 for (size_t i = 0; i < height; ++i, desti += dpitch, srci += spitch) std::memcpy(desti, srci, width);
93 } else if (nda::have_device) {
94 device_error_check(cudaMemcpy2D(dest, dpitch, src, spitch, width, height, cudaMemcpyDefault), "cudaMemcpy2D");
95 }
96 }
97
100} // 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 memcpy2D(void *dest, size_t dpitch, const void *src, size_t spitch, size_t width, size_t height)
Call CUDA's cudaMemcpy2D function or simulate its behavior on the Host based on the given address spa...
Definition memcpy.hpp:84
#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 memcpy(void *dest, void const *src, size_t count)
Call the correct memcpy function based on the given address spaces.
Definition memcpy.hpp:51
Macros used in the nda library.