2 Copyright (C) 2012 fmaj7b5.info
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 2 of the License, or
7 (at your option) any later version.
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.
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/>.
18 #ifndef FM7b5_DEVICE_MEMORY_CUH
19 #define FM7b5_DEVICE_MEMORY_CUH
22 #include <type_traits>
24 #include <cuda_runtime.h>
38 typedef typename std::add_pointer<T>::type pointer_type;
39 typedef typename std::add_const<pointer_type>::type const_pointer_type;
40 typedef typename std::add_reference<T>::type reference_type;
42 Linear(const size_t count = 0)
45 m_extent = sizeof(value_type);
46 md_data = static_cast<pointer_type>(allocate(m_extent * count));
54 void* allocate(const size_t byte)
59 status = cudaMalloc(&p, byte);
60 if (status != cudaSuccess) {
61 throw std::bad_alloc(cudaGetErrorString(status));
67 void deallocate(void* p)
72 void copy_from(const void* const src, const size_t byte)
74 cudaMemcpy(md_data, src, byte, cudaMemcpyHostToDevice);
77 void copy_to(void* const dst, const size_t byte)
79 cudaMemcpy(dst, md_data, byte, cudaMemcpyDeviceToHost);
82 size_t size() const { return m_extent * m_count; }
83 size_t count() const { return m_count; }
84 pointer_type data() const { return md_data; }
86 MDView<value_type, 1> ref() const
88 return MDView<value_type, 1>(md_data, &m_extent);
102 typedef T value_type;
103 typedef typename std::add_pointer<T>::type pointer_type;
104 typedef typename std::add_const<pointer_type>::type const_pointer_type;
105 typedef typename std::add_reference<T>::type reference_type;
107 LinearPitch(const size_t width, const size_t height)
108 : m_width(width), m_height(height)
110 md_data = static_cast<pointer_type>(allocate(width, height, &m_extent[0]));
111 m_extent[1] = sizeof(value_type);
119 void* allocate(const size_t width, const size_t height, size_t* pitch)
124 status = cudaMallocPitch(&p, pitch, sizeof(value_type) * width, height);
125 if (status != cudaSuccess) {
126 throw std::bad_alloc(cudaGetErrorString(status));
132 void deallocate(void* p)
137 void copy_from(const void* const src, const size_t width, const size_t height, size_t spitch = 0)
142 cudaMemcpy2D(md_data, pitch(), src, spitch, width, height, cudaMemcpyHostToDevice);
145 void copy_to(void* const dst, const size_t width, const size_t height, size_t dpitch = 0)
150 cudaMemcpy2D(dst, dpitch, md_data, pitch(), width, height, cudaMemcpyDeviceToHost);
153 size_t size() const { return m_extent[1] * m_height; }
154 size_t width() const { return m_width; }
155 size_t height() const { return m_height; }
156 size_t pitch() const { return m_extent[0]; }
157 pointer_type data() const { return md_data; }
159 MDView<value_type, 2> ref() const
161 return MDView<value_type, 2>(md_data, m_extent);
168 pointer_type md_data;
173 #endif /* FM7b5_DEVICE_MEMORY_CUH */