我正在尝试从 CUDA 中分配的数组访问数据。第一步是分配我定义的结构。在我将分配的结构传递给改变结构值的内核函数之后。最后,我将结构和数组传递给主机变量,以便读取它们。但实际上我在读取分配的向量时遇到问题。
#include <stdio.h>
#include <stdlib.h>
typedef struct x{
float *y;
float v;
}x_t;
__global__ void initTeste(x_t *param){
param->v = 10;
param->y[0] = 10;
param->y[1] = 10;
}
int main(void) {
x_t *hvar;
x_t hvarBackup;
float *temp = (float*)malloc(10*sizeof(float));
cudaError_t result;
cudaMalloc(&hvar , sizeof(x_t) );
cudaMalloc(&hvarBackup.y, 10*sizeof(float) );
cudaMemcpy(hvar, &hvarBackup, sizeof(x_t), cudaMemcpyHostToDevice);
initTeste<<<1,1>>>(hvar);
cudaMemcpy(&hvarBackup, hvar, sizeof(x_t), cudaMemcpyDeviceToHost);
cudaMemcpy(temp, &hvar->y, 10*sizeof(float), cudaMemcpyDeviceToHost);
printf("%f",(hvarBackup.v)); //here ok
printf("%f",(temp[0])); //here's the problem
return 0;
}