7
使用CUDA编程我面临着一些问题,试图将一些数据从主机复制到GPU。如何使用CUDA执行struct的深层复制?
我有3个嵌套的结构这样的:
typedef struct {
char data[128];
short length;
} Cell;
typedef struct {
Cell* elements;
int height;
int width;
} Matrix;
typedef struct {
Matrix* tables;
int count;
} Container;
所以Container
“包括” 一些Matrix
元件,其依次包括一些Cell
元件。
让我们假设我用这种方式动态地分配主机内存:
Container c;
c.tables = malloc(20 * sizeof(Matrix));
for(int i = 0;i<20;i++){
Matrix m;
m.elements = malloc(100 * sizeof(Cell));
c.tables[i] = m;
}
也就是说,20基质每100个细胞的容器。
- 我该如何使用cudaMemCpy()将此数据复制到设备内存?
- 是否有任何好的方法来执行从主机到设备的“结构struct”的深层副本?
谢谢你的时间。
Andrea
我读了很多关于使用线性内存和展平数组的内容。 实际上,我将'Matrix'结构中的字段'元素'作为线性内存管理,而高级表示则是2D。 我会尝试线性化/扁平化3结构,但我怎么能做到这一点?管理所有的索引难道不是太难吗? 无论如何,感谢您的帮助,不要去寻找一些旧的代码! – Andrea