【发布时间】:2011-09-14 06:51:07
【问题描述】:
我有一个节点树,我正在尝试将它复制到 GPU 内存。节点看起来像这样:
struct Node
{
char *Key;
int ChildCount;
Node *Children;
}
我的复制功能是这样的:
void CopyTreeToDevice(Node* node_s, Node* node_d)
{
//allocate node on device and copy host node
cudaMalloc( (void**)&node_d, sizeof(Node));
cudaMemcpy(node_d, node_s, sizeof(Node), cudaMemcpyHostToDevice);
//test
printf("ChildCount of node_s looks to be : %d\n", node_s->ChildCount);
printf("Key of node_s looks to be : %s\n", node_s->Key);
Node *temp;
temp =(Node *) malloc(sizeof(Node));
cudaMemcpy(temp, node_d, sizeof(Node), cudaMemcpyDeviceToHost);
printf("ChildCount of node_d on device is actually : %d\n", temp->ChildCount);
printf("Key of node_d on device is actually : %s\n", temp->Key);
free(temp);
// continue with child nodes
if(node_s->ChildCount > 0)
{
//problem here
cudaMalloc( (void**)&(node_d->Children), sizeof(Node)*(node_s->ChildCount));
cudaMemcpy(node_d->Children, node_s->Children,
sizeof(Node)*node_s->ChildCount, cudaMemcpyHostToDevice);
for(int i=0;i<node_s->ChildCount;i++)
{
CopyTreeToDevice(&(node_s->Children[i]), &(node_d->Children[i]));
}
}
}
但我的线路有问题:
cudaMalloc( (void**)&(node_d->Children), sizeof(Node)*(node_s->ChildCount));
给我访问冲突异常。测试部分工作顺利。初始化字段没有问题。
这是测试部分的输出:
ChildCount of node_s looks to be : 35
Key of node_s looks to be : root
ChildCount of node_d on device is actually : 35
Key of node_d on device is actually : root
这是什么原因?
谢谢。
【问题讨论】:
标签: c memory-management tree cuda gpu