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--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#include "../macros.hpp"
16
17#include <cstring>
18
19namespace nda::mem {
20
25
39 template <AddressSpace DestAdrSp, AddressSpace SrcAdrSp>
40 void memcpy(void *dest, void const *src, size_t count) {
42 static_assert(nda::have_device == nda::have_cuda, "Adjust function for new device types");
43
44 if constexpr (DestAdrSp == Host && SrcAdrSp == Host) {
45 std::memcpy(dest, src, count);
46 } else {
47 device_error_check(cudaMemcpy(dest, src, count, cudaMemcpyDefault), "cudaMemcpy");
48 }
49 }
50
72 template <AddressSpace DestAdrSp, AddressSpace SrcAdrSp>
73 void memcpy2D(void *dest, size_t dpitch, const void *src, size_t spitch, size_t width, size_t height) {
74 EXPECTS(width <= dpitch && width <= spitch);
76 static_assert(nda::have_device == nda::have_cuda, "Adjust function for new device types");
77
78 if constexpr (DestAdrSp == Host && SrcAdrSp == Host) {
79 auto *desti = static_cast<unsigned char *>(dest);
80 auto *srci = static_cast<const unsigned char *>(src);
81 for (size_t i = 0; i < height; ++i, desti += dpitch, srci += spitch) std::memcpy(desti, srci, width);
82 } else if (nda::have_device) {
83 device_error_check(cudaMemcpy2D(dest, dpitch, src, spitch, width, height, cudaMemcpyDefault), "cudaMemcpy2D");
84 }
85 }
86
88
89} // 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 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:73
#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 memcpy(void *dest, void const *src, size_t count)
Call the correct memcpy function based on the given address spaces.
Definition memcpy.hpp:40
Macros used in the nda library.