This lesson is being piloted (Beta version)

Introduction

Overview

Teaching: 20 min
Exercises: 0 min
Questions
  • How shared memory parallel programs work?

  • What is OpenMP?

  • How to write and compile parallel programs in C?

Objectives
  • Understand the shared memory programming environment provided by OpenMP

  • Learn how to write and compile simple parallel programs in C

Shared Memory OpenMP Programming Overview

View OpenMP specifications
View Full List of Compilers supporting OpenMP
For an overview of the past, present and future of the OpenMP read the paper “The Ongoing Evolution of OpenMP”.

OpenMP Execution Model

Compiling OpenMP Programs

Create a working directory in ~/scratch:

cd ~/scratch

Code examples on the training cluster can be obtained by following these steps:

cp /tmp/omp_workshop_2024.tar .
tar -xf omp_workshop_2024.tar

If you are using a real cluster, you can download examples from the web:

wget https://github.com/acenet-arc/ACENET_Summer_School_OpenMP_ACC/raw/gh-pages/code/omp_workshop_2024.tar 
tar -xf omp_workshop_2024.tar

A Very Quick Introduction to C

Preprocessor Directives

#include <header_file.h> 
# define SIZE 1000
#ifdef MACRO
controlled code
#endif 

Basic Syntax

Functions

Pointers

Arrays

int A[500]; 
int * A;  
size = 500; // Define array size
A = (int *)malloc(size*sizeof(int)); // Allocate memory
A = (int *)realloc(A,1000);  // Increase size
A[i]=10;  // Set array elements 
free(A); // Free memory 

Compiling C Code

Using C compilers on Compute Canada Systems

  • Currently the default environment on all general purpose clusters (Beluga, Cedar, Graham, Narval) is StdEnv/2023.
  • The default compilers available in this environment on Graham and Beluga are intel/2023.2.1 and gcc/12.3.
  • To load another compiler you can use the command module load. For example, the command to load gcc/10.3.0 is:
module load StdEnv/2020 gcc/10.3.0

Training cluster

The default environment on the training cluster is StdEnv/2020. We will use gcc/9.3.0

module load StdEnv/2020 gcc/9.3.0
  • Intel compilers are not available on the traiining cluster

The following is an example of a simple hello world program written in C.

/* --- File hello_serial.c --- */
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv) {
  printf("Hello World\n");
}

In order to compile this code, you would need to use the following command:

gcc -o hello hello_serial.c

This gives you an executable file hello that will print out the text “Hello World”. You run it with the command:

./hello
Hello World

Key Points

  • Shared-memory parallel programs break up large problems into a number of smaller ones and execute them simultaneously

  • OpenMP programs are limited to a single physical machine

  • OpenMP libraries are built into all commonly used C, C++, or Fortran compilers