I have the following:
struct LR { double eps_dielect;
double sgm_conductivity;
double eno_ns_surfref;
double frq_mhz;
double conf;
double rel;
double erp;
int radio_climate;
int pol;
float antenna_pattern[361][1001];
} LR;
I need to pass LR.antenna_pattern into a function, allocate the memory in a CUDA device then copy it. The float** type should represent LR.antenna_pattern[361][1001] just fine but I don’t know how to instantiate the float** variable so that it is a pointer to LR.antenna_pattern
I try float** antennaPattern = (void**)&LR.antenna_pattern but it doesn’t work. How do I create a pointer to LR.antenna_pattern?
One approach is to flatten your 2D array and handle it in a 1D fashion with pointer arithmetic to handle the row and column dimensions. First of all in your struct definition, replace the antenna_pattern element with:
Then you will need to do a host-side malloc to allocate space:
And a device side cuda malloc:
The copy to the device looks like:
When you want to reference into these arrays, you will have to do pointer arithmetic like:
If you want to maintain the 2D array subscripts throughout, you can do this with an appropriate typedef. For an example, see the first code sample in my answer to this question. To diagram this out, you would need to start with a typedef:
and modify your structure definition:
The host side malloc would look like:
The device side cuda malloc would look like:
The copy to the device looks like:
The device kernel definition will need a function parameter like:
Then inside the kernel you can access an element at x,y as:
Now in response to a follow-up question asking what to do if LR cannot be changed, here is a complete sample code which combines some of these ideas without modifying the LR structure:
If you prefer to use the flattened method, replace the
d_antenna_patterndefinition with:And change the kernel function parameter correspondingly to:
Then access using the pointer arithmetic method in the kernel: