APPENDIX E

Quick UPC Reference

KEYWORDS

  • THREADS: total number of threads
  • MYTHREAD: identification number of the current thread (between 0 and THREADS−1)
  • UPC_MAX_BLOCK_SIZE: maximum block size allowed by the compilation environment

SHARED VARIABLE DECLARATIONS

Shared Objects

Shared variables are declared using the type qualifier “shared.” Shared objects must be declared statically (i.e., either as global variables or with the keyword static).

Examples of Shared Object Declaration

shared int i;

shared int b[100*THREADS];

The following will not compile if you do not specify the number of threads:

shared int a[100];

All the elements of a are allocated in thread 0:

shared [] int a[100];

Distribute the elements in round-robin fashion by chunks of two elements: a[0] and a[1] are allocated in thread 0; a[2] and a[3] in thread 1;…:

shared [2] int a[100];

Shared Pointers

Pointer to shared object:

shared int* p;

Shared pointer to shared object:

shared int* shared sp;

Equivalent of memset:

upc_memset(dst, char, size)

Assign a block of characters to shared memory.

LOCKS

//Dynamic lock collectively allocated:


{
 upc_lock_t *l;
 l = upc_all_lock_alloc();

 //…
 upc_lock (l);
 // protected section
 upc_unlock (l);

 if( upc_lock_attempt(l))
   {
     //do something if l currently unlocked
   }

 //unallocates the lock
 upc_lock_free (l);
}

//Dynamic lock globally allocated:
upc_lock_t *l;

{
 if(MYTHREAD ==3)
  l = upc_global_lock_alloc();
}

GENERAL UTILITIES

Terminate the UPC program with exit status ...

Get UPC: DISTRIBUTED SHARED MEMORY PROGRAMMING now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.