ddg-exercises
ddg-exercises copied to clipboard
Eigen, NDEBUG, and matrix shapes
A student posted this on Piazza. Maintainers, please take a look :D
This might be done on purpose, but it did take me quite some time to figure out what is going on. In the CmakeLists.txt that lives in ddg-exercises/projects/simplicial-complex-operators/CMakeLists.txt, line 41 (pasted below), the flag -DNDEBUG is passed. This disables the Eigen shape-checking for matrix multiplication. This allows wrongly shaped matrices to be multiplied silently. Once I took out this flag debugging became much easier :).
# https://gitlab.kitware.com/cmake/cmake/-/issues/22484
include(CheckCXXCompilerFlag)
unset(COMPILER_SUPPORTS_MARCH_NATIVE CACHE)
CHECK_CXX_COMPILER_FLAG(-march=native COMPILER_SUPPORTS_MARCH_NATIVE)
if(COMPILER_SUPPORTS_MARCH_NATIVE)
SET(CMAKE_CXX_FLAGS_RELEASE "-O3 -march=native -DNDEBUG")
else()
SET(CMAKE_CXX_FLAGS_RELEASE "-O3 -mcpu=apple-m1 -DNDEBUG") # Apple M1
endif()
Hi, thanks for sharing!
Usually one sets debugging modes via compilation flags, rather than hardcoding it directly. In these exercises, by default, just compiling using cmake
(i.e., without explicitly setting the flag to Release
or Debug
) will compile in "release" mode. In "release" mode, the variable CMAKE_CXX_FLAGS_RELEASE
is naturally set to encode the -DNDEBUG
flag. If one compiles in "debug" mode, which can be done with command line arguments via cmake -DCMAKE_BUILD_TYPE=Debug
(or cmake -DCMAKE_BUILD_TYPE=Debug ..
if you are in the build
subdirectory of a project), then the value of CMAKE_CXX_FLAGS_DEBUG
is used. You can see that the value of CMAKE_CXX_FLAGS_DEBUG
being set earlier in the CMakeLists.txt
file, with the corresponding flags to enable debugging.
Does compiling with cmake -DCMAKE_BUILD_TYPE=Debug
enable Eigen shape-checking?
By the way, thank you for sharing this. Now that I think about it, it makes more sense to make the default mode "debug" rather than "release" for these exercises. I'll probably change this :)