Difference between revisions of "DPS921/OpenACC vs OpenMP Comparison"

From CDOT Wiki
Jump to: navigation, search
m
m
Line 40: Line 40:
 
#pragma acc kernels
 
#pragma acc kernels
 
{
 
{
for (int i = 0; i < N; i++) {
+
    for (int i = 0; i < N; i++) {
y[i] = a * x[i] + y[i];
+
        y[i] = a * x[i] + y[i];
}
+
    }
 
}
 
}
 
</source>
 
</source>
 
For example in this piece of code, the <code>kernels</code> directive tells the GPU that it is up to the GPU to decide how to parallelize the following loop.
 
For example in this piece of code, the <code>kernels</code> directive tells the GPU that it is up to the GPU to decide how to parallelize the following loop.
  
 +
<source>
 +
#pragma acc parallel loop
 +
{
 +
    for (int i = 0; i < N; i++) {
 +
        y[i] = a * x[i] + y[i];
 +
    }
 +
}
 +
</source>
 +
Or if you don't want the compiler to handle everything for you, you can specify there you want this loop to be parallelized, however you as the programmer need to be certain of what you are doing as this will take away some of compiler's freedom of parallelize the code for you.
  
 
== Compiler support ==
 
== Compiler support ==
Line 108: Line 117:
  
 
= OpenMP vs OpenACC =
 
= OpenMP vs OpenACC =
 +
 +
[[File:Openaccvsopenmp.png|800px]]
  
 
We are comparing with OpenMP for two reasons. First, OpenMP is also based on directives to parallelize code; second, OpenMP started support of offloading to accelerators starting OpenMP 4.0 using <code>target</code> constructs. OpenACC uses directives to tell the compiler where to parallelize loops, and how to manage data between host and accelerator memories. OpenMP takes a more generic approach, it allows programmers to explicitly spread the execution of loops, code regions and tasks across teams of threads.
 
We are comparing with OpenMP for two reasons. First, OpenMP is also based on directives to parallelize code; second, OpenMP started support of offloading to accelerators starting OpenMP 4.0 using <code>target</code> constructs. OpenACC uses directives to tell the compiler where to parallelize loops, and how to manage data between host and accelerator memories. OpenMP takes a more generic approach, it allows programmers to explicitly spread the execution of loops, code regions and tasks across teams of threads.
Line 119: Line 130:
  
  
== Code comparison ==
+
== Equivalent directives ==
  
 
'''Explicit conversions'''
 
'''Explicit conversions'''
Line 196: Line 207:
  
 
== Jacobi Iteration ==
 
== Jacobi Iteration ==
 +
Jacobi iteration is a common algorithm that iteratively computes the solution based on neighbour values. The following code sample is a serial version of 1D Jacobi iteration.
 +
<source>
 +
while ( err > tol && iter < iter_max ) {
 +
    err=0.0f;
 +
    for(int i = 1; i < nx-1; i++) {
 +
        Anew[i] = 0.5f * (A[i+1] + A[i-1]);
 +
        err = fmax(err, fabs(Anew[i] - A[i]));
 +
    }
 +
    for( int i = 1; i < nx-1; i++ ) {
 +
        A[i] = Anew[i];
 +
    }
 +
    iter++;
 +
}
 +
</source>
 +
 +
=== OpenMP Implementation ===
 +
An OpenMP implementation would look like the following, with shared data and reduction on summation values
 +
<source>
 +
while ( err > tol && iter < iter_max ) {
 +
    err=0.0f;
 +
    #pragma omp parallel for shared(nx, Anew, A) reduction(max:err)
 +
    for(int i = 1; i < nx-1; i++) {
 +
        Anew[i] = 0.5f * (A[i+1] + A[i-1]);
 +
        err = fmax(err, fabs(Anew[i] - A[i]));
 +
    }
 +
    #pragma omp parallel for shared(nx, Anew, A)
 +
    for( int i = 1; i < nx-1; i++ ) {
 +
        A[i] = Anew[i];
 +
    }
 +
    iter++;
 +
}
 +
</source>
 +
 +
=== OpenACC Basic Implementation ===
 +
A proper OpenACC implementation looks like this.
 +
<source>
 +
#pragma acc data copyin(A[0:nx]) copyout(Anew[0:nx])
 +
while ( err > tol && iter < iter_max ) {
 +
    err=0.0f;
 +
    #pragma acc parallel loop reduction(max:err)
 +
    for(int i = 1; i < nx-1; i++) {
 +
        Anew[i] = 0.5f * (A[i+1] + A[i-1]);
 +
        err = fmax(err, fabs(Anew[i] - A[i]));
 +
    }
 +
    #pragma acc parallel loop
 +
    for( int i = 1; i < nx-1; i++ ) {
 +
        A[i] = Anew[i];
 +
    }
 +
    iter++;
 +
}
 +
