2013-03-22 93 views
0

我有一个接收展平2D数组的内核,并且我想每次共享内存时复制一行数组,我的内核看起来像如下:如何从CUDA中将全局内存中的展平2D数组复制到共享内存中

__global__ void searchKMP(char *test,size_t pitch_test,int ittNbr){ 
    int tid = blockDim.x * blockIdx.x + threadIdx.x; 
    int strideId = tid * 50; 

    int m = 50; 

    __shared__ char s_test[m]; 

    int j; 
       //this loops over the number of lines in my 2D array   
        for(int k=0; k<ittNbr; k++){ 

        //this loops to store my flattened (basically threats 1 line at a time) array into shared memory  
        if(threadIdx.x==0){ 
        for(int n =0; n<50; ++n){ 
        s_test[n] = *(((char*)test + k * pitch_test) + n); 

       } 
      } 
      __syncthreads(); 


      j=0; 

      //this is loop to process my shared memory array against another 1D array 
      for(int i=strideID; i<(strideID+50); i++{ 
      ...dosomething... 
      (increment x if a condition is met) 
      ...dosomething... 
      } 
      __syncthreads(); 
      if(x!=0) 
       cache[0]+=x; 

      ...dosomething... 

} 

尽管当我验证x的值时,x的值随所有线程的数量而变化,或者随着线程数而变化。例如,根据执行情况,当250个线程的20个块返回值7或6时,10个500线程块返回9。我不知道这个问题是来自在共享内存中复制的2D平面阵列还是在这段代码中出现错误。

在你的代码块中的所有线程写:

+0

你用这个内核运行每个块有多少个线程? – talonmies 2013-03-22 14:59:34

+0

20块500线程或10块千块 – Anoracx 2013-03-22 15:07:50

+0

好吧,为什么块中的每个线程都试图将数据加载到共享内存数组中?正如所写的那样,您所显示的那些粗略的内核代码毫无意义,我担心...... – talonmies 2013-03-22 16:27:04

回答

0

共享内存在同一块

不是很清楚,为什么你需要共享内存和你正在做什么和所有线程共享相同的价值观,以你的共享内存很多次,但它是冗余

与共享memeory工作常见的方式是这样的:

if(threadIdx.x < m) 
    s_test[threadIdx.x] = *(global_mem_pointer + threadIdx.x); 

__syncthreads(); 

块中的所有线程都会在“同一时刻”写入自己的值并在__syncthreads();之后,您的内存中充满了您所需要的内容,并且可以看到块中的所有线程

+0

我在代码中添加了一些评论,以便更清楚。 – Anoracx 2013-03-22 14:00:30

1

看起来您的共享内存中的数组有20个元素:

int m = 20; 
    __shared__ char s_test[m]; 

但在你的内部循环您正在尝试写50个元素:

for(int n =0; n<50; ++n){ 
     s_test[n] = *(((char*)test + k * pitch_test) + n); 

我不知道这是专门你要找的人的问题,但看起来像它赢得没有工作。

+0

这是对的,我确实犯了一个错误,因为我一直在更改代码以尝试找出代码错误的原因:/但感谢您指出这一点。 – Anoracx 2013-03-22 20:49:13