我使用bash使用下面的代碼來檢索CUDA程序內核:猛砸if語句的一個案子,而不是爲了其他
kername="kernelnames"
loop="loop"
filecp="filecp"
funct="funct"
truncate -s 0 $kername
truncate -s 0 $loop
truncate -s 0 $funct
counter=1
funct_display(){
count=0
flag=1
while read line ; do
echo "$line"
if [[ "$line" == "{" ]] ; then echo "$line"; count=$((count+1)) ; echo "$counter $count incre" ;fi
if [[ "$line" == "}" ]] ; then count=$((count-1)) ; echo "$counter $count decre" ;fi
if [[ $count > 0 ]] ; then echo "$line" >> $funct ;fi
if [[ $count == 0 ]] ; then flag=0 ; fi
if [[ $flag == 0 ]] ; then break ; fi
#if [[ $count == 0 ]] ; then counter=$((counter+1)); fi
counter=$((counter+1))
done < $filecp
echo "}" >> $funct
echo "" >>$funct
}
file_copy(){
truncate -s 0 $filecp
while read line; do
echo "$line" >> $filecp
done < $filename
}
read -p "Enter the name of the file to be scanned: " filename
var=$(grep "__global__" < $filename | wc -l)
echo "kernels" >> $kername
for((i=1;i<=$var;i++))
do
temp1=$(grep -m$i "__global__" < $filename | tail -n1)
a=$(grep -n -m$i "__global__" < $filename | cut -d : -f 1 | tail -n1)
j=$((i+1))
temp2=$(grep -m$j "__global__" < $filename | tail -n1)
b=$(grep -n -m$j "__global__" < $filename | cut -d : -f 1 | tail -n1)
echo "$i $temp1 $a $b" >> $kername
temp5=$(sed -n " $a , $b p" $filename)
echo "$temp5" >> $loop
#echo "$a"
while read line ; do
if [[ "$line" == "$temp1" ]] ; then
echo "$line " >> $funct
echo "$line"
file_copy
for((k=$a;k>0;k--))
do
echo "$k"
sed -i "${k}d" $filecp
done
funct_display
fi
done < $filename
#echo "$temp1"
done
以上的shell腳本正在爲一個程序,以下內核:
__global__ void addKernel(int *c, const int *a, const int *b)
{
int i = threadIdx.x;
c[i] = a[i] + b[i];
}
__global__ void subKernel(int *c, const int *a, const int *b)
{
int i = threadIdx.x;
c[i] = a[i] - b[i];
}
但它不會對程序有以下內核工作:
__global__ void Vec_Mat_mul(float *gV, float*gM, float *gR, int Vecsize, int Matcols)
{ int i, j;
float sum=0.0;
i=(blockIdx.x*blockDim.x)+threadIdx.x; //For each block access
if(i<Matcols)
{
for(j=0;j<Vecsize;j++)
sum=sum+(gV[j]*gM[j+(i*Vecsize)]);
__syncthreads(); //Synchronization Barrier
gR[i]=sum;
}
}
我認爲它沒有進入if語句:if [[「$ line」==「{」]]出於某種原因,但我不明白爲什麼。先謝謝你!
是的,你說得對,'if [[「$ l {ine}」==「{」]]'只需要'{'字符就行。嘗試'if [[「$ {line}」〜=「{」]]''。但是這將匹配線上任何地方的「{」。這將會更容易和高效地將其轉換爲awk,但看起來你已經投入了大量時間,所以對你的情況最好。祝你好運。 – shellter