Skip to content

Commit

Permalink
LinalgRefactor
Browse files Browse the repository at this point in the history
- SGVector and GPUVector
- Eigen3 and ViennaCL transfer
- dot operation
- unit-tests
  • Loading branch information
OXPHOS committed Jul 4, 2016
1 parent 254a83f commit 3fe967a
Show file tree
Hide file tree
Showing 26 changed files with 852 additions and 1,004 deletions.
10 changes: 2 additions & 8 deletions src/shogun/base/init.cpp
Expand Up @@ -21,7 +21,6 @@
#include <shogun/base/SGObject.h>
#include <stdlib.h>
#include <string.h>

#ifdef TRACE_MEMORY_ALLOCS
#include <shogun/lib/Map.h>
shogun::CMap<void*, shogun::MemoryBlock>* sg_mallocs=NULL;
Expand All @@ -38,9 +37,7 @@ namespace shogun
Version* sg_version=NULL;
CMath* sg_math=NULL;
CRandom* sg_rand=NULL;
#ifdef HAVE_CXX11
std::unique_ptr<SGLinalg> sg_linalg(nullptr);
#endif

/// function called to print normal messages
void (*sg_print_message)(FILE* target, const char* str) = NULL;
Expand Down Expand Up @@ -70,10 +67,9 @@ namespace shogun
sg_math = new shogun::CMath();
if (!sg_rand)
sg_rand = new shogun::CRandom();
#ifdef HAVE_CXX11
if (!sg_linalg)
sg_linalg = std::unique_ptr<SGLinalg>(new shogun::SGLinalg());
#endif

#ifdef TRACE_MEMORY_ALLOCS
if (!sg_mallocs)
sg_mallocs = new shogun::CMap<void*, MemoryBlock>(631, 1024, false);
Expand Down Expand Up @@ -193,14 +189,12 @@ namespace shogun
SG_REF(sg_rand);
return sg_rand;
}

#ifdef HAVE_CXX11
#ifndef SWIG // SWIG should skip this part
SGLinalg* get_global_linalg()
{
return sg_linalg.get();
}
#endif

void init_from_env()
{
char* env_log_val = NULL;
Expand Down
10 changes: 3 additions & 7 deletions src/shogun/base/init.h
Expand Up @@ -109,17 +109,13 @@ void set_global_rand(CRandom* rand);
*/
CRandom* get_global_rand();

/** set the global linalg library object
*
* @param linalg linalg object to use
*/
void set_global_linlg(SGLinalg* linalg);

#ifndef SWIG // SWIG should skip this part
/** get the global linalg library object
*
* @return linalg object
*/
SGLinalg* get_global_linlg();
SGLinalg* get_global_linalg();
#endif

/** Checks environment variables and modifies global objects
*/
Expand Down
45 changes: 40 additions & 5 deletions src/shogun/lib/SGVector.cpp
Expand Up @@ -77,17 +77,24 @@ SGVector<T>::SGVector() : SGReferencedData()

template<class T>
SGVector<T>::SGVector(T* v, index_t len, bool ref_counting)
: SGReferencedData(ref_counting), vector(v), vlen(len)
: SGReferencedData(ref_counting), vector(v), vlen(len), gpu_vector(NULL)
{
}

template<class T>
SGVector<T>::SGVector(index_t len, bool ref_counting)
: SGReferencedData(ref_counting), vlen(len)
: SGReferencedData(ref_counting), vlen(len), gpu_vector(NULL)
{
vector=SG_MALLOC(T, len);
}

template<class T>
SGVector<T>::SGVector(GPUMemoryBase<T>* vector, index_t len)
: SGReferencedData(true), vector(NULL), vlen(len),
gpu_vector(std::shared_ptr<GPUMemoryBase<T>>(vector))
{
}

template<class T>
SGVector<T>::SGVector(const SGVector &orig) : SGReferencedData(orig)
{
Expand All @@ -108,47 +115,52 @@ SGVector<T>::~SGVector()

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

}

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

}

template <class T>
SGVector<T>::operator EigenVectorXtMap() const
{
assert_on_gpu();
return EigenVectorXtMap(vector, vlen);
}

template <class T>
SGVector<T>::operator EigenRowVectorXtMap() const
{
assert_on_gpu();
return EigenRowVectorXtMap(vector, vlen);
}

template<class T>
void SGVector<T>::zero()
{
assert_on_gpu();
if (vector && vlen)
set_const(0);
}

template <>
void SGVector<complex128_t>::zero()
{
assert_on_gpu();
if (vector && vlen)
set_const(complex128_t(0.0));
}

template<class T>
void SGVector<T>::set_const(T const_elem)
{
assert_on_gpu();
for (index_t i=0; i<vlen; i++)
vector[i]=const_elem ;
}
Expand All @@ -157,19 +169,22 @@ void SGVector<T>::set_const(T const_elem)
template<>
void SGVector<float64_t>::set_const(float64_t const_elem)
{
assert_on_gpu();
catlas_dset(vlen, const_elem, vector, 1);
}

template<>
void SGVector<float32_t>::set_const(float32_t const_elem)
{
assert_on_gpu();
catlas_sset(vlen, const_elem, vector, 1);
}
#endif // HAVE_CATLAS

template<class T>
void SGVector<T>::range_fill(T start)
{
assert_on_gpu();
range_fill_vector(vector, vlen, start);
}

Expand All @@ -178,6 +193,7 @@ COMPLEX128_ERROR_ONEARG(range_fill)
template<class T>
void SGVector<T>::random(T min_value, T max_value)
{
assert_on_gpu();
random_vector(vector, vlen, min_value, max_value);
}

