Skip to content

Commit

Permalink
changed function name style
Browse files Browse the repository at this point in the history
  • Loading branch information
Aditya Dendukuri committed Jun 5, 2024
1 parent 541e4d3 commit fe859f6
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 53 deletions.
55 changes: 28 additions & 27 deletions cmake/dependencies.cmake
Original file line number Diff line number Diff line change
@@ -1,32 +1,33 @@
find_package(PkgConfig REQUIRED)
include(FetchContent)

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

find_package(BLAS)
find_package(LAPACK)

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

if(TUVX_ENABLE_MEMCHECK)
find_file(MEMCHECK_SUPPRESS_FILE
find_file(
MEMCHECK_SUPPRESS_FILE
DOC "Suppression file for memory checking"
NAMES openmpi-valgrind.supp
PATHS
/usr/share/openmpi
/usr/lib64/openmpi/share
/usr/lib64/openmpi/share/openmpi
/usr/share)
PATHS /usr/share/openmpi /usr/lib64/openmpi/share
/usr/lib64/openmpi/share/openmpi /usr/share)
if(MEMCHECK_SUPPRESS_FILE)
set(MEMCHECK_SUPPRESS "--suppressions=${PROJECT_SOURCE_DIR}/test/valgrind.supp --suppressions=${MEMCHECK_SUPPRESS_FILE}")
set(MEMCHECK_SUPPRESS
"--suppressions=${PROJECT_SOURCE_DIR}/test/valgrind.supp --suppressions=${MEMCHECK_SUPPRESS_FILE}"
)
else()
set(MEMCHECK_SUPPRESS "--suppressions=${PROJECT_SOURCE_DIR}/test/valgrind.supp")
set(MEMCHECK_SUPPRESS
"--suppressions=${PROJECT_SOURCE_DIR}/test/valgrind.supp")
endif()
endif()

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

if(TUVX_ENABLE_OPENMP)
Expand All @@ -39,52 +40,52 @@ 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(
yaml-cpp
GIT_REPOSITORY https://github.com/jbeder/yaml-cpp/
GIT_TAG 0.8.0
GIT_PROGRESS NOT ${FETCHCONTENT_QUIET}
)
GIT_PROGRESS NOT
${FETCHCONTENT_QUIET})
FetchContent_MakeAvailable(yaml-cpp)

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

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

################################################################################
# ##############################################################################
# google test

if(TUVX_ENABLE_TESTS)
FetchContent_Declare(googletest
FetchContent_Declare(
googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG be03d00f5f0cc3a997d1a368bee8a1fe93651f48
)
GIT_TAG be03d00f5f0cc3a997d1a368bee8a1fe93651f48)

set(INSTALL_GTEST OFF CACHE BOOL "" FORCE)
set(BUILD_GMOCK OFF CACHE BOOL "" FORCE)
set(INSTALL_GTEST
OFF
CACHE BOOL "" FORCE)
set(BUILD_GMOCK
OFF
CACHE BOOL "" FORCE)

FetchContent_MakeAvailable(googletest)

