The OpenMP* Reduction code sample is a simple program to calculate pi; however, the program is implemented using C++ and OpenMP* for Intel® CPU and accelerators.
| Area | Description |
|---|---|
| What you will learn | How to use OpenMP for CPU and GPU offload |
| Time to complete | 10 min |
This example demonstrates how to perform reduction by using the CPU in serial mode, the CPU in parallel mode (using OpenMP), and the GPU using OpenMP offloading.
| Optimized for | Description |
|---|---|
| OS | Ubuntu* 18.04 |
| Hardware | Skylake with GEN9 or newer |
| Software | Intel® oneAPI DPC++ Compiler |
This code shows how to use OpenMP on the CPU host as well as using target offload capabilities.
The different modes use a simple calculation using the well known mathematical formula,
Each of the different functions calculates pi by breaking the range into many tiny rectangles and then summing up the results.
Note: For comprehensive information about oneAPI programming, see the Intel® oneAPI Programming Guide. (Use search or the table of contents to find relevant information quickly.)
When working with the command-line interface (CLI), you should configure the oneAPI toolkits using environment variables. Set up your CLI environment by sourcing the setvars script every time you open a new terminal window. This practice ensures that your compiler, libraries, and tools are ready for development.
Note: If you have not already done so, set up your CLI environment by sourcing the
setvarsscript in the root of your oneAPI installation.Linux*:
- For system wide installations:
. /opt/intel/oneapi/setvars.sh- For private installations:
. ~/intel/oneapi/setvars.sh- For non-POSIX shells, like csh, use the following command:
bash -c 'source <install-dir>/setvars.sh ; exec csh'For more information on configuring environment variables, see Use the setvars Script with Linux* or macOS*.
You can use Visual Studio Code* (VS Code) extensions to set your environment, create launch configurations, and browse and download samples.
The basic steps to build and run a sample using VS Code include:
- Configure the oneAPI environment with the extension Environment Configurator for Intel Software Developer Tools.
- Download a sample using the extension Code Sample Browser for Intel Software Developer Tools.
- Open a terminal in VS Code (Terminal > New Terminal).
- Run the sample in the VS Code terminal using the instructions below.
To learn more about the extensions and how to configure the oneAPI environment, see the Using Visual Studio Code with Intel® oneAPI Toolkits User Guide.
- Change to the sample directory.
- Build the program.
mkdir build cd build cmake .. make
If an error occurs, you can get more details by running make with the VERBOSE=1 argument:
make VERBOSE=1
If you receive an error message, troubleshoot the problem using the Diagnostics Utility for Intel® oneAPI Toolkits. The diagnostic utility provides configuration and system checks to help find missing dependencies, permissions errors, and other issues. See the Diagnostics Utility for Intel® oneAPI Toolkits User Guide for more information on using the utility.
- Run the program.
make run - Clean the program. (Optional)
make clean
The actual results depend on the target device.
Number of steps is 1000000
Cpu Seq calc: PI =3.14 in 0.00105 seconds
Host OpenMP: PI =3.14 in 0.0010 seconds
Offload OpenMP: PI =3.14 in 0.0005 seconds
success
Built target run
Code samples are licensed under the MIT license. See License.txt for details.
Third party program licenses are at third-party-programs.txt.