Skip to content

Commit 28133ff

Browse files
committed
cmake build [skip ci]
1 parent 2a62830 commit 28133ff

File tree

4 files changed

+156
-0
lines changed

4 files changed

+156
-0
lines changed

CMakeLists.txt

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
cmake_minimum_required(VERSION 3.0)
2+
project(torchcluster)
3+
set(CMAKE_CXX_STANDARD 14)
4+
set(TORCHCLUSTER_VERSION 1.5.4)
5+
6+
option(WITH_CUDA "Enable CUDA support" OFF)
7+
8+
if(WITH_CUDA)
9+
enable_language(CUDA)
10+
add_definitions(-D__CUDA_NO_HALF_OPERATORS__)
11+
endif()
12+
13+
find_package(Python3 COMPONENTS Development)
14+
find_package(Torch REQUIRED)
15+
16+
file(GLOB HEADERS csrc/cluster.h)
17+
file(GLOB OPERATOR_SOURCES csrc/cpu/*.h csrc/cpu/*.cpp csrc/*.cpp)
18+
if(WITH_CUDA)
19+
file(GLOB OPERATOR_SOURCES ${OPERATOR_SOURCES} csrc/cuda/*.h csrc/cuda/*.cu)
20+
endif()
21+
22+
add_library(${PROJECT_NAME} SHARED ${OPERATOR_SOURCES})
23+
target_link_libraries(${PROJECT_NAME} PRIVATE ${TORCH_LIBRARIES} Python3::Python)
24+
set_target_properties(${PROJECT_NAME} PROPERTIES EXPORT_NAME TorchCluster)
25+
26+
target_include_directories(${PROJECT_NAME} INTERFACE
27+
$<BUILD_INTERFACE:${HEADERS}>
28+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
29+
30+
include(GNUInstallDirs)
31+
include(CMakePackageConfigHelpers)
32+
33+
set(TORCHCLUSTER_CMAKECONFIG_INSTALL_DIR "share/cmake/TorchCluster" CACHE STRING "install path for TorchClusterConfig.cmake")
34+
35+
configure_package_config_file(cmake/TorchClusterConfig.cmake.in
36+
"${CMAKE_CURRENT_BINARY_DIR}/TorchClusterConfig.cmake"
37+
INSTALL_DESTINATION ${TORCHCLUSTER_CMAKECONFIG_INSTALL_DIR})
38+
39+
write_basic_package_version_file(${CMAKE_CURRENT_BINARY_DIR}/TorchClusterConfigVersion.cmake
40+
VERSION ${TORCHCLUSTER_VERSION}
41+
COMPATIBILITY AnyNewerVersion)
42+
43+
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/TorchClusterConfig.cmake
44+
${CMAKE_CURRENT_BINARY_DIR}/TorchClusterConfigVersion.cmake
45+
DESTINATION ${TORCHCLUSTER_CMAKECONFIG_INSTALL_DIR})
46+
47+
install(TARGETS ${PROJECT_NAME}
48+
EXPORT TorchClusterTargets
49+
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
50+
)
51+
52+
install(EXPORT TorchClusterTargets
53+
NAMESPACE TorchCluster::
54+
DESTINATION ${TORCHCLUSTER_CMAKECONFIG_INSTALL_DIR})
55+
56+
install(FILES ${HEADERS} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME})
57+
install(FILES
58+
csrc/cpu/fps_cpu.h
59+
csrc/cpu/graclus_cpu.h
60+
csrc/cpu/grid_cpu.h
61+
csrc/cpu/rw_cpu.h
62+
csrc/cpu/sampler_cpu.h
63+
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}/cpu)
64+
if(WITH_CUDA)
65+
install(FILES
66+
csrc/cuda/fps_cuda.h
67+
csrc/cuda/graclus_cuda.h
68+
csrc/cuda/grid_cuda.h
69+
csrc/cuda/knn_cuda.h
70+
csrc/cuda/nearest_cuda.h
71+
csrc/cuda/radius_cuda.h
72+
csrc/cuda/rw_cuda.h
73+
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}/cuda)
74+
endif()

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,3 +241,16 @@ tensor([[0, 1, 2, 4],
241241
```
242242
python setup.py test
243243
```
244+
245+
## C++ API
246+
247+
`torch-cluster` also offers a C++ API that contains C++ equivalent of python models.
248+
249+
```
250+
mkdir build
251+
cd build
252+
# Add -DWITH_CUDA=on support for the CUDA if needed
253+
cmake ..
254+
make
255+
make install
256+
```

cmake/TorchClusterConfig.cmake.in

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# TorchClusterConfig.cmake
2+
# --------------------
3+
#
4+
# Exported targets:: Cluster
5+
#
6+
7+
@PACKAGE_INIT@
8+
9+
set(PN TorchCluster)
10+
set(${PN}_INCLUDE_DIR "${PACKAGE_PREFIX_DIR}/@CMAKE_INSTALL_INCLUDEDIR@")
11+
set(${PN}_LIBRARY "")
12+
set(${PN}_DEFINITIONS USING_${PN})
13+
14+
check_required_components(${PN})
15+
16+
17+
if(NOT (CMAKE_VERSION VERSION_LESS 3.0))
18+
#-----------------------------------------------------------------------------
19+
# Don't include targets if this file is being picked up by another
20+
# project which has already built this as a subproject
21+
#-----------------------------------------------------------------------------
22+
if(NOT TARGET ${PN}::TorchCluster)
23+
include("${CMAKE_CURRENT_LIST_DIR}/${PN}Targets.cmake")
24+
25+
if(NOT TARGET torch_library)
26+
find_package(Torch REQUIRED)
27+
endif()
28+
if(NOT TARGET Python3::Python)
29+
find_package(Python3 COMPONENTS Development)
30+
endif()
31+
target_link_libraries(TorchCluster::TorchCluster INTERFACE ${TORCH_LIBRARIES} Python3::Python)
32+
33+
if(@WITH_CUDA@)
34+
target_compile_definitions(TorchCluster::TorchCluster INTERFACE WITH_CUDA)
35+
endif()
36+
37+
endif()
38+
endif()

csrc/cluster.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#pragma once
2+
3+
#include <torch/extension.h>
4+
5+
int64_t cuda_version();
6+
7+
torch::Tensor fps(torch::Tensor src, torch::Tensor ptr, double ratio,
8+
bool random_start);
9+
10+
torch::Tensor graclus(torch::Tensor rowptr, torch::Tensor col,
11+
torch::optional<torch::Tensor> optional_weight);
12+
13+
torch::Tensor grid(torch::Tensor pos, torch::Tensor size,
14+
torch::optional<torch::Tensor> optional_start,
15+
torch::optional<torch::Tensor> optional_end);
16+
17+
torch::Tensor knn(torch::Tensor x, torch::Tensor y, torch::Tensor ptr_x,
18+
torch::Tensor ptr_y, int64_t k, bool cosine);
19+
20+
torch::Tensor nearest(torch::Tensor x, torch::Tensor y, torch::Tensor ptr_x,
21+
torch::Tensor ptr_y);
22+
23+
torch::Tensor radius(torch::Tensor x, torch::Tensor y, torch::Tensor ptr_x,
24+
torch::Tensor ptr_y, double r, int64_t max_num_neighbors);
25+
26+
torch::Tensor random_walk(torch::Tensor rowptr, torch::Tensor col,
27+
torch::Tensor start, int64_t walk_length, double p,
28+
double q);
29+
30+
torch::Tensor neighbor_sampler(torch::Tensor start, torch::Tensor rowptr,
31+
int64_t count, double factor);

0 commit comments

Comments
 (0)