feat: CCCL CachingDeviceAllocator preload — 完整依赖链 288 files
从 cccl_upstream 递归追踪 cub/util_allocator.cuh 的全部 include 依赖: cub/ 9 files (config, util_*, version, detect_cuda_runtime) cuda/ libcudacxx type_traits, concepts, algorithm, iterator... nv/ target macros, preprocessor 总计 288 个头文件 (1.4MB),打包到 include/ 目录,编译时 -I include 即可完全脱离 CCCL 原始目录结构。 .cu 文件直接 #include <cub/util_allocator.cuh>, 走原版 CUB CachingDeviceAllocator,零 mock。 BI-V100 参数: growth=2 bins=[8..32] max_cached=8GB/device
This commit is contained in:
@@ -1,41 +1,43 @@
|
||||
#!/usr/bin/env bash
|
||||
# Build libcccl_allocator.so — LD_PRELOAD .so for CUB CachingDeviceAllocator
|
||||
# Build libcccl_allocator.so
|
||||
#
|
||||
# Full CCCL dependency chain (288 headers) in ./include/
|
||||
# Source: cccl_upstream/cub/cub/util_allocator.cuh + transitive deps
|
||||
#
|
||||
# Usage:
|
||||
# bash build_cccl_preload.sh [output_dir]
|
||||
#
|
||||
# On BI-V100 with CoreX SDK:
|
||||
# bash build_cccl_preload.sh /workspace/qwen3_6_scripts/cccl_preload
|
||||
#
|
||||
# The .so intercepts cudaMalloc/cudaFree and routes through CUB's
|
||||
# caching allocator, bypassing CoreX's "expandable segment not supported"
|
||||
# ASSERT in CUDACachingAllocator.cpp:545.
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
OUTPUT_DIR="${1:-${SCRIPT_DIR}}"
|
||||
SRC="${SCRIPT_DIR}/cccl_allocator_preload.cu"
|
||||
INC="${SCRIPT_DIR}/include"
|
||||
OUT="${OUTPUT_DIR}/libcccl_allocator.so"
|
||||
|
||||
# Find CoreX clang++ (preferred) or system g++
|
||||
if [[ -x /usr/local/corex-3.2.3/bin/clang++ ]]; then
|
||||
CXX=/usr/local/corex-3.2.3/bin/clang++
|
||||
echo "[build] Using CoreX clang++: ${CXX}"
|
||||
elif [[ -x /usr/local/corex/bin/clang++ ]]; then
|
||||
CXX=/usr/local/corex/bin/clang++
|
||||
echo "[build] Using CoreX clang++ (alt): ${CXX}"
|
||||
else
|
||||
CXX=g++
|
||||
echo "[build] CoreX clang++ not found, falling back to g++"
|
||||
fi
|
||||
[[ -d "${INC}/cub" ]] || { echo "CCCL include tree missing: ${INC}/cub"; exit 2; }
|
||||
[[ -d "${INC}/cuda" ]] || { echo "CCCL include tree missing: ${INC}/cuda"; exit 2; }
|
||||
|
||||
# Find CUDA include path
|
||||
# Find compiler
|
||||
CXX=""
|
||||
for candidate in \
|
||||
/usr/local/corex-3.2.3/bin/clang++ \
|
||||
/usr/local/corex/bin/clang++ \
|
||||
/usr/local/corex/lib64/clang/16/bin/clang++ \
|
||||
; do
|
||||
if [[ -x "${candidate}" ]]; then
|
||||
CXX="${candidate}"
|
||||
break
|
||||
fi
|
||||
done
|
||||
[[ -n "${CXX}" ]] || { CXX=g++; echo "[build] no CoreX clang++, falling back to g++"; }
|
||||
echo "[build] CXX=${CXX}"
|
||||
|
||||
# Find CUDA headers (for cuda_runtime_api.h)
|
||||
CUDA_INC=""
|
||||
for candidate in \
|
||||
/usr/local/corex/include \
|
||||
/usr/local/cuda/include \
|
||||
/usr/local/corex/lib64/clang/16/include \
|
||||
; do
|
||||
if [[ -f "${candidate}/cuda_runtime_api.h" ]]; then
|
||||
CUDA_INC="${candidate}"
|
||||
@@ -43,7 +45,7 @@ for candidate in \
|
||||
fi
|
||||
done
|
||||
|
||||
# Find CUDA lib path for linking
|
||||
# Find CUDA libs
|
||||
CUDA_LIB=""
|
||||
for candidate in \
|
||||
/usr/local/corex/lib64 \
|
||||
@@ -55,57 +57,43 @@ for candidate in \
|
||||
fi
|
||||
done
|
||||
|
||||
if [[ -z "${CUDA_INC}" ]]; then
|
||||
echo "[WARN] cuda_runtime_api.h not found — trying compile anyway"
|
||||
fi
|
||||
|
||||
echo "[build] CUDA include: ${CUDA_INC:-system}"
|
||||
echo "[build] CUDA lib: ${CUDA_LIB:-system}"
|
||||
echo "[build] Source: ${SRC}"
|
||||
echo "[build] Output: ${OUT}"
|
||||
echo "[build] CUDA lib: ${CUDA_LIB:-system}"
|
||||
echo "[build] CCCL include: ${INC} ($(find "${INC}" -type f | wc -l) files)"
|
||||
echo "[build] Source: ${SRC}"
|
||||
echo "[build] Output: ${OUT}"
|
||||
|
||||
COMMON_FLAGS=(
|
||||
-shared -fPIC -O2 -std=c++17
|
||||
-I"${INC}"
|
||||
${CUDA_INC:+-I"${CUDA_INC}"}
|
||||
${CUDA_LIB:+-L"${CUDA_LIB}"}
|
||||
-lcudart -ldl
|
||||
# Suppress CCCL warnings that don't affect correctness
|
||||
-Wno-unused-function
|
||||
-Wno-unknown-pragmas
|
||||
# CUB needs these for non-NVCC compilers
|
||||
-D_CCCL_COMPILER_GCC=1
|
||||
-D__CUDA_ARCH_LIST__=700
|
||||
-DCUB_DISABLE_NAMESPACE_MAGIC
|
||||
-DCUB_WRAPPED_NAMESPACE=cccl_preload
|
||||
)
|
||||
|
||||
# Build as shared library
|
||||
# -x cuda or -x c++ depending on compiler
|
||||
if [[ "${CXX}" == *clang++* ]]; then
|
||||
# CoreX clang++ can compile .cu natively
|
||||
${CXX} \
|
||||
-shared -fPIC \
|
||||
-O2 \
|
||||
${CUDA_INC:+-I"${CUDA_INC}"} \
|
||||
${CUDA_LIB:+-L"${CUDA_LIB}"} \
|
||||
-lcudart \
|
||||
-ldl \
|
||||
-std=c++17 \
|
||||
-o "${OUT}" \
|
||||
"${SRC}"
|
||||
"${CXX}" "${COMMON_FLAGS[@]}" -x c++ -o "${OUT}" "${SRC}" 2>&1
|
||||
else
|
||||
# g++ needs .cu renamed or treated as C++
|
||||
# cuda_runtime_api.h should still work with host compiler
|
||||
${CXX} \
|
||||
-shared -fPIC \
|
||||
-O2 \
|
||||
${CUDA_INC:+-I"${CUDA_INC}"} \
|
||||
${CUDA_LIB:+-L"${CUDA_LIB}"} \
|
||||
-lcudart \
|
||||
-ldl \
|
||||
-std=c++17 \
|
||||
-x c++ \
|
||||
-o "${OUT}" \
|
||||
"${SRC}"
|
||||
"${CXX}" "${COMMON_FLAGS[@]}" -x c++ -o "${OUT}" "${SRC}" 2>&1
|
||||
fi
|
||||
|
||||
if [[ -f "${OUT}" ]]; then
|
||||
SIZE=$(stat -c%s "${OUT}" 2>/dev/null || stat -f%z "${OUT}" 2>/dev/null || echo "?")
|
||||
SIZE=$(stat -c%s "${OUT}" 2>/dev/null || echo "?")
|
||||
echo ""
|
||||
echo "[build] SUCCESS: ${OUT} (${SIZE} bytes)"
|
||||
echo ""
|
||||
echo "Usage:"
|
||||
echo " LD_PRELOAD=${OUT} CCCL_ALLOC_DEBUG=1 python3 -c 'import torch; t=torch.zeros(1024, device=\"cuda\")'"
|
||||
echo ""
|
||||
echo "In computility-run.yaml, add to env:"
|
||||
echo " - name: LD_PRELOAD"
|
||||
echo " value: /workspace/qwen3_6_scripts/cccl_preload/libcccl_allocator.so"
|
||||
echo " - name: PYTORCH_CUDA_ALLOC_CONF"
|
||||
echo " value: expandable_segments:True"
|
||||
echo "Test:"
|
||||
echo " LD_PRELOAD=${OUT} CCCL_ALLOC_DEBUG=1 \\"
|
||||
echo " PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True \\"
|
||||
echo " python3 verify_preload.py"
|
||||
else
|
||||
echo "[build] FAILED"
|
||||
exit 1
|
||||
|
||||
Reference in New Issue
Block a user