clangupc / clang-upc

Clang UPC Front-End
https://clangupc.github.io/
Other
16 stars 5 forks source link

spec 1.3: semantics of shared array types #12

Closed gary-funck closed 11 years ago

gary-funck commented 11 years ago

The version 1.3 specification refines the semantics of UPC's shared array types. The issue is described here: http://code.google.com/p/upc-specification/issues/detail?id=3

A test case, td2_arry.c is documented here: http://code.google.com/p/upc-specification/issues/detail?id=3#c98

The purpose of this issue is to track clang-upc's conformance to the shared array semantics documented in UPC specification issue 3.

gary-funck commented 11 years ago

I can't find a button that let's me easily attach a test case. Is a 'gist' the preferred method?

For now, below is a literal copy of the t2d_array.c test case.

/*
 * Copyright 2012-2013 Cray Inc. All Rights Reserved 
 *
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are 
 * met: 
 *
 *  * Redistributions of source code must retain the above copyright 
 *    notice, this list of conditions and the following disclaimer. 
 *
 *  * Redistributions in binary form must reproduce the above copyright 
 *    notice, this list of conditions and the following disclaimer in the 
 *    documentation and/or other materials provided with the distribution. 
 *
 *  * Neither the name Cray Inc. nor the names of its contributors may be 
 *    used to endorse or promote products derived from this software 
 *    without specific prior written permission. 
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 *
 */
#include 
#include 
#define VFMT "%3d"
#define VTYPE int
#define EXPECTED(i,j) ((COLS * (i)) + (j))
#define TFMT "%1d"
#define TTYPE int
#define MAX_THREADS 10
#define EXPECTED_THR(i,j) ((((COLS * (i)) + (j)) / BLKS) % THREADS)
#define PFMT "%1d"
#define PTYPE int
#define BLKS (5)
#define EXPECTED_PHASE(i,j) (((COLS * (i)) + (j)) % BLKS)
/* One of these must be a multiple of THREADS */
#define ROWS (3*THREADS)
#define COLS (7)
/* Helper macros to save some typing and duplication */
#define t0_print \
    if ( !MYTHREAD ) printf
/* Pass i, j, and failed explicitly to call out that they are
 * used in the macro.
 */
#define TEST_ELEM( i,j,expr,failed ) \
    shared [BLKS] VTYPE *ptr = &(expr); \
    VTYPE actual = *ptr; \
    int _failed = 0; \
    if ( (actual != EXPECTED(i,j))                  || \
         (upc_threadof( ptr ) != EXPECTED_THR(i,j)) || \
         (upc_phaseof( ptr) != EXPECTED_PHASE(i,j)) || \
         check_addrfield(i,j,ptr) ) _failed = 1; \
    t0_print( "%s" VFMT "@" TFMT ":" PFMT, \
              (_failed == 0) ? "  " : "**", \
              actual, \
              (TTYPE)upc_threadof( ptr ), \
              (PTYPE)upc_phaseof( ptr ) ); \
    failed |= _failed
/* Create a nice "ugly" multidimensional shared array whose rows and columns
 * do not neatly fit into the specified blocks to try to ensure that some rows'
 * initial elements are not at phase 0.
 */
shared [BLKS] VTYPE array2d[ROWS][COLS];
/* Verify that ptr has a valid addrfield component */
int
check_addrfield( int i, int j, shared [BLKS] VTYPE *ptr )
{
    int failed = 0;
    int p_first_i = (BLKS * upc_threadof( ptr ) ) / COLS;
    int p_first_j = (BLKS * upc_threadof( ptr ) ) % COLS;
    shared [BLKS] VTYPE *p_first = &array2d[p_first_i][p_first_j];
    shared [] VTYPE *indef_ptr     = (shared [] VTYPE *)ptr;
    shared [] VTYPE *indef_p_first = (shared [] VTYPE *)p_first;
    size_t addr_diff  = (indef_ptr - indef_p_first) * sizeof( VTYPE );
    size_t exp_diff   = upc_affinitysize( ((COLS * i) + j + 1) *
                                          sizeof( VTYPE ), 
                                          BLKS * sizeof( VTYPE ),
                                          upc_threadof( ptr ) )
                        - sizeof( VTYPE );
    if ( addr_diff != exp_diff ) {
        failed = 1;
    }
    else if ( upc_threadof( ptr ) == MYTHREAD ) {
        size_t local_diff = (((VTYPE *)indef_ptr) - ((VTYPE *)indef_p_first))
                            * sizeof( VTYPE );
        if ( local_diff != exp_diff ) {
            failed = 1;
        }
    }
    return failed;
}
/* Initialize all elements of the array to known values based on position
 * in array
 */
