Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/151.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ CUDA,使用共享内存能提高我的性能吗?_C++_Performance_Cuda_Shared Memory - Fatal编程技术网

C++ CUDA,使用共享内存能提高我的性能吗?

C++ CUDA,使用共享内存能提高我的性能吗?,c++,performance,cuda,shared-memory,C++,Performance,Cuda,Shared Memory,我正在使用CUDA实现将图像转换为灰度的算法。我现在已经开始工作了,但我正在寻找提高性能的方法。 现在,整个彩色图像被传输到设备内存,然后每个线程通过查找相应的三个(r、g、b)颜色值来计算灰度像素值 我已经确保了对全局内存的访问是合并的,尽管这并没有真正提高我的性能(合并内存访问后36 mb映像的拍摄时间减少了0.003秒…)。现在,我想知道使用共享内存是否可以提高我的性能。以下是我现在拥有的: 我的CUDA内核: __global__ void darkenImage(const unsig

我正在使用CUDA实现将图像转换为灰度的算法。我现在已经开始工作了,但我正在寻找提高性能的方法。 现在,整个彩色图像被传输到设备内存,然后每个线程通过查找相应的三个(r、g、b)颜色值来计算灰度像素值

我已经确保了对全局内存的访问是合并的,尽管这并没有真正提高我的性能(合并内存访问后36 mb映像的拍摄时间减少了0.003秒…)。现在,我想知道使用共享内存是否可以提高我的性能。以下是我现在拥有的:

我的CUDA内核:

__global__ void darkenImage(const unsigned char * inputImage,
    unsigned char * outputImage, const int width, const int height, int iteration){

  int x = ((blockIdx.x * blockDim.x) + (threadIdx.x + (iteration * MAX_BLOCKS * nrThreads))) * 3;

  if(x+2 < (3 * width*height)){
    float grayPix = 0.0f;
    float r = static_cast< float >(inputImage[x]);
    float g = static_cast< float >(inputImage[x+1]);
    float b = static_cast< float >(inputImage[x+2]);

    grayPix = __fadd_rn(__fadd_rn(__fmul_rn(0.3f, r),__fmul_rn(0.59f, g)), __fmul_rn(0.11f, b));
    grayPix = fma(grayPix,0.6f,0.5f);


    outputImage[(x/3)] = static_cast< unsigned char >(grayPix);
  }
}
\uuuu全局\uuuuu无效暗图像(const unsigned char*inputImage,
无符号字符*输出图像,常量整数宽度,常量整数高度,整数迭代){
intx=((blockIdx.x*blockDim.x)+(threadIdx.x+(迭代*MAX_BLOCKS*nrThreads))*3;
如果(x+2<(3*宽*高)){
浮动灰度pix=0.0f;
浮动r=静态_转换(输入图像[x]);
float g=静态施法(输入图像[x+1]);
浮点b=静态_转换(输入图像[x+2]);
grayPix=uuuFadd_rn(uuFadd_rn(uufMul_rn(0.3f,r),uufMul_rn(0.59f,g)),uuufMul_rn(0.11f,b));
grayPix=fma(grayPix,0.6f,0.5f);
outputImage[(x/3)]=静态_cast(grayPix);
}
}
我的问题是,因为任何两个线程之间都没有共享内存,所以现在使用共享内存应该不会有什么帮助,对吗?还是我误解了

问候,


Linus

如果不多次使用相同的值,则使用共享内存(缓存)不会提高性能。但是您可以尝试删除
迭代
参数,并用每个块处理更多数据。尝试在内核中进行单个内核启动和循环,以便每个线程可以计算多个输出数据。

不,您是正确的,共享内存不会有任何帮助,因为您不会多次访问数据。

迭代值很少使用,仅用于超过特定(非常大)大小的图像,当无法分配足够的块来覆盖所有像素时,所以我不太担心这一点。