Skip to content

Commit

Permalink
Skip insertion in matrix if an error occurs
Browse files Browse the repository at this point in the history
  • Loading branch information
alxbilger committed Jan 11, 2024
1 parent 15979a5 commit 91dc63d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ namespace sofa::component::linearsystem
/// Check that the incoming rows and columns are expected by the constant sparsity pattern
struct CheckNoChangeInInsertionOrder : virtual core::matrixaccumulator::IndexVerificationStrategy
{
using verify_index = std::true_type;
using skip_insertion_if_error = std::true_type;

sofa::core::objectmodel::BaseObject* m_messageComponent { nullptr };

[[nodiscard]]
Expand All @@ -49,7 +52,7 @@ struct CheckNoChangeInInsertionOrder : virtual core::matrixaccumulator::IndexVer

std::size_t* currentId { nullptr };

void checkRowColIndices(const sofa::SignedIndex row, const sofa::SignedIndex col) override
bool checkRowColIndices(const sofa::SignedIndex row, const sofa::SignedIndex col) override
{
if (currentId)
{
Expand All @@ -63,7 +66,8 @@ struct CheckNoChangeInInsertionOrder : virtual core::matrixaccumulator::IndexVer
logger() << "According to the constant sparsity pattern, the "
"expected row and column are [" << expectedRow << ", " <<
expectedCol << "], but " << "[" << row << ", " << col <<
"] was received. Insertion is ignored.";
"] was received.";
return false;
}
}
else
Expand All @@ -72,8 +76,10 @@ struct CheckNoChangeInInsertionOrder : virtual core::matrixaccumulator::IndexVer
"The constant sparsity pattern did not expect more"
" incoming matrix values at this stage (current id = "
<< *currentId << ", insertion list size = " << pairInsertionOrderList.size() << ")";
return false;
}
}
return true;
}
};

Expand Down
10 changes: 8 additions & 2 deletions Sofa/framework/Core/src/sofa/core/MatrixAccumulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,31 +80,37 @@ logger() const
: msg_error("RangeVerification");
}

void matrixaccumulator::RangeVerification::checkRowIndex(sofa::SignedIndex row)
bool matrixaccumulator::RangeVerification::checkRowIndex(sofa::SignedIndex row)
{
if (row < minRowIndex)
{
logger() << "Trying to accumulate a matrix entry out of the allowed submatrix: minimum "
"row index is " << minRowIndex << " while " << row << " was provided";
return false;
}
if (row > maxRowIndex)
{
logger() << "Trying to accumulate a matrix entry out of the allowed submatrix: maximum "
"row index is " << maxRowIndex << " while " << row << " was provided";
return false;
}
return true;
}

void matrixaccumulator::RangeVerification::checkColIndex(sofa::SignedIndex col)
bool matrixaccumulator::RangeVerification::checkColIndex(sofa::SignedIndex col)
{
if (col < minColIndex)
{
logger() << "Trying to accumulate a matrix entry out of the allowed submatrix: minimum "
"column index is " << minColIndex << " while " << col << " was provided";
return false;
}
if (col > maxColIndex)
{
logger() << "Trying to accumulate a matrix entry out of the allowed submatrix: maximum "
"column index is " << maxColIndex << " while " << col << " was provided";
return false;
}
return true;
}
}
47 changes: 34 additions & 13 deletions Sofa/framework/Core/src/sofa/core/MatrixAccumulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,44 +75,54 @@ struct SOFA_CORE_API IndexVerificationStrategy
{
virtual ~IndexVerificationStrategy() = default;
using verify_index = std::true_type;
using skip_insertion_if_error = std::true_type;

virtual void checkRowColIndices(const sofa::SignedIndex row, const sofa::SignedIndex col) = 0;
virtual bool checkRowColIndices(const sofa::SignedIndex row, const sofa::SignedIndex col) = 0;
};

struct SOFA_CORE_API IndividualIndexVerificationStrategy : virtual IndexVerificationStrategy
{
void checkRowColIndices(const sofa::SignedIndex row, const sofa::SignedIndex col) override
bool checkRowColIndices(const sofa::SignedIndex row, const sofa::SignedIndex col) override
{
checkRowIndex(row);
checkColIndex(col);
const auto bRow = checkRowIndex(row);
const auto bCol = checkColIndex(col);
return bRow && bCol;
}

protected:
virtual void checkRowIndex(sofa::SignedIndex row) {}
virtual void checkColIndex(sofa::SignedIndex col) {}
virtual bool checkRowIndex(sofa::SignedIndex row) = 0;
virtual bool checkColIndex(sofa::SignedIndex col) = 0;
};


/**
* \brief The concatenation of multiple index verification strategies
* \tparam Strategies A list of strategy types deriving from @IndexVerificationStrategy
*/
template<class... Strategies>
struct CompositeIndexVerificationStrategy : Strategies...
{
using verify_index = std::bool_constant<std::disjunction_v<typename Strategies::verify_index...>>;
using skip_insertion_if_error = std::bool_constant<std::disjunction_v<typename Strategies::skip_insertion_if_error...>>;

void checkRowColIndices(const sofa::SignedIndex row, const sofa::SignedIndex col) override
bool checkRowColIndices(const sofa::SignedIndex row, const sofa::SignedIndex col) override
{
(Strategies::checkRowColIndices(row, col), ...);
return (Strategies::checkRowColIndices(row, col) && ...);
}
};

struct SOFA_CORE_API NoIndexVerification : virtual IndexVerificationStrategy
{
using verify_index = std::false_type;
using skip_insertion_if_error = std::false_type;
private:
void checkRowColIndices(sofa::SignedIndex /* row */, sofa::SignedIndex /* col */) override {}
bool checkRowColIndices(sofa::SignedIndex /* row */, sofa::SignedIndex /* col */) override { return true; }
};

struct SOFA_CORE_API RangeVerification : virtual IndividualIndexVerificationStrategy
{
using verify_index = std::true_type;
using skip_insertion_if_error = std::true_type;

sofa::SignedIndex minRowIndex { 0 };
sofa::SignedIndex maxRowIndex { std::numeric_limits<sofa::SignedIndex>::max() };
Expand All @@ -125,8 +135,8 @@ struct SOFA_CORE_API RangeVerification : virtual IndividualIndexVerificationStra
[[nodiscard]]
helper::logging::MessageDispatcher::LoggerStream logger() const;

void checkRowIndex(sofa::SignedIndex row) override;
void checkColIndex(sofa::SignedIndex col) override;
bool checkRowIndex(sofa::SignedIndex row) override;
bool checkColIndex(sofa::SignedIndex col) override;
};

}
Expand Down Expand Up @@ -233,10 +243,21 @@ class MatrixAccumulatorIndexChecker : public TBaseMatrixAccumulator
{
if (indexVerificationStrategy)
{
indexVerificationStrategy->checkRowColIndices(row, col);
const bool success = indexVerificationStrategy->checkRowColIndices(row, col);
if (!TStrategy::skip_insertion_if_error::value || success)
{
add(matrixaccumulator::no_check, row, col, value);
}
}
else
{
add(matrixaccumulator::no_check, row, col, value);
}
}
else
{
add(matrixaccumulator::no_check, row, col, value);
}
add(matrixaccumulator::no_check, row, col, value);
}
};

Expand Down

0 comments on commit 91dc63d

Please sign in to comment.