Skip to content

Commit 8178099

Browse files
authored
Merge pull request #1797 from krisdale/update-fortran-combi-logic-openmp-primes
Update Fortran combinational logic openmp-primes sample
2 parents a763cc4 + b550121 commit 8178099

File tree

7 files changed

+328
-227
lines changed

7 files changed

+328
-227
lines changed
Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,38 @@
1-
## =============================================================
2-
## Copyright © 2020 Intel Corporation
3-
##
4-
## SPDX-License-Identifier: MIT
5-
## =============================================================
6-
##
7-
##
8-
##******************************************************************************
9-
## Content:
10-
##
11-
## Build for openmp_sample
12-
##******************************************************************************
13-
14-
FC = ifort
15-
16-
release: openmp_sample.exe
17-
18-
debug: openmp_sample_dbg.exe
19-
20-
run: release ; @export DYLD_LIBRARY_PATH="$(LIBRARY_PATH)" ; ./openmp_sample.exe
21-
22-
debug_run: debug ; @export DYLD_LIBRARY_PATH="$(LIBRARY_PATH)" ; ./openmp_sample_dbg.exe
23-
24-
openmp_sample.exe: openmp_sample.o
25-
$(FC) -O2 -fpp -qopenmp $^ -o $@
26-
27-
openmp_sample_dbg.exe: openmp_sample_dbg.o
28-
$(FC) -O0 -g -fpp -qopenmp $^ -o $@
29-
30-
%.o: src/%.f90
31-
$(FC) -O2 -c -fpp -qopenmp -o $@ $<
32-
33-
%_dbg.o: src/%.f90
34-
$(FC) -O0 -g -c -fpp -qopenmp -o $@ $<
35-
36-
clean:
37-
/bin/rm -f core.* *.o *.exe
1+
## =============================================================
2+
## Copyright © 2020 Intel Corporation
3+
##
4+
## SPDX-License-Identifier: MIT
5+
## =============================================================
6+
##
7+
##
8+
##******************************************************************************
9+
## Content:
10+
##
11+
## Build for openmp_sample
12+
##******************************************************************************
13+
SHELL := /bin/bash
14+
15+
FC = ifx
16+
17+
release: openmp_sample
18+
19+
debug: openmp_sample_dbg
20+
21+
run: release ; time ./openmp_sample
22+
23+
debug_run: debug ; time ./openmp_sample_dbg
24+
25+
openmp_sample: openmp_sample.o
26+
$(FC) -O2 -qopenmp $^ -o $@
27+
28+
openmp_sample_dbg: openmp_sample_dbg.o
29+
$(FC) -O0 -g -qopenmp $^ -o $@
30+
31+
%.o: src/%.f90
32+
$(FC) -O2 -c -qopenmp -o $@ $<
33+
34+
%_dbg.o: src/%.f90
35+
$(FC) -O0 -g -c -qopenmp -o $@ $<
36+
37+
clean:
38+
/bin/rm -f core.* *.o openmp_sample openmp_sample_dbg

DirectProgramming/Fortran/CombinationalLogic/openmp-primes/README.md

Lines changed: 104 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,173 +1,210 @@
11
# `OpenMP* Primes` Samples
2-
The `OpenMP* Primes` sample is designed to illustrate how to use the OpenMP* API
3-
with the Intel® Fortran Compiler.
2+
3+
The `OpenMP* Primes` sample is designed to illustrate how to use the OpenMP* API with the Intel® Fortran Compiler.
44

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

910
| Area | Description
1011
|:--- |:---
11-
| What you will learn | How to build and run a Fortran OpenMP application using Intel® Fortran Compiler
12+
| What you will learn | How to build and run a Fortran OpenMP application using the Intel® Fortran Compiler
1213
| Time to complete | 10 minutes
1314

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

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

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

3234
## Prerequisites
35+
3336
| Optimized for | Description
3437
|:--- |:---
35-
| OS | macOS* <br> Xcode*
38+
| OS | Linux*<br>Windows*
3639
| Software | Intel® Fortran Compiler
3740

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

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

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