Expand All @@ -186,6 +202,7 @@ COMPLEX128_ERROR_TWOARGS(random)
template <class T>
index_t SGVector<T>::find_position_to_insert(T element)
{
assert_on_gpu();
index_t i;
for (i=0; i<vlen; ++i)
{
Expand All @@ -206,7 +223,10 @@ index_t SGVector<complex128_t>::find_position_to_insert(complex128_t element)
template<class T>
SGVector<T> SGVector<T>::clone() const
{
return SGVector<T>(clone_vector(vector, vlen), vlen);
if (on_gpu())
return SGVector<T>(gpu_vector->clone_vector(gpu_vector.get()), vlen);
else
return SGVector<T>(clone_vector(vector, vlen), vlen);
}

template<class T>
Expand Down Expand Up @@ -242,20 +262,23 @@ void SGVector<complex128_t>::range_fill_vector(complex128_t* vec,
template<class T>
const T& SGVector<T>::get_element(index_t index)
{
assert_on_gpu();
REQUIRE(vector && (index>=0) && (index<vlen), "Provided index (%d) must be between 0 and %d.\n", index, vlen);
return vector[index];
}

template<class T>
void SGVector<T>::set_element(const T& p_element, index_t index)
{
assert_on_gpu();
REQUIRE(vector && (index>=0) && (index<vlen), "Provided index (%d) must be between 0 and %d.\n", index, vlen);
vector[index]=p_element;
}

template<class T>
void SGVector<T>::resize_vector(int32_t n)
{
assert_on_gpu();
vector=SG_REALLOC(T, vector, vlen, n);

if (n > vlen)
Expand All @@ -267,6 +290,7 @@ void SGVector<T>::resize_vector(int32_t n)
template<class T>
SGVector<T> SGVector<T>::operator+ (SGVector<T> x)
{
assert_on_gpu();
REQUIRE(x.vector && vector, "Addition possible for only non-null vectors.\n");
REQUIRE(x.vlen == vlen, "Length of the two vectors to be added should be same. [V(%d) + V(%d)]\n", vlen, x.vlen);

Expand All @@ -278,6 +302,7 @@ SGVector<T> SGVector<T>::operator+ (SGVector<T> x)
template<class T>
void SGVector<T>::add(const SGVector<T> x)
{
assert_on_gpu();
REQUIRE(x.vector && vector, "Addition possible for only non-null vectors.\n");
REQUIRE(x.vlen == vlen, "Length of the two vectors to be added should be same. [V(%d) + V(%d)]\n", vlen, x.vlen);

Expand All @@ -288,6 +313,7 @@ void SGVector<T>::add(const SGVector<T> x)
template<class T>
void SGVector<T>::add(const T x)
{
assert_on_gpu();
REQUIRE(vector, "Addition possible for only non-null vectors.\n");
for (int32_t i=0; i<vlen; i++)
vector[i]+=x;
Expand All @@ -296,6 +322,7 @@ void SGVector<T>::add(const T x)
template<class T>
void SGVector<T>::add(const SGSparseVector<T>& x)
{
assert_on_gpu();
if (x.features)
{
for (int32_t i=0; i < x.num_feat_entries; i++)
Expand All @@ -310,12 +337,14 @@ void SGVector<T>::add(const SGSparseVector<T>& x)
template<class T>
void SGVector<T>::display_size() const
{
assert_on_gpu();
SG_SPRINT("SGVector '%p' of size: %d\n", vector, vlen)
}

template<class T>
void SGVector<T>::copy_data(const SGReferencedData &orig)
{
gpu_vector=std::shared_ptr<GPUMemoryBase<T>>(((SGVector*)(&orig))->gpu_vector);
vector=((SGVector*)(&orig))->vector;
vlen=((SGVector*)(&orig))->vlen;
}
Expand All @@ -325,6 +354,7 @@ void SGVector<T>::init_data()
{
vector=NULL;
vlen=0;
gpu_vector=NULL;
}

template<class T>
Expand All @@ -333,11 +363,13 @@ void SGVector<T>::free_data()
SG_FREE(vector);
vector=NULL;
vlen=0;
gpu_vector=NULL;
}

template<class T>
bool SGVector<T>::equals(SGVector<T>& other)
{
assert_on_gpu();
if (other.vlen!=vlen)
return false;

Expand Down Expand Up @@ -806,6 +838,7 @@ int32_t SGVector<complex128_t>::unique(complex128_t* output, int32_t size)
template <class T>
SGVector<index_t> SGVector<T>::find(T elem)
{
assert_on_gpu();
SGVector<index_t> idx(vlen);
index_t k=0;

Expand Down Expand Up @@ -880,6 +913,7 @@ void SGVector<complex128_t>::save(CFile* saver)

template <class T> SGVector<float64_t> SGVector<T>::get_real()
{
assert_on_gpu();
SGVector<float64_t> real(vlen);
for (int32_t i=0; i<vlen; i++)
real[i]=CMath::real(vector[i]);
Expand All @@ -888,6 +922,7 @@ template <class T> SGVector<float64_t> SGVector<T>::get_real()

template <class T> SGVector<float64_t> SGVector<T>::get_imag()
{
assert_on_gpu();
SGVector<float64_t> imag(vlen);
for (int32_t i=0; i<vlen; i++)
imag[i]=CMath::imag(vector[i]);
Expand Down

0 comments on commit 3fe967a

Please sign in to comment.