2011-09-21 58 views

回答

2

是,如下面的程序演示:

#include <stdio.h> 

struct my_struct 
{ 
    int x; 
}; 

// foo receives its argument by pointer 
__device__ void foo(my_struct *a) 
{ 
    a->x = 13; 
} 

__global__ void kernel() 
{ 
    my_struct a; 
    a.x = 7; 

    // expect 7 in the printed output 
    printf("a.x before foo: %d\n", a.x); 

    foo(&a); 

    // expect 13 in the printed output 
    printf("a.x after foo: %d\n", a.x); 
} 

int main() 
{ 
    kernel<<<1,1>>>(); 
    cudaThreadSynchronize(); 
    return 0; 
} 

结果:

$ nvcc -arch=sm_20 test.cu -run 
a.x before foo: 7 
a.x after foo: 13 
1

如果您已经在设备上分配了内存并仅在设备中使用它,那么您可以将它传递给您想要的任何设备功能。

您唯一需要担心的事情就是当您想要使用设备上主机的地址或主机上设备的地址时。在这些情况下,您必须先使用适当的memcopy并获取新设备或主机特定地址。