0

I seem to be having some difficulty in the use of texture objects in CUDA. I took the code from here and simplified it and fleshed it out a bit. When I go to build it I get the error "type name is not allowed". It occurs on line 18 in my code, does anyone have any idea why that is the case?

#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;
}
3
  • I can't reproduce this - your code compiles perfectly for me. What CUDA version and host compiler are you using? Commented Mar 30, 2014 at 6:34
  • I am compiling this in VS2012 on CUDA 5.5. The GPU I am running on is a Geforce GT 630M. Commented Mar 30, 2014 at 20:34
  • Also, I have attempted to compile the "Bindless Texture" sample program, and that compiled successfully. I feel like I am taking crazy pills... Commented Mar 30, 2014 at 20:52

1 Answer 1

2

You need to make sure to follow these two things:

  • You're using Cuda 5.0 or newer
  • Compiler settings are to compile only for devices of compute capability 3.0 or better ('-arch compute_30' flag for nvcc)

Texture objects are only available on these newer devices.

Sign up to request clarification or add additional context in comments.

3 Comments

I am using CUDA 5.5 on a Geforce GT 630M. I am not entirely sure if the 630M is a Kepler GPU or not though...
developer.nvidia.com/cuda-gpus says that it's not, so no texture objects can be used on this device.
So back to texture references I go?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.