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>
37 typedef typename std::add_pointer<T>::type pointer_type;
38 typedef typename std::add_const<pointer_type>::type const_pointer_type;
39 typedef typename std::add_reference<T>::type reference_type;
41 Linear(const size_t count = 0)
44 m_extent = sizeof(value_type);
45 md_data = static_cast<pointer_type>(allocate(m_extent * count));
53 void* allocate(const size_t byte)
58 status = cudaMalloc(&p, byte);
59 if (status != cudaSuccess) {
60 throw std::bad_alloc(cudaGetErrorString(status));
66 void deallocate(void* p)
71 void copy_from(const void* const src, const size_t byte)
73 cudaMemcpy(md_data, src, byte, cudaMemcpyHostToDevice);
76 void copy_to(void* const dst, const size_t byte)
78 cudaMemcpy(dst, md_data, byte, cudaMemcpyDeviceToHost);
81 size_t size() const { return m_extent * m_count; }
82 size_t count() const { return m_count; }
83 pointer_type data() const { return md_data; }
85 MDView<value_type, 1> ref() const
87 return MDView<value_type, 1>(md_data, &m_extent);
101 typedef T value_type;
102 typedef typename std::add_pointer<T>::type pointer_type;
103 typedef typename std::add_const<pointer_type>::type const_pointer_type;
104 typedef typename std::add_reference<T>::type reference_type;
106 LinearPitch(const size_t width, const size_t height)
107 : m_width(width), m_height(height)
109 md_data = static_cast<pointer_type>(allocate(width, height, &m_extent[0]));
110 m_extent[1] = sizeof(value_type);
118 void* allocate(const size_t width, const size_t height, size_t* pitch)
123 status = cudaMallocPitch(&p, pitch, sizeof(value_type) * width, height);
124 if (status != cudaSuccess) {
125 throw std::bad_alloc(cudaGetErrorString(status));
131 void deallocate(void* p)
136 void copy_from(const void* const src, const size_t width, const size_t height, size_t spitch = 0)
141 cudaMemcpy2D(md_data, pitch(), src, spitch, width, height, cudaMemcpyHostToDevice);
144 void copy_to(void* const dst, const size_t width, const size_t height, size_t dpitch = 0)
149 cudaMemcpy2D(dst, dpitch, md_data, pitch(), width, height, cudaMemcpyDeviceToHost);
152 size_t size() const { return m_extent[1] * m_height; }
153 size_t width() const { return m_width; }
154 size_t height() const { return m_height; }
155 size_t pitch() const { return m_extent[0]; }
156 pointer_type data() const { return md_data; }
158 MDView<value_type, 2> ref() const
160 return MDView<value_type, 2>(md_data, m_extent);
167 pointer_type md_data;
172 #endif /* FM7b5_DEVICE_MEMORY_CUH */