Skip to content

Commit e717a5b

Browse files
committed
Add vcpkg mkl
1 parent ef12d6c commit e717a5b

File tree

8 files changed

+70
-6
lines changed

8 files changed

+70
-6
lines changed

.github/workflows/vcpkg.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@ jobs:
2525
test_target: RUN_TESTS
2626
binary_cache: C:\Users\runneradmin\AppData\Local\vcpkg\archives
2727
python: python
28+
additional_vcpkg_ports: intel-mkl
2829
- os: ubuntu-latest
2930
triplet: x64-linux-release
3031
build_type: Release
3132
test_target: test
3233
binary_cache: /home/runner/.cache/vcpkg/archives
3334
python: python3
35+
additional_vcpkg_ports: intel-mkl
3436
- os: macos-latest
3537
triplet: arm64-osx-release
3638
build_type: Release
@@ -112,6 +114,7 @@ jobs:
112114
pybind11
113115
geographiclib
114116
eigen3
117+
${{ matrix.additional_vcpkg_ports }}
115118
116119
- name: On Failure, upload vcpkg logs
117120
if: failure()
@@ -178,6 +181,8 @@ jobs:
178181
-DGTSAM_USE_SYSTEM_PYBIND=ON \
179182
-DGTSAM_ENABLE_GEOGRAPHICLIB=ON \
180183
-DGTSAM_SUPPORT_NESTED_DISSECTION=ON \
184+
-DGTSAM_WITH_EIGEN_MKL=ON \
185+
-DGTSAM_WITH_EIGEN_MKL_OPENMP=ON \
181186
-DCTEST_EXTRA_ARGS='${{ matrix.ctest_extra_flags }}' \
182187
-DPYTEST_EXTRA_ARGS='${{ matrix.pytest_extra_flags }}'
183188

cmake/FindMKL.cmake

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,18 @@
2121
# OPEN - Open MPI library
2222
# SGI - SGI MPT Library
2323

24+
# vcpkg
25+
if(DEFINED VCPKG_INSTALLED_DIR)
26+
find_package(MKL CONFIG)
27+
if(MKL_FOUND)
28+
add_library(mkl-gtsam-if INTERFACE)
29+
target_link_libraries(mkl-gtsam-if INTERFACE MKL::MKL)
30+
set(MKL_LIBRARIES mkl-gtsam-if)
31+
list(APPEND GTSAM_EXPORTED_TARGETS mkl-gtsam-if)
32+
install(TARGETS mkl-gtsam-if EXPORT GTSAM-exports ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
33+
endif()
34+
else()
35+
2436
# linux
2537
IF(UNIX AND NOT APPLE)
2638
IF(${CMAKE_HOST_SYSTEM_PROCESSOR} STREQUAL "x86_64")
@@ -267,4 +279,6 @@ find_package_handle_standard_args(MKL DEFAULT_MSG MKL_INCLUDE_DIR MKL_LIBRARIES)
267279
# LINK_DIRECTORIES(${MKL_ROOT_DIR}/lib/${MKL_ARCH_DIR}) # hack
268280
#endif()
269281

282+
endif() # end of vcpkg
283+
270284
MARK_AS_ADVANCED(MKL_INCLUDE_DIR MKL_LIBRARIES)

cmake/HandleMKL.cmake

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,28 @@ else()
1515
set(GTSAM_USE_EIGEN_MKL 0)
1616
set(EIGEN_USE_MKL_ALL 0)
1717
endif()
18+
19+
if(WIN32 AND GTSAM_USE_EIGEN_MKL AND DEFINED VCPKG_INSTALLED_DIR)
20+
get_target_property(MKL_TARGETS "MKL::MKL" INTERFACE_LINK_LIBRARIES)
21+
set(RUNTIME_DLL_DIRS "")
22+
foreach(MKL_TARGET ${MKL_TARGETS})
23+
if(TARGET ${MKL_TARGET})
24+
get_target_property(MKL_DLL "${MKL_TARGET}" IMPORTED_LOCATION)
25+
if(MKL_DLL)
26+
cmake_path(GET MKL_DLL PARENT_PATH MKL_DLL_DIR)
27+
list (APPEND RUNTIME_DLL_DIRS "${MKL_DLL_DIR}/mkl_*.dll")
28+
endif()
29+
endif()
30+
endforeach()
31+
list(REMOVE_DUPLICATES RUNTIME_DLL_DIRS)
32+
file(GLOB MKL_DLLS CONFIGURE_DEPENDS ${RUNTIME_DLL_DIRS})
33+
list(REMOVE_DUPLICATES MKL_DLLS)
34+
add_custom_target(copy_mkl_dlls
35+
COMMAND ${CMAKE_COMMAND} -E copy_if_different
36+
"${MKL_DLLS}"
37+
"${CMAKE_RUNTIME_OUTPUT_DIRECTORY}"
38+
COMMAND_EXPAND_LISTS
39+
VERBATIM
40+
)
41+
add_dependencies(check copy_mkl_dlls)
42+
endif()

gtsam/linear/HessianFactor.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,8 +339,8 @@ double HessianFactor::error(const VectorValues& c) const {
339339
// NOTE may not be as efficient
340340
const Vector x = c.vector(keys());
341341
const double xtg = x.dot(linearTerm().col(0));
342-
auto AtA = informationView();
343-
const double xGx = x.transpose() * AtA * x;
342+
const auto AtA = informationView();
343+
const double xGx = x.dot(AtA * x);
344344
return 0.5 * (f - 2.0 * xtg + xGx);
345345
}
346346

gtsam/linear/tests/testHessianFactor.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ TEST(HessianFactor, Constructor1)
108108
// error 0.5*(f - 2*x'*g + x'*G*x)
109109
double expected = 80.375;
110110
double actual = factor.error(dx);
111-
double expected_manual = 0.5 * (f - 2.0 * dx[0].dot(g) + dx[0].transpose() * G.selfadjointView<Eigen::Upper>() * dx[0]);
111+
const double xGx = dx[0].dot(G * dx[0]);
112+
double expected_manual = 0.5 * (f - 2.0 * dx[0].dot(g) + xGx);
112113
EXPECT_DOUBLES_EQUAL(expected, expected_manual, 1e-10);
113114
EXPECT_DOUBLES_EQUAL(expected, actual, 1e-10);
114115
}

