debugging 如何调试CUDA google colab笔记本电脑?

nc1teljy  于 2022-12-23  发布在  Go
关注(0)|答案(2)|浏览(159)

我试图运行一个c程序使用cuda的代码做一些数学运算的一个数组的连续数字(其中每个线程添加元素的一行,并检查最后一个数组元素,并返回一个值的总和或零,如果条件得到满足).我没有NVIDIA GPU,所以我写我的代码在谷歌colab笔记本电脑.
我遇到的问题是无法调试程序。它什么也没有输出,没有错误信息,也没有输出。代码有问题,但我检查了几次后不知道哪里出了问题。
下面是代码:

#include <iostream>

__global__ void matrixadd(int *l,int *result,int digits ,int possible_ids )

{  
    int sum=0;
    int zeroflag=1;
    int identicalflag=1;
    int id=  blockIdx .x * blockDim .x + threadIdx .x;
 
if(id<possible_ids)
{
    if (l[(digits*id)+digits-1]==0) zeroflag=0;/*checking if the first number is zero*/

    for(int i=0; i< digits-1;i++)/*edited:for(int i=0; i< digits;i++) */
          {
            
            if(l[(digits*id)+i]-l[(digits*id)+i+1]==0)
            identicalflag+=1; /* checking if 2 consequitive numbers are identical*/

            sum = sum + l[(digits*id)+i]; /* finding the sum*/
         }
    if (identicalflag!=1)identicalflag=0;
    result[id]=sum*zeroflag*identicalflag;
}
}

int main()
{
     int digits=6;
     int possible_ids=pow(10,digits);
/*populate the array */
int* a ;
 a= (int *)malloc((possible_ids * digits) * sizeof(int));
 int the_id,temp=possible_ids;

  for (int i = 0; i < possible_ids; i++) 
    { 
        temp--;
        the_id=temp;
        for (int j = 0; j < digits; j++)
        {  
        a[i * digits + j] = the_id % 10;    
        if(the_id !=0) the_id /= 10;
        }

    }
 /*the numbers will appear in reversed order  */

/*allocate memory on host and device then invoke the kernel function*/
    int *d_a,*d_c,*c;
    int size=possible_ids * digits;
    c= (int *)malloc(possible_ids * sizeof(int));/*results matrix*/

    cudaMalloc((void **)&d_a,size*sizeof(int));
    cudaMemcpy(d_a,a,size*sizeof(int),cudaMemcpyHostToDevice);
    cudaMalloc((void **)&d_c,possible_ids*sizeof(int));
/*EDITED: cudaMalloc((void **)&d_c,digits*sizeof(int));*/
 
matrixadd<<<ceil(possible_ids/1024.0),1024>>>(d_a,d_c,digits,possible_ids);
cudaMemcpy(c,d_c,possible_ids*sizeof(int),cudaMemcpyDeviceToHost);

 int acc=0;
for (int k=0;k<possible_ids;k++)
{
    if (c[k]==7||c[k]==17||c[k]==11||c[k]==15)continue;
    acc += c[k];
 }
printf("The number of possible ids %d",acc);
}
3phpmpom

3phpmpom1#

在以下代码行中,您对l数组执行了无效索引:if(l[(digits*id)+i]-l[(digits*id)+i+1]==0)
来自罗伯特·科维拉的评论

kb5ga3dv

kb5ga3dv2#

你可以使用'pdb'内置断点函数.把下面的命令行放在你脚本的顶端.

import pdb

然后在要调试的行之前放置以下命令

pdb.set_trace()

你会得到'(pdb),then empty box'来插入命令。2如果你想继续到下一行,输入'n'或者你可以使用's'来查看你当前行命令的详细工作。
好好享受吧!

相关问题