TRIQS/nda
2.0.0
Multi-dimensional array library for C++
Toggle main menu visibility
Loading...
Searching...
No Matches
malloc.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 <cstdlib>
17
18
namespace
nda::mem {
19
24
37
template
<AddressSpace AdrSp>
38
void
*
malloc
(
size_t
size) {
39
check_adr_sp_valid<AdrSp>
();
40
static_assert
(
nda::have_device
==
nda::have_cuda
,
"Adjust function for new device types"
);
41
42
void
*ptr =
nullptr
;
43
if
constexpr
(AdrSp ==
Host
) {
44
ptr = std::malloc(size);
// NOLINT (we want to return a void*)
45
}
else
if
constexpr
(AdrSp ==
Device
) {
// NOLINT (branch is not repeated)
46
device_error_check
(cudaMalloc((
void
**)&ptr, size),
"cudaMalloc"
);
47
}
else
{
48
device_error_check
(cudaMallocManaged((
void
**)&ptr, size),
"cudaMallocManaged"
);
49
}
50
return
ptr;
51
}
52
63
template
<AddressSpace AdrSp>
64
void
free
(
void
*p) {
65
check_adr_sp_valid<AdrSp>
();
66
static_assert
(
nda::have_device
==
nda::have_cuda
,
"Adjust function for new device types"
);
67
68
if
constexpr
(AdrSp ==
Host
) {
69
std::free(p);
// NOLINT (we want to call free with a void*)
70
}
else
{
71
device_error_check
(cudaFree(p),
"cudaFree"
);
72
}
73
}
74
76
77
}
// 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
nda::mem::Device
@ Device
Using declaration for the Device 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
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::malloc
void * malloc(size_t size)
Call the correct malloc function based on the given address space.
Definition
malloc.hpp:38
nda::mem::free
void free(void *p)
Call the correct free function based on the given address space.
Definition
malloc.hpp:64
nda
mem
malloc.hpp
Generated by
1.17.0