Commit 14aa5e7c authored by GILLES Sebastien's avatar GILLES Sebastien
Browse files

#820 Petsc: add wrapper to MatSetValuesBlocked, indicated to be more efficient than MatSetValues.

parent fef0447d
...@@ -256,6 +256,27 @@ namespace HappyHeart ...@@ -256,6 +256,27 @@ namespace HappyHeart
} }
void Matrix::SetValuesBlocked(const std::vector<PetscInt>& row_indexing,
const std::vector<PetscInt>& column_indexing,
const PetscScalar* values, InsertMode insertOrAppend,
const char* invoking_file, int invoking_line)
{
int error_code = MatSetValuesBlocked(Internal(),
static_cast<PetscInt>(row_indexing.size()),
row_indexing.data(),
static_cast<PetscInt>(column_indexing.size()),
column_indexing.data(),
values,
insertOrAppend);
if (error_code)
throw ExceptionNS::Exception(error_code, "MatSetValuesBlocked", invoking_file, invoking_line);
}
void Matrix::SetValuesRow(PetscInt row_index, void Matrix::SetValuesRow(PetscInt row_index,
const PetscScalar* values, const PetscScalar* values,
const char* invoking_file, int invoking_line) const char* invoking_file, int invoking_line)
...@@ -376,11 +397,13 @@ namespace HappyHeart ...@@ -376,11 +397,13 @@ namespace HappyHeart
void Matrix::GetRow(PetscInt row_index, std::vector<PetscScalar>& row, void Matrix::GetRow(PetscInt row_index,
std::vector<std::pair<PetscInt, PetscScalar>>& row_content,
const char* invoking_file, int invoking_line) const const char* invoking_file, int invoking_line) const
{ {
PetscInt Nnon_zero_cols; assert(row_content.empty());
PetscInt Nnon_zero_cols;
const PetscInt* columns; const PetscInt* columns;
const PetscScalar* values; const PetscScalar* values;
...@@ -393,13 +416,11 @@ namespace HappyHeart ...@@ -393,13 +416,11 @@ namespace HappyHeart
if (error_code) if (error_code)
throw ExceptionNS::Exception(error_code, "MatGetRow", invoking_file, invoking_line); throw ExceptionNS::Exception(error_code, "MatGetRow", invoking_file, invoking_line);
const std::size_t Ncol = static_cast<std::size_t>(GetProgramWiseSize(__FILE__, __LINE__).second); const auto size = static_cast<std::size_t>(Nnon_zero_cols);
row_content.resize(size);
row.resize(Ncol, 0);
const auto Nnon_zero_cols_t = static_cast<std::size_t>(Nnon_zero_cols); for (auto i = 0ul; i < size; ++i)
for (std::size_t i = 0ul; i < Nnon_zero_cols_t; ++i) row_content[i] = std::make_pair(columns[i], values[i]);
row[static_cast<std::size_t>(columns[i])] = values[i];
error_code = MatRestoreRow(Internal(), error_code = MatRestoreRow(Internal(),
row_index, row_index,
......
...@@ -337,6 +337,29 @@ namespace HappyHeart ...@@ -337,6 +337,29 @@ namespace HappyHeart
void SetValuesRow(PetscInt row_index, void SetValuesRow(PetscInt row_index,
const PetscScalar* values, const PetscScalar* values,
const char* invoking_file, int invoking_line); const char* invoking_file, int invoking_line);
/*!
* \brief Add or modify a block of values into a Petsc matrix.
*
* According to Petsc documentation, it is likely more efficient than \a SetValues().
*
* \warning Assembly() must be called afterwards!
*
* \param[in] row_indexing Program-wise index of the rows which values will be set.
* \param[in] column_indexing Program-wise index of the columns which values will be set.
* Should be the same size as \a row_indexing.
* \param[in] values Values to put in the matrix. This array should be the same size as \a row_indexing
* (unfortunately we can't check that here as it is a C array).
* \param [in] insertOrAppend Petsc ADD_VALUES or INSERT_VALUES (see Petsc documentation).
* \param[in] invoking_file File that invoked the function or class; usually __FILE__.
* \param[in] invoking_line File that invoked the function or class; usually __LINE__.
*/
void SetValuesBlocked(const std::vector<PetscInt>& row_indexing,
const std::vector<PetscInt>& column_indexing,
const PetscScalar* values,
InsertMode insertOrAppend,
const char* invoking_file, int invoking_line);
...@@ -387,9 +410,12 @@ namespace HappyHeart ...@@ -387,9 +410,12 @@ namespace HappyHeart
* one of the row handled by the current processor. * one of the row handled by the current processor.
* \param[in] invoking_file File that invoked the function or class; usually __FILE__. * \param[in] invoking_file File that invoked the function or class; usually __FILE__.
* \param[in] invoking_line File that invoked the function or class; usually __LINE__. * \param[in] invoking_line File that invoked the function or class; usually __LINE__.
* \param[out] row Full (dense) content of the row. * \param[out] row_content Key is position of the non-zero values, value the actual value.
* \todo At the moment row_content is allocated each time; it might be useful not to do this... But this
* means structure has to be kept for each line of the matrix considered.
*/ */
void GetRow(PetscInt row_index, std::vector<PetscScalar>& row, void GetRow(PetscInt row_index,
std::vector<std::pair<PetscInt, PetscScalar>>& row_content,
const char* invoking_file, int invoking_line) const; const char* invoking_file, int invoking_line) const;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment