Skip to content

Commit

Permalink
Bump CMake version for CUDA features (resolves #232, resolves #241)
Browse files Browse the repository at this point in the history
  • Loading branch information
lindstro committed Dec 28, 2024
1 parent 96518de commit f3d31d7
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 55 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Change Log

### Fixed

- #178: Bump CMake version to make use of `CMAKE_{CUDA,HIP}_ARCHITECTURES`.
- #232: Bump CMake version to make use of `FindCUDAToolkit`.
- #241: Signed left shifts, integer overflow invoke undefined behavior.

---
Expand Down
40 changes: 20 additions & 20 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
cmake_minimum_required(VERSION 3.9)

if(ZFP_WITH_CUDA)
# 3.18 CMAKE_CUDA_ARCHITECTURES
# 3.20 FindCUDAToolkit
# 3.23 CMAKE_CUDA_ARCHITECTURES=all
cmake_minimum_required(VERSION 3.23)
endif()

if(ZFP_WITH_HIP)
# 3.21 enable_language(HIP)
cmake_minimum_required(VERSION 3.21)
endif()

Expand Down Expand Up @@ -87,6 +96,9 @@ message(STATUS "Compiling with C standard: ${CMAKE_C_STANDARD}")
if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 98)
endif()
if(ZFP_WITH_CUDA)
set(CMAKE_CXX_STANDARD 11)
endif()
if(ZFP_WITH_HIP)
set(CMAKE_CXX_STANDARD 14)
endif()
Expand Down Expand Up @@ -120,7 +132,7 @@ cmake_dependent_option(ZFP_ENABLE_PIC
"SHARED_LIBS_SUPPORTED" OFF)
set(CMAKE_POSITION_INDEPENDENT_CODE ${ZFP_ENABLE_PIC})

# Compile-time options.
# Compile-time options

set(ZFP_BIT_STREAM_WORD_SIZE 64 CACHE STRING
"Use smaller bit stream word type for finer rate granularity")
Expand Down Expand Up @@ -215,31 +227,19 @@ if(ZFP_WITH_OPENMP AND NOT OpenMP_C_LIBRARIES)
endif()

if(ZFP_WITH_CUDA)
# use CUDA_BIN_DIR hint
set(ENV{CUDA_BIN_PATH} ${CUDA_BIN_DIR})
find_package(CUDA)
if(NOT CUDA_FOUND)
message(FATAL_ERROR "ZFP_WITH_CUDA is enabled, but a CUDA installation was not found.")
endif()
if(${CUDA_VERSION_MAJOR} LESS 9)
message(FATAL_ERROR "zfp requires at least CUDA 9.0.")
endif()
if(${CUDA_VERSION_MAJOR} LESS 11)
# CUB is part of CUDA since CUDA 11
# Todo: get CUB from https://github.com/NVIDIA/cub.git
message(FATAL_ERROR "TODO: Install CUB for CUDA < 11")
endif()
set (CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS} \
-gencode arch=compute_60,code=compute_60 \
-gencode arch=compute_70,code=compute_70 \
-gencode arch=compute_80,code=compute_80")
enable_language(CUDA)
# Require CUDA 11.0 or later for CUB
find_package(CUDAToolkit 11 REQUIRED)
message(STATUS "Compiling for CUDA architecture(s): ${CMAKE_CUDA_ARCHITECTURES}")
endif()

if(ZFP_WITH_HIP)
if (DEFINED ENV{HIP_PATH} AND NOT $ENV{HIP_PATH} STREQUAL "")
if(DEFINED ENV{HIP_PATH} AND NOT $ENV{HIP_PATH} STREQUAL "")
set(HIP_PATH $ENV{HIP_PATH} CACHE PATH "Path to where HIP has been installed")
endif()
enable_language(HIP)
find_package(hip REQUIRED)
message(STATUS "Compiling for HIP architecture(s): ${CMAKE_HIP_ARCHITECTURES}")
endif()

if(NOT (ZFP_BIT_STREAM_WORD_SIZE EQUAL 64))
Expand Down
54 changes: 19 additions & 35 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,24 +1,4 @@
if(ZFP_WITH_OPENMP)
add_definitions(-DZFP_WITH_OPENMP)
endif()

if(ZFP_WITH_CUDA)
set(CMAKE_CXX_FLAGS_PREVIOUS ${CMAKE_CXX_FLAGS})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC" )

add_subdirectory(cuda)
cuda_include_directories(${PROJECT_SOURCE_DIR}/include)
cuda_wrap_srcs(zfp OBJ zfp_cuda_backend_obj cuda/interface.cu OPTIONS ${CMAKE_CUDA_FLAGS})
set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS_PREVIOUS})
add_definitions(-DZFP_WITH_CUDA)
endif()

if(ZFP_WITH_HIP)
add_definitions(-DZFP_WITH_HIP)
set(ZFP_HIP_SOURCE hip/interface.cpp)
set_source_files_properties(${ZFP_HIP_SOURCE} PROPERTIES LANGUAGE HIP)
endif()

# required source files
set(zfp_source
zfp.c
bitstream.c
Expand All @@ -30,20 +10,11 @@ set(zfp_source
decode3f.c decode3d.c decode3i.c decode3l.c
encode4f.c encode4d.c encode4i.c encode4l.c
decode4f.c decode4d.c decode4i.c decode4l.c
${ZFP_HIP_SOURCE})
)

if(ZFP_WITH_CUDA)
add_library(zfp ${zfp_source}
${zfp_cuda_backend_obj})
else()
add_library(zfp ${zfp_source})
endif()
add_library(zfp ${zfp_source})
add_library(zfp::zfp ALIAS zfp)

if(ZFP_WITH_OPENMP)
target_link_libraries(zfp PRIVATE OpenMP::OpenMP_C)
endif()

if(HAVE_LIBM_MATH)
target_link_libraries(zfp PRIVATE m)
endif()
Expand All @@ -54,12 +25,24 @@ if(WIN32 AND BUILD_SHARED_LIBS)
list(APPEND zfp_private_defs ZFP_SOURCE)
endif()

if(ZFP_WITH_OPENMP)
target_compile_definitions(zfp PUBLIC -DZFP_WITH_OPENMP)
target_link_libraries(zfp PRIVATE OpenMP::OpenMP_C)
endif()

if(ZFP_WITH_CUDA)
target_link_libraries(zfp PRIVATE ${CUDA_CUDART_LIBRARY} stdc++)
add_subdirectory(cuda)
target_sources(zfp PRIVATE cuda/interface.cu)
target_compile_definitions(zfp PUBLIC -DZFP_WITH_CUDA)
target_link_libraries(zfp PRIVATE CUDA::cudart stdc++)
set_property(TARGET zfp PROPERTY POSITION_INDEPENDENT_CODE ON)
endif()

if(ZFP_WITH_HIP)
find_package(hip REQUIRED)
add_subdirectory(hip)
target_sources(zfp PRIVATE hip/interface.cpp)
set_source_files_properties(hip/interface.cpp PROPERTIES LANGUAGE HIP)
target_compile_definitions(zfp PUBLIC -DZFP_WITH_HIP)
target_link_libraries(zfp PRIVATE hip::device)
endif()

Expand All @@ -81,4 +64,5 @@ set_property(TARGET zfp PROPERTY OUTPUT_NAME ${ZFP_LIBRARY_PREFIX}zfp)
install(TARGETS zfp EXPORT zfp-targets
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
)

0 comments on commit f3d31d7

Please sign in to comment.