尝试通过 Google Colab 在 CUDA 中编译时出错

问题描述 投票:0回答:1

我有这个简单的结构:

typedef struct {
Edge* edges;
int count;
}EdgeList;

我想要的只是在 Cuda 中的设备上分配空间。尤其。我已经在主机上有一个 EdgeList* allEdges ,并且已经填充了数据,现在我想在要复制此结构的主机上分配空间,但在设备上分配空间时遇到问题。我愿意:

`EdgeList *allEdges = (EdgeList*)malloc(sizeof(EdgeList));
allEdges->count = 0;
allEdges->edges = NULL;

countEdges(allEdges, file, numberOfVertices);

EdgeList *d_allEdges;
cudaMalloc((void **)&d_allEdges, sizeof(EdgeList));
cudaMalloc((void **)&d_allEdges->edges, allEdges->count * sizeof(Edge));
d_allEdges->count = 0;

cudaMemcpy(d_allEdges, allEdges, sizeof(EdgeList), cudaMemcpyHostToDevice);
cudaMemcpy(&(d_allEdges->edges), &allEdges->edges, allEdges->count * sizeof(Edge), cudaMemcpyHostToDevice);`

但我不明白为什么 d_allEdges->edges 的 cudaMalloc 不起作用...执行不会给我任何错误,但如果我在该 loine 之后执行 printf,它就不起作用。感谢您的帮助

c memory-management cuda
1个回答
0
投票

我认为您代码中的问题与边数组的

cudaMalloc
cudaMemcpy
的错误使用有关。 Edges数组是一个动态数组,需要单独为其分配内存。此外,您应该在为其分配内存后将 Edge 数组的数据从主机复制到设备。

如果先在主机上为edges数组分配内存,将数据复制到其中,然后在设备上为edges数组分配内存,并将数据复制到设备上。最后,完成后它会释放主机和设备上的内存。

这是一个例子:

EdgeList *allEdges = (EdgeList*)malloc(sizeof(EdgeList));
allEdges->count = 0;
allEdges->edges = NULL;

countEdges(allEdges, file, numberOfVertices);

// Allocate memory for the host edges array
allEdges->edges = (Edge*)malloc(allEdges->count * sizeof(Edge));

// Assuming you have populated the edges array with data

EdgeList *d_allEdges;

// Allocate memory for the device EdgeList
cudaMalloc((void **)&d_allEdges, sizeof(EdgeList));

// Allocate memory for the device edges array
cudaMalloc((void **)&(d_allEdges->edges), allEdges->count * sizeof(Edge));

// Copy EdgeList structure from host to device
cudaMemcpy(d_allEdges, allEdges, sizeof(EdgeList), cudaMemcpyHostToDevice);

// Copy edges array from host to device
cudaMemcpy(d_allEdges->edges, allEdges->edges, allEdges->count * sizeof(Edge), cudaMemcpyHostToDevice);

// You can print or manipulate d_allEdges on the device as needed

// Don't forget to free memory on the host and device when done
free(allEdges->edges);
free(allEdges);
cudaFree(d_allEdges->edges);
cudaFree(d_allEdges);
© www.soinside.com 2019 - 2024. All rights reserved.