@@ -30,7 +30,7 @@ option(CDDP-CPP_BUILD_TESTS "Whether to build tests." ON)
30
30
option (CDDP-CPP_GUROBI "Whether to use Gurobi solver." OFF )
31
31
option (GUROBI_ROOT "Path to Gurobi installation" "" )
32
32
set (GUROBI_ROOT /home/tom/.local/lib/gurobi1103/linux64 )
33
- option (CDDP-CPP_TORCH "Whether to use LibTorch." ON )
33
+ set (CDDP-CPP_TORCH "Whether to use LibTorch." ON ) # cannot be turned off
34
34
option (CDDP-CPP_TORCH_GPU "Whether to use GPU." ON )
35
35
36
36
# Find packages
@@ -66,6 +66,55 @@ if (CDDP-CPP_BUILD_TESTS)
66
66
include (GoogleTest )
67
67
endif ()
68
68
69
+ # LibTorch
70
+ if (CDDP-CPP_TORCH )
71
+ if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR} /build/libtorch/share/cmake/Torch/TorchConfig.cmake )
72
+ message (STATUS "Found LibTorch at ${CMAKE_CURRENT_SOURCE_DIR} /build/libtorch" )
73
+ else ()
74
+ message (STATUS "Downloading LibTorch..." )
75
+ # Download and extract LibTorch
76
+ if (CDDP-CPP_TORCH_GPU )
77
+ set (LIBTORCH_URL "https://download.pytorch.org/libtorch/cu124/libtorch-cxx11-abi-shared-with-deps-2.5.1%2Bcu124.zip" )
78
+ else ()
79
+ set (LIBTORCH_URL "https://download.pytorch.org/libtorch/cpu/libtorch-cxx11-abi-shared-with-deps-2.5.1%2Bcpu.zip" )
80
+ endif ()
81
+
82
+ # Set the download directory
83
+ set (DOWLOAD_DIR ${CMAKE_CURRENT_SOURCE_DIR} /build )
84
+
85
+ # Create the download directory
86
+ file (MAKE_DIRECTORY ${DOWLOAD_DIR} )
87
+
88
+ # Download the file
89
+ file (DOWNLOAD ${LIBTORCH_URL} ${DOWLOAD_DIR} /libtorch-shared-with-deps-latest.zip
90
+ STATUS DOWNLOAD_STATUS
91
+ SHOW_PROGRESS
92
+ )
93
+
94
+ # Check if the download was successful
95
+ list (GET DOWNLOAD_STATUS 0 DOWNLOAD_STATUS_CODE )
96
+ if (NOT DOWNLOAD_STATUS_CODE EQUAL 0 )
97
+ message (FATAL_ERROR "Failed to download LibTorch." )
98
+ endif ()
99
+
100
+ # Extract the file
101
+ execute_process (
102
+ COMMAND ${CMAKE_COMMAND} -E tar xvf ${DOWLOAD_DIR} /libtorch-shared-with-deps-latest.zip
103
+ WORKING_DIRECTORY ${DOWLOAD_DIR}
104
+ )
105
+
106
+ # Remove the zip file
107
+ file (REMOVE ${DOWLOAD_DIR} /libtorch-shared-with-deps-latest.zip )
108
+ endif ()
109
+
110
+ # Set the path to the LibTorch installation
111
+ set (LIBTORCH_DIR ${CMAKE_CURRENT_SOURCE_DIR} /build/libtorch )
112
+
113
+ find_package (Torch REQUIRED PATHS ${LIBTORCH_DIR} NO_DEFAULT_PATH )
114
+ message (STATUS "Found LibTorch: ${TORCH_LIBRARIES} " )
115
+ endif ()
116
+
117
+
69
118
# Include directories
70
119
include_directories (
71
120
${CMAKE_CURRENT_SOURCE_DIR} /include
@@ -79,6 +128,7 @@ set(cddp_core_srcs
79
128
src/cddp_core/objective.cpp
80
129
src/cddp_core/constraint.cpp
81
130
src/cddp_core/cddp_core.cpp
131
+ src/cddp_core/torch_dynamical_system.cpp
82
132
)
83
133
84
134
set (dynamics_model_srcs
@@ -102,11 +152,13 @@ target_link_libraries(${PROJECT_NAME}
102
152
Python3::Python
103
153
Python3::Module
104
154
Python3::NumPy
155
+ ${TORCH_LIBRARIES}
105
156
)
106
157
107
158
target_include_directories (${PROJECT_NAME} PUBLIC
108
159
$< BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR} /include/cddp-cpp>
109
160
$< INSTALL_INTERFACE:include>
161
+ ${TORCH_INCLUDE_DIRS}
110
162
)
111
163
112
164
# Gurobi
@@ -132,57 +184,6 @@ if (CDDP-CPP_GUROBI)
132
184
endif ()
133
185
endif ()
134
186
135
- # LibTorch
136
- if (CDDP-CPP_TORCH )
137
- if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR} /build/libtorch/share/cmake/Torch/TorchConfig.cmake )
138
- message (STATUS "Found LibTorch at ${CMAKE_CURRENT_SOURCE_DIR} /build/libtorch" )
139
- else ()
140
- message (STATUS "Downloading LibTorch..." )
141
- # Download and extract LibTorch
142
- if (CDDP-CPP_TORCH_GPU )
143
- set (LIBTORCH_URL "https://download.pytorch.org/libtorch/cu124/libtorch-cxx11-abi-shared-with-deps-2.5.1%2Bcu124.zip" )
144
- else ()
145
- set (LIBTORCH_URL "https://download.pytorch.org/libtorch/cpu/libtorch-cxx11-abi-shared-with-deps-2.5.1%2Bcpu.zip" )
146
- endif ()
147
-
148
- # Set the download directory
149
- set (DOWLOAD_DIR ${CMAKE_CURRENT_SOURCE_DIR} /build )
150
-
151
- # Create the download directory
152
- file (MAKE_DIRECTORY ${DOWLOAD_DIR} )
153
-
154
- # Download the file
155
- file (DOWNLOAD ${LIBTORCH_URL} ${DOWLOAD_DIR} /libtorch-shared-with-deps-latest.zip
156
- STATUS DOWNLOAD_STATUS
157
- SHOW_PROGRESS
158
- )
159
-
160
- # Check if the download was successful
161
- list (GET DOWNLOAD_STATUS 0 DOWNLOAD_STATUS_CODE )
162
- if (NOT DOWNLOAD_STATUS_CODE EQUAL 0 )
163
- message (FATAL_ERROR "Failed to download LibTorch." )
164
- endif ()
165
-
166
- # Extract the file
167
- execute_process (
168
- COMMAND ${CMAKE_COMMAND} -E tar xvf ${DOWLOAD_DIR} /libtorch-shared-with-deps-latest.zip
169
- WORKING_DIRECTORY ${DOWLOAD_DIR}
170
- )
171
-
172
- # Remove the zip file
173
- file (REMOVE ${DOWLOAD_DIR} /libtorch-shared-with-deps-latest.zip )
174
- endif ()
175
-
176
- # Set the path to the LibTorch installation
177
- set (LIBTORCH_DIR ${CMAKE_CURRENT_SOURCE_DIR} /build/libtorch )
178
-
179
- find_package (Torch REQUIRED PATHS ${LIBTORCH_DIR} NO_DEFAULT_PATH )
180
- target_link_libraries (${PROJECT_NAME} ${TORCH_LIBRARIES} )
181
- target_include_directories (${PROJECT_NAME} PUBLIC ${TORCH_INCLUDE_DIRS} )
182
- message (STATUS "Found LibTorch: ${TORCH_LIBRARIES} " )
183
- endif ()
184
-
185
-
186
187
# Build and register tests.
187
188
if (CDDP-CPP_BUILD_TESTS )
188
189
add_subdirectory (tests )
0 commit comments