void
init_array2d( void )
{
    int i,j;
    for ( i=0 ; i 0) ? 1 : 0;
}
int
main()
{
    int failed = 0;
    if ( THREADS > MAX_THREADS ) {
        t0_print( "Please run with %d or fewer threads.\n",
                  MAX_THREADS );
        return -1;
    }
    init_array2d();
    failed += print_array2d();
    failed += print_array2d_derefi();
    failed += print_array2d_derefj();
    failed += print_array2d_derefij();
    failed += print_array2dptr();
    if ( failed != 0 ) {
        printf("Thread %d failed %d tests!\n",MYTHREAD,failed);
    }
    return 0;
}
gary-funck commented 11 years ago

I compiled the test case and confirmed that clangupc behaves as prescribed in the 1.3 specification, and passes the test case.

dev$ clang-upc --version
bash: clang-upc: command not found...
[Exit 127 ]
dev$ clangupc --version 
clang version 3.1  (UPC 20130312)
Target: x86_64-unknown-linux-gnu
Thread model: posix
dev$ clangupc t_2darr.c -o t_2darr
clang-3: warning: treating 'c' input as 'upc' when in UPC mode, this behavior is deprecated

Here are the results for 2 threads. The test passes for higher numbers of threads as well.

dev$ t_2darr -n2
array2d:
    0@0:0    1@0:1    2@0:2    3@0:3    4@0:4    5@1:0    6@1:1
    7@1:2    8@1:3    9@1:4   10@0:0   11@0:1   12@0:2   13@0:3
   14@0:4   15@1:0   16@1:1   17@1:2   18@1:3   19@1:4   20@0:0
   21@0:1   22@0:2   23@0:3   24@0:4   25@1:0   26@1:1   27@1:2
   28@1:3   29@1:4   30@0:0   31@0:1   32@0:2   33@0:3   34@0:4
   35@1:0   36@1:1   37@1:2   38@1:3   39@1:4   40@0:0   41@0:1
array2d (derefi):
    0@0:0    1@0:1    2@0:2    3@0:3    4@0:4    5@1:0    6@1:1
    7@1:2    8@1:3    9@1:4   10@0:0   11@0:1   12@0:2   13@0:3
   14@0:4   15@1:0   16@1:1   17@1:2   18@1:3   19@1:4   20@0:0
   21@0:1   22@0:2   23@0:3   24@0:4   25@1:0   26@1:1   27@1:2
   28@1:3   29@1:4   30@0:0   31@0:1   32@0:2   33@0:3   34@0:4
   35@1:0   36@1:1   37@1:2   38@1:3   39@1:4   40@0:0   41@0:1
array2d (derefj):
    0@0:0    1@0:1    2@0:2    3@0:3    4@0:4    5@1:0    6@1:1
    7@1:2    8@1:3    9@1:4   10@0:0   11@0:1   12@0:2   13@0:3
   14@0:4   15@1:0   16@1:1   17@1:2   18@1:3   19@1:4   20@0:0
   21@0:1   22@0:2   23@0:3   24@0:4   25@1:0   26@1:1   27@1:2
   28@1:3   29@1:4   30@0:0   31@0:1   32@0:2   33@0:3   34@0:4
   35@1:0   36@1:1   37@1:2   38@1:3   39@1:4   40@0:0   41@0:1
array2d (derefij):
    0@0:0    1@0:1    2@0:2    3@0:3    4@0:4    5@1:0    6@1:1
    7@1:2    8@1:3    9@1:4   10@0:0   11@0:1   12@0:2   13@0:3
   14@0:4   15@1:0   16@1:1   17@1:2   18@1:3   19@1:4   20@0:0
   21@0:1   22@0:2   23@0:3   24@0:4   25@1:0   26@1:1   27@1:2
   28@1:3   29@1:4   30@0:0   31@0:1   32@0:2   33@0:3   34@0:4
   35@1:0   36@1:1   37@1:2   38@1:3   39@1:4   40@0:0   41@0:1
array2dptr:
    0@0:0    1@0:1    2@0:2    3@0:3    4@0:4    5@1:0    6@1:1
    7@1:2    8@1:3    9@1:4   10@0:0   11@0:1   12@0:2   13@0:3
   14@0:4   15@1:0   16@1:1   17@1:2   18@1:3   19@1:4   20@0:0
   21@0:1   22@0:2   23@0:3   24@0:4   25@1:0   26@1:1   27@1:2
   28@1:3   29@1:4   30@0:0   31@0:1   32@0:2   33@0:3   34@0:4
   35@1:0   36@1:1   37@1:2   38@1:3   39@1:4   40@0:0   41@0:1