SHOGUN  4.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
GPUVector.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/GPUVector.h>
40 #include <viennacl/vector.hpp>
41 
42 #include <shogun/lib/SGVector.h>
43 
45 
46 #include <type_traits>
47 
48 namespace shogun
49 {
50 
51 template <class T>
52 CGPUVector<T>::CGPUVector()
53 {
54  init();
55 }
56 
57 template <class T>
58 CGPUVector<T>::CGPUVector(index_t length) : vector(new VCLMemoryArray())
59 {
60  init();
61 
62  vlen = length;
63 
64  viennacl::backend::memory_create(*vector, sizeof(T)*vlen,
65  viennacl::context());
66 }
67 
68 template <class T>
69 CGPUVector<T>::CGPUVector(std::shared_ptr<VCLMemoryArray> mem, index_t length,
70  index_t mem_offset)
71 {
72  init();
73 
74  vector = mem;
75  vlen = length;
76  offset = mem_offset;
77 }
78 
79 template <class T>
80 CGPUVector<T>::CGPUVector(const SGVector<T>& cpu_vec) : vector(new VCLMemoryArray())
81 {
82  init();
83  vlen = cpu_vec.vlen;
84 
85  viennacl::backend::memory_create(*vector, sizeof(T)*vlen,
86  viennacl::context());
87 
88  viennacl::backend::memory_write(*vector, 0, vlen*sizeof(T),
89  cpu_vec.vector);
90 }
91 
92 template <class T>
93 CGPUVector<T>::CGPUVector(const EigenVectorXt& cpu_vec)
94 : vector(new VCLMemoryArray())
95 {
96  init();
97  vlen = cpu_vec.size();
98 
99  viennacl::backend::memory_create(*vector, sizeof(T)*vlen,
100  viennacl::context());
101 
102  viennacl::backend::memory_write(*vector, 0, vlen*sizeof(T),
103  cpu_vec.data());
104 }
105 
106 template <class T>
107 CGPUVector<T>::CGPUVector(const EigenRowVectorXt& cpu_vec)
108 : vector(new VCLMemoryArray())
109 {
110  init();
111  vlen = cpu_vec.size();
112 
113  viennacl::backend::memory_create(*vector, sizeof(T)*vlen,
114  viennacl::context());
115 
116  viennacl::backend::memory_write(*vector, 0, vlen*sizeof(T),
117  cpu_vec.data());
118 }
119 
120 template <class T>
121 CGPUVector<T>::operator EigenVectorXt() const
122 {
123  EigenVectorXt cpu_vec(vlen);
124 
125  viennacl::backend::memory_read(*vector, offset*sizeof(T), vlen*sizeof(T),
126  cpu_vec.data());
127 
128  return cpu_vec;
129 }
130 
131 template <class T>
132 CGPUVector<T>::operator EigenRowVectorXt() const
133 {
134  EigenRowVectorXt cpu_vec(vlen);
135 
136  viennacl::backend::memory_read(*vector, offset*sizeof(T), vlen*sizeof(T),
137  cpu_vec.data());
138 
139  return cpu_vec;
140 }
141 
142 template <class T>
143 CGPUVector<T>::operator SGVector<T>() const
144 {
145  SGVector<T> cpu_vec(vlen);
146 
147  viennacl::backend::memory_read(*vector, offset*sizeof(T), vlen*sizeof(T),
148  cpu_vec.vector);
149 
150  return cpu_vec;
151 }
152 
153 template <class T>
154 typename CGPUVector<T>::VCLVectorBase CGPUVector<T>::vcl_vector()
155 {
156  return VCLVectorBase(*vector, vlen, offset, 1);
157 }
158 
159 template <class T>
160 void CGPUVector<T>::display_vector(const char* name) const
161 {
162  ((SGVector<T>)*this).display_vector(name);
163 }
164 
165 template <class T>
166 void CGPUVector<T>::zero()
167 {
168  vcl_vector().clear();
169 }
170 
171 template <class T>
172 void CGPUVector<T>::set_const(T value)
173 {
174  VCLVectorBase v = vcl_vector();
175  viennacl::linalg::vector_assign(v, value);
176 }
177 
178 template <class T>
179 viennacl::const_entry_proxy< T > CGPUVector<T>::operator[](index_t index) const
180 {
181  return viennacl::const_entry_proxy<T>(offset+index, *vector);
182 }
183 
184 template <class T>
185 viennacl::entry_proxy< T > CGPUVector<T>::operator[](index_t index)
186 {
187  return viennacl::entry_proxy<T>(offset+index, *vector);
188 }
189 
190 template <class T>
191 void CGPUVector<T>::init()
192 {
193  vlen = 0;
194  offset = 0;
195 }
196 
197 template<typename T> struct dummy {};
198 template<typename T> class CGPUVector<dummy<T> > {};
199 
200 template class CGPUVector<char>;
201 template class CGPUVector<uint8_t>;
202 template class CGPUVector<int16_t>;
203 template class CGPUVector<uint16_t>;
204 template class CGPUVector<int32_t>;
205 template class CGPUVector<uint32_t>;
206 template class CGPUVector<std::conditional<viennacl::is_primitive_type<int64_t>::value, int64_t, dummy<int64_t> >::type>;
207 template class CGPUVector<std::conditional<viennacl::is_primitive_type<uint64_t>::value, uint64_t, dummy<uint64_t> >::type>;
208 template class CGPUVector<float32_t>;
209 template class CGPUVector<float64_t>;
210 }
211 
212 #endif // HAVE_CXX11
213 #endif // HAVE_VIENNACL
int32_t index_t
Definition: common.h:62
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18

SHOGUN Machine Learning Toolbox - Documentation