Skip to content

Commit 1e08692

Browse files
Disable LibTorch by default and conditionally include related sources and libraries (#68)
* Disable LibTorch by default and conditionally include related sources and libraries * Merge remote-tracking branch 'origin/master' into torch_cmake_issue
1 parent 14d44a1 commit 1e08692

File tree

3 files changed

+32
-19
lines changed

3 files changed

+32
-19
lines changed

CMakeLists.txt

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ option(GUROBI_ROOT "Path to Gurobi installation" "")
4040
set(GUROBI_ROOT $ENV{HOME}/.local/lib/gurobi1103/linux64)
4141

4242
# LibTorch Configuration
43-
set(CDDP_CPP_TORCH "Whether to use LibTorch" ON)
44-
option(CDDP_CPP_TORCH_GPU "Whether to use GPU support in LibTorch" ON)
43+
option(CDDP_CPP_TORCH "Whether to use LibTorch" OFF)
44+
option(CDDP_CPP_TORCH_GPU "Whether to use GPU support in LibTorch" OFF)
4545
set(LIBTORCH_DIR $ENV{HOME}/.local/lib/libtorch CACHE PATH "Path to local LibTorch installation") # FIXME: Change this to your local LibTorch installation directory
4646

4747
# Python Configuration
@@ -177,6 +177,8 @@ if (CDDP_CPP_TORCH)
177177

178178
# Export LibTorch variables for other parts of the build
179179
set(TORCH_INSTALL_PREFIX ${Torch_DIR}/../../../ CACHE PATH "LibTorch installation directory")
180+
181+
target_compile_definitions(${PROJECT_NAME} PRIVATE CDDP_CPP_TORCH_ENABLED=1)
180182
endif()
181183

182184

@@ -200,9 +202,12 @@ set(cddp_core_srcs
200202
src/cddp_core/clddp_core.cpp
201203
src/cddp_core/asddp_core.cpp
202204
src/cddp_core/logddp_core.cpp
203-
src/cddp_core/neural_dynamical_system.cpp
204205
)
205206

207+
if (CDDP_CPP_TORCH)
208+
list(APPEND cddp_core_srcs src/cddp_core/torch_helper.cpp)
209+
endif()
210+
206211
set(dynamics_model_srcs
207212
src/dynamics_model/pendulum.cpp
208213
src/dynamics_model/dubins_car.cpp
@@ -228,17 +233,20 @@ target_link_libraries(${PROJECT_NAME}
228233
Python3::Python
229234
Python3::Module
230235
Python3::NumPy
231-
${TORCH_LIBRARIES}
232236
)
233237

238+
if (CDDP_CPP_TORCH)
239+
target_link_libraries(${PROJECT_NAME} ${TORCH_LIBRARIES})
240+
endif()
241+
234242
target_include_directories(${PROJECT_NAME} PUBLIC
235243
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include/cddp-cpp>
236244
$<INSTALL_INTERFACE:include>
237245
${TORCH_INCLUDE_DIRS}
238246
)
239247

240248
# Ensure proper CUDA support if enabled
241-
if(CDDP_CPP_TORCH_GPU)
249+
if(TORCH_FOUND AND CDDP_CPP_TORCH_GPU)
242250
set_property(TARGET ${PROJECT_NAME} PROPERTY CUDA_ARCHITECTURES native)
243251
endif()
244252

examples/CMakeLists.txt

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,23 +44,25 @@ if (CDDP_CPP_CASADI)
4444
endif()
4545

4646
# Neural dynamics examples
47-
add_executable(prepare_pendulum neural_dynamics/prepare_pendulum.cpp)
48-
target_link_libraries(prepare_pendulum cddp)
47+
if (CDDP_CPP_TORCH)
48+
add_executable(prepare_pendulum neural_dynamics/prepare_pendulum.cpp)
49+
target_link_libraries(prepare_pendulum cddp)
4950

50-
# add_executable(prepare_cartpole neural_dynamics/prepare_cartpole.cpp)
51-
# target_link_libraries(prepare_cartpole cddp)
51+
# add_executable(prepare_cartpole neural_dynamics/prepare_cartpole.cpp)
52+
# target_link_libraries(prepare_cartpole cddp)
5253

53-
add_executable(train_pendulum neural_dynamics/train_pendulum.cpp)
54-
target_link_libraries(train_pendulum cddp)
54+
add_executable(train_pendulum neural_dynamics/train_pendulum.cpp)
55+
target_link_libraries(train_pendulum cddp)
5556

56-
# add_executable(train_cartpole neural_dynamics/train_cartpole.cpp)
57-
# target_link_libraries(train_cartpole cddp)
57+
# add_executable(train_cartpole neural_dynamics/train_cartpole.cpp)
58+
# target_link_libraries(train_cartpole cddp)
5859

59-
add_executable(run_pendulum neural_dynamics/run_pendulum.cpp)
60-
target_link_libraries(run_pendulum cddp)
60+
add_executable(run_pendulum neural_dynamics/run_pendulum.cpp)
61+
target_link_libraries(run_pendulum cddp)
6162

62-
# add_executable(run_cartpole neural_dynamics/run_cartpole.cpp)
63-
# target_link_libraries(run_cartpole cddp)
63+
# add_executable(run_cartpole neural_dynamics/run_cartpole.cpp)
64+
# target_link_libraries(run_cartpole cddp)
6465

65-
# add_executable(cddp_pendulum_neural _cddp_pendulum_neural.cpp)
66-
# target_link_libraries(cddp_pendulum_neural cddp)
66+
# add_executable(cddp_pendulum_neural _cddp_pendulum_neural.cpp)
67+
# target_link_libraries(cddp_pendulum_neural cddp)
68+
endif()

include/cddp-cpp/cddp.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@
3030
#include "cddp_core/helper.hpp"
3131
#include "cddp_core/boxqp.hpp"
3232
#include "cddp_core/qp_solver.hpp"
33+
34+
#ifdef CDDP_CPP_TORCH_ENABLED
3335
#include "cddp_core/neural_dynamical_system.hpp"
36+
#endif
3437

3538
// Models
3639
#include "dynamics_model/pendulum.hpp"

0 commit comments

Comments
 (0)