Loading [MathJax]/jax/output/HTML-CSS/config.js
ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages Concepts
CudaHostAllocator.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2010-2022 The ESPResSo project
3 *
4 * This file is part of ESPResSo.
5 *
6 * ESPResSo is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * ESPResSo is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#pragma once
21
22#include <cstddef>
23#include <type_traits>
24#include <vector>
25
26void cuda_malloc_host(void **p, std::size_t n);
27void cuda_free_host(void *p);
28
29/**
30 * @brief Allocator that uses CUDA to allocate CPU memory.
31 *
32 * Using the CUDA allocator can have performance benefits,
33 * because it returns pinned memory that is suitable for
34 * DMA.
35 *
36 * @tparam T Type to allocate memory for.
37 */
38template <class T> struct CudaHostAllocator {
39 using value_type = T;
40 using pointer = T *;
41 using reference = T &;
42 using const_reference = std::add_const_t<reference>;
43
44 CudaHostAllocator() noexcept = default;
45 template <class U> explicit CudaHostAllocator(const CudaHostAllocator<U> &) {}
46 template <class U> bool operator==(const CudaHostAllocator<U> &) const {
47 return true;
48 }
49 template <class U> bool operator!=(const CudaHostAllocator<U> &) const {
50 return false;
51 }
52
53 T *allocate(const std::size_t n) const {
54 T *result{};
55
56 cuda_malloc_host(reinterpret_cast<void **>(&result),
57 n * sizeof(value_type));
58
59 return result;
60 }
61 void deallocate(T *const p, std::size_t) const noexcept { cuda_free_host(p); }
62};
63
64template <class T> using pinned_vector = std::vector<T, CudaHostAllocator<T>>;
void cuda_free_host(void *p)
std::vector< T, CudaHostAllocator< T > > pinned_vector
void cuda_malloc_host(void **p, std::size_t n)
Allocator that uses CUDA to allocate CPU memory.
CudaHostAllocator() noexcept=default
T * allocate(const std::size_t n) const
void deallocate(T *const p, std::size_t) const noexcept
bool operator!=(const CudaHostAllocator< U > &) const
bool operator==(const CudaHostAllocator< U > &) const
std::add_const_t< reference > const_reference