我在CUDA架构中复制主机到设备的结构数据时遇到了问题。
以下是代码片段。
点a的x和y字段应该已经更改为100。但事实上,它仍然是初始化时的10。这里发生了什么?请帮忙解决。
以下是代码片段。
struct point
{
double x,y;
};
int main()
{
point * a = (point*)malloc(sizeof(point));
a->x=10.0;
a->y=10.0;
point * d_a;
cudaMalloc((void**)d_a,sizeof(point));
cudaMemcpy((void**)d_a,a,sizeof(point),cudaMemcpyHostToDevice);
dim3 dimblock(16,16);
dim3 dimgrid(1,1);
MyFunc<<<dimgrid,dimblock>>>(d_a);
cudaMemcpy((void**)a,d_a,sizeof(point),cudaMemcpyDeviceToHost);
printf("%lf %lf\n",a->x,a->y);
}
__global__ void MyFunc(point* d_a)
{
if(threadIdx.x == 0 && threadIdx.y == 0)
{
d_a->x=100.0;
d_a->y = 100.0;
}
}
点a的x和y字段应该已经更改为100。但事实上,它仍然是初始化时的10。这里发生了什么?请帮忙解决。