将结构的动态数组传递给 GPU 内核
Passing dynamic array of structs to GPU kernel
我尝试将我的动态结构数组传递给内核,但它不起作用。我得到 - "Segmentation fault (core dumped)"
我的代码 - 已编辑
#include <stdio.h>
#include <stdlib.h>
struct Test {
unsigned char *array;
};
__global__ void kernel(Test *dev_test) {
}
int main(void) {
int n = 4;
int size = 5;
unsigned char *array[size];
Test *dev_test;
// allocate for host
Test *test = (Test*)malloc(sizeof(Test)*n);
for(int i = 0; i < n; i++)
test[i].array = (unsigned char*)malloc(size);
// fill data
for(int i=0; i<n; i++) {
unsigned char temp[] = { 'a', 'b', 'c', 'd' , 'e' };
memcpy(test[i].array, temp, size);
}
// allocate for gpu
cudaMalloc((void**)&dev_test, n * sizeof(Test));
for(int i=0; i < n; i++) {
cudaMalloc((void**)&(array[i]), size * sizeof(unsigned char));
cudaMemcpy(&(dev_test[i].array), &(array[i]), sizeof(unsigned char *), cudaMemcpyHostToDevice);
}
kernel<<<1, 1>>>(dev_test);
return 0;
}
我应该如何正确分配 gpu 内存并将数据复制到该内存?
您需要为结构成员array
分配内存。
Test *test = malloc(sizeof(Test)*n);
for(int i = 0; i < n; i++)
test[i]->array = malloc(size);
我建议阅读 this answer 以解决此修复后的其他问题。
你的卡是什么?如果您的卡支持计算能力 >= 3.0,请尝试使用统一内存系统,以便在 host/device 内存
中拥有相同的数据
你可以看看here :
它可能看起来像这样:
int main(void) {
int n = 4;
int size = 5;
Test *test;
cudaMallocManaged(&test, n * size);
unsigned char values[] = { 'a', 'b', 'c', 'd' , 'e' };
for(int i=0; i<n; i++)
{
unsigned char* temp;
cudaMallocManaged(&temp, size*sizeof(char) );
memcpy(temp, values, sizeof(values) );
}
// avoid copy code, makes a deep copy of objects
kernel<<<1, 1>>>(test);
return 0;
}
我希望你知道,但不要忘记在分配的内存上调用 cudaFree & delete/free。 (最好使用 std::vector 并使用 data() 访问原始指针)
我尝试将我的动态结构数组传递给内核,但它不起作用。我得到 - "Segmentation fault (core dumped)"
我的代码 - 已编辑
#include <stdio.h>
#include <stdlib.h>
struct Test {
unsigned char *array;
};
__global__ void kernel(Test *dev_test) {
}
int main(void) {
int n = 4;
int size = 5;
unsigned char *array[size];
Test *dev_test;
// allocate for host
Test *test = (Test*)malloc(sizeof(Test)*n);
for(int i = 0; i < n; i++)
test[i].array = (unsigned char*)malloc(size);
// fill data
for(int i=0; i<n; i++) {
unsigned char temp[] = { 'a', 'b', 'c', 'd' , 'e' };
memcpy(test[i].array, temp, size);
}
// allocate for gpu
cudaMalloc((void**)&dev_test, n * sizeof(Test));
for(int i=0; i < n; i++) {
cudaMalloc((void**)&(array[i]), size * sizeof(unsigned char));
cudaMemcpy(&(dev_test[i].array), &(array[i]), sizeof(unsigned char *), cudaMemcpyHostToDevice);
}
kernel<<<1, 1>>>(dev_test);
return 0;
}
我应该如何正确分配 gpu 内存并将数据复制到该内存?
您需要为结构成员array
分配内存。
Test *test = malloc(sizeof(Test)*n);
for(int i = 0; i < n; i++)
test[i]->array = malloc(size);
我建议阅读 this answer 以解决此修复后的其他问题。
你的卡是什么?如果您的卡支持计算能力 >= 3.0,请尝试使用统一内存系统,以便在 host/device 内存
中拥有相同的数据你可以看看here :
它可能看起来像这样:
int main(void) {
int n = 4;
int size = 5;
Test *test;
cudaMallocManaged(&test, n * size);
unsigned char values[] = { 'a', 'b', 'c', 'd' , 'e' };
for(int i=0; i<n; i++)
{
unsigned char* temp;
cudaMallocManaged(&temp, size*sizeof(char) );
memcpy(temp, values, sizeof(values) );
}
// avoid copy code, makes a deep copy of objects
kernel<<<1, 1>>>(test);
return 0;
}
我希望你知道,但不要忘记在分配的内存上调用 cudaFree & delete/free。 (最好使用 std::vector 并使用 data() 访问原始指针)