zoukankan      html  css  js  c++  java
  • CUDA编程学习(三)

    我们知道一个grid包含多个block,而一个block又包含多个thread,下面将是如何进行下thread中的并行。

    /**** Splot a block into parallel threads****/
    
    _global_ void add(int *a, int *b, int *c)
    {
        c[threadIdx.x] = a[threadIdx.x] + b[threadIdx.x];
    }
    
    #define N 512
    
    int main()
    {
        int *a, *b, *c;            //host copies of a, b, c
        int *d_a, *d_b, *d_c;    //device copies of a, b, c
        int size = N * sizeof(int);
        
        //Alloc space for device copies of a, b, c
        cudaMalloc((void **)&d_a, size);
        cudaMalloc((void **)&d_b, size);
        cudaMalloc((void **)&d_c, size);
        
        //Alloc space for host copies of a, b, c and setup input values
        a = (int *)malloc(size); random_ints(a, N);
        b = (int *)malloc(size); random_ints(b, N);
        c = (int *)malloc(size); 
        
        //Copy the data into device
        cudeMemcpy(d_a, a, size, cudaMemcpyHostToDevice);
        cudaMemcpy(d_b, b, size, cudaMemcpyHostToDevice);
        
        //Launch add() kernel on GPU with N blocks
        add<<<1,N>>>(d_a, d_b, d_c);
        
        //Copy result back to host
        cudaMemcpy(c, d_c, size, cudaMemcpyDeviceToHost);
        
        //Cleanup
        free(a); free(b); free(c);
        cudeFree(d_a); cudaFree(d_b); cudaFree(d_c);
        return 0;
    
    }
    
    
    /**** What's the function of random_ints****/
    void random_ints(int* a, int N)
    {
     int i;
     for (i = 0; i < N; ++i)
     a[i] = rand();
    }

     

    重点语句变化: grid下的 add<<<1,1>>>(d_a, d_b, d_c)  到block下的 add<<<N,1>>>(d_a, d_b, d_c); 最后到 thread下   add<<<1,N>>>(d_a, d_b, d_c);

  • 相关阅读:
    基于麦克风阵列的声源定位算法之GCC-PHAT
    Parametric and Non-parametric Algorithms
    MATLAB中运算符优先级
    [HAOI2018]染色
    [SHOI2016]黑暗前的幻想乡
    [SCOI2012]滑雪
    [PA2014]Kuglarz
    Stroll
    [SDOI2010]大陆争霸
    解决IDEA Gradle构建报错"Cause: zip END header not found"
  • 原文地址:https://www.cnblogs.com/lxy2017/p/4130477.html
Copyright © 2011-2022 走看看