我正在写在CUDA的程序,并且该问题如下:计算2个矩阵之间的欧几里德距离在CUDA
两个矩阵A(N * 128)和B(M * 128)
我拿A的第一行,然后我逐个计算该向量和B的所有行之间的距离。
我写上的矩阵C的一个行的每个距离的结果,所以该元件C(I,J)C的包含行i A和B.
- 和我着手A.
我实现这种方式的下一行: 我得由(N * M)块,每块128个线程做了一个网格。 (1 * 128)。
该程序正在编译,但问题是它没有提供良好的距离。 我想不通自己做错了什么......
PS:我有CUDA 6.0了NVIDIA GTX 650(copute能力3.0)
__global__ void EuclidianDistances(float *A, float *B , float *C , int n , int m)
{
// SIZE is equal to 128
__shared__ float accumResult[SIZE];
__shared__ float sA[SIZE];
__shared__ float sB[SIZE];
// MAPPING
int bx = blockIdx.x; // n
int by = blockIdx.y; // m
int ty = threadIdx.y; // 128
int tx = threadIdx.x; // 1
sA[ty] = A [bx * SIZE + ty];
sB[ty] = B [by * SIZE + ty];
__syncthreads();
accumResult[ty] = (sA[ty] - sB[ty])*(sA[ty] - sB[ty]);
__syncthreads();
// Parallel tree-reduction
for (int stride = SIZE/2 ; stride < 0 ; stride >>= 1)
if (ty < stride)
{
accumResult[ty] += accumResult [stride + ty];
__syncthreads();
}
// Writing results to output matrix
if ((threadIdx.y == 0))
C [bx * m + by] = accumResult[ty];
__syncthreads();
}
'(ty
Levans
另外:条件似乎不对:'for(int stride = SIZE/2; stride <0; stride >> = 1)' –
@Levans:对不起,'pas'是'stride'。我只是纠正它。 – Madhatter