Skip to content

Commit 9b92db6

Browse files
authored
Merge pull request #93 from fabinsch/topic/check-runtime-malloc
Additional option to check eigen runtime malloc
2 parents 0920147 + e3427cf commit 9b92db6

File tree

4 files changed

+50
-21
lines changed

4 files changed

+50
-21
lines changed

.github/workflows/ci-linux-osx-win-conda.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,15 @@ jobs:
8383
cd build
8484
cmake .. -GNinja -DCMAKE_C_COMPILER_LAUNCHER=ccache -DCMAKE_CXX_COMPILER_LAUNCHER=ccache -DCMAKE_CXX_STANDARD=${{ matrix.cxx_std }} -DCMAKE_INSTALL_PREFIX=${CONDA_PREFIX} -DCMAKE_BUILD_TYPE=${{ matrix.build_type }} -DBUILD_PYTHON_INTERFACE:BOOL=ON -DPYTHON_EXECUTABLE=$(which python3) -DINSTALL_DOCUMENTATION:BOOL=ON -DTEST_JULIA_INTERFACE:BOOL=ON
8585
86+
- name: Configure [Conda/macOS-debug/CheckMalloc]
87+
if: contains(matrix.os, 'macos') && contains(matrix.build_type, 'Debug')
88+
shell: bash -l {0}
89+
run: |
90+
echo $(whereis ccache)
91+
echo $(which ccache)
92+
cd build
93+
cmake .. -GNinja -DCMAKE_C_COMPILER_LAUNCHER=ccache -DCMAKE_CXX_COMPILER_LAUNCHER=ccache -DCHECK_RUNTIME_MALLOC:BOOL=ON -DCMAKE_CXX_STANDARD=${{ matrix.cxx_std }} -DCMAKE_INSTALL_PREFIX=${CONDA_PREFIX} -DCMAKE_BUILD_TYPE=${{ matrix.build_type }} -DBUILD_PYTHON_INTERFACE:BOOL=ON -DPYTHON_EXECUTABLE=$(which python3) -DINSTALL_DOCUMENTATION:BOOL=ON -DTEST_JULIA_INTERFACE:BOOL=ON
94+
8695
- name: Configure [Conda/Windows-2019]
8796
if: contains(matrix.os, 'windows-2019')
8897
shell: bash -l {0}

CMakeLists.txt

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ apply_default_apple_configuration()
5050

5151
option(BUILD_PYTHON_INTERFACE "Build the Python bindings" OFF)
5252
option(INSTALL_DOCUMENTATION "Generate and install the C++ documentation" OFF)
53-
option(INITIALIZE_EIGEN_WITH_NAN "Initializa Eigen objects with NAN values" OFF)
53+
option(INITIALIZE_EIGEN_WITH_NAN "Initialize Eigen objects with NAN values" OFF)
54+
option(CHECK_RUNTIME_MALLOC
55+
"Check if some memory allocations are performed at runtime" OFF)
5456
option(SUFFIX_SO_VERSION "Suffix library name with its version" ON)
5557

