我有一个接收展平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平面阵列还是在这段代码中出现错误。
在你的代码块中的所有线程写:
你用这个内核运行每个块有多少个线程? – talonmies 2013-03-22 14:59:34
20块500线程或10块千块 – Anoracx 2013-03-22 15:07:50
好吧,为什么块中的每个线程都试图将数据加载到共享内存数组中?正如所写的那样,您所显示的那些粗略的内核代码毫无意义,我担心...... – talonmies 2013-03-22 16:27:04