51-
You can compile the program with all optimizations disabled using the `-O0` or
52-
at any level of optimization `-O1`, `-O2`, or `-O3`.
50+
Use the following options to compile the program versions.
51+
52+
- [`-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
53+
option results in a serial program.
54+
- [`-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).
5355

5456
>**Note**: You can find more information about these options in the *Compiler
5557
>Options* section of the [Intel® Fortran Compiler Developer Guide and
56-
>Reference](https://www.intel.com/content/www/us/en/develop/documentation/fortran-compiler-oneapi-dev-guide-and-reference).
58+
>Reference](https://www.intel.com/content/www/us/en/docs/fortran-compiler/developer-guide-reference/current/overview.html).
5759
5860
## Set Environment Variables
61+
5962
When working with the command-line interface (CLI), you should configure the
6063
oneAPI toolkits using environment variables. Set up your CLI environment by
6164
sourcing the `setvars` script every time you open a new terminal window. This
6265
practice ensures that your compiler, libraries, and tools are ready for
6366
development.
6467

65-
## Build the `OpenMP* Primes` Sample
6668
> **Note**: If you have not already done so, set up your CLI environment by
6769
> sourcing the `setvars` script in the root of your oneAPI installation.
6870
>
69-
> Linux and macOS*:
70-
> - For system wide installations: `. /opt/intel/oneapi/setvars.sh`
71+
> Linux:
72+
> - For system wide installations in the default installation directory: `. /opt/intel/oneapi/setvars.sh`
7173
> - For private installations: ` . ~/intel/oneapi/setvars.sh`
72-
> - For non-POSIX shells, like csh, use commands similar to the following: `bash
73-
> -c 'source <install-dir>/setvars.sh ; exec csh'`
74+
>
75+
> Windows:
76+
> - Under normal circumstances, you do not need to run the setvars.bat batch file. The terminal shortcuts
77+
> in the Windows Start menu, Intel oneAPI command prompt for <target architecture> for Visual Studio <year>,
78+
> set these variables automatically.
79+
>
80+
> 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).
7481
>
7582
> For more information on configuring environment variables, see [Use the
76-
> setvars Script with Linux* or
77-
> 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).
78-
79-
### Use Visual Studio Code* (VS Code) (Optional)
80-
You can use Visual Studio Code* (VS Code) extensions to set your environment,
81-
create launch configurations, and browse and download samples.
82-
83-
The basic steps to build and run a sample using VS Code include:
84-
1. Configure the oneAPI environment with the extension **Environment
85-
Configurator for Intel® oneAPI Toolkits**.
86-
2. Download a sample using the extension **Code Sample Browser for Intel®
87-
oneAPI Toolkits**.
88-
3. Open a terminal in VS Code (**Terminal > New Terminal**).
89-
4. Run the sample in the VS Code terminal using the instructions below.
90-
91-
To learn more about the extensions and how to configure the oneAPI environment,
92-
see the [Using Visual Studio Code with Intel® oneAPI Toolkits User
93-
Guide](https://www.intel.com/content/www/us/en/develop/documentation/using-vs-code-with-intel-oneapi/top.html).
94-
95-
### On macOS*
83+
> 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).
84+
85+
## Build the `OpenMP Primes` Sample
86+
9687
1. Change to the sample directory.
97-
2. Build release and debug versions of the program.
88+
2. Build debug and release versions of the program.
89+
90+
Linux:
91+
9892
```
9993
make clean
10094
make debug
95+
make
10196
```
10297

