1
votes

In Eigen, it appears the assertion that checks for matrix sizes for matrix multiplication is disabled by default when using a release mode of the cmake. Is there anyway to keep this assertion when using the release mode ?

I have a matrix product m1*m2 in my code where the m1.cols() != m2.rows(). Both matrix are of dynamic size. When I set(CMAKE_BUILD_TYPE Debug) Eigen will check the size of the matrices and throws an error. When I set(CMAKE_BUILD_TYPE Release) the size check gets disabled and the evaluation m1*m2 goes through, accessing out of bounds memory and the result is almost random.

2

2 Answers

1
votes

To make use of assert in release mode, you must undefine the NDEBUG macro. Take a look at the suggestions in this question.

However, I would not recommend this. It would enable assert calls in release mode all over the code you are building. It kind of breaks the contract for the NDEBUG macro. And if you are unlucky it may also affect performance.

If that specific assertion/test is important for you, add it yourself at the application level of your code using something other than assert.

0
votes

You can predefine eigen_assert before including any Eigen headers, e.g., making it throw (or cerr and terminate).

#define eigen_assert(cond) if(!(cond)) {throw std::runtime_error(#cond);}
#include <Eigen/Core>

int main()
{
    assert(false && "not triggered in release mode");
    Eigen::Matrix3d A;
    Eigen::MatrixXd B(3,2);
    A+=B;
}

https://godbolt.org/z/Rqz7BB