feat(linear-static-3d-euler-beam): step 9 - dense-math-adapters
This commit is contained in:
@@ -4,6 +4,8 @@ add_library(
|
||||
build_info.cpp
|
||||
core/diagnostic.cpp
|
||||
core/status.cpp
|
||||
math/matrix.cpp
|
||||
math/vector.cpp
|
||||
)
|
||||
|
||||
target_include_directories(
|
||||
@@ -12,6 +14,12 @@ target_include_directories(
|
||||
"${PROJECT_SOURCE_DIR}/include"
|
||||
)
|
||||
|
||||
target_link_libraries(
|
||||
fesa_solver
|
||||
PRIVATE
|
||||
Fesa::MKL
|
||||
)
|
||||
|
||||
# Product warnings are strict without imposing FESA policy on external targets.
|
||||
target_compile_options(
|
||||
fesa_solver
|
||||
|
||||
@@ -0,0 +1,150 @@
|
||||
#include "fesa/math/matrix.hpp"
|
||||
|
||||
#include <mkl.h>
|
||||
|
||||
#include <limits>
|
||||
#include <stdexcept>
|
||||
#include <utility>
|
||||
|
||||
namespace fesa {
|
||||
namespace {
|
||||
|
||||
MKL_INT toMklSize(const std::size_t size) {
|
||||
if (size > static_cast<std::size_t>((std::numeric_limits<MKL_INT>::max)())) {
|
||||
throw std::length_error{"Dense matrix dimension exceeds the MKL integer range."};
|
||||
}
|
||||
return static_cast<MKL_INT>(size);
|
||||
}
|
||||
|
||||
void copyValues(const std::vector<double>& source, std::vector<double>& destination) {
|
||||
if (source.empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
cblas_dcopy(toMklSize(source.size()), source.data(), 1, destination.data(), 1);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
Matrix::Matrix(
|
||||
const std::size_t rows,
|
||||
const std::size_t columns,
|
||||
const double value)
|
||||
: rows_(rows), columns_(columns), values_(rows * columns, value) {}
|
||||
|
||||
Matrix::Matrix(const Matrix& other)
|
||||
: rows_(other.rows_), columns_(other.columns_), values_(other.values_.size()) {
|
||||
copyValues(other.values_, values_);
|
||||
}
|
||||
|
||||
Matrix::Matrix(Matrix&& other) noexcept
|
||||
: rows_(other.rows_),
|
||||
columns_(other.columns_),
|
||||
values_(std::move(other.values_)) {
|
||||
other.rows_ = 0;
|
||||
other.columns_ = 0;
|
||||
other.values_.clear();
|
||||
}
|
||||
|
||||
Matrix& Matrix::operator=(const Matrix& other) {
|
||||
if (this != &other) {
|
||||
std::vector<double> copied(other.values_.size());
|
||||
copyValues(other.values_, copied);
|
||||
rows_ = other.rows_;
|
||||
columns_ = other.columns_;
|
||||
values_.swap(copied);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
Matrix& Matrix::operator=(Matrix&& other) noexcept {
|
||||
if (this != &other) {
|
||||
rows_ = other.rows_;
|
||||
columns_ = other.columns_;
|
||||
values_ = std::move(other.values_);
|
||||
other.rows_ = 0;
|
||||
other.columns_ = 0;
|
||||
other.values_.clear();
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
std::size_t Matrix::rows() const noexcept {
|
||||
return rows_;
|
||||
}
|
||||
|
||||
std::size_t Matrix::columns() const noexcept {
|
||||
return columns_;
|
||||
}
|
||||
|
||||
double& Matrix::operator()(const std::size_t row, const std::size_t column) {
|
||||
if (row >= rows_ || column >= columns_) {
|
||||
throw std::out_of_range{"Matrix index is outside its dimensions."};
|
||||
}
|
||||
return values_[row * columns_ + column];
|
||||
}
|
||||
|
||||
const double& Matrix::operator()(const std::size_t row, const std::size_t column) const {
|
||||
if (row >= rows_ || column >= columns_) {
|
||||
throw std::out_of_range{"Matrix index is outside its dimensions."};
|
||||
}
|
||||
return values_[row * columns_ + column];
|
||||
}
|
||||
|
||||
Vector Matrix::multiply(const Vector& rhs) const {
|
||||
if (columns_ != rhs.size()) {
|
||||
throw std::invalid_argument{"Matrix-vector multiplication has incompatible dimensions."};
|
||||
}
|
||||
|
||||
Vector result{rows_};
|
||||
if (rows_ == 0 || columns_ == 0) {
|
||||
return result;
|
||||
}
|
||||
|
||||
// The owned layout is row-major, so the leading dimension is the column
|
||||
// count for the adapter call and remains invisible to public consumers.
|
||||
cblas_dgemv(
|
||||
CblasRowMajor,
|
||||
CblasNoTrans,
|
||||
toMklSize(rows_),
|
||||
toMklSize(columns_),
|
||||
1.0,
|
||||
values_.data(),
|
||||
toMklSize(columns_),
|
||||
rhs.data(),
|
||||
1,
|
||||
0.0,
|
||||
result.data(),
|
||||
1);
|
||||
return result;
|
||||
}
|
||||
|
||||
Matrix Matrix::multiply(const Matrix& rhs) const {
|
||||
if (columns_ != rhs.rows_) {
|
||||
throw std::invalid_argument{"Matrix multiplication has incompatible dimensions."};
|
||||
}
|
||||
|
||||
Matrix result{rows_, rhs.columns_};
|
||||
if (rows_ == 0 || columns_ == 0 || rhs.columns_ == 0) {
|
||||
return result;
|
||||
}
|
||||
|
||||
cblas_dgemm(
|
||||
CblasRowMajor,
|
||||
CblasNoTrans,
|
||||
CblasNoTrans,
|
||||
toMklSize(rows_),
|
||||
toMklSize(rhs.columns_),
|
||||
toMklSize(columns_),
|
||||
1.0,
|
||||
values_.data(),
|
||||
toMklSize(columns_),
|
||||
rhs.values_.data(),
|
||||
toMklSize(rhs.columns_),
|
||||
0.0,
|
||||
result.values_.data(),
|
||||
toMklSize(rhs.columns_));
|
||||
return result;
|
||||
}
|
||||
|
||||
} // namespace fesa
|
||||
@@ -0,0 +1,119 @@
|
||||
#include "fesa/math/vector.hpp"
|
||||
|
||||
#include <mkl.h>
|
||||
|
||||
#include <limits>
|
||||
#include <stdexcept>
|
||||
#include <utility>
|
||||
|
||||
namespace fesa {
|
||||
namespace {
|
||||
|
||||
MKL_INT toMklSize(const std::size_t size) {
|
||||
if (size > static_cast<std::size_t>((std::numeric_limits<MKL_INT>::max)())) {
|
||||
throw std::length_error{"Dense vector size exceeds the MKL integer range."};
|
||||
}
|
||||
return static_cast<MKL_INT>(size);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
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_);
|
||||
}
|
||||
|
||||
Vector::Vector(Vector&& other) noexcept
|
||||
: values_(std::move(other.values_)) {
|
||||
other.values_.clear();
|
||||
}
|
||||
|
||||
Vector& Vector::operator=(const Vector& other) {
|
||||
if (this != &other) {
|
||||
std::vector<double> copied(other.size());
|
||||
copyValues(other.values_, copied);
|
||||
values_.swap(copied);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vector& Vector::operator=(Vector&& other) noexcept {
|
||||
if (this != &other) {
|
||||
values_ = std::move(other.values_);
|
||||
other.values_.clear();
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
std::size_t Vector::size() const noexcept {
|
||||
return values_.size();
|
||||
}
|
||||
|
||||
double* Vector::data() noexcept {
|
||||
return values_.data();
|
||||
}
|
||||
|
||||
const double* Vector::data() const noexcept {
|
||||
return values_.data();
|
||||
}
|
||||
|
||||
double& Vector::operator[](const std::size_t index) {
|
||||
return values_.at(index);
|
||||
}
|
||||
|
||||
const double& Vector::operator[](const std::size_t index) const {
|
||||
return values_.at(index);
|
||||
}
|
||||
|
||||
double Vector::dot(const Vector& rhs) const {
|
||||
if (size() != rhs.size()) {
|
||||
throw std::invalid_argument{"Vector dot product requires equal dimensions."};
|
||||
}
|
||||
if (values_.empty()) {
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
return cblas_ddot(toMklSize(size()), data(), 1, rhs.data(), 1);
|
||||
}
|
||||
|
||||
double Vector::norm() const {
|
||||
if (values_.empty()) {
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
return cblas_dnrm2(toMklSize(size()), data(), 1);
|
||||
}
|
||||
|
||||
void Vector::scale(const double alpha) {
|
||||
if (values_.empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
cblas_dscal(toMklSize(size()), alpha, data(), 1);
|
||||
}
|
||||
|
||||
void Vector::axpy(const double alpha, const Vector& x) {
|
||||
if (size() != x.size()) {
|
||||
throw std::invalid_argument{"Vector axpy requires equal dimensions."};
|
||||
}
|
||||
if (values_.empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
cblas_daxpy(toMklSize(size()), alpha, x.data(), 1, data(), 1);
|
||||
}
|
||||
|
||||
} // namespace fesa
|
||||
Reference in New Issue
Block a user