Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8906497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:40:17+00:00 2026-06-15T02:40:17+00:00

I have the following: struct LR { double eps_dielect; double sgm_conductivity; double eno_ns_surfref; double

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T02:40:18+00:00Added an answer on June 15, 2026 at 2:40 am

    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:

    struct LR { 
    .
    .
    float *antenna_pattern;
    } LR;
    

    Then you will need to do a host-side malloc to allocate space:

    #define COL 1001
    #define ROW 361
    #define DSIZE (ROW*COL)
    
    LR.antenna_pattern = (float *)malloc(DSIZE*sizeof(float));
    

    And a device side cuda malloc:

    float *d_antenna_pattern;
    cudaMalloc((void **) &d_antenna_pattern, DSIZE*sizeof(float));
    

    The copy to the device looks like:

    cudaMemcpy(d_antenna_pattern, LR.antenna_pattern, DSIZE*sizeof(float), cudaMemcpyHostToDevice);
    

    When you want to reference into these arrays, you will have to do pointer arithmetic like:

    float my_val_xy = ap[(x*COL)+y];  // to access element at [x][y] on the device
    
    float my_val_xy = LR.antenna_pattern[(x*COL)+y]; // on the host
    

    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:

    #define COL 1001
    #define ROW 361
    #define DSIZE (ROW*COL)
    
    typedef float aParray[COL];
    

    and modify your structure definition:

    struct LR { 
    .
    .
    aParray *antenna_pattern;
    } LR;
    

    The host side malloc would look like:

    LR.antenna_pattern = (aParray *)malloc(DSIZE*sizeof(float));
    

    The device side cuda malloc would look like:

    aParray *d_antenna_pattern;
    cudaMalloc((void **) &d_antenna_pattern, DSIZE*sizeof(float));
    

    The copy to the device looks like:

    cudaMemcpy(d_antenna_pattern, LR.antenna_pattern, DSIZE*sizeof(float), cudaMemcpyHostToDevice);
    

    The device kernel definition will need a function parameter like:

    __global__ void myKernel(float ap[][COL]) {
    

    Then inside the kernel you can access an element at x,y as:

    float my_val_xy = ap[x][y];
    

    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:

    #include<stdio.h>
    
    // for cuda error checking
    #define cudaCheckErrors(msg) \
        do { \
            cudaError_t __err = cudaGetLastError(); \
            if (__err != cudaSuccess) { \
                fprintf(stderr, "Fatal error: %s (%s at %s:%d)\n", \
                    msg, cudaGetErrorString(__err), \
                    __FILE__, __LINE__); \
                fprintf(stderr, "*** FAILED - ABORTING\n"); \
                return 1; \
            } \
        } while (0)
    
    
    
    struct LR {
      int foo;
      float antenna_pattern[361][1001];
      } LR;
    
    __global__ void mykernel(float ap[][1001]){
    
      int tid = threadIdx.x + (blockDim.x*blockIdx.x);
      float myval = 0.0;
    
      if (tid == 0){
        for (int i=0; i<361; i++)
          for (int j=0; j<1001; j++)
             ap[i][j] = myval++;
      }
    }
    
    
    int main(){
    
      typedef float aParray[1001];
    
      aParray *d_antenna_pattern;
      cudaMalloc((void **) &d_antenna_pattern, (361*1001)*sizeof(float));
      cudaCheckErrors("cudaMalloc fail");
      float *my_ap_ptr;
      my_ap_ptr = &(LR.antenna_pattern[0][0]);
    
      for (int i=0; i< 361; i++)
        for (int j=0; j<1001; j++)
          LR.antenna_pattern[i][j] = 0.0;
      cudaMemcpy(d_antenna_pattern, my_ap_ptr, (361*1001)*sizeof(float), cudaMemcpyHostToDevice);
      cudaCheckErrors("cudaMemcpy fail");
      mykernel<<<1,1>>>(d_antenna_pattern);
      cudaCheckErrors("Kernel fail");
    
      cudaMemcpy(my_ap_ptr, d_antenna_pattern, (361*1001)*sizeof(float), cudaMemcpyDeviceToHost);
      cudaCheckErrors("cudaMemcpy 2 fail");
      float myval = 0.0;
      for (int i=0; i<361; i++)
        for (int j=0; j<1001; j++)
          if (LR.antenna_pattern[i][j] != myval++) {printf("mismatch at offset x: %d y: %d actual: %f expected: %f\n", i, j, LR.antenna_pattern[i][j], --myval); return 1;}
      printf("Results match!\n");
      return 0;
    }
    

    If you prefer to use the flattened method, replace the d_antenna_pattern definition with:

    float *d_antenna_pattern;
    

    And change the kernel function parameter correspondingly to:

    __global__ void mykernel(float *ap){
    

    Then access using the pointer arithmetic method in the kernel:

    ap[(i*1001)+j] = myval++;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following struct: struct Records { int Number; char Name[20]; float Salary;
I have the following struct: struct Node { double linkCost[8]; int val; Node *prevNode;
I have the following variant from the boost lib: typedef boost::variant<int, float, double, long,
I have the following struct: struct FeatureMatch { int id1, id2; double score; };
I have the following struct dweDMPair { const dweller *occu; const double sqDM; float
Suppose I have the following simple struct: struct Vector3 { double x; double y;
I have the following struct private struct sData{ public int volume; public System.Timers.Timer aliveTimer;
I have defined the following struct in C: typedef struct point{ float x; float
I have declared the following struct: typedef struct _RECOGNITIONRESULT { int begin_time_ms, end_time_ms; char*
Say I have the following code: struct date { int day; int month; int

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.