Sync public subset from Flux (private)
This commit is contained in:
85
include/numerics/matequal.h
Normal file
85
include/numerics/matequal.h
Normal file
@@ -0,0 +1,85 @@
|
||||
#pragma once
|
||||
|
||||
#include "./core/omp_config.h"
|
||||
|
||||
#include "./utils/matrix.h"
|
||||
#include "./numerics/abs.h"
|
||||
|
||||
|
||||
namespace numerics{
|
||||
|
||||
// -------------- Serial ----------------
|
||||
template <typename T>
|
||||
bool matequal(const utils::Matrix<T>& A, const utils::Matrix<T>& B, double tol = 1e-9) {
|
||||
|
||||
if (A.rows() != B.rows() || A.cols() != B.cols()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool decimal = std::is_floating_point<T>::value;
|
||||
const uint64_t rows=A.rows(), cols=A.cols();
|
||||
|
||||
for (uint64_t i = 0; i < rows; ++i)
|
||||
for (uint64_t j = 0; j < cols; ++j)
|
||||
if (decimal) {
|
||||
if (numerics::abs(A(i,j) - B(i,j)) > static_cast<T>(tol)){
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
if (A(i,j) != B(i,j)){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// -------------- Parallel ----------------
|
||||
template <typename T>
|
||||
bool matequal_omp(const utils::Matrix<T>& A, const utils::Matrix<T>& B, double tol = 1e-9) {
|
||||
|
||||
if (A.rows() != B.rows() || A.cols() != B.cols()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool decimal = std::is_floating_point<T>::value;
|
||||
bool eq = true;
|
||||
const uint64_t rows=A.rows(), cols=A.cols();
|
||||
|
||||
#pragma omp parallel for collapse(2) schedule(static) reduction(&&:eq)
|
||||
for (uint64_t i = 0; i < rows; ++i)
|
||||
for (uint64_t j = 0; j < cols; ++j)
|
||||
if (decimal) {
|
||||
eq = eq && (numerics::abs(A(i,j) - B(i,j)) <= static_cast<T>(tol));
|
||||
} else {
|
||||
eq = eq && (A(i,j) == B(i,j));
|
||||
}
|
||||
return eq;
|
||||
}
|
||||
|
||||
// -------------- Auto OpenMP ----------------
|
||||
template <typename T>
|
||||
bool matequal_auto(const utils::Matrix<T>& A, const utils::Matrix<T>& B, double tol = 1e-9) {
|
||||
|
||||
if (A.rows() != B.rows() || A.cols() != B.cols()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
uint64_t work = A.rows() * A.cols();
|
||||
|
||||
#ifdef _OPENMP
|
||||
bool can_parallel = omp_config::omp_parallel_allowed();
|
||||
uint64_t threads = static_cast<uint64_t>(omp_get_max_threads());
|
||||
#else
|
||||
bool can_parallel = false;
|
||||
uint64_t threads = 1;
|
||||
#endif
|
||||
|
||||
if (can_parallel || work > threads * 4ull) {
|
||||
return matequal_omp(A,B,tol);
|
||||
}
|
||||
else{
|
||||
// Safe fallback
|
||||
return matequal(A,B,tol);
|
||||
}
|
||||
}
|
||||
} // namespace numerics
|
||||
Reference in New Issue
Block a user