New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Linalg - Vector Class #3277
Linalg - Vector Class #3277
Changes from 6 commits
f826ba6
a7d1e42
b954a77
9275d92
89dc62f
3baae80
bde4bd0
27f3341
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* | ||
* Copyright (c) 2016, Shogun-Toolbox e.V. <shogun-team@shogun-toolbox.org> | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
* | ||
* 1. Redistributions of source code must retain the above copyright notice, | ||
* this list of conditions and the following disclaimer. | ||
* | ||
* 2. Redistributions in binary form must reproduce the above copyright | ||
* notice, this list of conditions and the following disclaimer in the | ||
* documentation and/or other materials provided with the distribution. | ||
* | ||
* 3. Neither the name of the copyright holder nor the names of its | ||
* contributors may be used to endorse or promote products derived from | ||
* this software without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
* POSSIBILITY OF SUCH DAMAGE. | ||
* | ||
* Authors: 2016 Pan Deng, Soumyajit De, Viktor Gal | ||
*/ | ||
|
||
#include <shogun/mathematics/linalg/GPUVectorImpl.h> | ||
|
||
#ifdef HAVE_CXX11 | ||
#ifdef HAVE_VIENNACL | ||
|
||
namespace shogun | ||
{ | ||
|
||
template<class T> | ||
Vector<T>::GPUVectorImpl::GPUVectorImpl() | ||
{ | ||
} | ||
|
||
template<class T> | ||
Vector<T>::GPUVectorImpl::GPUVectorImpl(T* data, index_t len) | ||
:m_GPUptr(new VCLMemoryArray()), m_len(len), m_offset(0) | ||
{ | ||
viennacl::backend::memory_create(*m_GPUptr, sizeof(T)*m_len, | ||
viennacl::context()); | ||
|
||
viennacl::backend::memory_write(*m_GPUptr, 0, m_len*sizeof(T), data); | ||
} | ||
|
||
template<class T> | ||
Vector<T>::GPUVectorImpl::GPUVectorImpl | ||
(const Vector<T>::GPUVectorImpl &array) | ||
{ | ||
m_GPUptr = array.m_GPUptr; | ||
m_len = array.m_len; | ||
m_offset = array.m_offset; | ||
} | ||
|
||
template<class T> | ||
typename Vector<T>::GPUVectorImpl::VCLVectorBase Vector<T>::GPUVectorImpl::GPUvec() | ||
{ | ||
return VCLVectorBase(*m_GPUptr, m_len, m_offset, 1); | ||
} | ||
|
||
template<class T> | ||
typename Vector<T>::GPUVectorImpl::VCLVector Vector<T>::GPUVectorImpl::vector() | ||
{ | ||
return VCLVector(Vector<T>::GPUVectorImpl::GPUvec()); | ||
} | ||
|
||
template <class T> | ||
void Vector<T>::GPUVectorImpl::transferToCPU(T* data) | ||
{ | ||
viennacl::backend::memory_read(*m_GPUptr, m_offset*sizeof(T), m_len*sizeof(T), | ||
data); | ||
} | ||
|
||
template <class T> | ||
viennacl::const_entry_proxy<T> Vector<T>::GPUVectorImpl::operator[](index_t index) const | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a dangerous operations, as expensive. Remove. |
||
{ | ||
return viennacl::const_entry_proxy<T>(m_offset+index, *m_GPUptr); | ||
} | ||
|
||
template <class T> | ||
viennacl::entry_proxy<T> Vector<T>::GPUVectorImpl::operator[](index_t index) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here |
||
{ | ||
return viennacl::entry_proxy<T>(m_offset+index, *m_GPUptr); | ||
} | ||
|
||
template class Vector<int32_t>; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what about 64 bit? |
||
template class Vector<float32_t>; | ||
|
||
} | ||
|
||
#endif //HAVE_VIENNACL | ||
#endif //HAVE_CXX11 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* | ||
* Copyright (c) 2016, Shogun-Toolbox e.V. <shogun-team@shogun-toolbox.org> | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
* | ||
* 1. Redistributions of source code must retain the above copyright notice, | ||
* this list of conditions and the following disclaimer. | ||
* | ||
* 2. Redistributions in binary form must reproduce the above copyright | ||
* notice, this list of conditions and the following disclaimer in the | ||
* documentation and/or other materials provided with the distribution. | ||
* | ||
* 3. Neither the name of the copyright holder nor the names of its | ||
* contributors may be used to endorse or promote products derived from | ||
* this software without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
* POSSIBILITY OF SUCH DAMAGE. | ||
* | ||
* Authors: 2016 Pan Deng, Soumyajit De, Viktor Gal | ||
*/ | ||
|
||
|
||
|
||
#include <shogun/lib/config.h> | ||
#include <shogun/io/SGIO.h> | ||
#include <shogun/lib/SGVector.h> | ||
#include <shogun/mathematics/linalg/Vector.h> | ||
#include <memory> | ||
|
||
#ifndef GPU_VECTOR_IMPL_H__ | ||
#define GPU_VECTOR_IMPL_H__ | ||
|
||
#ifdef HAVE_CXX11 | ||
#ifdef HAVE_VIENNACL | ||
#include <viennacl/vector.hpp> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This class should be able to transfer vectors from CPU to GPU via multiple GPU libraries (and use whatever GPU library the user assigns) in the future |
||
#endif | ||
|
||
namespace shogun | ||
{ | ||
|
||
/** GPU array structure */ | ||
template <class T> | ||
class Vector<T>::GPUVectorImpl | ||
{ | ||
#ifdef HAVE_VIENNACL | ||
|
||
typedef viennacl::backend::mem_handle VCLMemoryArray; | ||
typedef viennacl::vector_base<T, std::size_t, std::ptrdiff_t> VCLVectorBase; | ||
typedef viennacl::vector<T> VCLVector; | ||
|
||
public: | ||
/** Default constructor */ | ||
GPUVectorImpl(); | ||
|
||
/** Creates a gpu vector with Vector */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please write clearer API docs. What does this mean? :) |
||
GPUVectorImpl(T* data, index_t len); | ||
|
||
/** Copy Constructor */ | ||
GPUVectorImpl(const Vector<T>::GPUVectorImpl &array); | ||
|
||
/** Returns a ViennaCL vector wrapped around the data of this vector. Can be | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This one is good |
||
* used to call native ViennaCL methods on this vector | ||
*/ | ||
VCLVectorBase GPUvec(); | ||
|
||
/** Cast of VCLVectorBase to VCLVector | ||
* allows element access | ||
*/ | ||
VCLVector vector(); | ||
|
||
/** Read only memory access. Note that this is very slow as it copies the | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove |
||
* element from the GPU to the CPU | ||
* | ||
* @param index Element index | ||
*/ | ||
viennacl::const_entry_proxy<T> operator[](index_t index) const; | ||
|
||
/** Read/write memory access. Note that this is very slow as it copies the | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove |
||
* element between the GPU and the CPU | ||
* | ||
* @param index Element index | ||
*/ | ||
viennacl::entry_proxy<T> operator[](index_t index); | ||
|
||
void transferToCPU(T* data); | ||
|
||
|
||
private: | ||
/** Memory segment holding the data for the vector */ | ||
alignas(CPU_CACHE_LINE_SIZE) std::shared_ptr<VCLMemoryArray> m_GPUptr; | ||
|
||
/** Vector length */ | ||
alignas(CPU_CACHE_LINE_SIZE) index_t m_len; | ||
|
||
/** Offset for the memory segment, i.e the data of the vector | ||
* starts at vector+offset | ||
*/ | ||
alignas(CPU_CACHE_LINE_SIZE) index_t m_offset; | ||
|
||
#endif //HAVE_CXX11 | ||
}; | ||
|
||
} | ||
|
||
#endif //HAVE_VIENNACL | ||
|
||
#endif |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we have a constructor that just creates an empty structure on the GPU? So that I can allocate giant heaps of memory directly in GPU without the need to transfer it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually for both vectors, matrices, etc