103-
#### Troubleshooting
104-
If you receive an error message, troubleshoot the problem using the
105-
**Diagnostics Utility for Intel® oneAPI Toolkits**. The diagnostic utility
106-
provides configuration and system checks to help find missing dependencies,
107-
permissions errors, and other issues. See the [Diagnostics Utility for Intel®
108-
oneAPI Toolkits User
109-
Guide](https://www.intel.com/content/www/us/en/develop/documentation/diagnostic-utility-user-guide/top.html)
110-
for more information on using the utility.
98+
Windows:
99+
100+
```
101+
build.bat
102+
```
103+
104+
## Run the `OpenMP* Primes` Program
111105

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

116109
### Experiment 1: Run the Debug Version
110+
117111
1. Run the program.
112+
113+
Linux:
114+
118115
```
119116
make debug_run
120117
```
121-
Notice the speed.
122118

123-
### Experiment 2: Run the Optimized Version
124-
1. Build and run the release version.
119+
Windows:
120+
125121
```
126-
make
122+
debug_run.bat
127123
```
128-
2. Run the program.
124+
125+
Notice the timestamp. With multi-threaded applications, use Elapsed Time to measure the time. CPU time is the time
126+
accumulated for all threads.
127+
128+
### Experiment 2: Run the Optimized Version
129+
130+
1. Run the program.
131+
132+
Linux:
133+
129134
```
130135
make run
131136
```
137+
138+
Windows:
139+
140+
```
141+
run.bat
142+
```
143+
132144
Did the debug (unoptimized) version run slower?
133145

134146
### Experiment 3: Change the Number of Threads
147+
135148
By default, an OpenMP application creates and uses as many threads as the number
136-
of "processors" in a system. A "processor" is defined as the number of logical
149+
of "processors" in a system. A "processor" is defined as the number of logical
137150
processors, which are twice the number of physical cores on hyperthreaded cores.
138151

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

142155
1. Experiment with a single thread.
156+
157+
Linux:
158+
143159
```
144-
export OMP_NUM_THREADS=1
160+
export OMP_NUM_THREADS=1`
145161
make run
146162
```
163+
164+
Windows:
165+
166+
```
167+
set OMP_NUM_THREADS=1
168+
run.bat
169+
```
170+
147171
Notice the number of threads reported by the application.
148172

149173
2. Experiment with 2 threads.
174+
175+
Linux:
176+
150177
```
151178
export OMP_NUM_THREADS=2
152179
make run
180+
```
181+
182+
Windows:
183+
184+
```
185+
set OMP_NUM_THREADS=2
186+
run.bat
153187
```
188+
154189
Notice if the application ran faster with more threads.
155190

156-
3. Experiment with the number of threads, and see changing threads numbers
191+
3. Experiment with the number of threads and see how changing the number of threads
157192
affects performance.
158193

159-
4. Clean the project files.
194+
4. On Linux clean the object and executable files.
195+
160196
```
161197
make clean
162198
```
163199

164200
## Further Reading
165-
Interested in learning more? Read about using OpenMP with the Intel® Fortran
166-
Compiler in the *OpenMP Support* section of the [Intel® Fortran Compiler
201+
202+
Read about using OpenMP with the Intel® Fortran Compiler in the *OpenMP Support* section of the [Intel® Fortran Compiler
167203
Developer Guide and
168-
Reference](https://www.intel.com/content/www/us/en/develop/documentation/fortran-compiler-oneapi-dev-guide-and-reference).
204+
Reference](https://www.intel.com/content/www/us/en/docs/fortran-compiler/developer-guide-reference/current/overview.html).
169205

170206
## License
207+
171208
Code samples are licensed under the MIT license. See
172209
[License.txt](https://github.com/oneapi-src/oneAPI-samples/blob/master/License.txt)
173210
for details.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
: =============================================================
2+
: Copyright © 2020 Intel Corporation
3+
:
4+
: SPDX-License-Identifier: MIT
5+
: =============================================================
6+
:
7+
:
8+
:******************************************************************************
9+
: Content:
10+
:
11+
: Build for openmp_sample
12+
:******************************************************************************
13+
14+
del *.obj *.exe
15+
16+
ifx /O2 /Qopenmp src/openmp_sample.f90 /o openmp_sample.exe
17+
18+
ifx /Od /Qopenmp src/openmp_sample.f90 /o openmp_sample_dbg.exe
19+
20+
21+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
: =============================================================
2+
: Copyright © 2020 Intel Corporation
3+
:
4+
: SPDX-License-Identifier: MIT
5+
: =============================================================
6+
:
7+
:
8+
:******************************************************************************
9+
: Content:
10+
:
11+
: Debug_run for openmp_sample
12+
:******************************************************************************
13+
14+
echo Start time=%time%
15+
16+
openmp_sample_dbg.exe
17+
18+
echo End time=%time%

0 commit comments

Comments
 (0)