gtsam/nonlinear/tests/testLinearContainerFactor.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,8 @@ TEST(TestLinearContainerFactor, hessian_factor_withlinpoints) {
219219
// Check linearization with corrections for updated linearization point
220220
Vector g1_prime = g_prime.head(3);
221221
Vector g2_prime = g_prime.tail(2);
222-
double f_prime = f + dv.transpose() * G.selfadjointView<Eigen::Upper>() * dv - 2.0 * dv.transpose() * g;
222+
const auto Gsym = G.selfadjointView<Eigen::Upper>();
223+
double f_prime = f + dv.dot(Gsym * dv) - 2.0 * dv.dot(g);
223224
HessianFactor expNewFactor(x1, l1, G11, G12, g1_prime, G22, g2_prime, f_prime);
224225
EXPECT(assert_equal(*expNewFactor.clone(), *actFactor.linearize(noisyValues), tol));
225226
}

gtsam_unstable/nonlinear/LinearizedFactor.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ double LinearizedHessianFactor::error(const Values& c) const {
194194
// error 0.5*(f - 2*x'*g + x'*G*x)
195195
double f = constantTerm();
196196
double xtg = dx.dot(linearTerm());
197-
double xGx = dx.transpose() * squaredTerm() * dx;
197+
double xGx = dx.dot(squaredTerm() * dx);
198198

199199
return 0.5 * (f - 2.0 * xtg + xGx);
200200
}
@@ -216,7 +216,7 @@ LinearizedHessianFactor::linearize(const Values& c) const {
216216

217217
// f2 = f1 - 2*dx'*g1 + dx'*G1*dx
218218
//newInfo(this->size(), this->size())(0,0) += -2*dx.dot(linearTerm()) + dx.transpose() * squaredTerm().selfadjointView<Eigen::Upper>() * dx;
219-
double f = constantTerm() - 2*dx.dot(linearTerm()) + dx.transpose() * squaredTerm() * dx;
219+
double f = constantTerm() - 2*dx.dot(linearTerm()) + dx.dot(squaredTerm() * dx);
220220

221221
// g2 = g1 - G1*dx
222222
//newInfo.rangeColumn(0, this->size(), this->size(), 0) -= squaredTerm().selfadjointView<Eigen::Upper>() * dx;

python/CMakeLists.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,15 @@ if(WIN32)
149149
COMMAND_EXPAND_LISTS
150150
VERBATIM
151151
)
152+
if(GTSAM_USE_EIGEN_MKL AND DEFINED VCPKG_INSTALLED_DIR)
153+
ADD_CUSTOM_COMMAND(TARGET ${GTSAM_PYTHON_TARGET} POST_BUILD
154+
COMMAND ${CMAKE_COMMAND} -E copy_if_different
155+
"${MKL_DLLS}"
156+
"${GTSAM_PYTHON_BUILD_DIRECTORY}/gtsam/"
157+
COMMAND_EXPAND_LISTS
158+
VERBATIM
159+
)
160+
endif()
152161
endif()
153162

154163
# Set the path for the GTSAM python module
@@ -263,6 +272,15 @@ if(GTSAM_UNSTABLE_BUILD_PYTHON)
263272
COMMAND_EXPAND_LISTS
264273
VERBATIM
265274
)
275+
if(GTSAM_USE_EIGEN_MKL AND DEFINED VCPKG_INSTALLED_DIR)
276+
ADD_CUSTOM_COMMAND(TARGET ${GTSAM_PYTHON_UNSTABLE_TARGET} POST_BUILD
277+
COMMAND ${CMAKE_COMMAND} -E copy_if_different
278+
"${MKL_DLLS}"
279+
"${GTSAM_PYTHON_BUILD_DIRECTORY}/gtsam_unstable/"
280+
COMMAND_EXPAND_LISTS
281+
VERBATIM
282+
)
283+
endif()
266284
endif()
267285

268286
add_custom_target(

0 commit comments

Comments
 (0)