cudaMalloc和cudaMallocPitch

原文链接

偶有兴趣测试了一下题目中提到的这两个函数,为了满足对齐访问数据,咱们平时可能会用到cudamallocPitch,以为它会带来更高的效率。呵呵,这里给出一段测试程序,大家可以在自己的机器上跑跑,你会发现这两个函数在某些情况下是一样的。

[cpp] view plaincopy
 
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <cuda_runtime_api.h>  
  4.   
  5. int main(int argc, char **argv)  
  6. {  
  7.     // device pointers.  
  8.     float *d_pitch;  
  9.     float *d_normal;  
  10.   
  11.     // matrix size.  
  12.     size_t cols = 63;  
  13.     size_t rows = 16;  
  14.       
  15.     size_t pitch = 0;  
  16.       
  17.     // alloc the data form gpu memory.  
  18.     cudaMallocPitch((void**)&d_pitch, &pitch, cols*sizeof(float), rows);  
  19.     cudaMalloc((void**)(&d_normal), rows*cols*sizeof(float));  
  20.       
  21.     // test the data address.  
  22.     fprintf(stdout, "row size(in bytes) = %.2f*128. ", pitch/128.0f);  
  23.     fprintf(stdout, "the head address of d_pitch  mod 128 = %x. ", ((unsigned int)d_pitch)%128);  
  24.     fprintf(stdout, "the head address of d_normal mod 128 = %x. ", ((unsigned int)d_normal)%128);  
  25.       
  26.     cudaFree(d_pitch);  
  27.     cudaFree(d_normal);  
  28.   
  29.     getchar();  
  30.     return 0;  
  31. }  

上面这段程序的运行结果如下:

[cpp] view plaincopy
 
  1. row size(in bytes) = 28.00*128.  
  2. the head address of d_pitch mod 128 = 0.  
  3. the head address of d_normal mod 128 = 0.   

我多次做过实验,我觉得从以上实验结果可以知道,无论如何改变实验的参数,两个显存申请函数返回的数据首地址都是128,256的整数倍,我猜想GPU上的每个计算单元的数据在全局中加载的时候一次可以连续加载2的幂次个数据,并且这些数据的加载其实地址一定也是2的幂次,所以warp使用全局内存中的数据的时候应该尽量按照对齐的原则加载数据,这样就可以获得更高的效率了。至于对齐原则可以在CUDA的编程手册中找到。

原文地址:https://www.cnblogs.com/liangliangdetianxia/p/4165946.html