将指针传递给三个嵌套函数

时间:2014-02-27 00:27:10

标签: c pointers cuda

我正在开发一个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的问题:我应该在调用foo2时给出指向指针StructA的地址吗?

__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.
  ...
}

我可以粘贴我的实际代码,但不想让事情复杂化。

1 个答案:

答案 0 :(得分:2)

这应该是你需要的。

 foo1(a, &b);

__device__ foo1(StructA *a, StructB *b)

   foo2(a, b, tid); //when we are inside foo1, foo1 has the pointers available 
    //so we just pass it to foo2.

__device__ foo2(StructA *a, StructB *b, int tid)

如果在foo1中执行foo2(a, &b, tid);,则传递包含指向结构的指针的指针变量的地址,但这不是必需的,只要您具有指向结构中可用结构的指针即可。你可以通过简单地说

将它传递给其他函数
`function_name(structA *pointer_to_strucutA)

关于作业你所做的是正确的但不是必要的

(*b)->elem2 = (*a)->elem2[tid]; //this is correct if you pass a pointer to pointer to struct 

如果您按照我的代码,您真正需要的是

b->elem2 = a->elem2[tid];