简体   繁体   中英

Accessing a struct member in CUDA?

For simpliflication, say I have this struct:
someheader.h

typedef struct
{
    float x
}someStruct;

in Cuda, how will a device function access the member of the struct if the struct is being shared by a C++ application?

For example:

__global__ void stuff( someStruct *g ) {
    g[0].x = 0.4f;
}

is that the correct way to do it? It doesn't seem to be working.

Struct itself is an abstract entity and has no physical representation on host or device side.

The memory layout of an object on device side is exactly the same as on the host side (if that's what you are really asking), so you can safetly copy big struct objects from host to device and vice versa.

Accessing a member of an object is nothing else than computing a correct offset at compile time and adding it to the object pointer ( this ) at run-time. CUDA is perfectly capable of doing that.

someStruct.x=2 will translate into something like this in assembly language:

mov [someStruct]+0, 2

where 0 is the offset of member x inside your struct.

Update:

Host and device memory are complete separate (one is in your RAM, the other on your GPU). Nothing is shared, everything has to be send back and forth (which can be quite time-consuming). Use CudaMemcpy function to copy data.

You will need to transfer the array of structs to the GPU. for example to access an array of floats inside the kernel, you will need to do the following

__global__ static void myKernel(float *val)
{
 val[0] = 0.4f;
}

int main()
{
...
cudaMemcpy(d_Val, h_Val, n * sizeof(float), cudaMemcpyHostToDevice);
...
}

This of course is basic knowledge. You could replace float with any data type and get the same behavior. Structs are nothing but user defined data types.

This is different from sending in a single float (and hence a single struct), because all the inputs to a kernel are pushed into some part of GPU memory system at run time (depending on the card) from where GPU can access these values. So if the input is a struct, the whole struct resides on the GPU when a kernel is trying to access it. But if you send in a pointer to structs which were generated on the host, the GPU has value of the pointer but not the actual data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM