Re: dynamically allocating memory for two dimensional array

From: Benjamin Byington (bbyingto_at_soe_dot_ucsc_dot_edu)
Date: Fri Jul 17 2009 - 09:25:16 PDT

  • Next message: Gary Funck: "Re: dynamically allocating memory for two dimensional array"
    Here is a really basic example doing 2D arrays with pointers to pointers all in the shared memory.  I've not done UPC in a little while so I'm a bit rusty and there may be more elegant ways, but I've checked this and it at least works.  Be sure to note, however, that this does a layout where the array of pointers is cyclically laid out among all processors, and each of the sub arrays belong to a singular processor, so you'll have to tweak things if you want a different layout.  Hope this helps
    
    -Ben
    
      shared int *shared * a;
    
      a = (shared int *shared *)upc_all_alloc(10,sizeof(shared int*));
      upc_forall(int i = 0; i < 10; i++; i)
      {
        a[i] = upc_alloc(10*sizeof(shared int));
        for(int j = 0; j < 10; j++)
        {
           a[i][j] = i * j;
        }
      }
      return 0;
    
    
    ----- Original Message -----
    From: "sainath l" <ls.sainath_at_gmail_dot_com>
    To: "Benjamin Byington" <bbyingto_at_soe_dot_ucsc_dot_edu>
    Cc: upc-users_at_lbl_dot_gov
    Sent: Friday, July 17, 2009 11:56:21 AM GMT -05:00 US/Canada Eastern
    Subject: Re: dynamically allocating memory for two dimensional array
    
    Thank you for the reply. 
    Actually I am not worried about the performance here. I am interested in finding out how much time the allocation routine in itself takes. 
    
    I would need an array of pointers like 
    
    shared int *shared a[N] 
    
    so that I can allocate to each of these pointers memory using some allocation routine. The problem is that N is not a constant . I need to adjust it depending on the time taken by the allocation routine. So my question is how can I get this shared array of pointers dynamically. It doesn't matter how I get this shared array of pointers but it is necessary that I get this. 
    
    Thank you very much. 
    
    Sainath 
    
    
    On Fri, Jul 17, 2009 at 4:07 PM, Benjamin Byington < bbyingto_at_soe_dot_ucsc_dot_edu > wrote: 
    
    
    Sainath, 
    
    In addition to what James said, are you *sure* that you want to use pointers to pointers? In the regular C land that is already a non-optimal way to do 2D arrays since not only does accessing a single element require two pointer preferences in stead of one, you also lose any benefits from having things in contiguous memory. This only get compounded in UPC since shared pointer accesses are even slower and can require remote communication. Maybe the performance of this section doesn't really matter, but you still might be better off allocating it as a single nxm array in shared memory, and then doing something like handling indexing yourself. 
    
    Ben 
    
    
    
    
    
    ----- Original Message ----- 
    From: "James Dinan" < dinan@cse.ohio-state.edu > 
    To: "sainath l" < ls.sainath_at_gmail_dot_com > 
    Cc: upc-users_at_lbl_dot_gov 
    Sent: Friday, July 17, 2009 10:50:42 AM GMT -05:00 US/Canada Eastern 
    Subject: Re: dynamically allocating memory for two dimensional array 
    
    sainath l wrote: 
    > Hi again, 
    > 
    > I still haven't worked out how to do this, and it is very important for me. 
    > 
    > I am trying to find out the overhead associated with allocation routines. 
    > For this I need to run the allocation routines N times. I do not know at 
    > compilation the value of N. 
    > 
    > I therefore need to allocate memory to the pointer 
    > 
    > shared int *shared *shared a; 
    > 
    > to obtain an array of shared pointers each of which points to shared 
    > integers. 
    > 
    > Once again, I would be very grateful if someone could suggest how I 
    > might achieve this. 
    
    Hi Sainath, 
    
    "*shared" is specifying storage for a so I think you only need to give 
    the shared once. I would suggest something like: 
    
    typedef shared int * shr_int_p; 
    shr_int_p *shared a; 
    
    Good luck, 
    ~Jim. 
    

  • Next message: Gary Funck: "Re: dynamically allocating memory for two dimensional array"