Skip to content
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

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/shogun/lib/SGReferencedData.cpp
Expand Up @@ -112,4 +112,9 @@ int32_t SGReferencedData::unref()
return c;
}
}

void SGReferencedData::allocate_ref()
{
m_refcount = new RefCount(0);
}
}
3 changes: 3 additions & 0 deletions src/shogun/lib/SGReferencedData.h
Expand Up @@ -60,6 +60,9 @@ class SGReferencedData
*/
int32_t unref();

/** Allocate a new m_refcount */
void allocate_ref();

/** needs to be overridden to copy data */
virtual void copy_data(const SGReferencedData &orig)=0;

Expand Down
6 changes: 6 additions & 0 deletions src/shogun/lib/SGVector.cpp
Expand Up @@ -81,6 +81,12 @@ SGVector<T>::SGVector(T* v, index_t len, bool ref_counting)
{
}

template<class T>
SGVector<T>::SGVector(T* v, index_t len, const SGReferencedData &orig)
: SGReferencedData(orig), vector(v), vlen(len)
{
}

template<class T>
SGVector<T>::SGVector(index_t len, bool ref_counting)
: SGReferencedData(ref_counting), vlen(len)
Expand Down
3 changes: 3 additions & 0 deletions src/shogun/lib/SGVector.h
Expand Up @@ -52,6 +52,9 @@ template<class T> class SGVector : public SGReferencedData
/** Constructor for setting params */
SGVector(T* v, index_t len, bool ref_counting=true);

/** Constructor with SGReferenceData */
SGVector(T* v, index_t len, const SGReferencedData &orig);

/** Wraps a vector around an existing memory segment with an offset */
SGVector(T* m, index_t len, index_t offset)
: SGReferencedData(false), vector(m+offset), vlen(len) { }
Expand Down
3 changes: 3 additions & 0 deletions src/shogun/lib/common.h
Expand Up @@ -42,6 +42,9 @@
#undef __STDC_FORMAT_MACROS
#endif

/** CPU cache line size */
#define CPU_CACHE_LINE_SIZE 64

/**
* Implementations tend to follow IEEE754
* @see http://ieeexplore.ieee.org/xpl/freeabs_all.jsp?arnumber=4610935
Expand Down
99 changes: 99 additions & 0 deletions src/shogun/mathematics/linalg/GPUVectorImpl.cpp
@@ -0,0 +1,99 @@
/*
* 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(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());
}

template<class T>
Vector<T>::GPUVectorImpl::GPUVectorImpl(T* data, index_t len)
Copy link
Member

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?

Copy link
Member

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

: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 Vector<int32_t>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about 64 bit?
Some backends dont support it I guess? Does yours?
IF the backend supports, it should be offered here as well

template class Vector<float32_t>;

}

#endif //HAVE_VIENNACL
#endif //HAVE_CXX11
114 changes: 114 additions & 0 deletions src/shogun/mathematics/linalg/GPUVectorImpl.h
@@ -0,0 +1,114 @@
/*
* 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>
Copy link
Member Author

@OXPHOS OXPHOS Jun 13, 2016

Choose a reason for hiding this comment

The 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 new vector
*
* @param length Number of elements
*/
GPUVectorImpl(index_t length);

/** Creates a gpu vector with Vector */
Copy link
Member

Choose a reason for hiding this comment

The 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
Copy link
Member

Choose a reason for hiding this comment

The 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();

/** Size */
inline index_t size() const { return m_len; }

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