suanPan
🧮 An Open Source, Parallel and Heterogeneous Finite Element Analysis Framework
Loading...
Searching...
No Matches
cuda_ptr.hpp
Go to the documentation of this file.
1/*******************************************************************************
2 * Copyright (C) 2017-2026 Theodore Chang
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 ******************************************************************************/
27#ifndef CUDA_PTR_HPP
28#define CUDA_PTR_HPP
29
30#include <cuda_runtime.h>
31
32class cuda_ptr {
33 void* ptr{};
34
35 [[nodiscard]] size_t total_size() const { return unit * size; }
36
37public:
38 size_t unit{};
39
40 int size{};
41
42 explicit cuda_ptr(const size_t in_unit = 0, const int in_size = 0)
43 : unit(in_unit)
44 , size(in_size) {
45 if(total_size() > 0) cudaMalloc(&ptr, total_size());
46 }
47 cuda_ptr(const cuda_ptr& other)
48 : cuda_ptr(other.unit, other.size) {}
49 cuda_ptr(cuda_ptr&&) = delete;
50 cuda_ptr& operator=(const cuda_ptr&) = delete;
51 cuda_ptr& operator=(cuda_ptr&& other) noexcept {
52 if(this != &other) {
53 cudaFree(ptr);
54 ptr = other.ptr;
55 unit = other.unit;
56 size = other.size;
57 other.ptr = nullptr;
58 other.unit = 0;
59 other.size = 0;
60 }
61 return *this;
62 }
63 ~cuda_ptr() { cudaFree(ptr); }
64
65 template<typename T = int> [[nodiscard]] T* get(const unsigned long long offset = 0) const { return static_cast<T*>(ptr) + offset; }
66
67 auto copy_from(const void* src, const cudaStream_t s) const { cudaMemcpyAsync(ptr, src, total_size(), cudaMemcpyHostToDevice, s); }
68
69 auto copy_to(void* dest, const cudaStream_t s) const {
70 cudaMemcpyAsync(dest, ptr, total_size(), cudaMemcpyDeviceToHost, s);
71 cudaStreamSynchronize(s);
72 }
73};
74
75#endif
76
Definition cuda_ptr.hpp:32
cuda_ptr(const cuda_ptr &other)
Definition cuda_ptr.hpp:47
cuda_ptr(const size_t in_unit=0, const int in_size=0)
Definition cuda_ptr.hpp:42
cuda_ptr(cuda_ptr &&)=delete
size_t unit
Definition cuda_ptr.hpp:38
cuda_ptr & operator=(const cuda_ptr &)=delete
auto copy_from(const void *src, const cudaStream_t s) const
Definition cuda_ptr.hpp:67
T * get(const unsigned long long offset=0) const
Definition cuda_ptr.hpp:65
~cuda_ptr()
Definition cuda_ptr.hpp:63
int size
Definition cuda_ptr.hpp:40
cuda_ptr & operator=(cuda_ptr &&other) noexcept
Definition cuda_ptr.hpp:51
auto copy_to(void *dest, const cudaStream_t s) const
Definition cuda_ptr.hpp:69