TRIQS/nda
2.0.0
Multi-dimensional array library for C++
Toggle main menu visibility
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
19
namespace
nda::mem {
20
25
39
template
<AddressSpace DestAdrSp, AddressSpace SrcAdrSp>
40
void
memcpy
(
void
*dest,
void
const
*src,
size_t
count) {
41
check_adr_sp_valid<DestAdrSp, SrcAdrSp>
();
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);
75
check_adr_sp_valid<DestAdrSp, SrcAdrSp>
();
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
address_space.hpp
Provides definitions and type traits involving the different memory address spaces supported by nda.
nda::mem::Host
@ Host
Using declaration for the Host address space (see nda::mem::AddressSpace).
Definition
address_space.hpp:51
device.hpp
Provides GPU and non-GPU specific functionality.
nda::mem::check_adr_sp_valid
static const auto check_adr_sp_valid
Check validity of a set of nda::mem::AddressSpace values.
Definition
address_space.hpp:146
nda::have_cuda
static constexpr bool have_cuda
Constexpr variable that is true if the project is configured with CUDA support.
Definition
device.hpp:135
nda::mem::memcpy2D
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
device_error_check
#define device_error_check(ARG1, ARG2)
Trigger a compilation error every time the nda::device_error_check function is called.
Definition
device.hpp:129
nda::have_device
static constexpr bool have_device
Constexpr variable that is true if the project is configured with GPU support.
Definition
device.hpp:132
nda::mem::memcpy
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.hpp
Macros used in the nda library.
nda
mem
memcpy.hpp
Generated by
1.17.0