5658
option(BUILD_WITH_VECTORIZATION_SUPPORT
@@ -71,6 +73,11 @@ message(STATUS "CMAKE_SYSTEM_PROCESSOR: ${CMAKE_SYSTEM_PROCESSOR}")
7173
if(INITIALIZE_EIGEN_WITH_NAN)
7274
add_definitions(-DEIGEN_INITIALIZE_MATRICES_BY_NAN)
7375
endif(INITIALIZE_EIGEN_WITH_NAN)
76+
if(CHECK_RUNTIME_MALLOC)
77+
message(STATUS "Check if some memory allocations are performed at runtime.")
78+
add_definitions(-DPROXSUITE_EIGEN_CHECK_MALLOC)
79+
add_definitions(-DEIGEN_RUNTIME_NO_MALLOC)
80+
endif(CHECK_RUNTIME_MALLOC)
7481

7582
# set CXX standard
7683
if(DEFINED CMAKE_CXX_STANDARD)
@@ -136,25 +143,6 @@ install(
136143
ARCHIVE DESTINATION ${CMAKE_INSTALL_FULL_LIBDIR}
137144
RUNTIME DESTINATION ${CMAKE_INSTALL_FULL_BINDIR})
138145

139-
# Activate only for advanced users to profile and benchmark the code
140-
if(ADVANCED_USERS)
141-
include(cmake/compiler_warnings.cmake)
142-
include(cmake/extra_local_settings.cmake)
143-
include(cmake/static_analyzers.cmake)
144-
include(cmake/sanitizers.cmake)
145-
146-
target_compile_definitions(proxsuite INTERFACE EIGEN_RUNTIME_NO_MALLOC)
147-
target_compile_definitions(proxsuite
148-
INTERFACE EIGEN_INITIALIZE_MATRICES_BY_NAN)
149-
add_library(project_warnings INTERFACE)
150-
add_library(project_options INTERFACE)
151-
152-
target_link_libraries(proxsuite INTERFACE project_options project_warnings)
153-
154-
set_project_warnings(project_warnings)
155-
enable_sanitizers(project_options)
156-
endif()
157-
158146
add_subdirectory(bindings)
159147
if(BUILD_TESTING)
160148
add_subdirectory(test)

include/proxsuite/fwd.hpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,33 @@
2020
#define PROXSUITE_MAYBE_UNUSED __attribute__((__unused__))
2121
#endif
2222

23+
// Same logic as in Pinocchio to check eigen malloc
24+
#ifdef PROXSUITE_EIGEN_CHECK_MALLOC
25+
#ifndef EIGEN_RUNTIME_NO_MALLOC
26+
#define EIGEN_RUNTIME_NO_MALLOC_WAS_NOT_DEFINED
27+
#define EIGEN_RUNTIME_NO_MALLOC
28+
#endif
29+
#endif
30+
31+
#include <Eigen/Core>
32+
33+
#ifdef PROXSUITE_EIGEN_CHECK_MALLOC
34+
#ifdef EIGEN_RUNTIME_NO_MALLOC_WAS_NOT_DEFINED
35+
#undef EIGEN_RUNTIME_NO_MALLOC
36+
#undef EIGEN_RUNTIME_NO_MALLOC_WAS_NOT_DEFINED
37+
#endif
38+
#endif
39+
40+
// Check memory allocation for Eigen
41+
#ifdef PROXSUITE_EIGEN_CHECK_MALLOC
42+
#define PROXSUITE_EIGEN_MALLOC(allowed) \
43+
::Eigen::internal::set_is_malloc_allowed(allowed)
44+
#define PROXSUITE_EIGEN_MALLOC_ALLOWED() PROXSUITE_EIGEN_MALLOC(true)
45+
#define PROXSUITE_EIGEN_MALLOC_NOT_ALLOWED() PROXSUITE_EIGEN_MALLOC(false)
46+
#else
47+
#define PROXSUITE_EIGEN_MALLOC(allowed)
48+
#define PROXSUITE_EIGEN_MALLOC_ALLOWED()
49+
#define PROXSUITE_EIGEN_MALLOC_NOT_ALLOWED()
50+
#endif
51+
2352
#endif // #ifndef __proxsuite_fwd_hpp__

include/proxsuite/proxqp/dense/solver.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#ifndef PROXSUITE_PROXQP_DENSE_SOLVER_HPP
99
#define PROXSUITE_PROXQP_DENSE_SOLVER_HPP
1010

11+
#include "proxsuite/fwd.hpp"
1112
#include "proxsuite/proxqp/dense/views.hpp"
1213
#include "proxsuite/proxqp/dense/linesearch.hpp"
1314
#include "proxsuite/proxqp/dense/helpers.hpp"
@@ -765,7 +766,8 @@ qp_solve( //
765766
RowMat test(2,2); // test it is full of nan for debug
766767
std::cout << "test " << test << std::endl;
767768
*/
768-
//::Eigen::internal::set_is_malloc_allowed(false);
769+
PROXSUITE_EIGEN_MALLOC_NOT_ALLOWED();
770+
769771
if (qpsettings.compute_timings) {
770772
qpwork.timer.stop();
771773
qpwork.timer.start();
@@ -1291,6 +1293,7 @@ qp_solve( //
12911293
<< std::endl;
12921294
}
12931295
qpwork.dirty = true;
1296+
PROXSUITE_EIGEN_MALLOC_ALLOWED();
12941297
}
12951298

12961299
} // namespace dense

0 commit comments

Comments
 (0)