Skip to content

Commit

Permalink
unit tests operate on one size (1000); formatting changes; use same r…
Browse files Browse the repository at this point in the history
…andom values to compare lapacke and tuvx
  • Loading branch information
Aditya Dendukuri committed Jul 2, 2024
1 parent 0e909a0 commit 9239c15
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 192 deletions.
38 changes: 13 additions & 25 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
################################################################################
# ##############################################################################
# Preamble

cmake_minimum_required(VERSION 3.17)

project(
tuv-x
VERSION 0.9.0
LANGUAGES Fortran CXX C
)
VERSION 0.8.0
LANGUAGES Fortran CXX C)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
set(CMAKE_MODULE_PATH "${CMAKE_MODULE_PATH};${PROJECT_SOURCE_DIR}/cmake")
Expand Down Expand Up @@ -37,15 +36,14 @@ if(${CMAKE_VERSION} VERSION_LESS "3.21")
endif()
endif()

################################################################################
# ##############################################################################
# Projet wide setup options

include(CMakeDependentOption)
option(TUVX_ENABLE_MPI "Enable MPI parallel support" OFF)
cmake_dependent_option(TUVX_ENABLE_OPENMP "Enable OpenMP support" OFF
"TUVX_ENABLE_MPI" OFF)
option(TUVX_ENABLE_TESTS "Build tests" ON)
option(TUVX_ENABLE_BENCHMARK "Build benchmark examples" ON)
option(TUVX_ENABLE_COVERAGE "Enable code coverage output" OFF)
option(TUVX_ENABLE_MEMCHECK "Enable memory checking in tests" OFF)
option(TUVX_ENABLE_NC_CONFIG "Use nc-config to determine NetCDF libraries" OFF)
Expand Down Expand Up @@ -96,12 +94,12 @@ if(TUVX_ENABLE_TESTS)
${CMAKE_CURRENT_SOURCE_DIR}/examples ${CMAKE_BINARY_DIR}/examples)
endif()

################################################################################
# ##############################################################################
# Dependencies

include(cmake/dependencies.cmake)

################################################################################
# ##############################################################################
# TUV-x targets

add_subdirectory(src)
Expand All @@ -117,11 +115,8 @@ if(NOT BUILD_SHARED_LIBS)
set_target_properties(tuv-x PROPERTIES POSITION_INDEPENDENT_CODE ON)
endif()

target_link_libraries(tuv-x
PUBLIC
musica::tuvx
yaml-cpp::yaml-cpp
)
target_link_libraries(tuv-x PUBLIC musica::tuvx yaml-cpp::yaml-cpp
${BLAS_LIBRARIES} ${LAPACK_LIBRARIES})

