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
Changes from all commits
0e5d0d8
aa577dd
f6c018b
18540fb
177f5ea
b73728f
12318a1
d4b5d8b
347e7bd
be52bba
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 |
---|---|---|
|
@@ -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> | ||
|
@@ -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); | ||
#endif | ||
|
||
/// function called to print normal messages | ||
void (*sg_print_message)(FILE* target, const char* str) = NULL; | ||
|
@@ -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()); | ||
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. probably then you won't need to create an instance here (if you use |
||
#endif | ||
#ifdef TRACE_MEMORY_ALLOCS | ||
if (!sg_mallocs) | ||
sg_mallocs = new shogun::CMap<void*, MemoryBlock>(631, 1024, false); | ||
|
@@ -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; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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); | ||
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. 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(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 ; | ||
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. make it const :) 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. oh for these methods that return something, you should use |
||
}; | ||
} | ||
|
||
#endif // HAVE_CXX11 | ||
|
||
#endif |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
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.
can you please try using shogun's Unique here? if that works then we won't need c++11 guard around all these