Why does cuda calculate each index in vector addition?

回眸只為那壹抹淺笑 提交于 2020-06-23 20:36:51

问题


In the following example of adding vectors, why should add the statement (int tid = threadidx.x;)I know this is to determine the index of the thread, but I am not sure why the index should be assigned?

   __global__ void add(int * dev_a, int* dev_b, int* dev_c){
        int tid = threadIdx.x;     //index of thread
        if(tid<N)                  
        dev_c[tid] = dev_a[tid] + dev_b[tid];
    }

     int main(){    // main
            int a[N];
            int b[N];
            int c[N];

            for(int i=0;i<N;i++)
            a[i]=i;
            for(int i=0;i<N;i++)
            b[i]=i;           

            int* dev_a;
            int* dev_b;
            int* dev_c;

            cudaMalloc(&dev_a, sizeof(int)*N);
            cudaMalloc(&dev_b, sizeof(int)*N);
            cudaMalloc(&dev_c, sizeof(int)*N);

            cudaMemcpy(dev_a,a,sizeof(int)*N, cudaMemcpyHostToDevice); 
            cudaMemcpy(dev_b,b,sizeof(int)*N, cudaMemcpyHostToDevice);

            add<<<1,N>>>(dev_a,dev_b,dev_c); 

            cudaMemcpy(c, dev_c, sizeof(int)*N, cudaMemcpyDeviceToHost);

            for(auto x:c) std::cout<<x<<std::endl;  //printf result
        }

来源:https://stackoverflow.com/questions/62149638/why-does-cuda-calculate-each-index-in-vector-addition

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!