NERSCPowering Scientific Discovery Since 1974

Introduction to PGAS Languages


Partitioned Global Address Space Languages (PGAS) provide a parallel programming model based on the assumption that the global memory adress space is logically partitioned with a portion of the memory being assigned to a specific processor. Two common PGAS languages are Unified Parallel C (UPC) and Co-array Fortran (CAF). The first implementation of this tutorial is based on UPC and CAF.

This tutorial assumes that you have some familiarity with UNIX, text editors and compiling programs from source code. It contains some material from the SC11 Tutorial, Intro to PGAS (UPC and CAF) and Hybrid for Multicore Programming, by Alice Koniges (LBL-NERSC), Katherine Yelick (LBL-NERSC), Rolf Rabenseifner (HLRS), Reinhold Bader(Leibniz Supercomputer Centre), and David Eder (LLNL). Eventually, more material from the tutorial will be available on this site.

This tutorial is specific to NERSC, however the codes should be portable. For example, the Berkeley UPC web pages provide examples as well as compilers.

Use these first programs to test your environment and get started. These experiments may be run on the NERSC Hopper machine. There are currently two suggested environments for running these exercises. Recall that one uses the "module load" commands on NERSC systems to set up links to compilers, etc. For UPC, you may use either Berkeley UPC or the Cray compilers. The Cray compilers support both UPC and CAF. For small examples it is convenient to set up interactive batch jobs. To do this, follow the instructions below.




qsub -I -q interactive -lmppwidth=24,mppnppn=24,walltime=00:20:00 -V

Then wait for a new shell.

Using Berkeley UPC

module load bupc

Go to previous working directory if in a newly started PBS shell:



  upcc  -O  -o myprog myprog.c  

Parallel Execution (requires a PBS shell):

  upcrun -n 1 -cpus-per-node 24 ./myprog

  upcrun -n 2 -cpus-per-node 24 ./myprog

  upcrun -n 4 -cpus-per-node 24 ./myprog

Using Cray Compilers

module switch PrgEnv-pgi PrgEnv-cray

Go to previous working directory if in a newly started PBS shell:


UPC Compilation:

  cc -h upc -o myprog myprog.c

or CAF compilation:

  ftn -e m -h caf -o myprog myprog.f90

Parallel Execution (requires a PBS shell):

   aprun -n 1 -N 1 ./myprog

   aprun -n 2 -N 2 ./myprog

   aprun -n 4 -N 4 ./myprog

CAF Hello

program hello
  implicit none

  integer            :: myrank, numprocs

  myrank   = this_image()
  numprocs = num_images()
  if (myrank == 1) print *, 'Hello world'
  print *, 'I am image number',myrank,' of ',numprocs
end program hello

UPC Hello

#include <stdio.h>
#include <upc.h>

int main(int argc, char** argv)
  if (MYTHREAD == 0) printf("hello world\n");
  printf("I am thread number %d of %d threads\n", MYTHREAD, THREADS);
  return 0;