SHOGUN  6.1.3
GPUMatrix.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014, Shogun Toolbox Foundation
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7 
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its
16  * contributors may be used to endorse or promote products derived from this
17  * software without specific prior written permission.
18 
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  *
31  * Written (W) 2014 Khaled Nasr
32  */
33 
34 #include <shogun/lib/config.h>
35 
36 #ifdef HAVE_VIENNACL
37 #ifdef HAVE_CXX11
38 
39 #include <shogun/lib/GPUMatrix.h>
40 #include <viennacl/matrix.hpp>
41 
43 #include <shogun/lib/SGMatrix.h>
44 
45 #include <type_traits>
46 
47 namespace shogun
48 {
49 
50 template <class T>
51 CGPUMatrix<T>::CGPUMatrix()
52 {
53  init();
54 }
55 
56 template <class T>
57 CGPUMatrix<T>::CGPUMatrix(index_t nrows, index_t ncols) : matrix(new VCLMemoryArray())
58 {
59  init();
60 
61  num_rows = nrows;
62  num_cols = ncols;
63 
64  viennacl::backend::memory_create(*matrix, sizeof(T)*num_rows*num_cols,
65  viennacl::context());
66 }
67 
68 template <class T>
69 CGPUMatrix<T>::CGPUMatrix(std::shared_ptr<VCLMemoryArray> mem, index_t nrows, index_t ncols,
70  index_t mem_offset)
71 {
72  init();
73 
74  matrix = mem;
75  num_rows = nrows;
76  num_cols = ncols;
77  offset = mem_offset;
78 }
79 
80 template <class T>
81 CGPUMatrix<T>::CGPUMatrix(const SGMatrix< T >& cpu_mat) : matrix(new VCLMemoryArray())
82 {
83  init();
84 
85  num_rows = cpu_mat.num_rows;
86  num_cols = cpu_mat.num_cols;
87 
88  viennacl::backend::memory_create(*matrix, sizeof(T)*num_rows*num_cols,
89  viennacl::context());
90 
91  viennacl::backend::memory_write(*matrix, 0, num_rows*num_cols*sizeof(T),
92  cpu_mat.matrix);
93 }
94 
95 template <class T>
96 CGPUMatrix<T>::CGPUMatrix(const EigenMatrixXt& cpu_mat)
97 : matrix(new VCLMemoryArray())
98 {
99  init();
100 
101  num_rows = cpu_mat.rows();
102  num_cols = cpu_mat.cols();
103 
104  viennacl::backend::memory_create(*matrix, sizeof(T)*num_rows*num_cols,
105  viennacl::context());
106 
107  viennacl::backend::memory_write(*matrix, 0, num_rows*num_cols*sizeof(T),
108  cpu_mat.data());
109 }
110 
111 template <class T>
112 CGPUMatrix<T>::operator EigenMatrixXt() const
113 {
114  EigenMatrixXt cpu_mat(num_rows, num_cols);
115 
116  viennacl::backend::memory_read(*matrix, offset*sizeof(T), num_rows*num_cols*sizeof(T),
117  cpu_mat.data());
118 
119  return cpu_mat;
120 }
121 
122 template <class T>
123 CGPUMatrix<T>::operator SGMatrix<T>() const
124 {
125  SGMatrix<T> cpu_mat(num_rows, num_cols);
126 
127  viennacl::backend::memory_read(*matrix, offset*sizeof(T), num_rows*num_cols*sizeof(T),
128  cpu_mat.matrix);
129 
130  return cpu_mat;
131 }
132 
133 template <class T>
134 typename CGPUMatrix<T>::VCLMatrixBase CGPUMatrix<T>::vcl_matrix()
135 {
136 #if VIENNACL_VERSION >= 10600
137  return VCLMatrixBase(*matrix, num_rows, offset, 1, num_rows, num_cols, 0, 1, num_cols, false);
138 #else
139  return VCLMatrixBase(*matrix, num_rows, offset, 1, num_rows, num_cols, 0, 1, num_cols);
140 #endif
141 }
142 
143 template <class T>
144 void CGPUMatrix<T>::display_matrix(const char* name) const
145 {
146  ((SGMatrix<T>)*this).display_matrix(name);
147 }
148 
149 template <class T>
150 void CGPUMatrix<T>::zero()
151 {
152  vcl_matrix().clear();
153 }
154 
155 template <class T>
156 void CGPUMatrix<T>::set_const(T value)
157 {
158  VCLMatrixBase m = vcl_matrix();
159  viennacl::linalg::matrix_assign(m, value);
160 }
161 
162 template <class T>
163 viennacl::const_entry_proxy<T> CGPUMatrix<T>::operator()(index_t i, index_t j) const
164 {
165  return viennacl::const_entry_proxy<T>(offset+i+j*num_rows, *matrix);
166 }
167 
168 template <class T>
169 viennacl::entry_proxy< T > CGPUMatrix<T>::operator()(index_t i, index_t j)
170 {
171  return viennacl::entry_proxy<T>(offset+i+j*num_rows, *matrix);
172 }
173 
174 template <class T>
175 viennacl::const_entry_proxy< T > CGPUMatrix<T>::operator[](index_t index) const
176 {
177  return viennacl::const_entry_proxy<T>(offset+index, *matrix);
178 }
179 
180 template <class T>
181 viennacl::entry_proxy< T > CGPUMatrix<T>::operator[](index_t index)
182 {
183  return viennacl::entry_proxy<T>(offset+index, *matrix);
184 }
185 
186 template <class T>
187 void CGPUMatrix<T>::init()
188 {
189  num_rows = 0;
190  num_cols = 0;
191  offset = 0;
192 }
193 
194 template<typename T> struct dummy {};
195 template<typename T> class CGPUMatrix<dummy<T> > {};
196 
197 template class CGPUMatrix<char>;
198 template class CGPUMatrix<uint8_t>;
199 template class CGPUMatrix<int16_t>;
200 template class CGPUMatrix<uint16_t>;
201 template class CGPUMatrix<int32_t>;
202 template class CGPUMatrix<uint32_t>;
203 template class CGPUMatrix<std::conditional<viennacl::is_primitive_type<int64_t>::value, int64_t, dummy<int64_t> >::type>;
204 template class CGPUMatrix<std::conditional<viennacl::is_primitive_type<uint64_t>::value, uint64_t, dummy<uint64_t> >::type>;
205 template class CGPUMatrix<float32_t>;
206 template class CGPUMatrix<float64_t>;
207 }
208 
209 #endif // HAVE_CXX11
210 #endif // HAVE_VIENNACL
int32_t index_t
Definition: common.h:72
void set_const(Container< T > &a, T value)
void zero(Container< T > &a)
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18

SHOGUN Machine Learning Toolbox - Documentation