Skip to content

Implement Eigen library as a benchmark comparison #17

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@
[submodule "cnpy"]
path = cnpy
url = [email protected]:rogersce/cnpy.git
[submodule "eigen"]
path = eigen
url = [email protected]:libigl/eigen.git
1 change: 1 addition & 0 deletions eigen
Submodule eigen added at 1f05f5
103 changes: 95 additions & 8 deletions src/solver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
#include "solver.h"
#include "utils.h"

template class Solver<double>;
template class Solver<float>;

/* CPP-based solver */
template <typename T>
NativeSolver<T>::NativeSolver(StateSpaceSystem<T> &system, const int &dataframes) : Solver<T>(system, dataframes)
Expand Down Expand Up @@ -73,6 +76,9 @@ void NativeSolver<T>::process(T *input, T *output)
std::swap(x, x1);
}

template class NativeSolver<double>;
template class NativeSolver<float>;

/* CBLAS_XGEMV-based solver */
template <typename T>
XGEMVSolver<T>::XGEMVSolver(StateSpaceSystem<T> &system, const int &dataframes) : Solver<T>(system, dataframes)
Expand Down Expand Up @@ -114,6 +120,9 @@ void XGEMVSolver<T>::process(T *input, T *output)
std::swap(x, x1);
}

template class XGEMVSolver<double>;
template class XGEMVSolver<float>;

/* CBLAS_XGEMM-based solver */
template <typename T>
XGEMMSolver<T>::XGEMMSolver(StateSpaceSystem<T> &system, const int &dataframes) : Solver<T>(system, dataframes)
Expand Down Expand Up @@ -158,14 +167,92 @@ void XGEMMSolver<T>::process(T *input, T *output)
}
}

template class Solver<double>;
template class Solver<float>;
template class XGEMMSolver<double>;
template class XGEMMSolver<float>;

template class NativeSolver<double>;
template class NativeSolver<float>;
/* Eigen MV-based solver */
template <typename T>
EigenMVSolver<T>::EigenMVSolver(StateSpaceSystem<T> &system, const int &dataframes) : Solver<T>(system, dataframes)
{
int n = this->system_.shape().n;
int m = this->system_.shape().m;
int p = this->system_.shape().p;
this->dataframes_ = dataframes;

template class XGEMVSolver<double>;
template class XGEMVSolver<float>;
x = vector_t::Zero(n);
}

template class XGEMMSolver<double>;
template class XGEMMSolver<float>;
template <typename T>
EigenMVSolver<T>::~EigenMVSolver()
{
}

template <typename T>
void EigenMVSolver<T>::process(T *input, T *output)
{
int n = this->system_.shape().n;
int m = this->system_.shape().m;
int p = this->system_.shape().p;

const_matrix_map A(this->system_.A(), n, n);
const_matrix_map B(this->system_.B(), n, m);
const_matrix_map C(this->system_.C(), p, n);
const_matrix_map D(this->system_.D(), p, m);

const_matrix_map u(input, m, this->dataframes_);
matrix_map y(output, p, this->dataframes_);

for (int i = 0; i < this->dataframes_; i++)
{
y.col(i) = C * x + D * u.col(i);
x = A * x + B * u.col(i);
}
}

template class EigenMVSolver<double>;
template class EigenMVSolver<float>;

/* Eigen MM-based solver */
template <typename T>
EigenMMSolver<T>::EigenMMSolver(StateSpaceSystem<T> &system, const int &dataframes) : Solver<T>(system, dataframes)
{
int n = this->system_.shape().n;
int m = this->system_.shape().m;
int p = this->system_.shape().p;
this->dataframes_ = dataframes;

X = matrix_t::Zero(n, this->dataframes_ + 1);
}

template <typename T>
EigenMMSolver<T>::~EigenMMSolver()
{
}

template <typename T>
void EigenMMSolver<T>::process(T *input, T *output)
{
int n = this->system_.shape().n;
int m = this->system_.shape().m;
int p = this->system_.shape().p;

const_matrix_map A(this->system_.A(), n, n);
const_matrix_map B(this->system_.B(), n, m);
const_matrix_map C(this->system_.C(), p, n);
const_matrix_map D(this->system_.D(), p, m);

const_matrix_map U(input, m, this->dataframes_);
matrix_map Y(output, p, this->dataframes_);

X.rightCols(this->dataframes_) = B * U;
for (int i = 0; i < this->dataframes_; i++)
{
X.col(i + 1) = A * X.col(i) + X.col(i + 1);
}
Y = C * X.leftCols(this->dataframes_) + D * U;

X.col(0) = X.col(this->dataframes_);
}

