我正在开发一个CUDA项目。但是,这基本上是C指针与CUDA本身没什么关系的概念。将指针传递给三个嵌套函数
我不知道我的引用/取消引用指针是否正确地完成,以反映我的kernel
函数(与C函数相同,但在GPU上完成)上的新值。
我kernel
得到一个指针作为参数:
__global__ kernel(StructA *a)
{
StructB b;
foo1(&a, &b); // passing both addresses to foo1
// I don't need to modify anything on StructA, might in future
// But, I will assign values to StructB (in foo1 and foo2)
...
// Work with StructB
...
}
质疑foo1
:我应该给指针的指针StructA在调用foo2
的地址?
__device__ foo1(StructA **a, StructB *b) // pointer-to pointer and pointer
{
int tid = blockIdx.x * blockDim.x + threadIdx.x;
if((*a)->elem1[tid]) // Access to value in elem1[tid]
foo2(a, &b, tid); // Pass structures to foo2
...
b->elem3 = 1; // Assign value to StructB
...
}
问题为foo2
:如果我通过StructA地址我将需要StructA第三级指针。但是,我迷失在这个级别的指针。
__device__ foo2(StructA **a, StructB **b, int tid)
{
// Assign value from elem2 in StructA for the thread to elem2 in StructB
(*b)->elem2 = (*a)->elem2[tid]; // Assign value to StructB from StructA
// HELP in previous line, not so sure if referencing the in the Structures
// are done correctly.
...
}
我可以粘贴我的实际代码,但不想让事情复杂化。
为什么你将指针传递给'foo1()'或'foo2()'的指针? – Macattack
@Macattack,因为我需要在'kernel'上反映值的赋值。 – mrei