-
-
Notifications
You must be signed in to change notification settings - Fork 1k
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 2 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,87 @@ | ||
/* | ||
* 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> | ||
LinalgVector<T>::GPUVectorImpl::GPUVectorImpl() | ||
{ | ||
} | ||
|
||
template<class T> | ||
LinalgVector<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> | ||
LinalgVector<T>::GPUVectorImpl::GPUVectorImpl | ||
(const LinalgVector<T>::GPUVectorImpl &array) | ||
{ | ||
m_GPUptr = array.m_GPUptr; | ||
m_len = array.m_len; | ||
m_offset = array.m_offset; | ||
} | ||
|
||
template<class T> | ||
typename LinalgVector<T>::GPUVectorImpl::VCLVectorBase LinalgVector<T>::GPUVectorImpl::GPUvec() | ||
{ | ||
return VCLVectorBase(*m_GPUptr, m_len, m_offset, 1); | ||
} | ||
|
||
template<class T> | ||
typename LinalgVector<T>::GPUVectorImpl::VCLVector LinalgVector<T>::GPUVectorImpl::vector() | ||
{ | ||
return VCLVector(LinalgVector<T>::GPUVectorImpl::GPUvec()); | ||
} | ||
|
||
template class LinalgVector<int32_t>; | ||
template class LinalgVector<float32_t>; | ||
|
||
} | ||
|
||
#endif //HAVE_VIENNACL | ||
#endif //HAVE_CXX11 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
* 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/linalgVector.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 |
||
|
||
namespace shogun | ||
{ | ||
|
||
/** GPU array structure */ | ||
template <class T> | ||
class LinalgVector<T>::GPUVectorImpl | ||
{ | ||
|
||
typedef viennacl::backend::mem_handle VCLMemoryArray; | ||
typedef viennacl::vector_base<T, std::size_t, std::ptrdiff_t> VCLVectorBase; | ||
typedef viennacl::vector<T> VCLVector; | ||
|
||
public: | ||
GPUVectorImpl(); | ||
|
||
/** Creates a gpu vector with LinalgVector */ | ||
GPUVectorImpl(T* data, index_t len); | ||
|
||
/** Copy Constructor */ | ||
GPUVectorImpl(const LinalgVector<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(); | ||
|
||
|
||
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
/* | ||
* 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/linalgVector.h> | ||
#include <shogun/mathematics/linalg/GPUVectorImpl.h> | ||
|
||
#ifdef HAVE_CXX11 | ||
|
||
namespace shogun | ||
{ | ||
|
||
template<class T> | ||
LinalgVector<T>::LinalgVector() | ||
{ | ||
init(); | ||
} | ||
|
||
template<class T> | ||
LinalgVector<T>::LinalgVector(SGVector<T> const &vector) | ||
{ | ||
init(); | ||
m_data = reinterpret_cast<T*>(SG_MALLOC(aligned_t, vector.vlen)); | ||
std::copy(vector.vector, vector.vector+vector.vlen, m_data); | ||
m_len = vector.vlen; | ||
} | ||
|
||
template<class T> | ||
LinalgVector<T>::LinalgVector(LinalgVector<T> const &vector) | ||
{ | ||
init(); | ||
m_data = vector.m_data; | ||
m_len = vector.m_len; | ||
m_onGPU = vector.m_onGPU; | ||
|
||
if (vector.onGPU()) | ||
{ | ||
m_onGPU = true; | ||
m_gpu_impl = std::unique_ptr<GPUVectorImpl>(new GPUVectorImpl(*(vector.m_gpu_impl))); | ||
} | ||
} | ||
|
||
template<class T> | ||
LinalgVector<T>::~LinalgVector() | ||
{ | ||
free(m_data); | ||
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. Does it work properly? Did you valgrind? 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. |
||
} | ||
|
||
template<class T> | ||
LinalgVector<T>& LinalgVector<T>::operator=(SGVector<T> const &vector) | ||
{ | ||
m_data = reinterpret_cast<T*>(SG_MALLOC(aligned_t, vector.vlen)); | ||
std::copy(vector.vector, vector.vector+vector.vlen, m_data); | ||
m_len = vector.vlen; | ||
m_onGPU = false; | ||
m_gpu_impl.release(); | ||
return *this; | ||
} | ||
|
||
template<class T> | ||
LinalgVector<T>& LinalgVector<T>::operator=(LinalgVector<T> const &vector) | ||
{ | ||
|
||
m_data = vector.m_data; | ||
m_len = vector.m_len; | ||
m_onGPU = vector.m_onGPU; | ||
if (vector.onGPU()) | ||
{ | ||
m_gpu_impl.reset(new GPUVectorImpl(*(vector.m_gpu_impl))); | ||
} | ||
return *this; | ||
|
||
} | ||
|
||
template<class T> | ||
LinalgVector<T>::operator SGVector<T>() const | ||
{ | ||
return SGVector<T>(m_data, m_len); | ||
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 the case when data is on GPU? Say, you transfer the data on GPU once and then did a bunch of in-place operations. Then the data pointed by m_data is no-longer what the user would want. I think if the data is on GPU, it should copy that to m_data first. Then it should create a new SGVector, deep copy m_data to the vector of that SGVector and then return. This thing above will lead to segfault. For example: SGVector<float64_t> v;
if (some_condition)
{
// create a LinalgVector instance, vec, and do some linalg, finally assign that to the above SGVector
v = vec;
// vec instance dies, along with its m_data
}
// try to use v, bam! dangling pointer, v.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. actually, when you fix the |
||
} | ||
|
||
template<class T> | ||
bool LinalgVector<T>::onGPU() const | ||
{ | ||
return m_onGPU; | ||
} | ||
|
||
template<class T> | ||
T* LinalgVector<T>::data() | ||
{ | ||
return m_data; | ||
} | ||
|
||
template<class T> | ||
T const * LinalgVector<T>::data() const | ||
{ | ||
return m_data; | ||
} | ||
|
||
template<class T> | ||
index_t LinalgVector<T>::size() const | ||
{ | ||
return m_len; | ||
} | ||
|
||
template<class T> | ||
void LinalgVector<T>::transferToGPU() | ||
{ | ||
#ifdef HAVE_VIENNACL | ||
m_gpu_impl = std::unique_ptr<GPUVectorImpl>(new GPUVectorImpl(m_data, m_len)); | ||
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. Thread safety? |
||
m_onGPU = true; | ||
#else | ||
SG_SERROR("User did not register GPU backend. \n"); | ||
#endif | ||
} | ||
|
||
template<class T> | ||
void LinalgVector<T>::transferToCPU() | ||
{ | ||
if (m_gpu_impl != nullptr) | ||
{ | ||
m_gpu_impl.release(); | ||
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 happens to the data on GPU? Shouldn't it be copied to the m_data? |
||
} | ||
m_onGPU = false; | ||
} | ||
|
||
template<class T> | ||
void LinalgVector<T>::init() | ||
{ | ||
m_data = nullptr; | ||
m_len = 0; | ||
m_onGPU = false; | ||
} | ||
|
||
template class LinalgVector<int32_t>; | ||
template class LinalgVector<float32_t>; | ||
|
||
} | ||
|
||
#endif //HAVE_CXX11 |
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.
why the double section?