gpt4 book ai didi

cuda - 强制 CUDA 的 thrust::reduce 以非并行方式执行

转载 作者:行者123 更新时间:2023-12-02 08:21:14 25 4
gpt4 key购买 nike

我有一个使用 thrust::reduce 来并行求和的 CUDA 程序:例如,

thrust::device_ptr<double> tmp(aux);
double my_sum = thrust::reduce(tmp, tmp + G);

其中 double* aux 指向设备上的 G 连续 double 。我需要将整个并行化程序的运行时间与没有并行计算的版本进行比较。有没有办法在设备上仅使用一个线程来运行 thrust::reduce?全局开关将是最方便的选择。

最佳答案

您应该能够通过使用串行执行策略在内核中调用 thrust::reduce 然后使用单个线程启动该内核来实现这一点。像这样的东西:

__global__ void serial_reduce(double *result, double *aux, int G)
{
*result = thrust::reduce(thrust::seq, aux, aux+G);
}

double *result;
cudaMallocManaged(&result, sizeof(double));
serial_reduce<<<1,1>>>(result, aux, G);
cudaDeviceSynchronize();

[注意在浏览器中编写,完全未经测试,使用风险自负]

关于cuda - 强制 CUDA 的 thrust::reduce 以非并行方式执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36921696/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com