CUDA Thrust Min_Element结果等于0

问题描述 投票:0回答:1

然而,CUDA和C ++的新功能已经解决了我注意到的一些问题。我想在CUDA中生成最小的数字和索引。目前我有

    __global__ void updateGB2(Particle *dev_p) {
    int i = threadIdx.x + blockIdx.x *blockDim.x;

    globalB[i] = dev_p[i].localBest;

    double *result = thrust::min_element(thrust::device,globalB, globalB + pSize);
    printf("%lf", result);
}

并且正在调用此方法,但结果仅打印0.0000。我可能错过了使用推力的一些信息,但从我读过的信息中我不确定还能做什么。 globalB定义为设备,粒子从CPU传递到GPU。

c++ cuda thrust
1个回答
1
投票

引用documentation的推力

min_element找到范围[first,last]中的最小元素。它返回[first,last]中的第一个迭代器i,使得[first,last]中没有其他迭代器指向小于* i的值。

在您的代码中,这意味着result是一个必须取消引用才能访问最小值的指针。一个完整的例子:

#include <cstdio>
#include <thrust/device_vector.h>
#include <thrust/extrema.h>
#include <thrust/copy.h>

__global__ void updateGB2(double *data, int pSize) {
    int i = threadIdx.x + blockIdx.x *blockDim.x;

    double* globalB = data + (i * pSize);
    double* result = thrust::min_element(thrust::device, globalB, globalB + pSize);
    printf("%d %lf\n", i, *result);
}

int main() 
{
    const int pSize = 16;
    const int Nvectors = 32;
    const int Nvals = Nvectors * pSize;

    {
        thrust::device_vector<double> dv(Nvals);

        thrust::counting_iterator<double> counter(10);
        thrust::copy(counter, counter+Nvals, dv.begin());

        double* d_h = thrust::raw_pointer_cast(dv.data());
        updateGB2<<<1, Nvectors>>>(d_h, pSize);
        cudaDeviceSynchronize();
    }
    cudaDeviceReset();

    return 0;
}

编译和运行如下:

$ nvcc -arch=sm_52 -o thrustdevice thrustdevice.cu 
$ ./thrustdevice 
0 10.000000
1 26.000000
2 42.000000
3 58.000000
4 74.000000
5 90.000000
6 106.000000
7 122.000000
8 138.000000
9 154.000000
10 170.000000
11 186.000000
12 202.000000
13 218.000000
14 234.000000
15 250.000000
16 266.000000
17 282.000000
18 298.000000
19 314.000000
20 330.000000
21 346.000000
22 362.000000
23 378.000000
24 394.000000
25 410.000000
26 426.000000
27 442.000000
28 458.000000
29 474.000000
30 490.000000
31 506.000000
© www.soinside.com 2019 - 2024. All rights reserved.