Skip to content

Commit

Permalink
🐛 Fix cuda link errors in test
Browse files Browse the repository at this point in the history
  • Loading branch information
yhs0602 committed Jan 2, 2025
1 parent 816e69b commit bf73a9a
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 17 deletions.
32 changes: 20 additions & 12 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -82,31 +82,39 @@ install(TARGETS craftground_native LIBRARY DESTINATION craftground)
option(BUILD_TESTS "Build tests" ON)
if(BUILD_TESTS)
enable_testing()
pybind11_add_module(craftground STATIC ${CRAFTGROUND_PY_SOURCES})
add_library(craftground STATIC ${CRAFTGROUND_PY_SOURCES})
find_package(Python3 REQUIRED COMPONENTS Development)
target_include_directories(
craftground PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/pybind11/include
${Python3_INCLUDE_DIRS}
)
if(APPLE)
if(Torch_FOUND)
target_link_libraries(craftground PRIVATE "${TORCH_LIBRARIES}")
target_link_libraries(craftground PUBLIC "${TORCH_LIBRARIES}")
set(LIBTORCH_DIR ${CMAKE_SOURCE_DIR}/src/cpp/libtorch)
target_link_libraries(craftground PRIVATE
target_link_libraries(craftground PUBLIC
${LIBTORCH_DIR}/libtorch_cpu_minimal.a
${LIBTORCH_DIR}/libc10.a
)
target_include_directories(craftground PRIVATE "${TORCH_INCLUDE_DIRS}")
target_compile_definitions(craftground PRIVATE HAS_TORCH)
target_compile_options(craftground PRIVATE -ffunction-sections -fdata-sections)
target_link_options(craftground PRIVATE -Wl,-dead_strip)
target_include_directories(craftground PUBLIC "${TORCH_INCLUDE_DIRS}")
target_compile_definitions(craftground PUBLIC HAS_TORCH)
target_compile_options(craftground PUBLIC -ffunction-sections -fdata-sections)
# target_link_options(craftground PUBLIC -Wl,-dead_strip)
endif()
elseif(CUDAToolkit_FOUND)
target_include_directories(craftground PRIVATE ${CUDAToolkit_INCLUDE_DIRS})
target_link_libraries(craftground PRIVATE ${CUDAToolkit_LIBRARIES})
message(STATUS "CUDA is available in tests")
target_include_directories(craftground PUBLIC ${CUDAToolkit_INCLUDE_DIRS})
target_link_libraries(craftground PUBLIC CUDA::cudart CUDA::cudart_static)
endif()
target_include_directories(craftground PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp)
target_link_options(craftground PRIVATE "-Wl,--whole-archive" "-Wl,--no-whole-archive")
target_include_directories(craftground PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp)

target_compile_options(craftground PRIVATE ${CRAFTGROUND_PY_COMPILE_OPTIONS})
target_compile_options(craftground PUBLIC ${CRAFTGROUND_PY_COMPILE_OPTIONS})

target_compile_definitions(
craftground
PRIVATE VERSION_INFO=${PRIVATE_VERSION_INFO}
PUBLIC VERSION_INFO=${PRIVATE_VERSION_INFO}
)
add_subdirectory(tests)
endif()
27 changes: 25 additions & 2 deletions tests/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,33 @@
find_package(GTest REQUIRED)
include_directories(${GTEST_INCLUDE_DIRS})

find_package(CUDAToolkit REQUIRED)

find_package(Python3 REQUIRED COMPONENTS Development)

# Build Test directory
add_executable(test_ipc test_ipc.cpp)
target_include_directories(test_ipc PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../../src/cpp)
target_link_libraries(test_ipc PRIVATE craftground GTest::GTest GTest::Main)

target_include_directories(
test_ipc PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/../../src/cpp
${CMAKE_CURRENT_SOURCE_DIR}/../../pybind11/include
${Python3_INCLUDE_DIRS}
)
target_include_directories(test_ipc PRIVATE ${CUDAToolkit_INCLUDE_DIRS})
message(STATUS "CUDA is available in tests")
message(STATUS "CUDa toolkit libraries: ${CUDAToolkit_LIBRARY_DIR}")
target_link_libraries(
test_ipc PRIVATE
GTest::GTest
GTest::Main
Python3::Python
CUDA::cudart
CUDA::cudart_static
craftground
)
target_compile_options(test_ipc PRIVATE -D_GLIBCXX_USE_CXX11_ABI=1)


# Integrate with CTest
add_test(NAME CppTests COMMAND test_ipc)
Expand Down
8 changes: 5 additions & 3 deletions tests/cpp/test_ipc.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#include <gtest/gtest.h>
#include <pybind11/pybind11.h>
#include "ipc.h"

TEST(IPCModuleTest, AddFunction) {
EXPECT_EQ(add(2, 3), 5);
EXPECT_EQ(initialize_from_mach_port(2, 3, 4), py::none());
}

TEST(IPCModuleTest, ExceptionHandling) {
EXPECT_THROW(safe_divide(1, 0), std::invalid_argument);
EXPECT_THROW(
mtl_tensor_from_cuda_mem_handle("", 0, 2), std::invalid_argument
);
}

0 comments on commit bf73a9a

Please sign in to comment.