feat(cpp-object-oriented-modular-refactoring): step 10 - dense-blas-adapter

This commit is contained in:
KOKO\Mimi
2026-08-16 07:49:57 +09:00
parent 6b0ff31db0
commit 89fc13c873
7 changed files with 146 additions and 49 deletions
+16 -24
View File
@@ -2,32 +2,22 @@
#include <mkl.h>
#include <limits>
#include <stdexcept>
#include <utility>
#include "math/dense_blas_internal.h"
namespace fesa {
namespace {
/// @brief Converts a dense vector size to the private MKL integer contract.
MKL_INT ToMklSize(const std::size_t size) {
if (size > static_cast<std::size_t>((std::numeric_limits<MKL_INT>::max)())) {
/// @brief Preserves the Vector exception contract around private size
/// conversion.
MKL_INT MklSizeOrThrow(const std::size_t size) {
const auto converted = dense_blas_internal::ToMklSize(size);
if (!converted.HasValue()) {
throw std::length_error{"Dense vector size exceeds the MKL integer range."};
}
return static_cast<MKL_INT>(size);
}
/// @brief Copies owned values without exposing the dense backend publicly.
void CopyValues(const std::vector<double>& source,
std::vector<double>& destination) {
if (source.empty()) {
return;
}
// Keep the backend operation in this translation unit so public ownership
// remains independent of MKL headers and integer types.
cblas_dcopy(ToMklSize(source.size()), source.data(), 1, destination.data(),
1);
return converted.Value();
}
} // namespace
@@ -36,7 +26,8 @@ Vector::Vector(const std::size_t size, const double value)
: values_(size, value) {}
Vector::Vector(const Vector& other) : values_(other.Size()) {
CopyValues(other.values_, values_);
dense_blas_internal::CopyValues(other.values_.data(), other.values_.size(),
values_.data());
}
Vector::Vector(Vector&& other) noexcept : values_(std::move(other.values_)) {
@@ -46,7 +37,8 @@ Vector::Vector(Vector&& other) noexcept : values_(std::move(other.values_)) {
Vector& Vector::operator=(const Vector& other) {
if (this != &other) {
std::vector<double> copied(other.Size());
CopyValues(other.values_, copied);
dense_blas_internal::CopyValues(other.values_.data(), other.values_.size(),
copied.data());
values_.swap(copied);
}
return *this;
@@ -83,7 +75,7 @@ double Vector::Dot(const Vector& rhs) const {
return 0.0;
}
return cblas_ddot(ToMklSize(Size()), Data(), 1, rhs.Data(), 1);
return cblas_ddot(MklSizeOrThrow(Size()), Data(), 1, rhs.Data(), 1);
}
double Vector::Norm() const {
@@ -91,7 +83,7 @@ double Vector::Norm() const {
return 0.0;
}
return cblas_dnrm2(ToMklSize(Size()), Data(), 1);
return cblas_dnrm2(MklSizeOrThrow(Size()), Data(), 1);
}
void Vector::Scale(const double alpha) {
@@ -99,7 +91,7 @@ void Vector::Scale(const double alpha) {
return;
}
cblas_dscal(ToMklSize(Size()), alpha, Data(), 1);
cblas_dscal(MklSizeOrThrow(Size()), alpha, Data(), 1);
}
void Vector::Axpy(const double alpha, const Vector& x) {
@@ -110,7 +102,7 @@ void Vector::Axpy(const double alpha, const Vector& x) {
return;
}
cblas_daxpy(ToMklSize(Size()), alpha, x.Data(), 1, Data(), 1);
cblas_daxpy(MklSizeOrThrow(Size()), alpha, x.Data(), 1, Data(), 1);
}
} // namespace fesa