11# Let's use a recent CMake version:
2- # 3.16+ for native sanitizers support
3- # 3.17+ for `FindCUDAToolkit`
4- # 3.18 for BLAS::BLAS target
5- # 3.25.2 for CUDA20 support
2+ #
3+ # * 3.16+ for native sanitizers support
4+ # * 3.17+ for `FindCUDAToolkit`
5+ # * 3.18 for BLAS::BLAS target
6+ # * 3.25.2 for CUDA20 support
7+ #
68# The good news is that Ubuntu 24.04 comes with 3.28!
79cmake_minimum_required (VERSION 3.25.2 FATAL_ERROR)
810
@@ -14,7 +16,8 @@ project(
1416 VERSION 0.3.5
1517 LANGUAGES CXX
1618 DESCRIPTION "Parallel Reductions Benchmark for CPUs & GPUs"
17- HOMEPAGE_URL "https://github.com/ashvardanian/ParallelReductionsBenchmark" )
19+ HOMEPAGE_URL "https://github.com/ashvardanian/ParallelReductionsBenchmark"
20+ )
1821
1922set (CMAKE_CXX_STANDARD 17)
2023set (CMAKE_CXX_STANDARD_REQUIRED YES )
@@ -30,9 +33,9 @@ message(STATUS "CMAKE_BUILD_TYPE: ${CMAKE_BUILD_TYPE}")
3033message (STATUS "----------------------------------------" )
3134
3235# Default to Release if no build type is set:
33- if (NOT CMAKE_BUILD_TYPE )
36+ if (NOT CMAKE_BUILD_TYPE )
3437 set (CMAKE_BUILD_TYPE Release)
35- endif ()
38+ endif ()
3639
3740# ------------------------------------------------------------------------------
3841# Detect CUDA Support
@@ -41,13 +44,13 @@ set(ENABLE_CUDA OFF)
4144include (CheckLanguage)
4245check_language(CUDA)
4346
44- if (CMAKE_CUDA_COMPILER)
47+ if (CMAKE_CUDA_COMPILER)
4548 enable_language (CUDA)
4649 set (ENABLE_CUDA ON )
4750 message (STATUS "CUDA detected! Using compiler: ${CMAKE_CUDA_COMPILER} " )
48- else ()
51+ else ()
4952 message (STATUS "CUDA not detected. Skipping CUDA-specific builds." )
50- endif ()
53+ endif ()
5154
5255# ------------------------------------------------------------------------------
5356# Options
@@ -56,13 +59,13 @@ option(USE_INTEL_TBB "Use Intel TBB for parallel STL algorithms" ON)
5659option (USE_NVIDIA_CCCL "Use Nvidia CCCL for CUDA acceleration" ON )
5760
5861# Enable or disable options based on system and CUDA support
59- if (ENABLE_CUDA)
62+ if (ENABLE_CUDA)
6063 set (USE_NVIDIA_CCCL ON )
6164 set (USE_INTEL_TBB OFF ) # Prioritize CUDA acceleration
62- elseif (CMAKE_SYSTEM_NAME STREQUAL "Linux" )
65+ elseif (CMAKE_SYSTEM_NAME STREQUAL "Linux" )
6366 set (USE_NVIDIA_CCCL OFF ) # Can't compile CCCL w/o CUDA
6467 set (USE_INTEL_TBB ON ) # Default to TBB on Linux without CUDA
65- endif ()
68+ endif ()
6669
6770message (STATUS "USE_INTEL_TBB: ${USE_INTEL_TBB} " )
6871message (STATUS "USE_NVIDIA_CCCL: ${USE_NVIDIA_CCCL} " )
@@ -101,43 +104,45 @@ set(BENCHMARK_ENABLE_GTEST_TESTS OFF)
101104set (BENCHMARK_USE_BUNDLED_GTEST ON )
102105FetchContent_MakeAvailable(benchmark)
103106
104- # Intel TBB for "Parallel STL" algorithms
105- # https://github.com/oneapi-src/oneTBB/tree/onetbb_2021
106- if (USE_INTEL_TBB)
107+ # Intel TBB for "Parallel STL" algorithms: https://github.com/oneapi-src/oneTBB/tree/onetbb_2021
108+ if (USE_INTEL_TBB)
107109 FetchContent_Declare(
108110 IntelTBB
109111 GIT_REPOSITORY https://github.com/uxlfoundation/oneTBB.git
110112 GIT_TAG master
111113 )
112114
113115 # Suppress TBB's own tests:
114- set (TBB_TEST OFF CACHE BOOL "Do not build TBB tests" FORCE)
116+ set (TBB_TEST
117+ OFF
118+ CACHE BOOL "Do not build TBB tests" FORCE
119+ )
115120 FetchContent_MakeAvailable(IntelTBB)
116121
117122 # ------------------------------------------------------------------------------
118123 # TBB fix for -Wstringop-overflow warnings treated as errors
119124 # ------------------------------------------------------------------------------
120- # The TBB library target is typically called "tbb". We can explicitly disable
121- # the `stringop-overflow` warning for TBB only:
122- if (TARGET tbb)
123- if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" )
125+ # The TBB library target is typically called "tbb". We can explicitly disable the `stringop-overflow` warning for
126+ # TBB only:
127+ if (TARGET tbb)
128+ if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" )
124129 target_compile_options (tbb PRIVATE -Wno-stringop-overflow)
125- endif ()
126- endif ()
127- endif ()
130+ endif ()
131+ endif ()
132+ endif ()
128133
129134# Nvidia's CUDA Core Compute Libraries for GPU acceleration
130- if (USE_NVIDIA_CCCL)
131- # CUB, Thrust, and other libraries of interest are now included into the
132- # CUDA Toolkit, so we don't need this anymore:
135+ if (USE_NVIDIA_CCCL)
136+ # CUB, Thrust, and other libraries of interest are now included into the CUDA Toolkit, so we don't need this
137+ # anymore:
133138 #
134139 # FetchContent_Declare(NvidiaCCCL GIT_REPOSITORY https://github.com/nvidia/cccl.git)
135140 # FetchContent_MakeAvailable(NvidiaCCCL)
136141 find_package (CUDAToolkit REQUIRED)
137142 message (STATUS "CUDA Toolkit Version: ${CUDAToolkit_VERSION} " )
138143 message (STATUS "CUDA Toolkit Include Path: ${CUDAToolkit_INCLUDE_DIRS} " )
139144 message (STATUS "CUDA Toolkit Libraries Path: ${CUDAToolkit_LIBRARY_DIR} " )
140- endif ()
145+ endif ()
141146
142147set (CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g" )
143148set (CMAKE_CUDA_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g" )
@@ -149,54 +154,62 @@ set(CMAKE_GCC_FLAGS "${CMAKE_GCC_FLAGS} -march=native -fopenmp")
149154add_executable (reduce_bench reduce_bench.cpp)
150155target_link_libraries (reduce_bench PRIVATE benchmark::benchmark fmt::fmt Threads::Threads BLAS::BLAS)
151156
152- if (USE_INTEL_TBB)
157+ if (USE_INTEL_TBB)
153158 target_link_libraries (reduce_bench PRIVATE TBB::tbb)
154- endif ()
159+ endif ()
155160
156- if (USE_NVIDIA_CCCL)
161+ if (USE_NVIDIA_CCCL)
157162 target_link_libraries (reduce_bench PRIVATE CUDA::cudart CUDA::cublas)
158- endif ()
163+ endif ()
159164
160- if (OpenMP_FOUND)
165+ if (OpenMP_FOUND)
161166 target_link_libraries (reduce_bench PRIVATE OpenMP::OpenMP_CXX)
162- endif ()
167+ endif ()
163168
164- if (OpenCL_FOUND)
169+ if (OpenCL_FOUND)
165170 target_link_libraries (reduce_bench PRIVATE OpenCL::OpenCL)
166- endif ()
171+ endif ()
167172
168173# On Linux we use libnuma for NUMA support.
169- if (UNIX AND NOT APPLE )
170- find_path (NUMA_INCLUDE_DIRS
174+ if (UNIX AND NOT APPLE )
175+ find_path (
176+ NUMA_INCLUDE_DIRS
171177 NAMES numa.h
172178 HINTS $ENV{HOME} /local/include /opt/local/include /usr/local/include /usr/include
173179 )
174180
175- find_library (NUMA_LIBRARIES
181+ find_library (
182+ NUMA_LIBRARIES
176183 NAMES numa
177- HINTS $ENV{HOME} /local/lib64 $ENV{HOME} /local/lib /usr/local/lib64 /usr/local/lib /opt/local/lib64 /opt/local/lib /usr/lib64 /usr/lib
178- /usr/lib/x86_64-linux-gnu/
179- /usr/lib/aarch64-linux-gnu/
184+ HINTS $ENV{HOME} /local/lib64
185+ $ENV{HOME} /local/lib
186+ /usr/local/lib64
187+ /usr/local/lib
188+ /opt/local/lib64
189+ /opt/local/lib
190+ /usr/lib64
191+ /usr/lib
192+ /usr/lib/x86_64-linux-gnu/
193+ /usr/lib/aarch64-linux-gnu/
180194 )
181195
182196 include (FindPackageHandleStandardArgs)
183197 find_package_handle_standard_args(NUMA DEFAULT_MSG NUMA_LIBRARIES NUMA_INCLUDE_DIRS)
184198
185- if (NUMA_FOUND)
199+ if (NUMA_FOUND)
186200 message (STATUS "Found NUMA: includes=${NUMA_INCLUDE_DIRS} , library=${NUMA_LIBRARIES} " )
187201 target_include_directories (reduce_bench PRIVATE ${NUMA_INCLUDE_DIRS} )
188202 target_link_libraries (reduce_bench PRIVATE ${NUMA_LIBRARIES} )
189- else ()
203+ else ()
190204 message (WARNING "NUMA library not found." )
191205 message (WARNING "Please install it using your package manager:" )
192206 message (WARNING " Debian/Ubuntu: sudo apt-get install libnuma1 libnuma-dev" )
193207 message (WARNING " RHEL/CentOS: sudo yum install numactl numactl-devel" )
194- endif ()
195- endif ()
208+ endif ()
209+ endif ()
196210
197- # List of all possible compiler IDs:
198- # https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_COMPILER_ID.html
199- if (CMAKE_CUDA_COMPILER_ID STREQUAL "NVIDIA" OR CMAKE_CUDA_COMPILER_ID STREQUAL "NVHPC" )
211+ # List of all possible compiler IDs: https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_COMPILER_ID.html
212+ if (CMAKE_CUDA_COMPILER_ID STREQUAL "NVIDIA" OR CMAKE_CUDA_COMPILER_ID STREQUAL "NVHPC" )
200213 enable_language (CUDA)
201214 message ("-- Detected Nvidia Compiler" )
202215 set_property (SOURCE reduce_bench.cpp PROPERTY LANGUAGE CUDA)
@@ -208,7 +221,7 @@ if(CMAKE_CUDA_COMPILER_ID STREQUAL "NVIDIA" OR CMAKE_CUDA_COMPILER_ID STREQUAL "
208221 set (CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} --expt-relaxed-constexpr --extended-lambda" )
209222 set (CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -gencode arch=compute_86,code=sm_86" )
210223
211- elseif (CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang" AND 0)
224+ elseif (CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang" AND 0)
212225 set (METAL_SRC ${CMAKE_CURRENT_SOURCE_DIR} /reduce_metal.msl)
213226 set (METALLIB_OUTPUT ${CMAKE_CURRENT_BINARY_DIR} /reduce_metal.metallib)
214227
@@ -222,23 +235,13 @@ elseif(CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang" AND 0)
222235 )
223236
224237 # Create a pseudo target to build the metallib
225- add_custom_target (MetalLibBuild ALL
226- DEPENDS ${METALLIB_OUTPUT}
227- )
238+ add_custom_target (MetalLibBuild ALL DEPENDS ${METALLIB_OUTPUT} )
228239
229240 enable_language (OBJCXX)
230241 set_property (SOURCE reduce_metal.hpp PROPERTY LANGUAGE OBJCXX)
231242 set_property (SOURCE reduce_bench.cpp PROPERTY LANGUAGE OBJCXX)
232- target_link_libraries (reduce_bench
233- PRIVATE
234- "-framework Metal"
235- "-framework Foundation"
236- )
237- set_source_files_properties (
238- reduce_bench.cpp
239- PROPERTIES
240- COMPILE_FLAGS "-x objective-c++ -fobjc-arc"
241- )
243+ target_link_libraries (reduce_bench PRIVATE "-framework Metal" "-framework Foundation" )
244+ set_source_files_properties (reduce_bench.cpp PROPERTIES COMPILE_FLAGS "-x objective-c++ -fobjc-arc" )
242245
243246 # Make sure reduce_bench depends on MetalLibBuild so the .metallib is built first
244247 add_dependencies (reduce_bench MetalLibBuild)
@@ -247,19 +250,17 @@ elseif(CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang" AND 0)
247250 add_custom_command (
248251 TARGET reduce_bench
249252 POST_BUILD
250- COMMAND ${CMAKE_COMMAND} -E copy_if_different
251- ${METALLIB_OUTPUT}
252- $<TARGET_FILE_DIR:reduce_bench>
253+ COMMAND ${CMAKE_COMMAND} -E copy_if_different ${METALLIB_OUTPUT} $<TARGET_FILE_DIR:reduce_bench>
253254 )
254255
255- elseif (CMAKE_CXX_COMPILER_ID STREQUAL "Clang" )
256+ elseif (CMAKE_CXX_COMPILER_ID STREQUAL "Clang" )
256257 message ("-- Detected Clang Compiler" )
257- elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" )
258+ elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" )
258259 message ("-- Detected GCC Compiler" )
259260 set (CMAKE_CXX_FLAGS "${CMAKE_GCC_FLAGS} " )
260261
261- elseif (CMAKE_CXX_COMPILER_ID STREQUAL "Intel" OR CMAKE_CXX_COMPILER_ID STREQUAL "IntelLLVM" )
262+ elseif (CMAKE_CXX_COMPILER_ID STREQUAL "Intel" OR CMAKE_CXX_COMPILER_ID STREQUAL "IntelLLVM" )
262263 message ("-- Detected Intel Compiler" )
263264 set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -w" )
264265 set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ferror-limit=1" )
265- endif ()
266+ endif ()
0 commit comments