2014-03-29 92 views
0

我似乎在使用CUDA中的紋理對象時遇到了一些困難。我從here獲取了代碼,並簡化了它並充實了一下。當我去建立它時,我得到錯誤「類型名稱是不允許的」。它發生在我的代碼中的第18行,有沒有人有任何想法爲什麼是這樣的情況?CUDA紋理對象問題

#include <cuda_runtime.h> 
#include <texture_fetch_functions.h> 
#include <cuda_texture_types.h> 
#include <texture_indirect_functions.h> 
#include <cuda.h> 
#include "device_launch_parameters.h" 
#include <vector> 

#include <iostream> 
#include <cstdlib> 
#include <cstring> 

#define L 16384 

__global__ void read(cudaTextureObject_t t, float *b){ 
    float offset = blockIdx.x + 0.5f; 
    b[blockIdx.x] = tex2D<float>(t, offset, 0.5f); 
} 

int main(){ 
    //device memory and host memory allocation 
    cudaChannelFormatDesc channelFormat = cudaCreateChannelDesc(32, 0, 0, 0, cudaChannelFormatKindFloat); 
    cudaArray *dev_buff_a; 
    float *dev_buff_b, *hst_buff, *print_buff; 
    hst_buff = (float *)malloc(L * sizeof(float)); 
    print_buff = (float *)malloc(L * sizeof(float)); 
    cudaMallocArray(&dev_buff_a, &channelFormat, L, 1); 
    cudaMalloc(&dev_buff_b, L * sizeof(float)); 
    for(int i = 0; i < L; i++){ 
     hst_buff[i] = 1.0f; 
    } 
    // 
    cudaMemcpyToArray(dev_buff_a, 0, 0, hst_buff, L * sizeof(float), cudaMemcpyHostToDevice); 

    //creating the texture object 
    //start with the resource descriptor 
    cudaResourceDesc resource; 
    memset(&resource, 0, sizeof(resource)); 
    resource.resType = cudaResourceTypeArray; 
    resource.res.array.array = dev_buff_a; 
    /*resource.res.linear.desc.f = cudaChannelFormatKindFloat; //channel format 
    resource.res.linear.desc.x = 32; //bits per channel 
    resource.res.linear.sizeInBytes = L * sizeof(float);*/ 

    //next, is the texture descriptor 
    cudaTextureDesc texDesc; 
    memset(&texDesc, 0, sizeof(texDesc)); 
    texDesc.readMode = cudaReadModeElementType; 

    //to create the actual texture object 
    cudaTextureObject_t tObj = 0; 
    cudaCreateTextureObject(&tObj, &resource, &texDesc, NULL); 

    //perform reading function 
    dim3 block(1, 0, 0); 
    dim3 grid(16384, 0, 0); 
    read<<<grid, block>>>(tObj, dev_buff_b); 

    //copy stuff over from dev_buff_b to print 
    cudaMemcpy(print_buff, dev_buff_b, L * sizeof(float), cudaMemcpyDeviceToHost); 

    //print out the arrays and compare 
    std::cout << "the original array was:\n"; 
    for(int i = 0; i < L; i++){ 
     std::cout << "element " << i << "is: " << hst_buff[i] << "\n"; 
    } 

    std::cout << "the new array is:\n"; 
    for(int i = 0; i < L; i++){ 
     std::cout << "element " << i << "is: " << print_buff[i] << "\n"; 
    } 

    //destroy the texture object 
    cudaDestroyTextureObject(tObj); 
    //free device memory 
    cudaFreeArray(dev_buff_a); 
    cudaFree(dev_buff_b); 

    return 0; 
} 
+0

我無法重現這一點 - 您的代碼完全適合我編譯。你使用的是什麼CUDA版本和主機編譯器? – talonmies

+0

我正在編譯VS2012中的CUDA 5.5。我正在使用的GPU是Geforce GT 630M。 – user3055073

+0

此外,我試圖編譯「Bindless Texture」示例程序,並且編譯成功。我覺得我正在服用瘋狂的藥丸...... – user3055073

回答

2

您需要確保遵循以下兩點:

  • 你使用CUDA 5.0或更新版本
  • 編譯器設置僅編譯計算能力3.0或更高的設備( '-arch compute_30'標誌爲nvcc)

紋理對象只適用於這些較新的設備。

+0

我在Geforce GT 630M上使用CUDA 5.5。我不完全確定630M是否是開普勒GPU,雖然... – user3055073

+0

https://developer.nvidia.com/cuda-gpus說,它不是,所以沒有紋理對象可以在此設備上使用。 – kunzmi

+0

那麼回到我參考的紋理參考? – user3055073