Как связать массив float * с 1D текстурой в cuda?
Я пытаюсь понять, как использовать текстурную память, связывая ее с массивом линейных устройств (не cudaArray). Мой код прост (ниже). У меня есть массив float * из 8 чисел, который я пытаюсь привязать к 1D-текстуре, а затем в своей функции ядра я пытаюсь считывать текстуру и помещать значения в выходной массив. Но когда я запускаю этот тест, все значения в моем выходном массиве равны нулю:
Ввод = 0.000000 1.000000 2.000000 3.000000 4.000000 5.000000 6.000000 7.000000
Выход = 0,000000 0,000000 0,000000 0,000000 0,000000 0,000000 0,000000 0,000000
Что мне здесь не хватает?
texture<float, 1, cudaReadModeElementType> texInput;
__global__ void copyKernel(float*output, int n) {
for (int i = 0; i < n; i++) {
output[i] = tex1D(texInput, (float)i);
}
}
int main(int argc, char*argv[]) {
const int WIDTH = 8;
float* hInput = (float*)malloc(sizeof(float) * WIDTH);
float*hOutput = (float*)malloc(sizeof(float) * WIDTH);
for (int i = 0; i < WIDTH; i++) {
hInput[i] = (float)i;
}
float* dInput = NULL, *dOutput = NULL;
size_t offset = 0;
texInput.addressMode[0] = cudaAddressModeBorder;
texInput.addressMode[1] = cudaAddressModeBorder;
texInput.filterMode = cudaFilterModePoint;
texInput.normalized = false;
checkCudaErrors(cudaMalloc((void**)&dInput, sizeof(float)*WIDTH));
checkCudaErrors(cudaMalloc((void**)&dOutput, sizeof(float)*WIDTH));
cudaMemcpy(dInput, hInput, sizeof(float)*WIDTH, cudaMemcpyHostToDevice);
cudaBindTexture(&offset, texInput, dInput, sizeof(float)*WIDTH);
copyKernel<<<1,1>>>(dOutput, WIDTH);
cudaMemcpy(hOutput, dOutput, sizeof(float)*WIDTH, cudaMemcpyDeviceToHost);
printf("\nInput = ");
for (int i = 0; i < WIDTH; i++) {
printf("%f\t",hInput[i]);
}
printf("\nOutput = ");
for (int i = 0; i < WIDTH; i++) {
printf("%f\t",hOutput[i]);
}
return 0;
}