Skip to content

Commit 955b1cf

Browse files
HunterTracerzenghongtai
andauthored
add FindMetis.cmake to locate metis, add -DWITH_METIS option, add cusparse & openmp linker (#299)
Co-authored-by: zenghongtai <1518445275@qq.com>
1 parent bfdca11 commit 955b1cf

File tree

2 files changed

+287
-1
lines changed

2 files changed

+287
-1
lines changed

CMakeLists.txt

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
cmake_minimum_required(VERSION 3.0)
1+
cmake_minimum_required(VERSION 3.10)
22
project(torchsparse)
33
set(CMAKE_CXX_STANDARD 14)
44
set(TORCHSPARSE_VERSION 0.6.15)
5+
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
56

67
option(WITH_CUDA "Enable CUDA support" OFF)
78
option(WITH_PYTHON "Link to Python when building" ON)
9+
option(WITH_METIS "Enable METIS support" OFF)
810

911
if(WITH_CUDA)
1012
enable_language(CUDA)
@@ -19,6 +21,11 @@ if (WITH_PYTHON)
1921
endif()
2022
find_package(Torch REQUIRED)
2123

24+
if (WITH_METIS)
25+
add_definitions(-DWITH_METIS)
26+
find_package(METIS)
27+
endif()
28+
2229
file(GLOB HEADERS csrc/*.h)
2330
file(GLOB OPERATOR_SOURCES csrc/*.* csrc/cpu/*.*)
2431
if(WITH_CUDA)
@@ -27,9 +34,23 @@ endif()
2734

2835
add_library(${PROJECT_NAME} SHARED ${OPERATOR_SOURCES})
2936
target_link_libraries(${PROJECT_NAME} PRIVATE ${TORCH_LIBRARIES})
37+
if (WITH_CUDA)
38+
target_link_libraries(${PROJECT_NAME} PRIVATE ${CUDA_cusparse_LIBRARY})
39+
endif()
3040
if (WITH_PYTHON)
3141
target_link_libraries(${PROJECT_NAME} PRIVATE Python3::Python)
3242
endif()
43+
if (WITH_METIS)
44+
target_include_directories(${PROJECT_NAME} PRIVATE ${METIS_INCLUDE_DIRS})
45+
target_link_libraries(${PROJECT_NAME} PRIVATE ${METIS_LIBRARIES})
46+
endif()
47+
find_package(OpenMP)
48+
if (OPENMP_FOUND)
49+
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
50+
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
51+
# set (CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -Xcompiler=${OpenMP_CXX_FLAGS}")
52+
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}")
53+
endif()
3354
set_target_properties(${PROJECT_NAME} PROPERTIES EXPORT_NAME TorchSparse)
3455

3556
target_include_directories(${PROJECT_NAME} INTERFACE

cmake/FindMetis.cmake

Lines changed: 265 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,265 @@
1+
###
2+
#
3+
# @copyright (c) 2009-2014 The University of Tennessee and The University
4+
# of Tennessee Research Foundation.
5+
# All rights reserved.
6+
# @copyright (c) 2012-2014 Inria. All rights reserved.
7+
# @copyright (c) 2012-2014 Bordeaux INP, CNRS (LaBRI UMR 5800), Inria, Univ. Bordeaux. All rights reserved.
8+
#
9+
###
10+
#
11+
# - Find METIS include dirs and libraries
12+
# Use this module by invoking find_package with the form:
13+
# find_package(METIS
14+
# [REQUIRED] # Fail with error if metis is not found
15+
# )
16+
#
17+
# This module finds headers and metis library.
18+
# Results are reported in variables:
19+
# METIS_FOUND - True if headers and requested libraries were found
20+
# METIS_INCLUDE_DIRS - metis include directories
21+
# METIS_LIBRARY_DIRS - Link directories for metis libraries
22+
# METIS_LIBRARIES - metis component libraries to be linked
23+
#
24+
# The user can give specific paths where to find the libraries adding cmake
25+
# options at configure (ex: cmake path/to/project -DMETIS_DIR=path/to/metis):
26+
# METIS_DIR - Where to find the base directory of metis
27+
# METIS_INCDIR - Where to find the header files
28+
# METIS_LIBDIR - Where to find the library files
29+
# The module can also look for the following environment variables if paths
30+
# are not given as cmake variable: METIS_DIR, METIS_INCDIR, METIS_LIBDIR
31+
32+
#=============================================================================
33+
# Copyright 2012-2013 Inria
34+
# Copyright 2012-2013 Emmanuel Agullo
35+
# Copyright 2012-2013 Mathieu Faverge
36+
# Copyright 2012 Cedric Castagnede
37+
# Copyright 2013 Florent Pruvost
38+
#
39+
# Distributed under the OSI-approved BSD License (the "License");
40+
# see accompanying file MORSE-Copyright.txt for details.
41+
#
42+
# This software is distributed WITHOUT ANY WARRANTY; without even the
43+
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
44+
# See the License for more information.
45+
#=============================================================================
46+
# (To distribute this file outside of Morse, substitute the full
47+
# License text for the above reference.)
48+
49+
if (NOT METIS_FOUND)
50+
set(METIS_DIR "" CACHE PATH "Installation directory of METIS library")
51+
if (NOT METIS_FIND_QUIETLY)
52+
message(STATUS "A cache variable, namely METIS_DIR, has been set to specify the install directory of METIS")
53+
endif()
54+
endif()
55+
56+
# Looking for include
57+
# -------------------
58+
59+
# Add system include paths to search include
60+
# ------------------------------------------
61+
unset(_inc_env)
62+
set(ENV_METIS_DIR "$ENV{METIS_DIR}")
63+
set(ENV_METIS_INCDIR "$ENV{METIS_INCDIR}")
64+
if(ENV_METIS_INCDIR)
65+
list(APPEND _inc_env "${ENV_METIS_INCDIR}")
66+
elseif(ENV_METIS_DIR)
67+
list(APPEND _inc_env "${ENV_METIS_DIR}")
68+
list(APPEND _inc_env "${ENV_METIS_DIR}/include")
69+
list(APPEND _inc_env "${ENV_METIS_DIR}/include/metis")
70+
else()
71+
if(WIN32)
72+
string(REPLACE ":" ";" _inc_env "$ENV{INCLUDE}")
73+
else()
74+
string(REPLACE ":" ";" _path_env "$ENV{INCLUDE}")
75+
list(APPEND _inc_env "${_path_env}")
76+
string(REPLACE ":" ";" _path_env "$ENV{C_INCLUDE_PATH}")
77+
list(APPEND _inc_env "${_path_env}")
78+
string(REPLACE ":" ";" _path_env "$ENV{CPATH}")
79+
list(APPEND _inc_env "${_path_env}")
80+
string(REPLACE ":" ";" _path_env "$ENV{INCLUDE_PATH}")
81+
list(APPEND _inc_env "${_path_env}")
82+
endif()
83+
endif()
84+
list(APPEND _inc_env "${CMAKE_PLATFORM_IMPLICIT_INCLUDE_DIRECTORIES}")
85+
list(APPEND _inc_env "${CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES}")
86+
list(REMOVE_DUPLICATES _inc_env)
87+
88+
89+
# Try to find the metis header in the given paths
90+
# -------------------------------------------------
91+
# call cmake macro to find the header path
92+
if(METIS_INCDIR)
93+
set(METIS_metis.h_DIRS "METIS_metis.h_DIRS-NOTFOUND")
94+
find_path(METIS_metis.h_DIRS
95+
NAMES metis.h
96+
HINTS ${METIS_INCDIR})
97+
else()
98+
if(METIS_DIR)
99+
set(METIS_metis.h_DIRS "METIS_metis.h_DIRS-NOTFOUND")
100+
find_path(METIS_metis.h_DIRS
101+
NAMES metis.h
102+
HINTS ${METIS_DIR}
103+
PATH_SUFFIXES "include" "include/metis")
104+
else()
105+
set(METIS_metis.h_DIRS "METIS_metis.h_DIRS-NOTFOUND")
106+
find_path(METIS_metis.h_DIRS
107+
NAMES metis.h
108+
HINTS ${_inc_env})
109+
endif()
110+
endif()
111+
mark_as_advanced(METIS_metis.h_DIRS)
112+
113+
114+
# If found, add path to cmake variable
115+
# ------------------------------------
116+
if (METIS_metis.h_DIRS)
117+
set(METIS_INCLUDE_DIRS "${METIS_metis.h_DIRS}")
118+
else ()
119+
set(METIS_INCLUDE_DIRS "METIS_INCLUDE_DIRS-NOTFOUND")
120+
if(NOT METIS_FIND_QUIETLY)
121+
message(STATUS "Looking for metis -- metis.h not found")
122+
endif()
123+
endif()
124+
125+
126+
# Looking for lib
127+
# ---------------
128+
129+
# Add system library paths to search lib
130+
# --------------------------------------
131+
unset(_lib_env)
132+
set(ENV_METIS_LIBDIR "$ENV{METIS_LIBDIR}")
133+
if(ENV_METIS_LIBDIR)
134+
list(APPEND _lib_env "${ENV_METIS_LIBDIR}")
135+
elseif(ENV_METIS_DIR)
136+
list(APPEND _lib_env "${ENV_METIS_DIR}")
137+
list(APPEND _lib_env "${ENV_METIS_DIR}/lib")
138+
else()
139+
if(WIN32)
140+
string(REPLACE ":" ";" _lib_env "$ENV{LIB}")
141+
else()
142+
if(APPLE)
143+
string(REPLACE ":" ";" _lib_env "$ENV{DYLD_LIBRARY_PATH}")
144+
else()
145+
string(REPLACE ":" ";" _lib_env "$ENV{LD_LIBRARY_PATH}")
146+
endif()
147+
list(APPEND _lib_env "${CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES}")
148+
list(APPEND _lib_env "${CMAKE_C_IMPLICIT_LINK_DIRECTORIES}")
149+
endif()
150+
endif()
151+
list(REMOVE_DUPLICATES _lib_env)
152+
153+
# Try to find the metis lib in the given paths
154+
# ----------------------------------------------
155+
# call cmake macro to find the lib path
156+
if(METIS_LIBDIR)
157+
set(METIS_metis_LIBRARY "METIS_metis_LIBRARY-NOTFOUND")
158+
find_library(METIS_metis_LIBRARY
159+
NAMES metis
160+
HINTS ${METIS_LIBDIR})
161+
else()
162+
if(METIS_DIR)
163+
set(METIS_metis_LIBRARY "METIS_metis_LIBRARY-NOTFOUND")
164+
find_library(METIS_metis_LIBRARY
165+
NAMES metis
166+
HINTS ${METIS_DIR}
167+
PATH_SUFFIXES lib lib32 lib64)
168+
else()
169+
set(METIS_metis_LIBRARY "METIS_metis_LIBRARY-NOTFOUND")
170+
find_library(METIS_metis_LIBRARY
171+
NAMES metis
172+
HINTS ${_lib_env})
173+
endif()
174+
endif()
175+
mark_as_advanced(METIS_metis_LIBRARY)
176+
177+
178+
# If found, add path to cmake variable
179+
# ------------------------------------
180+
if (METIS_metis_LIBRARY)
181+
get_filename_component(metis_lib_path "${METIS_metis_LIBRARY}" PATH)
182+
# set cmake variables
183+
set(METIS_LIBRARIES "${METIS_metis_LIBRARY}")
184+
set(METIS_LIBRARY_DIRS "${metis_lib_path}")
185+
else ()
186+
set(METIS_LIBRARIES "METIS_LIBRARIES-NOTFOUND")
187+
set(METIS_LIBRARY_DIRS "METIS_LIBRARY_DIRS-NOTFOUND")
188+
if(NOT METIS_FIND_QUIETLY)
189+
message(STATUS "Looking for metis -- lib metis not found")
190+
endif()
191+
endif ()
192+
193+
# check a function to validate the find
194+
if(METIS_LIBRARIES)
195+
196+
set(REQUIRED_INCDIRS)
197+
set(REQUIRED_LIBDIRS)
198+
set(REQUIRED_LIBS)
199+
200+
# METIS
201+
if (METIS_INCLUDE_DIRS)
202+
set(REQUIRED_INCDIRS "${METIS_INCLUDE_DIRS}")
203+
endif()
204+
if (METIS_LIBRARY_DIRS)
205+
set(REQUIRED_LIBDIRS "${METIS_LIBRARY_DIRS}")
206+
endif()
207+
set(REQUIRED_LIBS "${METIS_LIBRARIES}")
208+
# m
209+
find_library(M_LIBRARY NAMES m)
210+
mark_as_advanced(M_LIBRARY)
211+
if(M_LIBRARY)
212+
list(APPEND REQUIRED_LIBS "-lm")
213+
endif()
214+
215+
# set required libraries for link
216+
set(CMAKE_REQUIRED_INCLUDES "${REQUIRED_INCDIRS}")
217+
set(CMAKE_REQUIRED_LIBRARIES)
218+
foreach(lib_dir ${REQUIRED_LIBDIRS})
219+
list(APPEND CMAKE_REQUIRED_LIBRARIES "-L${lib_dir}")
220+
endforeach()
221+
list(APPEND CMAKE_REQUIRED_LIBRARIES "${REQUIRED_LIBS}")
222+
string(REGEX REPLACE "^ -" "-" CMAKE_REQUIRED_LIBRARIES "${CMAKE_REQUIRED_LIBRARIES}")
223+
224+
# test link
225+
unset(METIS_WORKS CACHE)
226+
include(CheckFunctionExists)
227+
check_function_exists(METIS_NodeND METIS_WORKS)
228+
mark_as_advanced(METIS_WORKS)
229+
230+
if(NOT METIS_WORKS)
231+
if(NOT METIS_FIND_QUIETLY)
232+
message(STATUS "Looking for METIS : test of METIS_NodeND with METIS library fails")
233+
message(STATUS "CMAKE_REQUIRED_LIBRARIES: ${CMAKE_REQUIRED_LIBRARIES}")
234+
message(STATUS "CMAKE_REQUIRED_INCLUDES: ${CMAKE_REQUIRED_INCLUDES}")
235+
message(STATUS "Check in CMakeFiles/CMakeError.log to figure out why it fails")
236+
endif()
237+
endif()
238+
set(CMAKE_REQUIRED_INCLUDES)
239+
set(CMAKE_REQUIRED_FLAGS)
240+
set(CMAKE_REQUIRED_LIBRARIES)
241+
endif()
242+
243+
if (METIS_LIBRARIES)
244+
list(GET METIS_LIBRARIES 0 first_lib)
245+
get_filename_component(first_lib_path "${first_lib}" PATH)
246+
if (${first_lib_path} MATCHES "/lib(32|64)?$")
247+
string(REGEX REPLACE "/lib(32|64)?$" "" not_cached_dir "${first_lib_path}")
248+
set(METIS_DIR_FOUND "${not_cached_dir}" CACHE PATH "Installation directory of METIS library" FORCE)
249+
else()
250+
set(METIS_DIR_FOUND "${first_lib_path}" CACHE PATH "Installation directory of METIS library" FORCE)
251+
endif()
252+
endif()
253+
mark_as_advanced(METIS_DIR)
254+
mark_as_advanced(METIS_DIR_FOUND)
255+
256+
# check that METIS has been found
257+
# ---------------------------------
258+
include(FindPackageHandleStandardArgs)
259+
find_package_handle_standard_args(METIS DEFAULT_MSG
260+
METIS_LIBRARIES
261+
METIS_WORKS
262+
METIS_INCLUDE_DIRS)
263+
#
264+
# TODO: Add possibility to check for specific functions in the library
265+
#

0 commit comments

Comments
 (0)