</source>
 +
 +
Or you can let the compiler handle it by using <code>kernel</code> instead of <code>parallel loop</code>. You will be notified during compilation how the compiler thinks this thing should be parallelized.
 +
<source>
 +
#pragma acc data copyin(A[0:nx]) copyout(Anew[0:nx])
 +
while ( err > tol && iter < iter_max ) {
 +
    err=0.0f;
 +
    #pragma acc kernel
 +
    for(int i = 1; i < nx-1; i++) {
 +
        Anew[i] = 0.5f * (A[i+1] + A[i-1]);
 +
        err = fmax(err, fabs(Anew[i] - A[i]));
 +
    }
 +
    #pragma acc kernel
 +
    for( int i = 1; i < nx-1; i++ ) {
 +
        A[i] = Anew[i];
 +
    }
 +
    iter++;
 +
}
 +
</source>
 +
 +
=== Execution time ===
 +
Without access to GPU, the OpenACC code runs about twice faster comparing to the OpenMP one, using the Nvidia HPC SDK <code>nvc</code> compiler. According to other data sources, with access to GPU, the OpenACC version should run about 7 times faster than the OpenMP solution that runs on CPU; and the OpenACC version runs about 2.5 times faster than an OpenMP version with GPU offloading.
  
 
= Collaboration =
 
= Collaboration =

Revision as of 02:40, 3 December 2020

Project Overview

The idea of this project is to introduce OpenACC as a parallel processing library, compare how parallelization is done in different libraries, and identify benefits of using each of the libraries. According to description of both libraries, OpenACC does parallelization more automatically whereas OpenMP allows developers to manually set regions to parallelize and assign to threads. The deliverable of this project would be a introduction to OpenACC along with performance comparison between OpenMP and OpenACC, and a discussion on usage of each one.

Group Members

1. Ruiqi Yu

2. Hanlin Li

3. Le Minh Pham

Progress

- Nov 9, 2020: Added project description
- Nov 13, 2020: Determine content sections to be discussed
- Nov 18, 2020: Successful installation of required compiler and compilation of OpenACC code
- Nov 19, 2020: Adding MPI into discussion

OpenACC

GPU parallelization vs CPU

The GPU (Graphics Processing Unit) often consists of thousands of cores, whereas a typical modern multiple core CPU has somewhere between 2 - 128 cores. Even though the GPU cores are smaller than the CPU cores and are much slower at processing serial codes, the high number of cores makes GPUs perform better with parallelized codes in many cases.

Core-Difference-Between-CPU-and-GPU.gif

Example

If we want to render a 4k image which has 8.2 million pixels (3,840 x 2,160), we have to do the same rendering algorithm on 8.2 million pixels. The GPU can accomplish this task much more efficient since it has a much higher number of processors that can execute the algorithm at the same time.

What is OpenACC

OpenACC (Open Accelerators) is a programming standard for parallel computing on accelerators such as GPUs, which mainly targets Nvidia GPUs. OpenACC is designed to simplify GPU programming, unlike CUDA and OpenCL where you need to write your programs in a different way to achieve GPU acceleration, OpenACC takes a similar approach as OpenMP, which is inserting directives into the code to offload computation onto GPUs and parallelize the code at CUDA core level. It is possible for programmers to create efficient parallel OpenACC code with only minor changes to a serial CPU code.

Benefits of using OpenACC

  • achieve parallelization on GPUs without having to learn an accelerator language such as CUDA
  • similar philosophy to OpenMP and very easy to learn

Example

#pragma acc kernels
{
    for (int i = 0; i < N; i++) {
        y[i] = a * x[i] + y[i];
    }
}

For example in this piece of code, the kernels directive tells the GPU that it is up to the GPU to decide how to parallelize the following loop.

#pragma acc parallel loop
{
    for (int i = 0; i < N; i++) {
        y[i] = a * x[i] + y[i];
    }
}

Or if you don't want the compiler to handle everything for you, you can specify there you want this loop to be parallelized, however you as the programmer need to be certain of what you are doing as this will take away some of compiler's freedom of parallelize the code for you.

Compiler support

Originally, OpenACC compilation is supported by the PGI compiler which requires an expensive subscription, there has been new options in recent years.

Nvidia HPC SDK[1]

Evolved from PGI Compiler community edition. Installation guide are provided in the official website. Currently only supports Linux systems, but Windows support will come soon.

wget https://developer.download.nvidia.com/hpc-sdk/20.9/nvhpc-20-9_20.9_amd64.deb \

  https://developer.download.nvidia.com/hpc-sdk/20.9/nvhpc-2020_20.9_amd64.deb

sudo apt-get install ./nvhpc-20-9_20.9_amd64.deb ./nvhpc-2020_20.9_amd64.deb

