From 815b4a1d35b785cfd546198cc4b5ff17eb8b5d09 Mon Sep 17 00:00:00 2001 From: "Dmitry Yu. Naumov" <github@naumov.de> Date: Thu, 24 Sep 2015 23:10:05 +0000 Subject: [PATCH] [MaL] Update member functions of Lis matrix/vector --- MathLib/LinAlg/Lis/LisMatrix.cpp | 12 +++++--- MathLib/LinAlg/Lis/LisMatrix.h | 51 ++++++++++++++++---------------- MathLib/LinAlg/Lis/LisVector.cpp | 7 +++-- MathLib/LinAlg/Lis/LisVector.h | 10 +++---- 4 files changed, 44 insertions(+), 36 deletions(-) diff --git a/MathLib/LinAlg/Lis/LisMatrix.cpp b/MathLib/LinAlg/Lis/LisMatrix.cpp index 6b3e420fd2e..db5a238a819 100644 --- a/MathLib/LinAlg/Lis/LisMatrix.cpp +++ b/MathLib/LinAlg/Lis/LisMatrix.cpp @@ -37,8 +37,12 @@ LisMatrix::LisMatrix(std::size_t n_rows, LisOption::MatrixType mat_type) checkLisError(ierr); } -LisMatrix::LisMatrix(std::size_t n_rows, int nnz, int* row_ptr, int* col_idx, double* data) -: _n_rows(n_rows), _mat_type(LisOption::MatrixType::CRS), _is_assembled(false), _use_external_arrays(true) +LisMatrix::LisMatrix(std::size_t n_rows, int nnz, IndexType *row_ptr, + IndexType *col_idx, double *data) + : _n_rows(n_rows), + _mat_type(LisOption::MatrixType::CRS), + _is_assembled(false), + _use_external_arrays(true) { int ierr = lis_matrix_create(0, &_AA); checkLisError(ierr); @@ -81,7 +85,7 @@ void LisMatrix::setZero() _is_assembled = false; } -int LisMatrix::setValue(std::size_t rowId, std::size_t colId, double v) +int LisMatrix::setValue(IndexType rowId, IndexType colId, double v) { lis_matrix_set_value(LIS_INS_VALUE, rowId, colId, v, _AA); if (rowId==colId) @@ -90,7 +94,7 @@ int LisMatrix::setValue(std::size_t rowId, std::size_t colId, double v) return 0; } -int LisMatrix::add(std::size_t rowId, std::size_t colId, double v) +int LisMatrix::add(IndexType rowId, IndexType colId, double v) { lis_matrix_set_value(LIS_ADD_VALUE, rowId, colId, v, _AA); if (rowId==colId) diff --git a/MathLib/LinAlg/Lis/LisMatrix.h b/MathLib/LinAlg/Lis/LisMatrix.h index 1166d2cfff2..1756accbde3 100644 --- a/MathLib/LinAlg/Lis/LisMatrix.h +++ b/MathLib/LinAlg/Lis/LisMatrix.h @@ -64,7 +64,8 @@ public: * @param col_idx array of column indexes * @param data the non-zero entry values */ - LisMatrix(std::size_t n_rows, int nonzero, int* row_ptr, int* col_idx, double* data); + LisMatrix(std::size_t n_rows, int nonzero, IndexType* row_ptr, IndexType* col_idx, + double* data); /** * @@ -87,10 +88,10 @@ public: void setZero(); /// set entry - int setValue(std::size_t rowId, std::size_t colId, double v); + int setValue(IndexType rowId, IndexType colId, double v); /// add value - int add(std::size_t rowId, std::size_t colId, double v); + int add(IndexType rowId, IndexType colId, double v); /// printout this equation for debugging void write(const std::string &filename) const; @@ -107,7 +108,7 @@ public: /// Add sub-matrix at positions \c row_pos and same column positions as the /// given row positions. template<class T_DENSE_MATRIX> - void add(std::vector<std::size_t> const& row_pos, + void add(std::vector<IndexType> const& row_pos, const T_DENSE_MATRIX &sub_matrix, double fkt = 1.0) { @@ -116,18 +117,17 @@ public: /// Add sub-matrix at positions given by \c indices. template<class T_DENSE_MATRIX> - void add(RowColumnIndices<std::size_t> const& indices, + void add(RowColumnIndices<IndexType> const& indices, const T_DENSE_MATRIX &sub_matrix, double fkt = 1.0) { this->add(indices.rows, indices.columns, sub_matrix, fkt); } - /// template <class T_DENSE_MATRIX> - void add(std::vector<std::size_t> const& row_pos, - std::vector<std::size_t> const& col_pos, const T_DENSE_MATRIX &sub_matrix, - double fkt = 1.0); + void add(std::vector<IndexType> const& row_pos, + std::vector<IndexType> const& col_pos, + const T_DENSE_MATRIX& sub_matrix, double fkt = 1.0); /// get this matrix type LisOption::MatrixType getMatrixType() const { return _mat_type; } @@ -141,8 +141,8 @@ private: LIS_MATRIX _AA; LIS_VECTOR _diag; bool _is_assembled; - LIS_INT _is; ///< location where the partial matrix _AA starts in global matrix. - LIS_INT _ie; ///< location where the partial matrix _AA ends in global matrix. + IndexType _is; ///< location where the partial matrix _AA starts in global matrix. + IndexType _ie; ///< location where the partial matrix _AA ends in global matrix. bool _use_external_arrays; // friend function @@ -152,17 +152,19 @@ private: friend struct SetMatrixSparsity; }; -template<class T_DENSE_MATRIX> -void -LisMatrix::add(std::vector<std::size_t> const& row_pos, std::vector<std::size_t> const& col_pos, - const T_DENSE_MATRIX &sub_matrix, double fkt) +template <class T_DENSE_MATRIX> +void LisMatrix::add(std::vector<IndexType> const& row_pos, + std::vector<IndexType> const& col_pos, + const T_DENSE_MATRIX& sub_matrix, double fkt) { - const std::size_t n_rows = row_pos.size(); - const std::size_t n_cols = col_pos.size(); - for (std::size_t i = 0; i < n_rows; i++) { - const std::size_t row = row_pos[i]; - for (std::size_t j = 0; j < n_cols; j++) { - const std::size_t col = col_pos[j]; + auto const n_rows = row_pos.size(); + auto const n_cols = col_pos.size(); + for (auto i = decltype(n_rows){0}; i < n_rows; i++) + { + auto const row = row_pos[i]; + for (auto j = decltype(n_cols){0}; j < n_cols; j++) + { + auto const col = col_pos[j]; add(row, col, fkt * sub_matrix(i, j)); } } @@ -178,12 +180,12 @@ struct SetMatrixSparsity<LisMatrix, SPARSITY_PATTERN> void operator()(LisMatrix &matrix, SPARSITY_PATTERN const& sparsity_pattern) { - std::size_t n_rows = matrix.getNRows(); - std::vector<int> row_sizes; + auto const n_rows = matrix.getNRows(); + std::vector<LisMatrix::IndexType> row_sizes; row_sizes.reserve(n_rows); // LIS needs 1 more entry, otherewise it starts reallocating arrays. - for (std::size_t i = 0; i < n_rows; i++) + for (auto i = decltype(n_rows){0}; i < n_rows; i++) row_sizes.push_back(sparsity_pattern.getNodeDegree(i) + 1); int ierr = lis_matrix_malloc(matrix._AA, 0, row_sizes.data()); @@ -195,4 +197,3 @@ void operator()(LisMatrix &matrix, SPARSITY_PATTERN const& sparsity_pattern) } // MathLib #endif //LISMATRIX_H_ - diff --git a/MathLib/LinAlg/Lis/LisVector.cpp b/MathLib/LinAlg/Lis/LisVector.cpp index 2a86b055db2..924471175d5 100644 --- a/MathLib/LinAlg/Lis/LisVector.cpp +++ b/MathLib/LinAlg/Lis/LisVector.cpp @@ -12,6 +12,8 @@ * */ +#include <cassert> + #include "LisVector.h" #include "LisCheck.h" @@ -66,10 +68,11 @@ LisVector& LisVector::operator=(double v) std::size_t LisVector::size() const { - LIS_INT dummy; - LIS_INT size; + IndexType dummy; + IndexType size; int const ierr = lis_vector_get_size(_vec, &dummy, &size); checkLisError(ierr); + assert(size >= 0); // For safe implicit conversion to std::size_t. return size; } diff --git a/MathLib/LinAlg/Lis/LisVector.h b/MathLib/LinAlg/Lis/LisVector.h index 9fbbd3e0cd7..e2b3f82ef2c 100644 --- a/MathLib/LinAlg/Lis/LisVector.h +++ b/MathLib/LinAlg/Lis/LisVector.h @@ -62,9 +62,9 @@ public: LisVector& operator=(double v); /// access entry - double operator[](std::size_t rowId) const { return get(rowId); } + double operator[](IndexType rowId) const { return get(rowId); } /// get entry - double get(std::size_t rowId) const + double get(IndexType rowId) const { double v = .0; lis_vector_get_value(_vec, rowId, &v); @@ -72,13 +72,13 @@ public: } /// set entry - void set(std::size_t rowId, double v) + void set(IndexType rowId, double v) { lis_vector_set_value(LIS_INS_VALUE, rowId, v, _vec); } /// add entry - void add(std::size_t rowId, double v) + void add(IndexType rowId, double v) { lis_vector_set_value(LIS_ADD_VALUE, rowId, v, _vec); } @@ -99,7 +99,7 @@ public: /// template <class T_SUBVEC> - void add(const std::vector<std::size_t>& pos, const T_SUBVEC& sub_vec) + void add(const std::vector<IndexType>& pos, const T_SUBVEC& sub_vec) { for (std::size_t i = 0; i < pos.size(); ++i) { -- GitLab