强制CUDA的推力:: reduce执行没有并行性

lan*_*dau 2 cuda thrust

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

thrust::device_ptr<double> tmp(aux);
double my_sum = thrust::reduce(tmp, tmp + G);
Run Code Online (Sandbox Code Playgroud)

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

tal*_*ies 6

您应该能够通过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();
Run Code Online (Sandbox Code Playgroud)

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