Re: shared pointer as a return type

From: Paul H. Hargrove (PHHargrove_at_lbl_dot_gov)
Date: Sun Jan 03 2010 - 00:40:19 PST

  • Next message: James Dinan: "Re: shared pointer as a return type"
    Oystein,
    
    The following does NOT address your problem, but I believe I see 3 
    problems in your allocate2DArray function:
    1) You need a upc_barrier between the "array[r] = ..." and the return.  
    Otherwise it would be possible for a thread to access A[i][j] before 
    A[i] is initialized.
    2) The upc_barrier between the upc_all_alloc and the for loop is unnecessary
    3) The for loop + if are a "classic" example of a upc_forall
    
    Of these three only #1 is critical to correct code.
    
    So, I believe the following is a more correct version (hopefully 
    somebody will correct me it I am mistaken).
    
    shared [] int *shared * allocate2DArray(int rows, int columns) {
       shared [] int *shared * array;
       int r;
    
       array = (shared [] int *shared *)upc_all_alloc(rows, sizeof(shared [] 
    int *shared));
       upc_forall (r = 0; r < rows; ++r; &array[r]) {
               array[r] = (shared [] int *shared)upc_alloc(sizeof(shared 
    int) * columns);
       }
       upc_barrier;
    
       return array;
    }
    
    -Paul
    
    
    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
    
    
    -- 
    Paul H. Hargrove                          PHHargrove_at_lbl_dot_gov
    Future Technologies Group                 Tel: +1-510-495-2352
    HPC Research Department                   Fax: +1-510-486-6900
    Lawrence Berkeley National Laboratory     
    

  • Next message: James Dinan: "Re: shared pointer as a return type"