After installation, the compilers can be found under /opt/nvidia/hpc_sdk/Linux_x86_64/20.9/compilers/bin, and OpenACC code can be compiled with nvc -acc -gpu=manage demo.c, where -acc indicates that the code will include OpenACC directives, and -gpu=manage indicates how should memory be managed. nvc is used here because source code is C code, there is nvc++ for compiling C++ code.

The compiler can also tell how the parallel regions are generalized if you pass in a -Minfo option like

$ nvc -acc -gpu=managed -Minfo demo.c
main:
     79, Generating implicit copyin(A[:256][:256]) [if not already present]
         Generating implicit copy(_error) [if not already present]
         Generating implicit copyout(Anew[1:254][1:254]) [if not already present]
     83, Loop is parallelizable
     85, Loop is parallelizable
         Accelerator kernel generated
         Generating Tesla code
         83, #pragma acc loop gang, vector(4) /* blockIdx.y threadIdx.y */
             Generating implicit reduction(max:_error)
         85, #pragma acc loop gang, vector(32) /* blockIdx.x threadIdx.x */
     91, Generating implicit copyout(A[1:254][1:254]) [if not already present]
         Generating implicit copyin(Anew[1:254][1:254]) [if not already present]
     95, Loop is parallelizable
     97, Loop is parallelizable
         Accelerator kernel generated
         Generating Tesla code
         95, #pragma acc loop gang, vector(4) /* blockIdx.y threadIdx.y */
         97, #pragma acc loop gang, vector(32) /* blockIdx.x threadIdx.x */

This tells which loops are parallelized with line numbers for reference.

For Windows users that would like to try this SDK, WSL2 is one option. WSL2 does not fully support this SDK at this moment, due to the fact that most virtualization technologies cannot let virtualized systems use the graphic card directly. Nvidia had released a preview driver[2] that allows the Linux subsystem to recognize graphic cards installed on the machine, it allows WSL2 users to compile programs with CUDA toolkits but not with the HPC SDK yet.

Nvidia CUDA WSL2

We are not going to go over how to deal with CUDA on WSL2. We included the installation guide for using CUDA on WSL2 here for anyone's interest [3]. Note that you need to have registered in the Windows Insider Program to get one of the preview Win10 versions.

GCC[4]

GCC has added support to OpenACC since GCC 5. The latest GCC version, GCC 10 has support to OpenACC 2.6.

To compile OpenACC code with GCC, you need to run

gcc -fopenacc demo.c

However, this does not enable any GPU offloading capability. In order to enable GCC with GPU offloading, we need to build some accelerators by ourselves.

For example, to enable GPU offloading on Nvidia GPUs, you need to rebuild GCC with Nvidia PTX, then use the option -foffload=<target> to offload generated instructions to accelerator devices.

The instruction of building GCC is over complicated therefore will not be shared here.

OpenMP vs OpenACC

Openaccvsopenmp.png

We are comparing with OpenMP for two reasons. First, OpenMP is also based on directives to parallelize code; second, OpenMP started support of offloading to accelerators starting OpenMP 4.0 using target constructs. OpenACC uses directives to tell the compiler where to parallelize loops, and how to manage data between host and accelerator memories. OpenMP takes a more generic approach, it allows programmers to explicitly spread the execution of loops, code regions and tasks across teams of threads.

OpenMP's directives tell the compiler to generate parallel code in that specific way, leaving little room to the discretion of the compiler and the optimizer. The compiler must do as instructed. It is up to the programmer to guarantee that generated code is correct, parallelization and scheduling are also responsibility of the programmer, not the compiler at runtime.

OpenACC's parallel directives tells the compiler that the loop is a parallel loop. It is up to the compiler to decide how to parallelize the loop. For example the compiler can generate code to run the iterations across threads, or run the iterations across SIMD lanes. The compiler gets to decide method of parallelization based on the underlying hardware architecture, or use a mixture of different methods.

So the real difference between the two is how much freedom is given to the compilers.


Equivalent directives

Explicit conversions

OpenACC                                 OpenMP

#pragma acc kernels                     #pragma omp target			
{                                       {
    #pragma acc loop worker                 #pragma omp parallel for private(tmp)
    for(int i = 0; i < N; i++){             for(int i = 0; i < N; i++){
        tmp = …;                                tmp = …;
        array[i] = tmp * …;                     array[i] = tmp * …;
    }                                       }
    #pragma acc loop vector                 #pragma omp simd
    for(int i = 0; i < N; i++)                  for(int i = 0; i < N; i++)
        array2[i] = …;                              array2[i] = …;
}                                       }

ACC parallel

OpenACC                                 OpenMP

