2

我在设备全局内存中有一个大型字符数组,由线程以合并方式访问。我在某处读过,我可以通过在每个线程的一个内存事务中读取 4 或 16 个字符来加快内存访问。我相信我必须使用纹理和 char4 或 int4 结构。但是,我找不到任何关于此的文档或示例。有人可以在这里提供一个简单的示例或指向我可以在哪里了解更多信息的指针吗?

在我的代码中,我将 char 数组定义为

char *database = NULL;
cudaMalloc( (void**) &database, SIZE * sizeof(char) );

如果我想使用纹理和 char4(或 int4),定义是什么?

非常感谢。

4

1 回答 1

1

我终于找到了自己问题的答案。char4 的定义是

char4 *database = NULL;
cudaMalloc( (void**) &database, SIZE * sizeof(char4)/4 );

不需要纹理。内核确实使用 char4 将速度提高了三倍,但如果我进行循环展开,则减少到两倍。为了完整起见,我的内核是

__global__ void kernel(unsigned int jobs_todo, char* database, float* results ) {

  unsigned int id = threadIdx.x + blockIdx.x * blockDim.x;
  float A = 0; int i; char ch;
  if(id < jobs_todo) {
    for(i = 0; i < 1000; i += 1){
     ch = database[jobs_todo*i + id];
     if(ch == 'A') A++;
    }
    results[id] = A;
  }
}

和 char4 它是

__global__ void kernel4(unsigned int jobs_todo, char4* database, float* results ) {

  unsigned int id = threadIdx.x + blockIdx.x * blockDim.x;
  float A = 0; int i; char4 ch4;
  if(id < jobs_todo) {
    for(i = 0; i < 1000/4; i += 1){
     ch4 = database[jobs_todo*i + id];
     if(ch4.x == 'A') A++;
     if(ch4.y == 'A') A++;
     if(ch4.z == 'A') A++;
     if(ch4.w == 'A') A++;
    }
    results[id] = A;
  }
}

我也试过 int4 但它比 char4 时间快 0.0002 秒。

于 2013-01-01T21:11:15.163 回答