• cuda中当数组数大于线程数的处理方法


    参考stackoverflow一篇帖子的处理方法:https://stackoverflow.com/questions/26913683/different-way-to-index-threads-in-cuda-c

    代码中cuda_gridsize函数参考yolo。

    代码如下:

    #include "cuda_runtime.h"
    #include "device_launch_parameters.h"
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <iostream>
    #include <ctime>
    
    using namespace std;
    #define BLOCK 512
    
    dim3 cuda_gridsize(size_t n){
        size_t k = (n - 1) / BLOCK + 1;
        unsigned int x = k;
        unsigned int y = 1;
        if (x > 65535){
            x = ceil(sqrt(k));
            y = (n - 1) / (x*BLOCK) + 1;
        }
        dim3 d = { x, y, 1 };
        //printf("%ld %ld %ld %ld
    ", n, x, y, x*y*BLOCK);
        return d;
    }
    
    __global__ void gpuCalc(unsigned char *img,long H,long W)
    {
        long threadId_2D = threadIdx.x + threadIdx.y*blockDim.x;
        long blockId_2D = blockIdx.x + blockIdx.y*gridDim.x;
        long i = threadId_2D + (blockDim.x*blockDim.y)*blockId_2D;
        
        //另一种索引方式
        //long i = (gridDim.x*blockDim.x)*(threadIdx.y + blockDim.y*blockIdx.y) + (threadIdx.x + blockDim.x*blockIdx.x);
    
        while (i < H*W){
            img[i] = 255 - img[i];
            i += (gridDim.x*blockDim.x)*(gridDim.y*blockDim.y);
        }
    }
    
    void addWithCuda(unsigned char *img, long H,long W)
    {
        unsigned char *dev_a = 0;
    
        cudaSetDevice(0);
    
        cudaMalloc((void**)&dev_a, H*W * sizeof(unsigned char));
        cudaMemcpy(dev_a, img, H*W * sizeof(unsigned char), cudaMemcpyHostToDevice);
    
        gpuCalc<<<cuda_gridsize(H*W),BLOCK>> >(dev_a, H, W);
    
        cudaMemcpy(img, dev_a, H*W * sizeof(unsigned char), cudaMemcpyDeviceToHost);
        cudaFree(dev_a);
    
        cudaGetLastError();
    }
    
    void cpuCalc(unsigned char *img,long W, long H)
    {
        for (long i = 0; i < H*W; i++)
            img[i] = 255 - img[i];
    }
    
    int main()
    {
        long W = 20000;
        long H = 20000;
    
        unsigned char *img = new unsigned char[W*H];
        unsigned char *cmp = new unsigned char[W*H];
    
        for (long i = 0; i < H*W; i++)
            img[i] = rand() % 100;
    
        memcpy(cmp, img, H*W);
    
        cpuCalc(img, W, H);
        printf("cpu calc end
    ");
    
        addWithCuda(img, W,H);
        printf("gpu calc end
    ");
    
        bool flag = true;
        for (long i = 0; i < H*W; i++)
        {
            if (img[i] != cmp[i])
            {
                printf("no pass
    ");
                flag = false;
                break;
            }
        }
        if (flag)
            printf("pass");
    
        delete[] cmp;
        delete[] img;
        getchar();
    
        return 0;
    }
  • 相关阅读:
    大数据架构师技能图谱
    2018年,Java程序员转型大数据开发,是不是一个好选择?
    如何将java web项目上线/部署到公网
    Flume调优
    Spark流处理调优步骤
    zookeeper的WEB客户端zkui使用
    HBase各版本对Hadoop版本的支持情况
    java 代码实现使用Druid 链接池获取数据库链接
    安装postgreSQL出现configure: error: zlib library not found解决方法
    修改postgres密码
  • 原文地址:https://www.cnblogs.com/tiandsp/p/9460130.html
Copyright © 2020-2023  润新知