#pragma acc parallel                    #pragma omp target
{                                       #pragma omp parallel
    #pragma acc loop                    {
    for(int i = 0; i < N; i++){             #pragma omp for private(tmp) nowait
        tmp = …;                            for(int i = 0; i < N; i++){
        array[i] = tmp * …;                     tmp = …;			
    }                                           array[i] = tmp * …;
    #pragma acc loop                        }
    for(int i = 0; i < N; i++)              #pragma omp for simd
        array2[i] = …;                      for(int i = 0; i < N; i++)
}                                               array2[i] = …;
                                        }

ACC Kernels

OpenACC                                 OpenMP

#pragma acc kernels                     #pragma omp target
{                                       #pragma omp parallel
    for(int i = 0; i < N; i++){         {	
        tmp = …;                            #pragma omp for private(tmp)
        array[i] = tmp * …;                 for(int i = 0; i < N; i++){
    }                                           tmp = …; 
    for(int i = 0; i < N; i++)                  array[i] = tmp * …;
        array2[i] = …                       }	
}                                           #pragma omp for simd
                                            for(int i = 0; i < N; i++)
                                                array2[i] = …
                                        }

Copy vs. PCopy

OpenACC                                     OpenMP

int x[10],y[10];                            int x[10],y[10];
#pragma acc data copy(x) pcopy(y)           #pragma omp target data map(x,y)
{                                           {
    ...                                         ...
    #pragma acc kernels copy(x) pcopy(y)        #pragma omp target update to(x)
    {                                           #pragma omp target map(y)
        // Accelerator Code                     {
    ...                                             // Accelerator Code
    }                                               ...
    ...                                         }
}                                           }

Performance Comparison

Jacobi Iteration

Jacobi iteration is a common algorithm that iteratively computes the solution based on neighbour values. The following code sample is a serial version of 1D Jacobi iteration.

while ( err > tol && iter < iter_max ) {
    err=0.0f;
    for(int i = 1; i < nx-1; i++) {
        Anew[i] = 0.5f * (A[i+1] + A[i-1]);
        err = fmax(err, fabs(Anew[i] - A[i]));
    }
    for( int i = 1; i < nx-1; i++ ) {
        A[i] = Anew[i];
    }
    iter++;
}

OpenMP Implementation

An OpenMP implementation would look like the following, with shared data and reduction on summation values

while ( err > tol && iter < iter_max ) {
    err=0.0f;
    #pragma omp parallel for shared(nx, Anew, A) reduction(max:err)
    for(int i = 1; i < nx-1; i++) {
        Anew[i] = 0.5f * (A[i+1] + A[i-1]);
        err = fmax(err, fabs(Anew[i] - A[i]));
    }
    #pragma omp parallel for shared(nx, Anew, A)
    for( int i = 1; i < nx-1; i++ ) {
        A[i] = Anew[i];
    }
    iter++;
}

OpenACC Basic Implementation

A proper OpenACC implementation looks like this.

#pragma acc data copyin(A[0:nx]) copyout(Anew[0:nx])
while ( err > tol && iter < iter_max ) {
    err=0.0f;
    #pragma acc parallel loop reduction(max:err)
    for(int i = 1; i < nx-1; i++) {
        Anew[i] = 0.5f * (A[i+1] + A[i-1]);
        err = fmax(err, fabs(Anew[i] - A[i]));
    }
    #pragma acc parallel loop
    for( int i = 1; i < nx-1; i++ ) {
        A[i] = Anew[i];
    }
    iter++;
}

Or you can let the compiler handle it by using kernel instead of parallel loop. You will be notified during compilation how the compiler thinks this thing should be parallelized.

#pragma acc data copyin(A[0:nx]) copyout(Anew[0:nx])
while ( err > tol && iter < iter_max ) {
    err=0.0f;
    #pragma acc kernel
    for(int i = 1; i < nx-1; i++) {
        Anew[i] = 0.5f * (A[i+1] + A[i-1]);
        err = fmax(err, fabs(Anew[i] - A[i]));
    }
    #pragma acc kernel
    for( int i = 1; i < nx-1; i++ ) {
        A[i] = Anew[i];
    }
    iter++;
}

Execution time

Without access to GPU, the OpenACC code runs about twice faster comparing to the OpenMP one, using the Nvidia HPC SDK nvc compiler. According to other data sources, with access to GPU, the OpenACC version should run about 7 times faster than the OpenMP solution that runs on CPU; and the OpenACC version runs about 2.5 times faster than an OpenMP version with GPU offloading.

Collaboration

OpenACC with OpenMP

OpenMP and OpenACC can be used together. However, PGI stated that there are still some issues when interoperating between OpenMP and OpenACC [5], since their runtime library are not completely thread-safe. They are looking forward to improving the interaction between the two libraries in the future releases.

OpenACC with MPI

As we learned that MPI is used to allow communication and data transfer between threads during parallel execution. In the case of multiple accelerators, one of the ways we can use the two together is to use MPI to communicate between different accelerators.