Skip to content

Commit

Permalink
LinalgRefactor - Memory Transfer Mutex (#3635)
Browse files Browse the repository at this point in the history
* atomic gpu transfer methods
  • Loading branch information
OXPHOS committed Mar 6, 2017
1 parent e4e6def commit 1d3adbe
Show file tree
Hide file tree
Showing 10 changed files with 327 additions and 154 deletions.
35 changes: 33 additions & 2 deletions src/shogun/lib/SGMatrix.cpp
Expand Up @@ -39,13 +39,25 @@ SGMatrix<T>::SGMatrix(bool ref_counting) : SGReferencedData(ref_counting)
template <class T>
SGMatrix<T>::SGMatrix(T* m, index_t nrows, index_t ncols, bool ref_counting)
: SGReferencedData(ref_counting), matrix(m),
num_rows(nrows), num_cols(ncols), gpu_ptr(nullptr) { }
num_rows(nrows), num_cols(ncols), gpu_ptr(nullptr)
{
m_on_gpu.store(false, std::memory_order_release);
}

template <class T>
SGMatrix<T>::SGMatrix(T* m, index_t nrows, index_t ncols, index_t offset)
: SGReferencedData(false), matrix(m+offset),
num_rows(nrows), num_cols(ncols)
{
m_on_gpu.store(false, std::memory_order_release);
}

template <class T>
SGMatrix<T>::SGMatrix(index_t nrows, index_t ncols, bool ref_counting)
: SGReferencedData(ref_counting), num_rows(nrows), num_cols(ncols), gpu_ptr(nullptr)
{
matrix=SG_MALLOC(T, ((int64_t) nrows)*ncols);
m_on_gpu.store(false, std::memory_order_release);
}

template <class T>
Expand All @@ -56,6 +68,7 @@ SGMatrix<T>::SGMatrix(SGVector<T> vec) : SGReferencedData(vec)
num_rows=vec.vlen;
num_cols=1;
gpu_ptr = vec.gpu_ptr;
m_on_gpu.store(vec.on_gpu(), std::memory_order_release);
}

template <class T>
Expand All @@ -72,13 +85,15 @@ SGMatrix<T>::SGMatrix(SGVector<T> vec, index_t nrows, index_t ncols)
num_rows=nrows;
num_cols=ncols;
gpu_ptr = vec.gpu_ptr;
m_on_gpu.store(vec.on_gpu(), std::memory_order_release);
}

template<class T>
SGMatrix<T>::SGMatrix(GPUMemoryBase<T>* mat, index_t nrows, index_t ncols)
: SGReferencedData(true), matrix(NULL), num_rows(nrows), num_cols(ncols),
gpu_ptr(std::shared_ptr<GPUMemoryBase<T>>(mat))
{
m_on_gpu.store(true, std::memory_order_release);
}

template <class T>
Expand All @@ -92,7 +107,7 @@ SGMatrix<T>::SGMatrix(EigenMatrixXt& mat)
: SGReferencedData(false), matrix(mat.data()),
num_rows(mat.rows()), num_cols(mat.cols()), gpu_ptr(nullptr)
{

m_on_gpu.store(false, std::memory_order_release);
}

template <class T>
Expand All @@ -102,6 +117,19 @@ SGMatrix<T>::operator EigenMatrixXtMap() const
return EigenMatrixXtMap(matrix, num_rows, num_cols);
}

template<class T>
SGMatrix<T>& SGMatrix<T>::operator=(const SGMatrix<T>& other)
{
if(&other == this)
return *this;

unref();
copy_data(other);
copy_refcount(other);
ref();
return *this;
}

template <class T>
SGMatrix<T>::~SGMatrix()
{
Expand Down Expand Up @@ -1066,6 +1094,8 @@ void SGMatrix<T>::copy_data(const SGReferencedData &orig)
matrix=((SGMatrix*)(&orig))->matrix;
num_rows=((SGMatrix*)(&orig))->num_rows;
num_cols=((SGMatrix*)(&orig))->num_cols;
m_on_gpu.store(((SGMatrix*)(&orig))->m_on_gpu.load(
std::memory_order_acquire), std::memory_order_release);
}

template<class T>
Expand All @@ -1075,6 +1105,7 @@ void SGMatrix<T>::init_data()
num_rows=0;
num_cols=0;
gpu_ptr=nullptr;
m_on_gpu.store(false, std::memory_order_release);
}

template<class T>
Expand Down
13 changes: 9 additions & 4 deletions src/shogun/lib/SGMatrix.h
Expand Up @@ -19,6 +19,7 @@
#include <shogun/lib/SGReferencedData.h>

#include <memory>
#include <atomic>

