Re: shared pointer as a return type

From: Oystein Thorsen (othorsen_at_mtu_dot_edu)
Date: Thu Jan 07 2010 - 23:47:26 PST

  • Next message: Evi Dube: "QUESTION: as a new UPC user"
    Thanks for feedback (Paul and Jim)!
    
    Just for completeness I'll post the working code (with a few comments 
    afterwards):
    
    #include <upc_relaxed.h>
    #include <stdio.h>
    
    typedef shared [] int *shared array_elem_t;
    
    array_elem_t * allocate2DArray(int rows, int columns);
    
    int main() {
        array_elem_t * array;
    
        array = allocate2DArray(5, 3);
    
        return 0;
    }
    
    array_elem_t * allocate2DArray(int rows, int columns) {
        array_elem_t * array;
        int r;
    
        array = (array_elem_t *)upc_all_alloc(rows, sizeof(array_elem_t));
        upc_forall(r = 0; r < rows; ++r; &array[r]) {
            array[r] = (array_elem_t)upc_alloc(sizeof(shared int) * columns);
        }
    
        upc_barrier;
    
        return array;
    }
    
    Adding an extra shared to the return type actually triggers the 
    "duplicate shared" warning, but just by using the typedef somehow got 
    rid of the warning anyway. Is there any particular reason why this 
    happens? I was under the impression that typedefs was more or less a 
    search and replace thing...
    
    Also, Paul is 100% correct about placing the barrier before the return, 
    but it is a conservative use of a barrier. This ensures that a barrier 
    is placed between allocation and use, but it would in many cases lead to 
    having more barriers in an execution than necessary.
    
    Example:
    
    int main(){
        array_elem_t * array1;
        array_elem_t * array2;
    
        array1 = allocate2DArray(5, 3);
        array2 = allocate2DArray(5, 3);
    
        return 0;
    }
    
    This program would be more efficient (less synchronization) with a 
    barrier after the two calls to allocate2DArray() instead of the barrier 
    in allocate2DArray.
    
    - Oystein
    
    James Dinan wrote:
    > Hi Orsten,
    >
    > I have started the habit of using typedefs whenever I have nested shared
    > pointers in UPC and this has helped resolve warnings like this in the
    > past.  That would look something like:
    >
    > typedef shared [] int *shared array_elem_t;
    >
    > and the function would return "shared array_elem_t *".  I'm not sure if
    > it's significant, but that's one more shared than you've got in your
    > example.  IIRC the compiler will give you an error if you write "shared
    > shared" so that may have been another motivation for the typedef.
    >
    > Good luck,
    >  ~Jim.
    >
    > Oystein Thorsen wrote:
    >   
    >> Hi,
    >>
    >> I was looking at the code for allocating a shared 2D array (posted on
    >> this email list early December 09) and tried to put it in a function
    >> that returns the pointer to the array, but I get a warning from the
    >> compiler that doesn't make much sense to me: (The program seems to work
    >> fine if I ignore the error though)
    >>
    >> $ upcc test.c
    >> test.c: In function `allocate2DArray':
    >> test.c:26: warning: return from incompatible pointer type
    >> $
    >>
    >> Here is a simplified version of the code (All in one file):
    >>
    >> #include <upc_relaxed.h>
    >> #include <stdio.h>
    >>
    >> shared [] int *shared * allocate2DArray(int rows, int columns);
    >>
    >> int main() {
    >>    shared [] int *shared * data;
    >>
    >>    data = allocate2DArray(5, 3);
    >>
    >>    return 0;
    >> }
    >>
    >> // Allocate a 2D Array in shared space with a row-wise round-robin
    >> distribution.
    >> shared [] int *shared * allocate2DArray(int rows, int columns) {
    >>    shared [] int *shared * array;
    >>    int r, c;
    >>
    >>    array = (shared [] int *shared *)upc_all_alloc(rows, sizeof(shared []
    >> int *shared));
    >>    upc_barrier;
    >>    for (r = 0; r < rows; ++r) {
    >>        if (upc_threadof(&array[r]) == MYTHREAD) {
    >>            array[r] = (shared [] int *shared)upc_alloc(sizeof(shared
    >> int) * columns);
    >>        }
    >>    }
    >>
    >>    return array; //<===== Warning points to this line
    >> }
    >>
    >> end of code
    >>
    >> I'm using the Berkeley upcc compiler version 2.10.0 with remote
    >> translation.
    >>
    >> The warning goes away if I move the declaration of allocate2DArray() to
    >> where the prototype is (and remove the prototype). Or if I cast array to
    >> remove the blocking factor on return:
    >>
    >> return (shared int *shared *)array;
    >>
    >> The warning comes back if I do both....
    >>
    >> Regards,
    >>
    >> Oystein
    >>     
    >
    >   
    

  • Next message: Evi Dube: "QUESTION: as a new UPC user"