Skip to content

Commit af3a4ac

Browse files
authored
add function to build specific gpu-arches [REVIEW] (#766)
This PR adds in functions to the root level build.sh so that you can specify which GPU architectures you want to build for. Currently the only options were to build for the current system or for all systems. It also adds this option into `examples/build.sh` in addition to fixing bugs with the clean command. A `--gpu-arch` option is added that is meant to be used to provide which architectures to build for e.g. `./build.sh bench-ann --gpu-arch="80-real;90-real"` Authors: - Rohan Varma (https://github.com/nvrohanv) - Gil Forsyth (https://github.com/gforsyth) Approvers: - Ben Karsin (https://github.com/bkarsin) - Corey J. Nolet (https://github.com/cjnolet) - Micka (https://github.com/lowener) - Gil Forsyth (https://github.com/gforsyth) URL: #766
1 parent d77c73a commit af3a4ac

2 files changed

Lines changed: 106 additions & 32 deletions

File tree

build.sh

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ HELP="$0 [<target> ...] [<flag> ...] [--cmake-args=\"<args>\"] [--cache-tool=<to
4242
--limit-tests - semicolon-separated list of test executables to compile (e.g. NEIGHBORS_TEST;CLUSTER_TEST)
4343
--limit-bench-ann - semicolon-separated list of ann benchmark executables to compute (e.g. HNSWLIB_ANN_BENCH;RAFT_IVF_PQ_ANN_BENCH)
4444
--allgpuarch - build for all supported GPU architectures
45+
--gpu-arch=\"<arch>\" - build for specific GPU architectures (e.g. \"80-real;90-real\")
46+
Values from this flag are passed to CUDA_ARCHITECTURES in cmake.
47+
See https://cmake.org/cmake/help/latest/prop_tgt/CUDA_ARCHITECTURES.html
48+
for more details.
49+
To summarize, specifying \"80-virtual\" would generate PTX for compute capability 8.0
50+
whereas \"80-real\" will generate SASS.
51+
If you are unsure which to do, use -real suffix --gpu-arch=\"80-real;90-real\"
4552
--no-mg - disable multi-GPU support
4653
--no-nvtx - disable nvtx (profiling markers), but allow enabling it in downstream projects
4754
--no-shared-libs - build without shared libraries
@@ -194,6 +201,37 @@ function buildMetrics {
194201
fi
195202
}
196203

204+
function gpuArch {
205+
# Check if both --gpu-arch and --allgpuarch are specified
206+
if hasArg --allgpuarch && [[ -n $(echo $ARGS | { grep -E "\-\-gpu\-arch" || true; } ) ]]; then
207+
echo "Error: Cannot specify both --gpu-arch and --allgpuarch"
208+
echo "Use either:"
209+
echo " --gpu-arch=\"80-real;90-real\" (for specific architectures)"
210+
echo " --allgpuarch (for all supported architectures)"
211+
exit 1
212+
fi
213+
214+
# Check for multiple gpu-arch options
215+
if [[ $(echo $ARGS | { grep -Eo "\-\-gpu\-arch" || true; } | wc -l ) -gt 1 ]]; then
216+
echo "Error: Multiple --gpu-arch options were provided. Please combine architectures into a single option."
217+
echo "Instead of: --gpu-arch=80-real --gpu-arch=90-real"
218+
echo "Use: --gpu-arch=\"80-real;90-real\""
219+
exit 1
220+
fi
221+
222+
# Check for gpu-arch option
223+
if [[ -n $(echo $ARGS | { grep -E "\-\-gpu\-arch" || true; } ) ]]; then
224+
GPU_ARCH_ARG=$(echo $ARGS | { grep -Eo "\-\-gpu\-arch=.+( |$)" || true; })
225+
if [[ -n ${GPU_ARCH_ARG} ]]; then
226+
# Remove the full argument from ARGS
227+
ARGS=${ARGS//$GPU_ARCH_ARG/}
228+
# Extract just the architecture value
229+
CUVS_CMAKE_CUDA_ARCHITECTURES=$(echo $GPU_ARCH_ARG | sed -e 's/--gpu-arch=//' -e 's/ .*//')
230+
echo "Building for specified GPU architectures: ${CUVS_CMAKE_CUDA_ARCHITECTURES}"
231+
fi
232+
fi
233+
}
234+
197235
if hasArg -h || hasArg --help; then
198236
echo "${HELP}"
199237
exit 0
@@ -206,6 +244,7 @@ if (( ${NUMARGS} != 0 )); then
206244
limitTests
207245
limitAnnBench
208246
buildMetrics
247+
gpuArch
209248
for a in ${ARGS}; do
210249
if ! (echo " ${VALIDARGS} " | grep -q " ${a} "); then
211250
echo "Invalid option: ${a}"
@@ -264,10 +303,6 @@ if hasArg -g; then
264303
BUILD_TYPE=Debug
265304
fi
266305

267-
if hasArg --allgpuarch; then
268-
BUILD_ALL_GPU_ARCH=1
269-
fi
270-
271306
if hasArg --no-mg; then
272307
BUILD_MG_ALGOS=OFF
273308
fi
@@ -311,6 +346,7 @@ fi
311346
if hasArg --incl-cache-stats; then
312347
BUILD_REPORT_INCL_CACHE_STATS=ON
313348
fi
349+
314350
if [[ ${CMAKE_TARGET} == "" ]]; then
315351
CMAKE_TARGET="all"
316352
fi
@@ -339,20 +375,23 @@ if (( NUMARGS == 0 )) || hasArg libcuvs || hasArg docs || hasArg tests || hasArg
339375
CMAKE_TARGET="${CMAKE_TARGET};cuvs"
340376
fi
341377

342-
if (( ${BUILD_ALL_GPU_ARCH} == 0 )); then
343-
CUVS_CMAKE_CUDA_ARCHITECTURES="NATIVE"
344-
echo "Building for the architecture of the GPU in the system..."
345-
else
346-
CUVS_CMAKE_CUDA_ARCHITECTURES="RAPIDS"
347-
echo "Building for *ALL* supported GPU architectures..."
348-
fi
349-
350378
# get the current count before the compile starts
351379
CACHE_TOOL=${CACHE_TOOL:-sccache}
352380
if [[ "$BUILD_REPORT_INCL_CACHE_STATS" == "ON" && -x "$(command -v ${CACHE_TOOL})" ]]; then
353381
"${CACHE_TOOL}" --zero-stats
354382
fi
355383

384+
# Set default GPU architecture if not already set by gpuArch function
385+
if [[ -z "${CUVS_CMAKE_CUDA_ARCHITECTURES}" ]]; then
386+
if hasArg --allgpuarch; then
387+
CUVS_CMAKE_CUDA_ARCHITECTURES="RAPIDS"
388+
echo "Building for *ALL* supported GPU architectures..."
389+
else
390+
CUVS_CMAKE_CUDA_ARCHITECTURES="NATIVE"
391+
echo "Building for the architecture of the GPU in the system..."
392+
fi
393+
fi
394+
356395
mkdir -p ${LIBCUVS_BUILD_DIR}
357396
cd ${LIBCUVS_BUILD_DIR}
358397
cmake -S ${REPODIR}/cpp -B ${LIBCUVS_BUILD_DIR} \

examples/build.sh

Lines changed: 55 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,69 @@
77
# Abort script on first error
88
set -e
99

10+
NUMARGS=$#
11+
ARGS=$*
12+
1013
function hasArg {
11-
(( NUMARGS != 0 )) && (echo " ${ARGS} " | grep -q " $1 ")
14+
(( ${NUMARGS} != 0 )) && (echo " ${ARGS} " | grep -q " $1 ")
1215
}
1316

14-
PARALLEL_LEVEL=${PARALLEL_LEVEL:=`nproc`}
17+
if hasArg clean; then
18+
rm -rf c/build
19+
rm -rf cpp/build
20+
exit 0
21+
fi
22+
23+
function gpuArch {
24+
25+
if hasArg --allgpuarch && [[ -n $(echo $ARGS | { grep -E "\-\-gpu\-arch" || true; } ) ]]; then
26+
echo "Error: Cannot specify both --gpu-arch and --allgpuarch"
27+
echo "Use either:"
28+
echo " --gpu-arch=\"80-real;90-real\" (for specific architectures)"
29+
echo " --allgpuarch (for all supported architectures)"
30+
exit 1
31+
fi
32+
33+
if [[ $(echo $ARGS | { grep -Eo "\-\-gpu\-arch" || true; } | wc -l ) -gt 1 ]]; then
34+
echo "Error: Multiple --gpu-arch options were provided. Please combine architectures into a single option."
35+
echo "Instead of: --gpu-arch=80-real --gpu-arch=90-real"
36+
echo "Use: --gpu-arch=\"80-real;90-real\""
37+
exit 1
38+
fi
39+
40+
if [[ -n $(echo $ARGS | { grep -E "\-\-gpu\-arch" || true; } ) ]]; then
41+
GPU_ARCH_ARG=$(echo $ARGS | { grep -Eo "\-\-gpu\-arch=.+( |$)" || true; })
42+
if [[ -n ${GPU_ARCH_ARG} ]]; then
43+
# Extract just the architecture value
44+
echo ${GPU_ARCH_ARG} | sed -e 's/--gpu-arch=//' -e 's/ .*//'
45+
return
46+
fi
47+
fi
48+
49+
# Handle --allgpuarch
50+
if hasArg --allgpuarch; then
51+
echo "RAPIDS"
52+
return
53+
fi
54+
55+
# Default to NATIVE
56+
echo "NATIVE"
57+
}
1558

59+
# Set up build configuration
60+
PARALLEL_LEVEL=${PARALLEL_LEVEL:=`nproc`}
1661
BUILD_TYPE=Release
1762
BUILD_DIR=build/
18-
1963
CUVS_REPO_REL=""
2064
EXTRA_CMAKE_ARGS=""
21-
BUILD_ALL_GPU_ARCH=0
22-
if hasArg --allgpuarch; then
23-
BUILD_ALL_GPU_ARCH=1
24-
fi
65+
66+
67+
CUVS_CMAKE_CUDA_ARCHITECTURES=$(gpuArch)
68+
case ${CUVS_CMAKE_CUDA_ARCHITECTURES} in
69+
"RAPIDS") echo "Building for *ALL* supported GPU architectures..." ;;
70+
"NATIVE") echo "Building for the architecture of the GPU in the system..." ;;
71+
*) echo "Building for specified GPU architectures: ${CUVS_CMAKE_CUDA_ARCHITECTURES}" ;;
72+
esac
2573

2674
# Root of examples
2775
EXAMPLES_DIR=$(dirname "$(realpath "$0")")
@@ -34,19 +82,6 @@ else
3482
EXTRA_CMAKE_ARGS="${EXTRA_CMAKE_ARGS} -Dcuvs_ROOT=${LIB_BUILD_DIR}"
3583
fi
3684

37-
if [ "$1" == "clean" ]; then
38-
rm -rf build
39-
exit 0
40-
fi
41-
42-
if (( ${BUILD_ALL_GPU_ARCH} == 0 )); then
43-
CUVS_CMAKE_CUDA_ARCHITECTURES="NATIVE"
44-
echo "Building for the architecture of the GPU in the system..."
45-
else
46-
CUVS_CMAKE_CUDA_ARCHITECTURES="RAPIDS"
47-
echo "Building for *ALL* supported GPU architectures..."
48-
fi
49-
5085
################################################################################
5186
# Add individual libcuvs examples build scripts down below
5287

0 commit comments

Comments
 (0)