ARTICLE AD BOX
I would like to create a struct that can work on the host and device that stores an array that has been dynamically allocated (unknown size at compile time). This struct would be sent to a kernel so that every thread has a pointer to it.
How could I get this to work?
The constructor and destructor would need to be different for a host and device using new[] and cudaMalloc() respectively.
How could this struct be cleanly sent in both directions without duplicate code every time I want to send the host version to the device by sending the inner array, and then sending a duplicate of the struct with a different pointer to the device array instead of the host array.
I have tried something like this but to no avail:
struct MyStruct { int array_size; int* array; MyStruct(int size) { array_size = size; array = new int[size]; } ~MyStruct() { delete[] array; array = nullptr; } __host__ __device__ int getValue(index) { if (index >= 0 && index < array_size) { return array[index]; } // Throw an error; } }Would this struct need to be a Cuda file? Would host or device need to be before any functions?
Apologies for the many questions.
