2014-06-05 76 views
0

我正在写在CUDA的程序,并且该问题如下:计算2个矩阵之间的欧几里德距离在CUDA

  • 两个矩阵A(N * 128)和B(M * 128)

  • 我拿A的第一行,然后我逐个计算该向量和B的所有行之间的距离。

  • 我写上的矩阵C的一个行的每个距离的结果,所以该元件C(I,J)C的包含行i A和B.

的行j之间的距离

- 和我着手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(); 
} 
+1

'(ty Levans

+0

另外:条件似乎不对:'for(int stride = SIZE/2; stride <0; stride >> = 1)' –

+0

@Levans:对不起,'pas'是'stride'。我只是纠正它。 – Madhatter

回答

2

条件看起来错误:

for (int stride = SIZE/2 ; stride < 0 ; stride >>= 1) 

假设SIZE是128,如你所说,这将不会被执行。另外,如果语句内__synchthread可能停滞整个事情


编辑:阅读OP的意见后,我意识到这是一个语言的问题..这里是一个片段:

#include <iostream> 
using namespace std; 

int main() { 

    int SIZE = 128; 

    for (int stride = SIZE/2 ; stride < 0 ; stride >>= 1) 
     cout << "Hello I'm running" << endl; 



    return 0; 
} 

http://ideone.com/AyhXYF

输出结果是:没有。看看C++中的for loop syntax,第二部分是应持续循环的整个持续时间的条件。如果你以假条件开始,你的循环永远不会被执行。

+0

我刚将'pas'改成'stride'。抱歉,是我的错。那为什么这个条件错了? – Madhatter

+0

@ Jeb11他的回答仍然有效,你是不是指'步幅> 0'而不是'步幅<0'? – Levans

+0

@Levans现在正在用'stride> 0'工作,谢谢。我想我要删除这个帖子,解决方案是非常明显的.. – Madhatter

相关问题