CUDA统计直方图

#include <iostream>
#include <cuda.h>
#include <curand_kernel.h>

const int N = 1 << 16; // 数据点数量
const int bins = 256;  // 直方图的箱数

__global__ void histogramKernel(unsigned char *data, int *hist, int n) {
    // 每个线程计算一个数据点的直方图
    int idx = blockIdx.x * blockDim.x + threadIdx.x;
    if (idx < n) {
        atomicAdd(&hist[data[idx]], 1); //假设data[idx] = 5,hist[5]代表第6个数数量加1,
    }
}

void generateRandomData(unsigned char *data, int n) {
    for (int i = 0; i < n; i++) {
        data[i] = rand() % bins; // 生成随机数
    }
}

int main() {
    unsigned char *h_data = new unsigned char[N];
    int *h_hist = new int[bins]();
    unsigned char *d_data;
    int *d_hist;

    // 生成随机数据
    generateRandomData(h_data, N);

    // 分配设备内存
    cudaMalloc(&d_data, N * sizeof(unsigned char));
    cudaMalloc(&d_hist, bins * sizeof(int));
    cudaMemset(d_hist, 0, bins * sizeof(int));

    // 将数据从主机复制到设备
    cudaMemcpy(d_data, h_data, N * sizeof(unsigned char), cudaMemcpyHostToDevice);

    // 启动CUDA内核
    int blockSize = 256;
    int numBlocks = (N + blockSize - 1) / blockSize;
    histogramKernel<<<numBlocks, blockSize>>>(d_data, d_hist, N);

    // 将结果从设备复制到主机
    cudaMemcpy(h_hist, d_hist, bins * sizeof(int), cudaMemcpyDeviceToHost);

    // 打印直方图结果
    for (int i = 0; i < bins; i++) {
        std::cout << "Bin " << i << ": " << h_hist[i] << std::endl;
    }

    // 清理内存
    delete[] h_data;
    delete[] h_hist;
    cudaFree(d_data);
    cudaFree(d_hist);

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值