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 benchmark #3247

Merged
Merged
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
149 changes: 149 additions & 0 deletions benchmarks/linalg_refactor_benchmark.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
#include <shogun/base/init.h>
#include <shogun/lib/SGVector.h>
#include <shogun/mathematics/linalgrefactor/linalgRefactor.h>

#include <shogun/mathematics/eigen3.h>
#include <viennacl/linalg/inner_prod.hpp>
#include <viennacl/vector.hpp>

#include <algorithm>
#include <memory>
#include <hayai/hayai.hpp>

#include <iostream>

using namespace shogun;

/**
* Instructions :
* 1. Install benchmarking toolkit "hayai" (https://github.com/nickbruun/hayai)
* 2. Compile against libhayai_main, e.g.
* g++ -O3 -std=c++11 linglg_refactor_benchmark.cpp -I/usr/include/eigen3 \
* -lshogun -lhayai_main -lOpenCL -o benchmark
* 3. ./benchmark
*/

/** Generate data only once */
typedef float32_t T;
typedef viennacl::vector_base<T, std::size_t, std::ptrdiff_t> VCLVectorBase;

template<typename value_type>
struct Data
Copy link
Member

Choose a reason for hiding this comment

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

If you create this as a template, then we can easily check with the tests with different data-types. For example, the real use cases would mostly be float64_t and float32_t. can you please do that?

{
typedef viennacl::backend::mem_handle VCLMemoryArray;

Data()
{
num_rows = 100;
init();
}

Data(index_t num_rows)
{
this->num_rows = num_rows;
init();
}
Copy link
Member

Choose a reason for hiding this comment

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

You could have just created a init() method that does all those stuff, and then could have called that method from both the constructor. Anyway, not a big deal


~Data()
{
exit_shogun();
}

void init()
{
A = init_sg(1);
B = init_sg(2);
Av = init_v(A);
Bv = init_v(B);
Ac = std::unique_ptr<BaseVector<value_type>> (init_c(A));
Bc = std::unique_ptr<BaseVector<value_type>> (init_c(B));
Ag = std::unique_ptr<BaseVector<value_type>> (init_g(A));
Bg = std::unique_ptr<BaseVector<value_type>> (init_g(B));

init_shogun_with_defaults();
GPUBackend viennaclBackend;
sg_linalg->set_gpu_backend(&viennaclBackend);
}

/** SGVector **/
SGVector<value_type> init_sg(value_type begin)
{
SGVector<value_type> m(num_rows);
m.range_fill(begin);

return m;
}

/** ViennaCL Vector for test **/
VCLVectorBase init_v(SGVector<value_type> m)
{
VCLVectorBase mv;
std::shared_ptr<VCLMemoryArray> vector(new VCLMemoryArray());
viennacl::backend::memory_create(*vector, sizeof(value_type)*num_rows,
viennacl::context());
viennacl::backend::memory_write(*vector, 0, num_rows*sizeof(value_type),
m.vector);
mv = VCLVectorBase(*vector, num_rows, 0, 1);

return mv;
}

/** CPUVector derived from BaseVector **/
std::unique_ptr<BaseVector<value_type>> init_c(SGVector<value_type> m)
{
std::unique_ptr<CPUVector<value_type>> mc(new CPUVector<value_type>(m));
return std::move(mc);
}

/** GPUVector derived from BaseVector **/
std::unique_ptr<BaseVector<value_type>> init_g(SGVector<value_type> m)
{
std::unique_ptr<GPU_Vector<value_type>> mg(new GPU_Vector<value_type>(m));
return std::move(mg);
}

SGVector<value_type> A;
SGVector<value_type> B;
VCLVectorBase Av;
Copy link
Member

Choose a reason for hiding this comment

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

Indentation

VCLVectorBase Bv;
std::unique_ptr<BaseVector<value_type>> Ac;
std::unique_ptr<BaseVector<value_type>> Bc;
std::unique_ptr<BaseVector<value_type>> Ag;
std::unique_ptr<BaseVector<value_type>> Bg;

index_t num_rows;
};

BENCHMARK_P(CPUVector, dot_explict_eigen3, 10, 1000,
(const SGVector<T> &A, const SGVector<T> &B))
{
typedef Eigen::Matrix<T, Eigen::Dynamic, 1> VectorXt;
Eigen::Map<VectorXt> vec_A(A.vector, A.vlen);
Eigen::Map<VectorXt> vec_B(B.vector, B.vlen);

auto C = vec_A.dot(vec_B);
}

BENCHMARK_P(CPUVector, dot_eigen3, 10, 1000,
(BaseVector<T> *A, BaseVector<T> *B))
{
auto C = sg_linalg->dot(A, B);
}

BENCHMARK_P(GPU_Vector, dot_explict_viennacl, 10, 1000,
(const VCLVectorBase &A, const VCLVectorBase &B))
{
auto C = viennacl::linalg::inner_prod(A, B);
}

BENCHMARK_P(GPU_Vector, dot_viennacl, 10, 1000,
(BaseVector<T> *A, BaseVector<T> *B))
{
auto C = sg_linalg->dot(A, B);
}

Data<T> data(1000000);
BENCHMARK_P_INSTANCE(CPUVector, dot_explict_eigen3, (data.A, data.B));
BENCHMARK_P_INSTANCE(CPUVector, dot_eigen3, (data.Ac.get(), data.Bc.get()));
BENCHMARK_P_INSTANCE(GPU_Vector, dot_explict_viennacl, (data.Av, data.Bv));
BENCHMARK_P_INSTANCE(GPU_Vector, dot_viennacl, (data.Ag.get(), data.Bg.get()));