我似乎在CUDA中使用纹理对象时遇到了一些困难。我从here获取了代码并对其进行了简化并对其进行了充实。当我去构建它时,我得到错误“不允许输入类型名称”。它发生在我的代码的第18行,是否有人知道为什么会这样?
#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 :(得分:2)
您需要确保遵循以下两点:
纹理对象仅适用于这些较新的设备。