Skip to content

Commit

Permalink
benchmarking code changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Aditya Dendukuri committed Jun 28, 2024
1 parent 830184d commit 5e63955
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 53 deletions.
14 changes: 4 additions & 10 deletions benchmark/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
add_library(tuvx_benchmark_utils OBJECT)

target_compile_features(tuvx_benchmark_utils PUBLIC cxx_std_11)

# link benchmark library to sources in this file
target_sources(tuvx_benchmark_utils PUBLIC benchmark_tridiagonal_solver.cpp)
target_link_libraries(tuvx_benchmark_utils PUBLIC musica::tuvx LAPACK::LAPACK
benchmark::benchmark)
target_include_directories(tuvx_benchmark_utils PUBLIC ${LAPACK_INCLUDE_DIRS})

add_executable(benchmark_tridiagonal_solver benchmark_tridiagonal_solver.cpp)
target_include_directories(
benchmark_tridiagonal_solver PUBLIC ${OpenBLAS_INCLUDE_DIRS}
${LAPACK_INCLUDE_DIRS})

target_link_libraries(benchmark_tridiagonal_solver
PUBLIC LAPACK::LAPACK benchmark::benchmark musica::tuvx)
57 changes: 32 additions & 25 deletions benchmark/benchmark_tridiagonal_solver.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#include "mkl_lapacke.h"

#include <tuvx/linear_algebra/linear_algebra.hpp>

#include <benchmark/benchmark.h>

#include <mkl_lapack.h>
#ifdef TUVX_COMPILE_WITH_INTEL
#include <mkl_lapacke.h>
#elif TUVX_COMPILE_WITH_GCC
#include <lapacke.h>
#endif

using namespace tuvx;
typedef TridiagonalMatrix<double> trid_matd;
Expand All @@ -13,21 +16,24 @@ typedef std::vector<double> vecd;
typedef TridiagonalMatrix<float> trid_matf;
typedef std::vector<float> vecf;

const std::size_t system_size = 1000;
const std::size_t system_size = 1e6;

const bool diagonally_dominant = false;

static void BM_LAPACKE_SINGLE_PRECISISON(benchmark::State& state)
{
vecf x(system_size);
vecf b(system_size);
trid_matf A(system_size);

FillRandom<float>(A);
FillRandom<float>(x);
b = Dot<float>(A, x);

// Perform setup here
for (auto _ : state)
{
state.PauseTiming();
FillRandom<float>(A, diagonally_dominant);
FillRandom<float>(x);
b = Dot<float>(A, x);
state.ResumeTiming();

LAPACKE_sgtsv(
LAPACK_ROW_MAJOR,
system_size,
Expand All @@ -46,13 +52,13 @@ static void BM_LAPACKE_DOUBLE_PRECISISON(benchmark::State& state)
vecd b(system_size);
trid_matd A(system_size);

FillRandom<double>(A);
FillRandom<double>(x);
b = Dot<double>(A, x);

// Perform setup here
for (auto _ : state)
{
state.PauseTiming();
FillRandom<double>(A, diagonally_dominant);
FillRandom<double>(x);
b = Dot<double>(A, x);
state.ResumeTiming();
LAPACKE_dgtsv(
LAPACK_ROW_MAJOR,
system_size,
Expand All @@ -70,15 +76,14 @@ static void BM_TUVX_DOUBLE_PRECISISON(benchmark::State& state)
vecd x(system_size);
vecd b(system_size);
trid_matd A(system_size);
vecd x_approx(system_size);
FillRandom<double>(A);
FillRandom<double>(x);
b = Dot<double>(A, x);

// Perform setup here
for (auto _ : state)
{
x_approx = Solve<double>(A, b);
state.PauseTiming();
FillRandom<double>(A, diagonally_dominant);
FillRandom<double>(x);
state.ResumeTiming();
Solve<double>(A, b);
}
}

Expand All @@ -87,18 +92,20 @@ static void BM_TUVX_SINGLE_PRECISISON(benchmark::State& state)
vecf x(system_size);
vecf b(system_size);
trid_matf A(system_size);
vecf x_approx(system_size);
FillRandom<float>(A);
FillRandom<float>(x);
b = Dot<float>(A, x);

// Perform setup here
for (auto _ : state)
{
x_approx = Solve<float>(A, b);
state.PauseTiming();
FillRandom<float>(A, diagonally_dominant);
FillRandom<float>(x);
b = Dot<float>(A, x);
state.ResumeTiming();
Solve<float>(A, b);
}
}
// Register the function as a benchmark

// Registering benchmarks
BENCHMARK(BM_LAPACKE_DOUBLE_PRECISISON);
BENCHMARK(BM_LAPACKE_SINGLE_PRECISISON);
BENCHMARK(BM_TUVX_DOUBLE_PRECISISON);
Expand Down
8 changes: 4 additions & 4 deletions include/tuvx/linear_algebra/linear_algebra.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ namespace tuvx
/// @brief Fills a matrix with uniformly distributed random values.
/// @param A tridiagonal matrix to allocate and fill
template<typename T>
void FillRandom(TridiagonalMatrix<T> &A);
void FillRandom(TridiagonalMatrix<T> &A, bool diagonally_dominant = false);

/// @fn print vector function
/// @brief displays the data stored inside a std::vector
Expand All @@ -53,12 +53,12 @@ namespace tuvx
void Print(const TridiagonalMatrix<T> &A);

/// @fn Tridiagonal Linear System Solver
/// @brief Thomas' algorithm for solving tridiagonal linear system (A x = b)
/// @brief Thomas' algorithm for solving tridiagonal linear system (A x = b).
/// store solution in b.
/// @param A tridiagonal coeffcient matrix
/// @param b right hand side vector of the tridiagonal system.
/// @returns x solution that solves A x = b.
template<typename T>
std::vector<T> Solve(TridiagonalMatrix<T> A, std::vector<T> b);
void Solve(TridiagonalMatrix<T> &A, std::vector<T> &b);

/// @fn Tridiagonal Matrix - vector dot product
/// @brief Specialized dot product function for tridiagonal matrices.
Expand Down
21 changes: 12 additions & 9 deletions include/tuvx/linear_algebra/linear_algebra.inl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace tuvx
}

template<typename T>
inline std::vector<T> Solve(TridiagonalMatrix<T> A, std::vector<T> b)
inline void Solve(TridiagonalMatrix<T> &A, std::vector<T> &b)
{
T temp;
std::size_t N = b.size();
Expand All @@ -49,7 +49,6 @@ namespace tuvx
break;
}
}
return b;
}

