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 Refactor - CPU dot #3230

Merged
merged 10 commits into from Jun 9, 2016
Merged
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
15 changes: 15 additions & 0 deletions src/shogun/base/init.cpp
Expand Up @@ -14,6 +14,7 @@

#include <shogun/mathematics/Math.h>
#include <shogun/mathematics/Random.h>
#include <shogun/mathematics/linalg/SGLinalg.h>
#include <shogun/io/SGIO.h>
#include <shogun/base/Parallel.h>
#include <shogun/base/Version.h>
Expand All @@ -37,6 +38,9 @@ namespace shogun
Version* sg_version=NULL;
CMath* sg_math=NULL;
CRandom* sg_rand=NULL;
#ifdef HAVE_CXX11
std::unique_ptr<SGLinalg> sg_linalg(nullptr);
Copy link
Member

Choose a reason for hiding this comment

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

can you please try using shogun's Unique here? if that works then we won't need c++11 guard around all these

#endif

/// function called to print normal messages
void (*sg_print_message)(FILE* target, const char* str) = NULL;
Expand Down Expand Up @@ -66,6 +70,10 @@ 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());
Copy link
Member

Choose a reason for hiding this comment

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

probably then you won't need to create an instance here (if you use Unique)

#endif
#ifdef TRACE_MEMORY_ALLOCS
if (!sg_mallocs)
sg_mallocs = new shogun::CMap<void*, MemoryBlock>(631, 1024, false);
Expand Down Expand Up @@ -186,6 +194,13 @@ namespace shogun
return sg_rand;
}

#ifdef HAVE_CXX11
SGLinalg* get_global_linalg()
{
return sg_linalg.get();
}
#endif

void init_from_env()
{
char* env_log_val = NULL;
Expand Down
13 changes: 13 additions & 0 deletions src/shogun/base/init.h
Expand Up @@ -22,6 +22,7 @@ namespace shogun
class Version;
class Parallel;
class CRandom;
class SGLinalg;

/** This function must be called before libshogun is used. Usually shogun does
* not provide any output messages (neither debugging nor error; apart from
Expand Down Expand Up @@ -108,6 +109,18 @@ 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);
Copy link
Member

Choose a reason for hiding this comment

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

Is it defined somewhere? I see definition of get_global_linalg() in the cpp but not this one.

Also, maybe this is not needed. Just remove this for now :)


/** get the global linalg library object
*
* @return linalg object
*/
SGLinalg* get_global_linlg();

/** Checks environment variables and modifies global objects
*/
void init_from_env();
Expand Down
63 changes: 63 additions & 0 deletions src/shogun/mathematics/linalg/BaseVector.h
@@ -0,0 +1,63 @@
/*
* 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/lib/SGVector.h>

#ifndef BASEVECTOR_H__
#define BASEVECTOR_H__

#ifdef HAVE_CXX11

namespace shogun
{

/** Vector structure
* Base vector for both CPU and GPU vector
*/
template <class T>
struct BaseVector
{
/** Default Constructor */
BaseVector()
{
}

/** Data Storage */
virtual bool onGPU() = 0 ;
Copy link
Member

Choose a reason for hiding this comment

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

make it const :)

Copy link
Member

Choose a reason for hiding this comment

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

oh for these methods that return something, you should use @return and then just write the description :)

};
}

#endif // HAVE_CXX11

#endif
63 changes: 63 additions & 0 deletions src/shogun/mathematics/linalg/CPUBackend.cpp
@@ -0,0 +1,63 @@
/*
* 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/CPUBackend.h>

#ifdef HAVE_CXX11

namespace shogun
{

CPUBackend::CPUBackend()
{
}

CPUBackend::CPUBackend(const CPUBackend& cpubackend)
{
}

template <typename T>
T CPUBackend::dot(const CPUVector<T> &a, const CPUVector<T> &b) const
{
typedef Eigen::Matrix<T, Eigen::Dynamic, 1> VectorXt;
Eigen::Map<VectorXt> vec_a(a.CPUptr, a.vlen);
Eigen::Map<VectorXt> vec_b(b.CPUptr, b.vlen);
return vec_a.dot(vec_b);
}

template int32_t CPUBackend::dot<int32_t>(const CPUVector<int32_t> &a, const CPUVector<int32_t> &b) const;
template float32_t CPUBackend::dot<float32_t>(const CPUVector<float32_t> &a, const CPUVector<float32_t> &b) const;

}

#endif //HAVE_CXX11
74 changes: 74 additions & 0 deletions src/shogun/mathematics/linalg/CPUBackend.h
@@ -0,0 +1,74 @@
/*
* 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/mathematics/eigen3.h>
#include <shogun/mathematics/linalg/CPUVector.h>

#ifndef CPUBACKEND_H__
#define CPUBACKEND_H__

#ifdef HAVE_CXX11

namespace shogun
{

/** CPU Backend Class */
class CPUBackend
{
public:

/** Default Constructor */
CPUBackend();

/** Copy constructor */
CPUBackend(const CPUBackend& cpubackend);

/**
* Implementation of Eigen3 vector dot-product that works
* with CPUVectors (SGVectors)
*
* @param a first vector
* @param b second vector
* @return the dot product of \f$\mathbf{a}\f$ and \f$\mathbf{b}\f$, represented
* as \f$\sum_i a_i b_i\f$
*/
template <typename T>
T dot(const CPUVector<T> &a, const CPUVector<T> &b) const;
};

}

#endif //HAVE_CXX11

#endif
63 changes: 63 additions & 0 deletions src/shogun/mathematics/linalg/CPUVector.cpp
@@ -0,0 +1,63 @@
/*
* 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/CPUVector.h>

#ifdef HAVE_CXX11

namespace shogun

{
template <class T>
CPUVector<T>::CPUVector():CPUptr(nullptr), vlen(0)
{
}

template <class T>
CPUVector<T>::CPUVector(const SGVector<T> &vector)
: CPUptr(vector.vector), vlen(vector.vlen)
{
}

template <class T>
CPUVector<T>::CPUVector(const CPUVector<T> &vector)
: CPUptr(vector.CPUptr), vlen(vector.vlen)
{
}

template struct CPUVector<int32_t>;
template struct CPUVector<float32_t>;

}

#endif //HAVE_CXX11