nvcc_lazy 2.7 KB
Newer Older
1
#!/usr/bin/env bash
2 3
unset GREP_OPTIONS
set -e
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
# 
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#     http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

## CUDA_MODULE_LOADING=EAGER,DEFAULT,LAZY

X
xiaoxiaohehe001 已提交
21 22 23
# set cicc PATH for Centos
export PATH=$PATH:/usr/local/cuda/nvvm/bin

24 25
# check nvcc version, if nvcc >= 11.7, just run nvcc itself
CUDA_VERSION=$(nvcc --version | grep -oP '(?<=V)\d*\.\d*')
26 27
CUDA_VERSION_MAJOR=${CUDA_VERSION%.*}
CUDA_VERSION_MINOR=${CUDA_VERSION#*.}
28
if (( CUDA_VERSION_MAJOR > 11 || (CUDA_VERSION_MAJOR == 11 && CUDA_VERSION_MINOR >= 7) )); then
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
  nvcc "$@"
  exit
fi

BUILDDIR=$(mktemp -d  /tmp/nvcc-lazy-build.XXXXXXXX)
echo "$@" > ${BUILDDIR}/args
BUILDSH=${BUILDDIR}/build.sh
/usr/local/cuda/bin/nvcc --dryrun --keep --keep-dir=${BUILDDIR} "$@" 2>&1 | sed -e 's/#\$ //;/^rm/d' > $BUILDSH
sed -i -e '/^\s*--/d' $BUILDSH
sed -ne '1,/^cicc.*cudafe1.stub.c/p' ${BUILDSH} > ${BUILDSH}.pre
sed -e '1,/^cicc.*cudafe1.stub.c/d' ${BUILDSH} > ${BUILDSH}.post

sed -i -e '/LIBRARIES=/{s/\s//g;s/""/ /g}' ${BUILDSH}.pre

/usr/bin/env bash ${BUILDSH}.pre
STUBF=$(find $BUILDDIR -name *.cudafe1.stub.c)
CUFILE=$(basename -s '.cudafe1.stub.c' $STUBF)
sed -i -e '/__sti____cudaRegisterAll.*__attribute__/a static void __try____cudaRegisterAll(int);' $STUBF
sed -i -e 's/__sti____cudaRegisterAll\(.*{\)/__do____cudaRegisterAll\1/' $STUBF
# sed -i -e "/__do____cudaRegisterAll\(.*{\)/a static void __try____cudaRegisterAll(int l){static int _ls = 0; if (_ls) return; const char* lm = getenv(\"CUDA_MODULE_LOADING\"); if (lm&&(lm[0]=='L')&&(lm[1]=='A')&&(lm[2]=='Z')&&(lm[3]=='Y')&&(l!=1)) return; _ls = 1; fprintf(stderr,\"===> ${CUFILE} lazy-load? %d\\\\n\", l); __do____cudaRegisterAll();}" $STUBF
sed -i -e "/__do____cudaRegisterAll\(.*{\)/a static void __try____cudaRegisterAll(int l){static int _ls = 0; if (_ls) return; const char* lm = getenv(\"CUDA_MODULE_LOADING\"); if (lm&&(lm[0]=='L')&&(lm[1]=='A')&&(lm[2]=='Z')&&(lm[3]=='Y')&&(l!=1)) return; _ls = 1; __do____cudaRegisterAll();}" $STUBF
sed -i -e '/__try____cudaRegisterAll\(.*{\)/a static void __sti____cudaRegisterAll(void){__try____cudaRegisterAll(0);}' $STUBF
sed -i -e 's/{\(__device_stub__\)/{__try____cudaRegisterAll(1);\1/' $STUBF
/usr/bin/env bash ${BUILDSH}.post
rm -rf $BUILDDIR