这个问题与我几周前发布的一个现有问题有关:TERCOM 算法 - 从单线程更改为 CUDA 中的多线程
简单解释一下,内核中的每个线程都会计算一个 MAD 值,我想知道最小值及其位置。
我试过像这样使用 atomicMin
__global__ void kernel (int m, int n, int h, int N, int *f, float heading, float *measurements, int *global_min)
{
int idx = blockIdx.x * blockDim.x + threadIdx.x;
int idy = blockIdx.y * blockDim.y + threadIdx.y;
float MAD=0;
float pos[2];
float theta=heading*(PI/180);
float fval = 0;
// Calculate how much to move in x and y direction
float offset_x = h*cos(theta);
float offset_y = -h*sin(theta);
//Calculate Mean Absolute Difference
if(idx < n && idy < m)
{
for(float g=0; g<N; g++)
{
float fval = tex2D (tex, idx+(g-2)*offset_x+0.5f, idy+(g-2)*offset_y+0.5f);
MAD += abs(measurements[(int)g]-fval);
}
}
cuPrintf("%.2f \n",MAD);
atomicMin(global_min, MAD);
pos[0]=idx;
pos[1]=idy;
f[0]=*global_min;
f[1]=pos[0];
f[2]=pos[1];
}
它会产生正确的结果,但 atomicMin 无法找到最小值的位置。
我也尝试使用推力库
__global__ void kernel (int m, int n, int h, int N, int *f, float heading, float *measurements, int *global_min, float *dev_MAD)
{
int idx = blockIdx.x * blockDim.x + threadIdx.x;
int idy = blockIdx.y * blockDim.y + threadIdx.y;
float theta=heading*(PI/180);
float fval = 0;
// Calculate how much to move in x and y direction
float offset_x = h*cos(theta);
float offset_y = -h*sin(theta);
//Calculate Mean Absolute Difference
if(idx < n && idy < m)
{
for(float g=0; g<N; g++)
{
float fval = tex2D (tex, idx+(g-2)*offset_x+0.5f, idy+(g-2)*offset_y+0.5f);
*dev_MAD += abs(measurements[(int)g]-fval);
}
}
cuPrintf("%.2f \n",MAD);
}
像这样调用内核
kernel <<< dimGrid,dimBlock >>> (m, n, h, N, dev_results, heading, dev_measurements, global_min, dev_MAD);
thrust::device_ptr<float> dev_ptr(dev_MAD);
thrust::device_ptr<float> min_pos = thrust::min_element(dev_ptr, dev_ptr + n*m);
int abs_pos = min_pos - dev_ptr;
float min_val=min_pos[0];
cudaMemcpy(&min_val, dev_MAD+abs_pos, sizeof(float), cudaMemcpyDeviceToHost);
// Print out the result
printf("Min=%.2f pos=%d\n",min_val,abs_pos);
但是这个程序打印出来: Min=-207521258711807190000000000000000000000.00 pos=0
我看过很多减少示例,但似乎每个人都将值存储在数组中,而不是在每个单独的线程中。
所以对于问题:
- 是否可以让 atomicMin 函数返回位置?
- 谁能给我一个关于如何解决推力库问题的提示?