SHOGUN  6.0.0
GPUMemoryViennaCL.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016, Shogun-Toolbox e.V. <shogun-team@shogun-toolbox.org>
3  * All rights reserved.
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  *
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  *
14  * 3. Neither the name of the copyright holder nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  *
30  * Authors: 2016 Pan Deng, Soumyajit De, Heiko Strathmann, Viktor Gal
31  */
32 
33 #ifndef GPU_MEMORY_VIENNACL_H__
34 #define GPU_MEMORY_VIENNACL_H__
35 
36 #include <shogun/lib/common.h>
37 
38 #ifdef HAVE_VIENNACL
39 #include <viennacl/vector.hpp>
40 #include <viennacl/matrix.hpp>
41 #include <memory>
42 
43 namespace shogun
44 {
45 
50 template <typename T>
51 struct GPUMemoryViennaCL : public GPUMemoryBase<T>
52 {
53  friend class LinalgBackendViennaCL;
54 
55  typedef viennacl::backend::mem_handle VCLMemoryArray;
56  typedef viennacl::vector_base<T, std::size_t, std::ptrdiff_t> VCLVectorBase;
57 
59 #if VIENNACL_VERSION >= 10600
60  typedef viennacl::matrix_base<T, std::size_t, std::ptrdiff_t> VCLMatrixBase;
61 #else
62  typedef viennacl::matrix_base<T, viennacl::column_major, std::size_t, std::ptrdiff_t> VCLMatrixBase;
63 #endif
64 
66  GPUMemoryViennaCL() : m_data(new VCLMemoryArray())
67  {
68  init();
69  };
70 
75  GPUMemoryViennaCL(index_t len): m_data(new VCLMemoryArray())
76  {
77  init();
78  viennacl::backend::memory_create(*m_data, sizeof(T)*len,
79  viennacl::context());
80  }
81 
86  GPUMemoryViennaCL(GPUMemoryBase<T>* gpu_ptr) : m_data(new VCLMemoryArray())
87  {
88  GPUMemoryViennaCL<T>* temp_ptr = static_cast<GPUMemoryViennaCL<T>*>(gpu_ptr);
89  init();
90  m_data = temp_ptr->m_data;
91  m_offset = temp_ptr->m_offset;
92  };
93 
99  GPUMemoryBase<T>* clone_vector(GPUMemoryBase<T>* vector, index_t vlen) const
100  {
101  GPUMemoryViennaCL<T>* src_ptr = static_cast<GPUMemoryViennaCL<T>*>(vector);
102  GPUMemoryViennaCL<T>* gpu_ptr = new GPUMemoryViennaCL<T>();
103 
104  viennacl::backend::memory_create(*(gpu_ptr->m_data), sizeof(T)*vlen,
105  viennacl::context());
106  viennacl::backend::memory_copy(*(src_ptr->m_data), *(gpu_ptr->m_data),
107  0, 0, vlen*sizeof(T));
108 
109  return gpu_ptr;
110  }
111 
116  VCLVectorBase data_vector(index_t len)
117  {
118  return VCLVectorBase(*m_data, len, m_offset, 1);
119  }
120 
126  VCLMatrixBase data_matrix(index_t nrows, index_t ncols)
127  {
128  #if VIENNACL_VERSION >= 10600
129  return VCLMatrixBase(*m_data, nrows, m_offset, 1, nrows, ncols, 0, 1, ncols, false);
130  #else
131  return VCLMatrixBase(*m_data, nrows, m_offset, 1, nrows, ncols, 0, 1, ncols);
132  #endif
133  }
134 
135 private:
136  void init()
137  {
138  m_offset = 0;
139  }
140 
142  alignas(CPU_CACHE_LINE_SIZE) std::shared_ptr<VCLMemoryArray> m_data;
143 
147  alignas(CPU_CACHE_LINE_SIZE) index_t m_offset;
148 };
149 
150 }
151 #endif // HAVE_VIENNACL
152 
153 #endif //GPU_MEMORY_VIENNACL_H__
int32_t index_t
Definition: common.h:72
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
constexpr size_t CPU_CACHE_LINE_SIZE
Definition: common.h:80

SHOGUN Machine Learning Toolbox - Documentation