template class EigenMMSolver<double>;
template class EigenMMSolver<float>;
42 changes: 42 additions & 0 deletions src/solver.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#include "state_space_system.h"
#include "utils.h"
#define EIGEN_USE_BLAS
#include <eigen/Eigen/Core>

/// @brief Base solver.
template <typename T>
Expand Down Expand Up @@ -57,11 +59,51 @@ class XGEMMSolver : public Solver<T>
{
private:
T *X;
typedef Eigen::Matrix<float, 1, Eigen::Dynamic> MatrixType;
typedef Eigen::Map<MatrixType> MapType;

public:
XGEMMSolver(StateSpaceSystem<T> &system, const int &dataframes);
~XGEMMSolver();
void process(T *input, T *output);
};

/// @brief Eigen MV-based solver.
template <typename T>
class EigenMVSolver : public Solver<T>
{
private:
typedef Eigen::Matrix<T, Eigen::Dynamic, 1> vector_t;
typedef Eigen::Map<vector_t> vector_map;
typedef Eigen::Map<const vector_t> const_vector_map;

typedef Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> matrix_t;
typedef Eigen::Map<matrix_t> matrix_map;
typedef Eigen::Map<const matrix_t> const_matrix_map;

vector_t x;

public:
EigenMVSolver(StateSpaceSystem<T> &system, const int &dataframes);
~EigenMVSolver();
void process(T *input, T *output);
};

/// @brief Eigen MM-based solver.
template <typename T>
class EigenMMSolver : public Solver<T>
{
private:
typedef Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> matrix_t;
typedef Eigen::Map<matrix_t> matrix_map;
typedef Eigen::Map<const matrix_t> const_matrix_map;

matrix_t X;

public:
EigenMMSolver(StateSpaceSystem<T> &system, const int &dataframes);
~EigenMMSolver();
void process(T *input, T *output);
};

#endif // SOLVER_H_
4 changes: 4 additions & 0 deletions src/solver_benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,9 @@ BENCHMARK(BM_Solver<XGEMVSolver<float>>)->ArgsProduct({{10, 100, 1000}, {2}, {5}
BENCHMARK(BM_Solver<XGEMVSolver<double>>)->ArgsProduct({{10, 100, 1000}, {2}, {5}, {128}});
BENCHMARK(BM_Solver<XGEMMSolver<float>>)->ArgsProduct({{10, 100, 1000}, {2}, {5}, {128}});
BENCHMARK(BM_Solver<XGEMMSolver<double>>)->ArgsProduct({{10, 100, 1000}, {2}, {5}, {128}});
BENCHMARK(BM_Solver<EigenMVSolver<float>>)->ArgsProduct({{10, 100, 1000}, {2}, {5}, {128}});
BENCHMARK(BM_Solver<EigenMVSolver<double>>)->ArgsProduct({{10, 100, 1000}, {2}, {5}, {128}});
BENCHMARK(BM_Solver<EigenMMSolver<float>>)->ArgsProduct({{10, 100, 1000}, {2}, {5}, {128}});
BENCHMARK(BM_Solver<EigenMMSolver<double>>)->ArgsProduct({{10, 100, 1000}, {2}, {5}, {128}});

BENCHMARK_MAIN();
16 changes: 16 additions & 0 deletions tests/test_solver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ int main(int argc, char const *argv[])
NativeSolver<double> dnat_solver(system, dataframes);
XGEMVSolver<double> dgemv_solver(system, dataframes);
XGEMMSolver<double> dgemm_solver(system, dataframes);
EigenMVSolver<double> eigmv_solver(system, dataframes);
EigenMMSolver<double> eigmm_solver(system, dataframes);

dnat_solver.process(input.data<double>(), output);
std::cout << "True out:" << std::endl;
Expand Down Expand Up @@ -60,5 +62,19 @@ int main(int argc, char const *argv[])
print_data(output, p, dataframes);
std::cout << "DGEMV Solver - " << l2err(output, dtout) << std::endl; */

eigmv_solver.process(input.data<double>(), output);
std::cout << "True out:" << std::endl;
print_data(true_output);
std::cout << "Calculated out:" << std::endl;
print_data(output, p, dataframes);
std::cout << "EigMV Solver - " << l2err(output, dtout) << std::endl;

eigmm_solver.process(input.data<double>(), output);
std::cout << "True out:" << std::endl;
print_data(true_output);
std::cout << "Calculated out:" << std::endl;
print_data(output, p, dataframes);
std::cout << "EigMM Solver - " << l2err(output, dtout) << std::endl;

free(output);
}