SHOGUN  4.1.0
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
GPUVector.cpp
浏览该文件的文档.
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 
44 #ifdef HAVE_EIGEN3
46 #endif
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 #ifdef HAVE_EIGEN3
93 template <class T>
94 CGPUVector<T>::CGPUVector(const EigenVectorXt& cpu_vec)
95 : vector(new VCLMemoryArray())
96 {
97  init();
98  vlen = cpu_vec.size();
99 
100  viennacl::backend::memory_create(*vector, sizeof(T)*vlen,
101  viennacl::context());
102 
103  viennacl::backend::memory_write(*vector, 0, vlen*sizeof(T),
104  cpu_vec.data());
105 }
106 
107 template <class T>
108 CGPUVector<T>::CGPUVector(const EigenRowVectorXt& cpu_vec)
109 : vector(new VCLMemoryArray())
110 {
111  init();
112  vlen = cpu_vec.size();
113 
114  viennacl::backend::memory_create(*vector, sizeof(T)*vlen,
115  viennacl::context());
116 
117  viennacl::backend::memory_write(*vector, 0, vlen*sizeof(T),
118  cpu_vec.data());
119 }
120 
121 template <class T>
122 CGPUVector<T>::operator EigenVectorXt() const
123 {
124  EigenVectorXt cpu_vec(vlen);
125 
126  viennacl::backend::memory_read(*vector, offset*sizeof(T), vlen*sizeof(T),
127  cpu_vec.data());
128 
129  return cpu_vec;
130 }
131 
132 template <class T>
133 CGPUVector<T>::operator EigenRowVectorXt() const
134 {
135  EigenRowVectorXt cpu_vec(vlen);
136 
137  viennacl::backend::memory_read(*vector, offset*sizeof(T), vlen*sizeof(T),
138  cpu_vec.data());
139 
140  return cpu_vec;
141 }
142 #endif
143 
144 template <class T>
145 CGPUVector<T>::operator SGVector<T>() const
146 {
147  SGVector<T> cpu_vec(vlen);
148 
149  viennacl::backend::memory_read(*vector, offset*sizeof(T), vlen*sizeof(T),
150  cpu_vec.vector);
151 
152  return cpu_vec;
153 }
154 
155 template <class T>
156 typename CGPUVector<T>::VCLVectorBase CGPUVector<T>::vcl_vector()
157 {
158  return VCLVectorBase(*vector,vlen, offset, 1);
159 }
160 
161 template <class T>
162 void CGPUVector<T>::display_vector(const char* name) const
163 {
164  ((SGVector<T>)*this).display_vector(name);
165 }
166 
167 template <class T>
168 void CGPUVector<T>::zero()
169 {
170  vcl_vector().clear();
171 }
172 
173 template <class T>
174 void CGPUVector<T>::set_const(T value)
175 {
176  VCLVectorBase v = vcl_vector();
177  viennacl::linalg::vector_assign(v, value);
178 }
179 
180 template <class T>
181 viennacl::const_entry_proxy< T > CGPUVector<T>::operator[](index_t index) const
182 {
183  return viennacl::const_entry_proxy<T>(offset+index, *vector);
184 }
185 
186 template <class T>
187 viennacl::entry_proxy< T > CGPUVector<T>::operator[](index_t index)
188 {
189  return viennacl::entry_proxy<T>(offset+index, *vector);
190 }
191 
192 template <class T>
193 void CGPUVector<T>::init()
194 {
195  vlen = 0;
196  offset = 0;
197 }
198 
199 template class CGPUVector<char>;
200 template class CGPUVector<uint8_t>;
201 template class CGPUVector<int16_t>;
202 template class CGPUVector<uint16_t>;
203 template class CGPUVector<int32_t>;
204 template class CGPUVector<uint32_t>;
205 template class CGPUVector<int64_t>;
206 template class CGPUVector<uint64_t>;
207 template class CGPUVector<float32_t>;
208 template class CGPUVector<float64_t>;
209 }
210 
211 #endif // HAVE_CXX11
212 #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 机器学习工具包 - 项目文档