0
我希望有一个Container类的实例在初始化时分配一些设备和主机内存。我想在设备代码中使用分配的内存,而不传递实际的指针(API原因)。在设备代码中使用指向设备内存的主机类成员
如何创建全局__device__
指向指向设备内存的成员的指针?如果有帮助,我很乐意使用推力。
这里是一个小例子:
#include <iostream>
struct Container {
int *h_int = (int*)malloc(4*sizeof(int));
int *d_int;
Container() {
h_int[0] = 6; h_int[1] = 6; h_int[2] = 6; h_int[3] = 6;
cudaMalloc(&d_int, 4*sizeof(int));
memcpyHostToDevice();
}
void memcpyHostToDevice() {
cudaMemcpy(d_int, h_int, 4*sizeof(int), cudaMemcpyHostToDevice);
}
void memcpyDeviceToHost() {
cudaMemcpy(h_int, d_int, 4*sizeof(int), cudaMemcpyDeviceToHost);
}
};
Container stuff;
__device__ auto d_int = &stuff.d_int; // How do I get that right?
__global__ void edit() { // To keep the API simple I do not want to pass the pointer
auto i = blockIdx.x*blockDim.x + threadIdx.x;
d_int[i] = 1 + 2*(i > 0) + 4*(i > 2);
}
int main(int argc, char const *argv[]) {
edit<<<4, 1>>>();
stuff.memcpyDeviceToHost();
std::cout << stuff.h_int[0] << stuff.h_int[1] << stuff.h_int[2] << stuff.h_int[3] << "\n";
return 0;
}
其实它的工作,如果我宣布'的东西在全球范围内。感谢您的回答! – qiv
@qiv:你不能依靠它工作。这是未定义的行为,它会在某些时候停止对你的工作。 – talonmies
这可能是这个奇怪问题背后的原因:在一个测试案例中,势头通常只在第一次执行时才被保留,但不是在连续执行中?声明求解器类非全局函数可以避免它(就像在内核中打印坐标或者改变测试顺序一样)。 – qiv