Skip to content

Update Fortran combinational logic openmp-primes sample #1797

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 38 additions & 37 deletions DirectProgramming/Fortran/CombinationalLogic/openmp-primes/Makefile
Original file line number Diff line number Diff line change
@@ -1,37 +1,38 @@
## =============================================================
## Copyright © 2020 Intel Corporation
##
## SPDX-License-Identifier: MIT
## =============================================================
##
##
##******************************************************************************
## Content:
##
## Build for openmp_sample
##******************************************************************************

FC = ifort

release: openmp_sample.exe

debug: openmp_sample_dbg.exe

run: release ; @export DYLD_LIBRARY_PATH="$(LIBRARY_PATH)" ; ./openmp_sample.exe

debug_run: debug ; @export DYLD_LIBRARY_PATH="$(LIBRARY_PATH)" ; ./openmp_sample_dbg.exe

openmp_sample.exe: openmp_sample.o
$(FC) -O2 -fpp -qopenmp $^ -o $@

openmp_sample_dbg.exe: openmp_sample_dbg.o
$(FC) -O0 -g -fpp -qopenmp $^ -o $@

%.o: src/%.f90
$(FC) -O2 -c -fpp -qopenmp -o $@ $<

%_dbg.o: src/%.f90
$(FC) -O0 -g -c -fpp -qopenmp -o $@ $<

clean:
/bin/rm -f core.* *.o *.exe
## =============================================================
## Copyright © 2020 Intel Corporation
##
## SPDX-License-Identifier: MIT
## =============================================================
##
##
##******************************************************************************
## Content:
##
## Build for openmp_sample
##******************************************************************************
SHELL := /bin/bash

FC = ifx

release: openmp_sample

debug: openmp_sample_dbg

run: release ; time ./openmp_sample

debug_run: debug ; time ./openmp_sample_dbg

openmp_sample: openmp_sample.o
$(FC) -O2 -qopenmp $^ -o $@

openmp_sample_dbg: openmp_sample_dbg.o
$(FC) -O0 -g -qopenmp $^ -o $@

%.o: src/%.f90
$(FC) -O2 -c -qopenmp -o $@ $<

%_dbg.o: src/%.f90
$(FC) -O0 -g -c -qopenmp -o $@ $<

clean:
/bin/rm -f core.* *.o openmp_sample openmp_sample_dbg
171 changes: 104 additions & 67 deletions DirectProgramming/Fortran/CombinationalLogic/openmp-primes/README.md
Original file line number Diff line number Diff line change
@@ -1,173 +1,210 @@
# `OpenMP* Primes` Samples
The `OpenMP* Primes` sample is designed to illustrate how to use the OpenMP* API
with the Intel® Fortran Compiler.

The `OpenMP* Primes` sample is designed to illustrate how to use the OpenMP* API with the Intel® Fortran Compiler.

This program finds all primes in the first 40,000,000 integers, the number of
4n+1 primes, and the number of 4n-1 primes in the same range. The sample
illustrates two OpenMP* directives to help speed up code.
demonstrates how to use two OpenMP* directives to help speed up code.


| Area | Description
|:--- |:---
| What you will learn | How to build and run a Fortran OpenMP application using Intel® Fortran Compiler
| What you will learn | How to build and run a Fortran OpenMP application using the Intel® Fortran Compiler
| Time to complete | 10 minutes

## Purpose

This program finds all primes in the first 40,000,000 integers, the number of
4n+1 primes, and the number of 4n-1 primes in the same range. It illustrates two
OpenMP* directives to help speed up the code.
4n+1 primes, and the number of 4n-1 primes in the same range. It shows how to use
two OpenMP directives to help speed up the code.

First, a dynamic schedule clause is used with the OpenMP* for a directive.
First, a dynamic schedule clause is used with the OpenMP for a directive.
Because the workload of the DO loop increases as its index get bigger, the
default static scheduling does not work well. Instead, dynamic scheduling
accounts for the increased workload. Dynamic scheduling itself has more overhead
than static scheduling, so a chunk size of 10 is used to reduce the overhead for
dynamic scheduling.

