我想將Nx3數組傳遞給內核,並從紋理內存中讀取數據並寫入第二個數組。這是我用N簡化代碼= 8:二維數組CUDA 2D紋理CUDA
#include <cstdio>
#include "handle.h"
using namespace std;
texture<float,2> tex_w;
__global__ void kernel(int imax, float(*w)[3], float (*f)[3])
{
int i = threadIdx.x;
int j = threadIdx.y;
if(i<imax)
f[i][j] = tex2D(tex_w, i, j);
}
void print_to_stdio(int imax, float (*w)[3])
{
for (int i=0; i<imax; i++)
{
printf("%2d %3.6f\t %3.6f\t %3.6f\n",i, w[i][0], w[i][1], w[i][2]);
}
}
int main(void)
{
int imax = 8;
float (*w)[3];
float (*d_w)[3], (*d_f)[3];
dim3 grid(imax,3);
w = (float (*)[3])malloc(imax*3*sizeof(float));
for(int i=0; i<imax; i++)
{
for(int j=0; j<3; j++)
{
w[i][j] = i + 0.01f*j;
}
}
cudaMalloc((void**) &d_w, 3*imax*sizeof(float));
cudaMalloc((void**) &d_f, 3*imax*sizeof(float));
cudaChannelFormatDesc desc = cudaCreateChannelDesc<float>();
HANDLE_ERROR(cudaBindTexture2D(NULL, tex_w, d_w, desc, imax, 3, sizeof(float)*imax));
cudaMemcpy(d_w, w, 3*imax*sizeof(float), cudaMemcpyHostToDevice);
// just use threads for simplicity
kernel<<<1,grid>>>(imax, d_w, d_f);
cudaMemcpy(w, d_f, 3*imax*sizeof(float), cudaMemcpyDeviceToHost);
cudaUnbindTexture(tex_w);
cudaFree(d_w);
cudaFree(d_f);
print_to_stdio(imax, w);
free(w);
return 0;
}
運行這段代碼,我期望得到:
0 0.000000 0.010000 0.020000
1 1.000000 1.010000 1.020000
2 2.000000 2.010000 2.020000
3 3.000000 3.010000 3.020000
4 4.000000 4.010000 4.020000
5 5.000000 5.010000 5.020000
6 6.000000 6.010000 6.020000
7 7.000000 7.010000 7.020000
而是我得到:
0 0.000000 2.020000 5.010000
1 0.010000 3.000000 5.020000
2 0.020000 3.010000 6.000000
3 1.000000 3.020000 6.010000
4 1.010000 4.000000 6.020000
5 1.020000 4.010000 7.000000
6 2.000000 4.020000 7.010000
7 2.010000 5.000000 7.020000
我覺得這有什麼與我給予cudaBindTexture2D的音高參數有關,但使用較小的值會導致無效的參數錯誤。
在此先感謝!
謝謝。你還可以告訴如何爲此創建適當的通道描述符?你的代碼假設tex_w已經有一個,而CUDA文檔並不是很清楚。 – Michael 2017-06-30 17:40:32