target_include_directories(
tuv-x PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>
Expand All @@ -131,14 +126,14 @@ if(TUVX_ENABLE_OPENMP)
target_link_libraries(tuv-x PUBLIC OpenMP::OpenMP_Fortran)
endif()

################################################################################
# ##############################################################################
# TUV-x docs

if(TUVX_BUILD_DOCS)
add_subdirectory(docs)
endif()

################################################################################
# ##############################################################################
# TUV-x tests

if(PROJECT_IS_TOP_LEVEL AND TUVX_ENABLE_TESTS)
Expand All @@ -160,22 +155,15 @@ if(PROJECT_IS_TOP_LEVEL AND TUVX_ENABLE_TESTS)

enable_testing()
add_subdirectory(test)
add_subdirectory(benchmark)
endif()

################################################################################
# ##############################################################################
# Packaging

# only include packaging if we are the top level project being built
if(PROJECT_IS_TOP_LEVEL)
add_subdirectory(packaging)
endif()

################################################################################
# benchmarking

if(TUVX_ENABLE_BENCHMARK)
add_subdirectory(benchmark)
endif()


################################################################################
# ##############################################################################
72 changes: 36 additions & 36 deletions benchmark/benchmark_tridiagonal_solver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,33 @@

#include <benchmark/benchmark.h>

#include <random>
#include <vector>

#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;
typedef std::vector<double> vecd;

typedef TridiagonalMatrix<float> trid_matf;
typedef std::vector<float> vecf;

const std::size_t system_size = 1e6;

const bool diagonally_dominant = false;
const bool diagonally_dominant = true;

const unsigned random_number_seed = 1;

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

std::vector<float> x(system_size);
std::vector<float> b(system_size);
tuvx::TridiagonalMatrix<float> A(system_size);
std::mt19937 random_device(random_number_seed);
for (auto _ : state)
{
state.PauseTiming();
FillRandom<float>(A, diagonally_dominant);
FillRandom<float>(x);
b = Dot<float>(A, x);
tuvx::FillRandom<float>(A, random_number_seed, diagonally_dominant);
tuvx::FillRandom<float>(x, random_number_seed);
b = tuvx::Dot<float>(A, x);
state.ResumeTiming();

LAPACKE_sgtsv(
Expand All @@ -48,18 +46,20 @@ static void BM_LAPACKE_SINGLE_PRECISISON(benchmark::State& state)

static void BM_LAPACKE_DOUBLE_PRECISISON(benchmark::State& state)
{
vecd x(system_size);
vecd b(system_size);
trid_matd A(system_size);
std::vector<double> x(system_size);
std::vector<double> b(system_size);
tuvx::TridiagonalMatrix<double> A(system_size);

std::mt19937 random_device(random_number_seed);
// Perform setup here
for (auto _ : state)
{
state.PauseTiming();
FillRandom<double>(A, diagonally_dominant);
FillRandom<double>(x);
b = Dot<double>(A, x);
tuvx::FillRandom<double>(A, random_number_seed, diagonally_dominant);
tuvx::FillRandom<double>(x, random_number_seed);
b = tuvx::Dot<double>(A, x);
state.ResumeTiming();

LAPACKE_dgtsv(
LAPACK_ROW_MAJOR,
system_size,
Expand All @@ -74,37 +74,37 @@ static void BM_LAPACKE_DOUBLE_PRECISISON(benchmark::State& state)

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);
std::vector<double> x(system_size);
std::vector<double> b(system_size);
tuvx::TridiagonalMatrix<double> A(system_size);
std::vector<double> x_approx(system_size);

for (auto _ : state)
{
state.PauseTiming();
FillRandom<double>(A, diagonally_dominant);
FillRandom<double>(x);
tuvx::FillRandom<double>(A, random_number_seed, diagonally_dominant);
tuvx::FillRandom<double>(x, random_number_seed);
state.ResumeTiming();
Solve<double>(A, b);
tuvx::Solve<double>(A, b);
}
}

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);
std::vector<float> x(system_size);
std::vector<float> b(system_size);
tuvx::TridiagonalMatrix<float> A(system_size);
std::vector<float> x_approx(system_size);

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

Expand Down
19 changes: 9 additions & 10 deletions cmake/dependencies.cmake
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
find_package(PkgConfig REQUIRED)
include(FetchContent)

################################################################################
# ##############################################################################
# LAPACK

find_package(BLAS)
find_package(LAPACK)
find_package(LAPACKE)

################################################################################
# ##############################################################################
# Memory check

if(TUVX_ENABLE_MEMCHECK)
Expand All @@ -28,7 +28,7 @@ if(TUVX_ENABLE_MEMCHECK)
endif()
endif()

################################################################################
# ##############################################################################
# OpenMP

if(TUVX_ENABLE_OPENMP)
Expand All @@ -41,15 +41,15 @@ if(TUVX_ENABLE_OPENMP)
endif()
endif()

################################################################################
# ##############################################################################
# NetCDF library

find_package(PkgConfig REQUIRED)

pkg_check_modules(netcdff IMPORTED_TARGET REQUIRED netcdf-fortran)
pkg_check_modules(netcdfc IMPORTED_TARGET REQUIRED netcdf)

################################################################################
# ##############################################################################
# yaml-cpp

FetchContent_Declare(
Expand All @@ -60,23 +60,22 @@ FetchContent_Declare(
${FETCHCONTENT_QUIET})
FetchContent_MakeAvailable(yaml-cpp)

################################################################################
# ##############################################################################
# Docs

if(TUVX_BUILD_DOCS)
find_package(Doxygen REQUIRED)
find_package(Sphinx REQUIRED)
endif()

################################################################################
# ##############################################################################
# google test and benchmark

if(TUVX_ENABLE_BENCHMARK)
FetchContent_Declare(
googlebenchmark
GIT_REPOSITORY https://github.com/google/benchmark.git
GIT_TAG v1.8.3
)
GIT_TAG v1.8.3)

set(BENCHMARK_DOWNLOAD_DEPENDENCIES ON)
set(BENCHMARK_ENABLE_GTEST_TESTS OFF)
Expand Down Expand Up @@ -105,4 +104,4 @@ if(TUVX_ENABLE_TESTS)
set_target_properties(gtest_main PROPERTIES CXX_CLANG_TIDY "")
endif()

################################################################################
# ##############################################################################
4 changes: 2 additions & 2 deletions include/tuvx/linear_algebra/linear_algebra.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ namespace tuvx
/// @brief Fill a std::vector with random values
/// @param x Vector to allocate and fill
template<typename T>
void FillRandom(std::vector<T> &x);
void FillRandom(std::vector<T> &x, const unsigned &seed);

/// @fn Initialize random matrix function
/// @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, bool diagonally_dominant = false);
void FillRandom(TridiagonalMatrix<T> &A, const unsigned &seed, bool diagonally_dominant = false);

/// @fn print vector function
/// @brief displays the data stored inside a std::vector
Expand Down
17 changes: 9 additions & 8 deletions include/tuvx/linear_algebra/linear_algebra.inl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

#include <random>
namespace tuvx
{

Expand Down Expand Up @@ -52,24 +53,24 @@ namespace tuvx
}

template<typename T>
inline void FillRandom(std::vector<T> &x)
inline void FillRandom(std::vector<T> &x, const unsigned &seed)
{
std::random_device dev;
std::mt19937 rng(dev());
// sample from normal distribution

std::mt19937 random_device(seed);
std::normal_distribution<double> distribution(5.0, 1.0);
for (std::size_t i = 0; i < x.size(); i++)
{
x[i] = (T)distribution(rng);
x[i] = (T)distribution(random_device);
}
}

template<typename T>
inline void FillRandom(TridiagonalMatrix<T> &A, bool diagonally_dominant)
inline void FillRandom(TridiagonalMatrix<T> &A, const unsigned &seed, bool diagonally_dominant)
{
FillRandom<T>(A.main_diagonal_);
FillRandom<T>(A.lower_diagonal_);
FillRandom<T>(A.upper_diagonal_);
FillRandom<T>(A.main_diagonal_, seed);
FillRandom<T>(A.lower_diagonal_, seed);
FillRandom<T>(A.upper_diagonal_, seed);

if (diagonally_dominant)
{
Expand Down
Loading

0 comments on commit 9239c15

Please sign in to comment.