namespace Eigen
{
Expand Down Expand Up @@ -56,9 +57,7 @@ template<class T> class SGMatrix : public SGReferencedData
SGMatrix(T* m, index_t nrows, index_t ncols, bool ref_counting=true);

/** Wraps a matrix around an existing memory segment with an offset */
SGMatrix(T* m, index_t nrows, index_t ncols, index_t offset)
: SGReferencedData(false), matrix(m+offset),
num_rows(nrows), num_cols(ncols) { }
SGMatrix(T* m, index_t nrows, index_t ncols, index_t offset);

/** Constructor to create new matrix in memory */
SGMatrix(index_t nrows, index_t ncols, bool ref_counting=true);
Expand Down Expand Up @@ -122,6 +121,9 @@ template<class T> class SGMatrix : public SGReferencedData

/** Wraps an Eigen3 matrix around the data of this matrix */
operator EigenMatrixXtMap() const;

/** Copy assign operator */
SGMatrix<T>& operator=(const SGMatrix<T>&);
#endif // SWIG

/** Copy constructor */
Expand Down Expand Up @@ -423,7 +425,10 @@ template<class T> class SGMatrix : public SGReferencedData
/** overridden to free data */
virtual void free_data();

private:
private:
/** Atomic variable of vector on_gpu status */
std::atomic<bool> m_on_gpu;

/** Assert whether the data is on GPU
* and raise error if the data is on GPU
*/
Expand Down
30 changes: 28 additions & 2 deletions src/shogun/lib/SGVector.cpp
Expand Up @@ -79,20 +79,30 @@ template<class T>
SGVector<T>::SGVector(T* v, index_t len, bool ref_counting)
: SGReferencedData(ref_counting), vector(v), vlen(len), gpu_ptr(NULL)
{
m_on_gpu.store(false, std::memory_order_release);
}

template<class T>
SGVector<T>::SGVector(T* m, index_t len, index_t offset)
: SGReferencedData(false), vector(m+offset), vlen(len)
{
m_on_gpu.store(false, std::memory_order_release);
}

template<class T>
SGVector<T>::SGVector(index_t len, bool ref_counting)
: SGReferencedData(ref_counting), vlen(len), gpu_ptr(NULL)
{
vector=SG_MALLOC(T, len);
m_on_gpu.store(false, std::memory_order_release);
}

template<class T>
SGVector<T>::SGVector(GPUMemoryBase<T>* vector, index_t len)
: SGReferencedData(true), vector(NULL), vlen(len),
gpu_ptr(std::shared_ptr<GPUMemoryBase<T>>(vector))
{
m_on_gpu.store(true, std::memory_order_release);
}

template<class T>
Expand All @@ -101,6 +111,19 @@ SGVector<T>::SGVector(const SGVector &orig) : SGReferencedData(orig)
copy_data(orig);
}

template<class T>
SGVector<T>& SGVector<T>::operator=(const SGVector<T>& other)
{
if(&other == this)
return *this;

unref();
copy_data(other);
copy_refcount(other);
ref();
return *this;
}

template<class T>
void SGVector<T>::set(SGVector<T> orig)
{
Expand All @@ -117,14 +140,14 @@ template <class T>
SGVector<T>::SGVector(EigenVectorXt& vec)
: SGReferencedData(false), vector(vec.data()), vlen(vec.size()), gpu_ptr(NULL)
{

m_on_gpu.store(false, std::memory_order_release);
}

template <class T>
SGVector<T>::SGVector(EigenRowVectorXt& vec)
: SGReferencedData(false), vector(vec.data()), vlen(vec.size()), gpu_ptr(NULL)
{

m_on_gpu.store(false, std::memory_order_release);
}

template <class T>
Expand Down Expand Up @@ -331,6 +354,8 @@ void SGVector<T>::copy_data(const SGReferencedData &orig)
gpu_ptr=std::shared_ptr<GPUMemoryBase<T>>(((SGVector*)(&orig))->gpu_ptr);
vector=((SGVector*)(&orig))->vector;
vlen=((SGVector*)(&orig))->vlen;
m_on_gpu.store(((SGVector*)(&orig))->m_on_gpu.load(
std::memory_order_acquire), std::memory_order_release);
}

template<class T>
Expand All @@ -339,6 +364,7 @@ void SGVector<T>::init_data()
vector=NULL;
vlen=0;
gpu_ptr=NULL;
m_on_gpu.store(false, std::memory_order_release);
}

template<class T>
Expand Down
9 changes: 7 additions & 2 deletions src/shogun/lib/SGVector.h
Expand Up @@ -22,6 +22,7 @@
#include <shogun/mathematics/linalg/GPUMemoryBase.h>

#include <memory>
#include <atomic>

namespace Eigen
{
Expand Down Expand Up @@ -59,8 +60,7 @@ template<class T> class SGVector : public SGReferencedData
SGVector(T* v, index_t len, bool ref_counting=true);

/** 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) { }
SGVector(T* m, index_t len, index_t offset);

/** Constructor to create new vector in memory */
SGVector(index_t len, bool ref_counting=true);
Expand Down Expand Up @@ -142,6 +142,8 @@ template<class T> class SGVector : public SGReferencedData
return vector;
}

SGVector<T>& operator=(const SGVector<T>&);

/** Cast to pointer */
operator T*() { return vector; }

Expand Down Expand Up @@ -524,6 +526,9 @@ template<class T> class SGVector : public SGReferencedData
virtual void free_data();

private:
/** Atomic variable of vector on_gpu status */
std::atomic<bool> m_on_gpu;

/** Assert whether the data is on GPU
* and raise error if the data is on GPU
*/
Expand Down

0 comments on commit 1d3adbe

Please sign in to comment.