2012-09-23 44 views
-1

我正在學習如何通過簡單的示例在CUDA中使用紋理。我嘗試了下面的例子,但它不工作。它顯示值0.CUDA紋理內存程序不能正常工作

#include "cuPrintf.cu" 
texture<int,1,cudaReadModeElementType> ref; 
__global__ void kernel(int *a) 
{ 
    int b=tex1D(ref,0); 
    cuPrintf("value is %d",b); 

} 

int main() 
{ 
    int *a; 
    cudaMalloc((void**)&a,32000*sizeof(int)); 
    cudaMemset(a,1,32000*sizeof(int)); 
    cudaChannelFormatDesc bit = cudaCreateChannelDesc<int>(); 
    cudaBindTexture(0,ref,a,bit,32000*sizeof(int)); 
    kernel<<<1,1>>>(a); 
} 
+3

此代碼不完整,並且根本不包含API錯誤檢查。發佈問題時請儘量多加一點。 – talonmies

回答

3

您已通過使用cudaMalloc函數分配了內存。如果將紋理綁定到分配了cudaMalloc的內存,則可以使用內核中的函數tex1Dfetch()訪問它。使用tex1D將返回零。

但如果你綁定紋理由cudaMallocPitchcudaArray分配的內存,那麼它使用內核內部tex1Dtex2D功能的訪問。