The following simple example is used to demonstrate capabilities of the Clang UPC toolset. Copy the lines of code bellow into a file (e.g. sample.upc).

#include <upc.h>  /* Optional ... */
#include <stdio.h>

shared int sint[THREADS];

int
main(void)
{
  int i;
  for (i=0; i<THREADS; ++i)
    {
      if (i == MYTHREAD)
        {
          sint[MYTHREAD] = MYTHREAD;
          printf ("%d\n", MYTHREAD);
        }
      upc_barrier;
    }
  if (!MYTHREAD)
    {
      for (i=0; i<THREADS; ++i)
        {
          if (sint[i] != i)
            printf ("Error at entry %d (%d)\n", i, sint[i]);
        }
    }
}

And, compile it with the following command:

clang-upc -o sample sample.upc

Executable, sample, is created with the compiler’s default options (packed pointer representation, dynamic number of threads).

Run the executable with five (5) UPC threads:

./sample -n 5

and observe the following program output:

0
1
2
3
4