# don't run clang-tidy on google test
set_target_properties(gtest PROPERTIES CXX_CLANG_TIDY "")
set_target_properties(gtest_main PROPERTIES CXX_CLANG_TIDY "")
endif()
endif()
15 changes: 7 additions & 8 deletions include/tuvx/linalg/linalg.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,44 +20,43 @@ template <typename T> struct TridiagonalMatrix {
/// @brief Allocates a vector to a given size and fills it with random values
/// @param x Vector to allocate and fill
/// @param size Size of the vector
template <typename T> void fill_rand_vec(std::vector<T> &x, std::size_t size);
template <typename T> void FillRandVec(std::vector<T> &x, std::size_t size);

/// @brief Allocates a matrix to a given size and fills it with random values
/// @param A tridiagonal matrix to allocate and fill
/// @param size Size of the vector
template <typename T>
void fill_rand_mat(TridiagonalMatrix<T> &A, std::size_t n);
template <typename T> void FillRandMat(TridiagonalMatrix<T> &A, std::size_t n);

/// @brief displays the data stored inside a std::vector
/// @param x Vector to print
template <typename T> void print_vec(std::vector<T>);
template <typename T> void PrintVec(std::vector<T>);

/// @brief displays the data stored inside a tridiagonal matrix.
/// For now, this function simply calls printvec() on the three diagonals.
/// @param A tridiagonal matrix to print
template <typename T> void print_trid_mat(TridiagonalMatrix<T> x);
template <typename T> void PrintTridMat(TridiagonalMatrix<T> x);

/// @fn Tridiagonal Linear System Solver
/// @brief Thomas' algorithm for solving tridiagonal linear system (A x = 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> tridiag_solve(TridiagonalMatrix<T> A, std::vector<T> b);
std::vector<T> TridiagSolve(TridiagonalMatrix<T> A, std::vector<T> b);

/// @fn Tridiagonal Matrix - vector dot product
/// @brief Specialized dot product function for tridiagonal matrices.
/// @param A tridiagonal matrix
/// @param x vector to multiply the matrix with
/// @returns dot product between A and x
template <typename T> std::vector<T> dot(TridiagonalMatrix<T>, std::vector<T>);
template <typename T> std::vector<T> Dot(TridiagonalMatrix<T>, std::vector<T>);

/// @fn LP norm
/// @brief LP of a vectors. Used for computing errors
/// @param x vector to compute the norm of
/// @param p of the norm
/// @returns $\frac{1}{N}(\sum_{i=1}^{N} |x|^p)^{\frac{1}{p}}$
template <typename T> T norm(std::vector<T> x, int p);
template <typename T> T Norm(std::vector<T> x, int p);

} // namespace tuvx
#include "linalg.inl"
20 changes: 10 additions & 10 deletions include/tuvx/linalg/linalg.inl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
namespace tuvx {

template <typename T>
inline std::vector<T> dot(TridiagonalMatrix<T> A, std::vector<T> x) {
inline std::vector<T> Dot(TridiagonalMatrix<T> A, std::vector<T> x) {
std::size_t size = x.size();
std::vector<T> v(size);
v[0] = A.main_diagonal_[0] * x[0] + A.upper_diagonal_[0] * x[1];
Expand All @@ -17,7 +17,7 @@ inline std::vector<T> dot(TridiagonalMatrix<T> A, std::vector<T> x) {
}

template <typename T>
inline std::vector<T> tridiag_solve(TridiagonalMatrix<T> A, std::vector<T> b) {
inline std::vector<T> TridiagSolve(TridiagonalMatrix<T> A, std::vector<T> b) {
T temp;
std::size_t N = b.size();
std::vector<T> x(N);
Expand All @@ -36,7 +36,7 @@ inline std::vector<T> tridiag_solve(TridiagonalMatrix<T> A, std::vector<T> b) {
}

template <typename T>
inline void fill_rand_vec(std::vector<T> &x, std::size_t size) {
inline void FillRandVec(std::vector<T> &x, std::size_t size) {
std::random_device dev;
std::mt19937 rng(dev());
std::uniform_int_distribution<std::mt19937::result_type> dist6(1, 6);
Expand All @@ -47,33 +47,33 @@ inline void fill_rand_vec(std::vector<T> &x, std::size_t size) {
}

template <typename T>
inline void fill_rand_mat(TridiagonalMatrix<T> &tridmat, std::size_t n) {
inline void FillRandMat(TridiagonalMatrix<T> &tridmat, std::size_t n) {
tridmat.size = n;
fill_rand_vec(tridmat.main_diagonal_, n);
fill_rand_vec(tridmat.lower_diagonal_, n - 1);
fill_rand_vec(tridmat.upper_diagonal_, n - 1);
FillRandVec<T>(tridmat.main_diagonal_, n);
FillRandVec<T>(tridmat.lower_diagonal_, n - 1);
FillRandVec<T>(tridmat.upper_diagonal_, n - 1);
for (int i = 0; i < n; i++) {
tridmat.main_diagonal_[i] = std::pow(tridmat.main_diagonal_[i], 3);
}
}

template <typename T> inline void norm(std::vector<T> x, int order) {
template <typename T> inline void Norm(std::vector<T> x, int order) {
T nrm = (T)0;
for (int i = 0; i < x.size(); i++) {
nrm += std::pow(x[i], order);
}
return (1.0 / nrm) * std::pow(nrm, 1.0 / order);
}

template <typename T> inline void print_vec(std::vector<T> x) {
template <typename T> inline void PrintVec(std::vector<T> x) {
std::cout << std::endl;
for (int i = 0; i < (int)x.size(); i++) {
std::cout << x.at(i) << std::endl;
}
std::cout << std::endl;
}

template <typename T> inline void print_trid_mat(TridiagonalMatrix<T> x) {
template <typename T> inline void PrintTridMat(TridiagonalMatrix<T> x) {
print_vec(x.upper_diagonal_);
print_vec(x.main_diagonal_);
print_vec(x.lower_diagonal_);
Expand Down
Empty file.
18 changes: 10 additions & 8 deletions test/unit/linalg/test_tridiag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ TEST(TridiagSolveCPP, DoublePrecision) {
trid_matd A;

// initialize random matrix A and vector
fill_rand_mat(A, size);
fill_rand_vec(x, size);
b = dot(A, x);
FillRandMat(A, size);
FillRandVec(x, size);
b = Dot(A, x);

// reconstruct x by tridiagonal solve
vecd x_approx = tridiag_solve(A, b);
vecd x_approx = TridiagSolve(A, b);

// compute reconstruction error
double error = 0.0;
Expand All @@ -55,12 +55,12 @@ TEST(TridiagSolveCPP, SinglePrecision) {
vecf b;
trid_matf A;

fill_rand_mat(A, size);
fill_rand_vec(x, size);
FillRandMat(A, size);
FillRandVec(x, size);

b = dot(A, x);
b = Dot(A, x);

vecf x_approx = tridiag_solve(A, b);
vecf x_approx = TridiagSolve(A, b);
float error = 0.0;
for (int i = 0; i < x_approx.size(); i++) {
error += std::pow(std::abs(x[i] - x_approx[i]), 2);
Expand All @@ -71,3 +71,5 @@ TEST(TridiagSolveCPP, SinglePrecision) {
<< std::numeric_limits<float>::epsilon()
<< std::endl;
}

/// @test Comparing Tridiagonal

0 comments on commit fe859f6

Please sign in to comment.