Second, a reduction clause is used instead of an OpenMP* critical directive to
Second, a reduction clause is used instead of an OpenMP critical directive to
eliminate lock overhead. Using a critical directive would cause excessive lock
overhead due to the one-thread-at-time update of the shared variables each time
through the DO loop. Instead, the reduction clause causes only one update of the
shared variables once at the end of the loop.

## Prerequisites

| Optimized for | Description
|:--- |:---
| OS | macOS* <br> Xcode*
| OS | Linux*<br>Windows*
| Software | Intel® Fortran Compiler

>**Note**: The Intel® Fortran Compiler is included in the [Intel® oneAPI HPC
>Toolkit (HPC
>Kit)](https://www.intel.com/content/www/us/en/developer/tools/oneapi/hpc-toolkit.html).
>Toolkit](https://www.intel.com/content/www/us/en/developer/tools/oneapi/hpc-toolkit.html) or available as a
[stand-alone download](https://www.intel.com/content/www/us/en/developer/articles/tool/oneapi-standalone-components.html#fortran).

## Key Implementation Details
The Intel® Fortran Compiler includes all libraries and headers necessary to
compile and run OpenMP* enabled Fortran applications.

You must use the following options to compile the program versions.
- `-qopenmp` enables compiler recognition of OpenMP* directives. (Omitting this
option results in a serial program.)
- `-fpp` enables the Fortran preprocessor.
The Intel Fortran Compiler includes all libraries and headers necessary to
compile and run OpenMP-enabled Fortran applications.

You can compile the program with all optimizations disabled using the `-O0` or
at any level of optimization `-O1`, `-O2`, or `-O3`.
Use the following options to compile the program versions.

- [`-qopenmp` (Linux) or `/Qopenmp` (Windows)](https://www.intel.com/content/www/us/en/docs/fortran-compiler/developer-guide-reference/current/qopenmp-qopenmp.html) enables compiler recognition of OpenMP* directives. Omitting this
option results in a serial program.
- [`-O[n]` (Linux) or `/O[n]` (Windows)](https://www.intel.com/content/www/us/en/docs/fortran-compiler/developer-guide-reference/current/o-001.html) sets the optimization level from level 1 (`-O1`) to level 3 (`-O3`). You can disable all optimizations using `-O0` (Linux) or `/Od` (Windows).

>**Note**: You can find more information about these options in the *Compiler
>Options* section of the [Intel® Fortran Compiler Developer Guide and
>Reference](https://www.intel.com/content/www/us/en/develop/documentation/fortran-compiler-oneapi-dev-guide-and-reference).
>Reference](https://www.intel.com/content/www/us/en/docs/fortran-compiler/developer-guide-reference/current/overview.html).

## Set Environment Variables

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.

## Build the `OpenMP* Primes` Sample
> **Note**: If you have not already done so, set up your CLI environment by
> sourcing the `setvars` script in the root of your oneAPI installation.
>
> Linux and macOS*:
> - For system wide installations: `. /opt/intel/oneapi/setvars.sh`
> Linux:
> - For system wide installations in the default installation directory: `. /opt/intel/oneapi/setvars.sh`
> - For private installations: ` . ~/intel/oneapi/setvars.sh`
> - For non-POSIX shells, like csh, use commands similar to the following: `bash
> -c 'source <install-dir>/setvars.sh ; exec csh'`
>
> Windows:
> - Under normal circumstances, you do not need to run the setvars.bat batch file. The terminal shortcuts
> in the Windows Start menu, Intel oneAPI command prompt for <target architecture> for Visual Studio <year>,
> set these variables automatically.
>
> For additional information, see [Use the Command Line on Windows](https://www.intel.com/content/www/us/en/docs/fortran-compiler/developer-guide-reference/current/use-the-command-line-on-windows.html).
>
> For more information on configuring environment variables, see [Use the
> setvars Script with Linux* or
> macOS*](https://www.intel.com/content/www/us/en/develop/documentation/oneapi-programming-guide/top/oneapi-development-environment-setup/use-the-setvars-script-with-linux-or-macos.html).

### Use Visual Studio Code* (VS Code) (Optional)
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:
1. Configure the oneAPI environment with the extension **Environment
Configurator for Intel® oneAPI Toolkits**.
2. Download a sample using the extension **Code Sample Browser for Intel®
oneAPI Toolkits**.
3. Open a terminal in VS Code (**Terminal > New Terminal**).
4. 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](https://www.intel.com/content/www/us/en/develop/documentation/using-vs-code-with-intel-oneapi/top.html).

### On macOS*
> setvars Script with Linux and Windows](https://www.intel.com/content/www/us/en/docs/fortran-compiler/developer-guide-reference/current/specifying-the-location-of-compiler-components.html).

## Build the `OpenMP Primes` Sample

1. Change to the sample directory.
2. Build release and debug versions of the program.
2. Build debug and release versions of the program.

Linux:

```
make clean
make debug
make
```

#### Troubleshooting
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](https://www.intel.com/content/www/us/en/develop/documentation/diagnostic-utility-user-guide/top.html)
for more information on using the utility.
Windows:

```
build.bat
```

## Run the `OpenMP* Primes` Program

## Run the `OpenMP* Primes` Programs
You can run different versions of the program to discover application runtime
changes.

### Experiment 1: Run the Debug Version

1. Run the program.

Linux:

```
make debug_run
```
Notice the speed.

### Experiment 2: Run the Optimized Version
1. Build and run the release version.
Windows:

```
make
debug_run.bat
```
2. Run the program.

Notice the timestamp. With multi-threaded applications, use Elapsed Time to measure the time. CPU time is the time
accumulated for all threads.

### Experiment 2: Run the Optimized Version

1. Run the program.

Linux:

```
make run
```

Windows:

```
run.bat
```

Did the debug (unoptimized) version run slower?

### Experiment 3: Change the Number of Threads

By default, an OpenMP application creates and uses as many threads as the number
of "processors" in a system. A "processor" is defined as the number of logical
of "processors" in a system. A "processor" is defined as the number of logical
processors, which are twice the number of physical cores on hyperthreaded cores.

OpenMP uses the environment variable `OMP_NUM_THREADS` to set the number of
threads to use.

1. Experiment with a single thread.

Linux:

```
export OMP_NUM_THREADS=1
export OMP_NUM_THREADS=1`
make run
```

Windows:

```
set OMP_NUM_THREADS=1
run.bat
```

Notice the number of threads reported by the application.

2. Experiment with 2 threads.

Linux:

```
export OMP_NUM_THREADS=2
make run
```

Windows:

```
set OMP_NUM_THREADS=2
run.bat
```

Notice if the application ran faster with more threads.

3. Experiment with the number of threads, and see changing threads numbers
3. Experiment with the number of threads and see how changing the number of threads
affects performance.

4. Clean the project files.
4. On Linux clean the object and executable files.

```
make clean
```

## Further Reading
Interested in learning more? Read about using OpenMP with the Intel® Fortran
Compiler in the *OpenMP Support* section of the [Intel® Fortran Compiler

Read about using OpenMP with the Intel® Fortran Compiler in the *OpenMP Support* section of the [Intel® Fortran Compiler
Developer Guide and
Reference](https://www.intel.com/content/www/us/en/develop/documentation/fortran-compiler-oneapi-dev-guide-and-reference).
Reference](https://www.intel.com/content/www/us/en/docs/fortran-compiler/developer-guide-reference/current/overview.html).

## License

Code samples are licensed under the MIT license. See
[License.txt](https://github.com/oneapi-src/oneAPI-samples/blob/master/License.txt)
for details.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
: =============================================================
: Copyright © 2020 Intel Corporation
:
: SPDX-License-Identifier: MIT
: =============================================================
:
:
:******************************************************************************
: Content:
:
: Build for openmp_sample
:******************************************************************************

del *.obj *.exe

ifx /O2 /Qopenmp src/openmp_sample.f90 /o openmp_sample.exe

ifx /Od /Qopenmp src/openmp_sample.f90 /o openmp_sample_dbg.exe



Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
: =============================================================
: Copyright © 2020 Intel Corporation
:
: SPDX-License-Identifier: MIT
: =============================================================
:
:
:******************************************************************************
: Content:
:
: Debug_run for openmp_sample
:******************************************************************************

echo Start time=%time%

openmp_sample_dbg.exe

echo End time=%time%
Loading