Array of structure initialization on mapped memory

Hi everyone, I am new to cuda, many thanks in advance to who will help.

I have a struct defined like the following:

struct myStruct
{
    myType t;

    myVector pos, vel;

    int body;
}

where myType can be float or double depending on a user input, and myVector is a struct containing 3 coordinates declared again as “myType”.

My kernel save data inside this structures, and I have allocated the memory for it as follows:

myStruct *ptr_h;
int SIZE = sizeof(myStruct)*N;
cudaHostAlloc(&ptr_h, SIZE, cudaHostAllocMapped);

where SIZE is the number of structs that I need to write in.

It seems that I am able to write inside this mapped array of structs correctly from my kernel, but I cannot find a way to initialize this array of structs before passing it to the kernel.

My goal, actually, will be to just set the integer “body” to a given integer, just to check after the kernel if a certain struct have been modified.

Mapped memory is zero-copy memory. It stays on the CPU side, and all writes by the GPU will be done directly through the PCIe-bus (unfortunately that also applies to the reads!)

You should be able to use a for loop on the CPU on the array pointed to by ptr_h to initialize the array before calling the kernel.

I also assume you’re doing a cudaHostGetDevicePointer() on the ptr_h to obtain a ptr_d for the device before passing it to the kernel, right?

A relevant code sample is here: cuda memory example · GitHub

Thanks a lot for the fast reply

I am aware of that, I tried to check if this approach could keep the computational time decent, and in my case it seems it does that!

yes, your assumption is correct, at least now, but I have to admit that initially I was not using it :)

Thanks for the suggestion, I was trying to do it (without any success up to now) and I was wondering if any better approach exists.