template<typename T>
Expand All @@ -65,19 +64,23 @@ namespace tuvx
}

template<typename T>
inline void FillRandom(TridiagonalMatrix<T> &A)
inline void FillRandom(TridiagonalMatrix<T> &A, bool diagonally_dominant)
{
FillRandom<T>(A.main_diagonal_);
FillRandom<T>(A.lower_diagonal_);
FillRandom<T>(A.upper_diagonal_);
// make diagonally dominant (diagonal value greater than sum of its row)
std::size_t i = 0;
A.main_diagonal_[i] += A.upper_diagonal_[i] + (T)500;
for (i = 1; i < A.size_ - 1; i++)

if (diagonally_dominant)
{
A.main_diagonal_[i] += A.lower_diagonal_[i - 1] + A.upper_diagonal_[i] + (T)500;
// make diagonally dominant (diagonal value greater than sum of its row)
std::size_t i = 0;
A.main_diagonal_[i] += A.upper_diagonal_[i];
for (i = 1; i < A.size_ - 1; i++)
{
A.main_diagonal_[i] += A.lower_diagonal_[i - 1] + A.upper_diagonal_[i];
}
A.main_diagonal_[i] += A.lower_diagonal_[i - 1];
}
A.main_diagonal_[i] += A.lower_diagonal_[i - 1] + (T)500;
}

template<typename T>
Expand Down
10 changes: 5 additions & 5 deletions test/unit/linear_algebra/test_tridiagonal_solver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ typedef std::vector<float> vecf;
const double tol_dp = std::numeric_limits<double>::epsilon();
const float tol_sp = std::numeric_limits<float>::epsilon();

const std::size_t number_of_runs = 100;
const std::size_t number_of_runs = 20;

std::size_t sizes[5] = { 1000, 10000, 100000, 1000000, 10000000 };

Expand All @@ -44,9 +44,9 @@ TEST(TridiagSolveTest, SinglePrecision)
FillRandom<float>(x);
b = Dot<float>(A, x);

vecf x_approx = Solve<float>(A, b);
Solve<float>(A, b);

errors[i] += ComputeError<float>(x, x_approx);
errors[i] += ComputeError<float>(x, b);
}

errors[i] /= number_of_runs;
Expand Down Expand Up @@ -75,9 +75,9 @@ TEST(TridiagSolveTest, DoublePrecision)
FillRandom<double>(x);
b = Dot<double>(A, x);

vecd x_approx = Solve<double>(A, b);
Solve<double>(A, b);

errors[i] += ComputeError<double>(x, x_approx);
errors[i] += ComputeError<double>(x, b);
}
errors[i] /= number_of_runs;
EXPECT_LE(errors[i], tol_dp);
Expand Down

0 comments on commit 5e63955

Please sign in to comment.