feat(cpp-object-oriented-modular-refactoring): step 3 - foundation-google-style
This commit is contained in:
+99
-110
@@ -1,4 +1,4 @@
|
||||
#include "fesa/math/matrix.hpp"
|
||||
#include "fesa/math/matrix.h"
|
||||
|
||||
#include <mkl.h>
|
||||
|
||||
@@ -9,151 +9,140 @@
|
||||
namespace fesa {
|
||||
namespace {
|
||||
|
||||
std::size_t checkedStorageSize(const std::size_t rows, const std::size_t columns) {
|
||||
// Reject shape multiplication overflow before logical dimensions and storage diverge.
|
||||
if (columns != 0 &&
|
||||
rows > (std::numeric_limits<std::size_t>::max)() / columns) {
|
||||
throw std::length_error{"Dense matrix dimensions exceed the storage size range."};
|
||||
}
|
||||
return rows * columns;
|
||||
/// @brief Rejects shape overflow before logical dimensions diverge from
|
||||
/// storage.
|
||||
std::size_t CheckedStorageSize(const std::size_t rows,
|
||||
const std::size_t columns) {
|
||||
if (columns != 0 &&
|
||||
rows > (std::numeric_limits<std::size_t>::max)() / columns) {
|
||||
throw std::length_error{
|
||||
"Dense matrix dimensions exceed the storage size range."};
|
||||
}
|
||||
return rows * columns;
|
||||
}
|
||||
|
||||
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);
|
||||
/// @brief Converts a dense matrix dimension 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)())) {
|
||||
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;
|
||||
}
|
||||
/// @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;
|
||||
}
|
||||
|
||||
cblas_dcopy(toMklSize(source.size()), source.data(), 1, destination.data(), 1);
|
||||
cblas_dcopy(ToMklSize(source.size()), source.data(), 1, destination.data(),
|
||||
1);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace
|
||||
|
||||
Matrix::Matrix(
|
||||
const std::size_t rows,
|
||||
const std::size_t columns,
|
||||
const double value)
|
||||
: rows_(rows), columns_(columns), values_(checkedStorageSize(rows, columns), value) {}
|
||||
Matrix::Matrix(const std::size_t rows, const std::size_t columns,
|
||||
const double value)
|
||||
: rows_(rows),
|
||||
columns_(columns),
|
||||
values_(CheckedStorageSize(rows, columns), value) {}
|
||||
|
||||
Matrix::Matrix(const Matrix& other)
|
||||
: rows_(other.rows_), columns_(other.columns_), values_(other.values_.size()) {
|
||||
copyValues(other.values_, values_);
|
||||
: 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();
|
||||
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;
|
||||
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;
|
||||
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::Rows() const noexcept { return rows_; }
|
||||
|
||||
std::size_t Matrix::columns() const noexcept {
|
||||
return columns_;
|
||||
}
|
||||
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];
|
||||
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];
|
||||
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 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);
|
||||
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 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_));
|
||||
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
|
||||
} // namespace fesa
|
||||
|
||||
+165
-204
@@ -1,6 +1,4 @@
|
||||
#include "fesa/math/sparse_matrix.hpp"
|
||||
|
||||
#include "fesa/fem/dof_manager.hpp"
|
||||
#include "fesa/math/sparse_matrix.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
@@ -10,230 +8,193 @@
|
||||
#include <tuple>
|
||||
#include <utility>
|
||||
|
||||
#include "fesa/fem/dof_manager.hpp"
|
||||
|
||||
namespace fesa {
|
||||
namespace {
|
||||
|
||||
Status sparseFailure(
|
||||
const std::string& code,
|
||||
const std::string& identity,
|
||||
const std::string& message) {
|
||||
return Status::failure(
|
||||
FailureCategory::model,
|
||||
{{Severity::error,
|
||||
code,
|
||||
{{}, 0U},
|
||||
"SPARSE_MATRIX",
|
||||
identity,
|
||||
message}});
|
||||
/// @brief Builds a structured sparse-matrix model failure.
|
||||
Status SparseFailure(const std::string& code, const std::string& identity,
|
||||
const std::string& message) {
|
||||
return Status::Failure(
|
||||
FailureCategory::kModel,
|
||||
{{Severity::kError, code, {{}, 0U}, "SPARSE_MATRIX", identity, message}});
|
||||
}
|
||||
|
||||
Status validateCsr(
|
||||
const std::size_t rows,
|
||||
const std::size_t columns,
|
||||
const std::vector<std::size_t>& rowOffsets,
|
||||
const std::vector<std::size_t>& columnIndices,
|
||||
const std::vector<double>* const values) {
|
||||
if (rows == (std::numeric_limits<std::size_t>::max)() ||
|
||||
rowOffsets.size() != rows + 1U) {
|
||||
return sparseFailure(
|
||||
"invalid-sparse-shape",
|
||||
"row-offset-count",
|
||||
"CSR row offsets must contain exactly rows plus one entries.");
|
||||
}
|
||||
if (rowOffsets.empty() || rowOffsets.front() != 0U ||
|
||||
rowOffsets.back() != columnIndices.size()) {
|
||||
return sparseFailure(
|
||||
"invalid-sparse-pattern",
|
||||
"row-offset-range",
|
||||
"CSR row offsets must start at zero and end at the column count.");
|
||||
}
|
||||
if (values != nullptr && values->size() != columnIndices.size()) {
|
||||
return sparseFailure(
|
||||
"invalid-sparse-shape",
|
||||
"value-count",
|
||||
"CSR column and value arrays must have equal sizes.");
|
||||
}
|
||||
/// @brief Validates canonical CSR shape, order, index, and finite-value rules.
|
||||
Status ValidateCsr(const std::size_t rows, const std::size_t columns,
|
||||
const std::vector<std::size_t>& row_offsets,
|
||||
const std::vector<std::size_t>& column_indices,
|
||||
const std::vector<double>* const values) {
|
||||
if (rows == (std::numeric_limits<std::size_t>::max)() ||
|
||||
row_offsets.size() != rows + 1U) {
|
||||
return SparseFailure(
|
||||
"invalid-sparse-shape", "row-offset-count",
|
||||
"CSR row offsets must contain exactly rows plus one entries.");
|
||||
}
|
||||
if (row_offsets.empty() || row_offsets.front() != 0U ||
|
||||
row_offsets.back() != column_indices.size()) {
|
||||
return SparseFailure(
|
||||
"invalid-sparse-pattern", "row-offset-range",
|
||||
"CSR row offsets must start at zero and end at the column count.");
|
||||
}
|
||||
if (values != nullptr && values->size() != column_indices.size()) {
|
||||
return SparseFailure("invalid-sparse-shape", "value-count",
|
||||
"CSR column and value arrays must have equal sizes.");
|
||||
}
|
||||
|
||||
for (std::size_t row = 0U; row < rows; ++row) {
|
||||
const std::size_t begin = rowOffsets[row];
|
||||
const std::size_t end = rowOffsets[row + 1U];
|
||||
if (begin > end || end > columnIndices.size()) {
|
||||
return sparseFailure(
|
||||
"invalid-sparse-pattern",
|
||||
std::to_string(row),
|
||||
"CSR row offsets must be nondecreasing and remain in range.");
|
||||
}
|
||||
for (std::size_t position = begin; position < end; ++position) {
|
||||
if (columnIndices[position] >= columns) {
|
||||
return sparseFailure(
|
||||
"invalid-sparse-index",
|
||||
std::to_string(position),
|
||||
"CSR column index is outside the matrix dimensions.");
|
||||
}
|
||||
if (position > begin &&
|
||||
columnIndices[position - 1U] >= columnIndices[position]) {
|
||||
return sparseFailure(
|
||||
"invalid-sparse-pattern",
|
||||
std::to_string(row),
|
||||
"CSR columns must be sorted and unique within each row.");
|
||||
}
|
||||
if (values != nullptr && !std::isfinite((*values)[position])) {
|
||||
return sparseFailure(
|
||||
"nonfinite-sparse-value",
|
||||
std::to_string(position),
|
||||
"CSR values must be finite.");
|
||||
}
|
||||
}
|
||||
for (std::size_t row = 0U; row < rows; ++row) {
|
||||
const std::size_t begin = row_offsets[row];
|
||||
const std::size_t end = row_offsets[row + 1U];
|
||||
if (begin > end || end > column_indices.size()) {
|
||||
return SparseFailure(
|
||||
"invalid-sparse-pattern", std::to_string(row),
|
||||
"CSR row offsets must be nondecreasing and remain in range.");
|
||||
}
|
||||
return Status::ok();
|
||||
for (std::size_t position = begin; position < end; ++position) {
|
||||
if (column_indices[position] >= columns) {
|
||||
return SparseFailure(
|
||||
"invalid-sparse-index", std::to_string(position),
|
||||
"CSR column index is outside the matrix dimensions.");
|
||||
}
|
||||
if (position > begin &&
|
||||
column_indices[position - 1U] >= column_indices[position]) {
|
||||
return SparseFailure(
|
||||
"invalid-sparse-pattern", std::to_string(row),
|
||||
"CSR columns must be sorted and unique within each row.");
|
||||
}
|
||||
if (values != nullptr && !std::isfinite((*values)[position])) {
|
||||
return SparseFailure("nonfinite-sparse-value", std::to_string(position),
|
||||
"CSR values must be finite.");
|
||||
}
|
||||
}
|
||||
}
|
||||
return Status::Ok();
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace
|
||||
|
||||
Result<SparseMatrix> SparseMatrix::fromCoo(
|
||||
const std::size_t rows,
|
||||
const std::size_t columns,
|
||||
Result<SparseMatrix> SparseMatrix::FromCoo(
|
||||
const std::size_t rows, const std::size_t columns,
|
||||
std::vector<CooContribution> contributions,
|
||||
const SparsePattern& expectedPattern) {
|
||||
const Status patternStatus = validateCsr(
|
||||
rows,
|
||||
columns,
|
||||
expectedPattern.rowOffsets,
|
||||
expectedPattern.columnIndices,
|
||||
nullptr);
|
||||
if (!patternStatus.isOk()) {
|
||||
return Result<SparseMatrix>::failure(patternStatus);
|
||||
const SparsePattern& expected_pattern) {
|
||||
const Status pattern_status =
|
||||
ValidateCsr(rows, columns, expected_pattern.rowOffsets,
|
||||
expected_pattern.columnIndices, nullptr);
|
||||
if (!pattern_status.IsOk()) {
|
||||
return Result<SparseMatrix>::Failure(pattern_status);
|
||||
}
|
||||
|
||||
for (const auto& contribution : contributions) {
|
||||
if (contribution.row >= rows || contribution.column >= columns) {
|
||||
return Result<SparseMatrix>::Failure(SparseFailure(
|
||||
"invalid-sparse-index",
|
||||
std::to_string(contribution.row) + ":" +
|
||||
std::to_string(contribution.column),
|
||||
"COO contribution index is outside the matrix dimensions."));
|
||||
}
|
||||
if (!std::isfinite(contribution.value)) {
|
||||
return Result<SparseMatrix>::Failure(
|
||||
SparseFailure("nonfinite-sparse-value",
|
||||
std::to_string(contribution.element_order) + ":" +
|
||||
std::to_string(contribution.local_order),
|
||||
"COO contribution values must be finite."));
|
||||
}
|
||||
}
|
||||
|
||||
// The complete tuple fixes duplicate summation order independently of
|
||||
// worker completion order. stable_sort also preserves exact tuple ties.
|
||||
std::stable_sort(
|
||||
contributions.begin(), contributions.end(),
|
||||
[](const CooContribution& left, const CooContribution& right) {
|
||||
return std::tie(left.row, left.column, left.element_order,
|
||||
left.local_order) < std::tie(right.row, right.column,
|
||||
right.element_order,
|
||||
right.local_order);
|
||||
});
|
||||
|
||||
std::vector<double> values(expected_pattern.columnIndices.size(), 0.0);
|
||||
for (const auto& contribution : contributions) {
|
||||
const std::size_t begin = expected_pattern.rowOffsets[contribution.row];
|
||||
const std::size_t end = expected_pattern.rowOffsets[contribution.row + 1U];
|
||||
const auto first = expected_pattern.columnIndices.begin() + begin;
|
||||
const auto last = expected_pattern.columnIndices.begin() + end;
|
||||
const auto found = std::lower_bound(first, last, contribution.column);
|
||||
if (found == last || *found != contribution.column) {
|
||||
return Result<SparseMatrix>::Failure(SparseFailure(
|
||||
"sparse-pattern-mismatch",
|
||||
std::to_string(contribution.row) + ":" +
|
||||
std::to_string(contribution.column),
|
||||
"COO contribution is absent from the expected sparse pattern."));
|
||||
}
|
||||
|
||||
for (const auto& contribution : contributions) {
|
||||
if (contribution.row >= rows || contribution.column >= columns) {
|
||||
return Result<SparseMatrix>::failure(sparseFailure(
|
||||
"invalid-sparse-index",
|
||||
std::to_string(contribution.row) + ":" +
|
||||
std::to_string(contribution.column),
|
||||
"COO contribution index is outside the matrix dimensions."));
|
||||
}
|
||||
if (!std::isfinite(contribution.value)) {
|
||||
return Result<SparseMatrix>::failure(sparseFailure(
|
||||
"nonfinite-sparse-value",
|
||||
std::to_string(contribution.elementOrder) + ":" +
|
||||
std::to_string(contribution.localOrder),
|
||||
"COO contribution values must be finite."));
|
||||
}
|
||||
const std::size_t position = static_cast<std::size_t>(
|
||||
std::distance(expected_pattern.columnIndices.begin(), found));
|
||||
values[position] += contribution.value;
|
||||
if (!std::isfinite(values[position])) {
|
||||
return Result<SparseMatrix>::Failure(SparseFailure(
|
||||
"nonfinite-sparse-value",
|
||||
std::to_string(contribution.row) + ":" +
|
||||
std::to_string(contribution.column),
|
||||
"Ordered COO duplicate summation produced a nonfinite value."));
|
||||
}
|
||||
}
|
||||
|
||||
// The complete tuple fixes duplicate summation order independently of
|
||||
// worker completion order. stable_sort also preserves exact tuple ties.
|
||||
std::stable_sort(
|
||||
contributions.begin(),
|
||||
contributions.end(),
|
||||
[](const CooContribution& left, const CooContribution& right) {
|
||||
return std::tie(
|
||||
left.row,
|
||||
left.column,
|
||||
left.elementOrder,
|
||||
left.localOrder) <
|
||||
std::tie(
|
||||
right.row,
|
||||
right.column,
|
||||
right.elementOrder,
|
||||
right.localOrder);
|
||||
});
|
||||
SparseMatrix matrix{rows, columns, expected_pattern.rowOffsets,
|
||||
expected_pattern.columnIndices, std::move(values)};
|
||||
const Status status = matrix.Validate();
|
||||
if (!status.IsOk()) {
|
||||
return Result<SparseMatrix>::Failure(status);
|
||||
}
|
||||
return Result<SparseMatrix>::Success(std::move(matrix));
|
||||
}
|
||||
|
||||
std::vector<double> values(expectedPattern.columnIndices.size(), 0.0);
|
||||
for (const auto& contribution : contributions) {
|
||||
const std::size_t begin = expectedPattern.rowOffsets[contribution.row];
|
||||
const std::size_t end = expectedPattern.rowOffsets[contribution.row + 1U];
|
||||
const auto first = expectedPattern.columnIndices.begin() + begin;
|
||||
const auto last = expectedPattern.columnIndices.begin() + end;
|
||||
const auto found = std::lower_bound(first, last, contribution.column);
|
||||
if (found == last || *found != contribution.column) {
|
||||
return Result<SparseMatrix>::failure(sparseFailure(
|
||||
"sparse-pattern-mismatch",
|
||||
std::to_string(contribution.row) + ":" +
|
||||
std::to_string(contribution.column),
|
||||
"COO contribution is absent from the expected sparse pattern."));
|
||||
}
|
||||
std::size_t SparseMatrix::Rows() const noexcept { return rows_; }
|
||||
|
||||
const std::size_t position = static_cast<std::size_t>(
|
||||
std::distance(expectedPattern.columnIndices.begin(), found));
|
||||
values[position] += contribution.value;
|
||||
if (!std::isfinite(values[position])) {
|
||||
return Result<SparseMatrix>::failure(sparseFailure(
|
||||
"nonfinite-sparse-value",
|
||||
std::to_string(contribution.row) + ":" +
|
||||
std::to_string(contribution.column),
|
||||
"Ordered COO duplicate summation produced a nonfinite value."));
|
||||
}
|
||||
std::size_t SparseMatrix::Columns() const noexcept { return columns_; }
|
||||
|
||||
const std::vector<std::size_t>& SparseMatrix::RowOffsets() const noexcept {
|
||||
return row_offsets_;
|
||||
}
|
||||
|
||||
const std::vector<std::size_t>& SparseMatrix::ColumnIndices() const noexcept {
|
||||
return column_indices_;
|
||||
}
|
||||
|
||||
const std::vector<double>& SparseMatrix::Values() const noexcept {
|
||||
return values_;
|
||||
}
|
||||
|
||||
Vector SparseMatrix::Multiply(const Vector& rhs) const {
|
||||
if (columns_ != rhs.Size()) {
|
||||
throw std::invalid_argument{
|
||||
"Sparse matrix-vector multiplication has incompatible dimensions."};
|
||||
}
|
||||
|
||||
Vector result{rows_};
|
||||
for (std::size_t row = 0U; row < rows_; ++row) {
|
||||
double value = 0.0;
|
||||
for (std::size_t position = row_offsets_[row];
|
||||
position < row_offsets_[row + 1U]; ++position) {
|
||||
value += values_[position] * rhs[column_indices_[position]];
|
||||
}
|
||||
|
||||
SparseMatrix matrix{
|
||||
rows,
|
||||
columns,
|
||||
expectedPattern.rowOffsets,
|
||||
expectedPattern.columnIndices,
|
||||
std::move(values)};
|
||||
const Status status = matrix.validate();
|
||||
if (!status.isOk()) {
|
||||
return Result<SparseMatrix>::failure(status);
|
||||
}
|
||||
return Result<SparseMatrix>::success(std::move(matrix));
|
||||
result[row] = value;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
std::size_t SparseMatrix::rows() const noexcept {
|
||||
return rows_;
|
||||
Status SparseMatrix::Validate() const {
|
||||
return ValidateCsr(rows_, columns_, row_offsets_, column_indices_, &values_);
|
||||
}
|
||||
|
||||
std::size_t SparseMatrix::columns() const noexcept {
|
||||
return columns_;
|
||||
}
|
||||
|
||||
const std::vector<std::size_t>& SparseMatrix::rowOffsets() const noexcept {
|
||||
return rowOffsets_;
|
||||
}
|
||||
|
||||
const std::vector<std::size_t>& SparseMatrix::columnIndices() const noexcept {
|
||||
return columnIndices_;
|
||||
}
|
||||
|
||||
const std::vector<double>& SparseMatrix::values() const noexcept {
|
||||
return values_;
|
||||
}
|
||||
|
||||
Vector SparseMatrix::multiply(const Vector& rhs) const {
|
||||
if (columns_ != rhs.size()) {
|
||||
throw std::invalid_argument{
|
||||
"Sparse matrix-vector multiplication has incompatible dimensions."};
|
||||
}
|
||||
|
||||
Vector result{rows_};
|
||||
for (std::size_t row = 0U; row < rows_; ++row) {
|
||||
double value = 0.0;
|
||||
for (std::size_t position = rowOffsets_[row];
|
||||
position < rowOffsets_[row + 1U];
|
||||
++position) {
|
||||
value += values_[position] * rhs[columnIndices_[position]];
|
||||
}
|
||||
result[row] = value;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
Status SparseMatrix::validate() const {
|
||||
return validateCsr(
|
||||
rows_, columns_, rowOffsets_, columnIndices_, &values_);
|
||||
}
|
||||
|
||||
SparseMatrix::SparseMatrix(
|
||||
const std::size_t rows,
|
||||
const std::size_t columns,
|
||||
std::vector<std::size_t> rowOffsets,
|
||||
std::vector<std::size_t> columnIndices,
|
||||
std::vector<double> values)
|
||||
SparseMatrix::SparseMatrix(const std::size_t rows, const std::size_t columns,
|
||||
std::vector<std::size_t> row_offsets,
|
||||
std::vector<std::size_t> column_indices,
|
||||
std::vector<double> values)
|
||||
: rows_{rows},
|
||||
columns_{columns},
|
||||
rowOffsets_{std::move(rowOffsets)},
|
||||
columnIndices_{std::move(columnIndices)},
|
||||
row_offsets_{std::move(row_offsets)},
|
||||
column_indices_{std::move(column_indices)},
|
||||
values_{std::move(values)} {}
|
||||
|
||||
} // namespace fesa
|
||||
} // namespace fesa
|
||||
|
||||
+66
-69
@@ -1,4 +1,4 @@
|
||||
#include "fesa/math/vector.hpp"
|
||||
#include "fesa/math/vector.h"
|
||||
|
||||
#include <mkl.h>
|
||||
|
||||
@@ -9,111 +9,108 @@
|
||||
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);
|
||||
/// @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)())) {
|
||||
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;
|
||||
}
|
||||
/// @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);
|
||||
// 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
|
||||
} // 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(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(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;
|
||||
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;
|
||||
if (this != &other) {
|
||||
values_ = std::move(other.values_);
|
||||
other.values_.clear();
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
std::size_t Vector::size() const noexcept {
|
||||
return values_.size();
|
||||
}
|
||||
std::size_t Vector::Size() const noexcept { return values_.size(); }
|
||||
|
||||
double* Vector::data() noexcept {
|
||||
return values_.data();
|
||||
}
|
||||
double* Vector::Data() noexcept { return values_.data(); }
|
||||
|
||||
const double* Vector::data() const 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);
|
||||
return values_.at(index);
|
||||
}
|
||||
|
||||
const double& Vector::operator[](const std::size_t index) const {
|
||||
return values_.at(index);
|
||||
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;
|
||||
}
|
||||
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);
|
||||
return cblas_ddot(ToMklSize(Size()), Data(), 1, rhs.Data(), 1);
|
||||
}
|
||||
|
||||
double Vector::norm() const {
|
||||
if (values_.empty()) {
|
||||
return 0.0;
|
||||
}
|
||||
double Vector::Norm() const {
|
||||
if (values_.empty()) {
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
return cblas_dnrm2(toMklSize(size()), data(), 1);
|
||||
return cblas_dnrm2(ToMklSize(Size()), Data(), 1);
|
||||
}
|
||||
|
||||
void Vector::scale(const double alpha) {
|
||||
if (values_.empty()) {
|
||||
return;
|
||||
}
|
||||
void Vector::Scale(const double alpha) {
|
||||
if (values_.empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
cblas_dscal(toMklSize(size()), alpha, data(), 1);
|
||||
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;
|
||||
}
|
||||
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);
|
||||
cblas_daxpy(ToMklSize(Size()), alpha, x.Data(), 1, Data(), 1);
|
||||
}
|
||||
|
||||
} // namespace fesa
|
||||
} // namespace fesa
|
||||
|
||||
Reference in New Issue
Block a user