我似乎在使用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;
}
我無法重現這一點 - 您的代碼完全適合我編譯。你使用的是什麼CUDA版本和主機編譯器? – talonmies
我正在編譯VS2012中的CUDA 5.5。我正在使用的GPU是Geforce GT 630M。 – user3055073
此外,我試圖編譯「Bindless Texture」示例程序,並且編譯成功。我覺得我正在服用瘋狂的藥丸...... – user3055073