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