Skip to content

Commit b1b73bf

Browse files
committed
docs: add how-to for OpenMP usage (#128)
Adds example for calculating PI using OpenMP and fpm
1 parent 22b3950 commit b1b73bf

File tree

4 files changed

+103
-1
lines changed

4 files changed

+103
-1
lines changed

pages/how-to/compute-pi-openmp.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Computing PI with OpenMP
2+
3+
This is a simple example of how to use OpenMP with fpm.
4+
It is an adapted version of the OpenMP example that can be found
5+
[here](https://github.com/gjbex/Fortran-MOOC/blob/master/source_code/computing_pi/compute_pi_omp.f90) under a
6+
[CC-BY-4.0](https://github.com/gjbex/Fortran-MOOC/blob/master/LICENSE) license.
7+
8+
The code approximates the value of PI by performing parallelized numerical
9+
integration over a quarter of the unit circle.
10+
The code is structured as follows:
11+
12+
```{literalinclude} ../../src/how-to/compute-pi-openmp/app/main.f90
13+
:language: fortran
14+
:caption: app/main.f90
15+
```
16+
17+
## Using OpenMP as a dependency
18+
19+
To use OpenMP in your project, you need to add the `openmp` dependency to your `fpm.toml` file:
20+
21+
```{literalinclude} ../../src/how-to/compute-pi-openmp/fpm.toml
22+
:language: toml
23+
:caption: fpm.toml
24+
:emphasize-lines: 4-5
25+
```
26+
27+
OpenMP is a _built-in dependency_ (i.e. metapackage), which means the above
28+
syntax needs to be used. To find out more about metapackages, see [](../spec/metapackages).
29+
30+
## Building and running the code
31+
32+
To build and run the code, one can use the following commands:
33+
34+
```{code-block} text
35+
❯ fpm run
36+
Project is up to date
37+
Iterations: 10000, PI: 3.141391477611324
38+
Took: 0.092s, with absolute error: 2.0E-04
39+
```
40+
41+
And increasing the number of iterations for the approximation while
42+
simultaneously enabling compiler optimizations with `--profile-release`
43+
44+
```{code-block} text
45+
❯ fpm run --profile-release -- 1000000000
46+
main.f90 done.
47+
compute-pi-openmp done.
48+
[100%] Project compiled successfully.
49+
Iterations: 1000000000, PI: 3.141592651589789
50+
Took: 3.511s, with absolute error: 2.0E-09
51+
```

pages/how-to/index.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
# How-To guides
44

5-
:::{note}
65
This section contains practical guides and recipes for solving specific problems with fpm.
6+
7+
:::{toctree}
8+
compute-pi-openmp
79
:::
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
program compute_pi_openmp
2+
use, intrinsic :: iso_fortran_env, only: dp => real64, i8 => int64, real128
3+
implicit none
4+
integer(kind=i8) :: i, n_iterations
5+
real(kind=dp) :: delta, x, pi
6+
real(kind=dp) :: start, end
7+
8+
pi = 0.0_dp
9+
n_iterations = get_iterations(10000_i8)
10+
delta = 1.0_dp / n_iterations
11+
x = 0.0_dp
12+
13+
call cpu_time(start)
14+
!$omp parallel do default(none) private(x) shared(delta, n_iterations) reduction(+:pi)
15+
do i = 1, n_iterations
16+
x = i * delta
17+
pi = pi + sqrt(1.0_dp - x**2)
18+
end do
19+
!$omp end parallel do
20+
call cpu_time(end)
21+
22+
pi = 4.0_dp * pi / n_iterations
23+
print "(A, I16, A, F25.15)", "Iterations: ", n_iterations, ", PI: ", pi
24+
print "(A, F8.3, A, ES8.1)", "Took: ", end - start, "s, with absolute error: ", acos(-1.0_real128) - pi
25+
26+
contains
27+
28+
integer(i8) function get_iterations(default_iterations)
29+
integer(kind=i8), intent(in) :: default_iterations
30+
character(len=100) :: buffer, msg
31+
integer :: stat
32+
33+
get_iterations = default_iterations
34+
if (command_argument_count() >= 1) then
35+
call get_command_argument(1, buffer)
36+
read (buffer, fmt=*, iostat=stat, iomsg=msg) get_iterations
37+
if (stat /= 0) stop msg
38+
end if
39+
end function get_iterations
40+
41+
end program compute_pi_openmp

src/how-to/compute-pi-openmp/fpm.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
name = "compute-pi-openmp"
2+
version = "0.1.0"
3+
4+
[dependencies]
5+
openmp = "*"
6+
7+
[[executable]]
8+
name = "compute-pi-openmp"

0 commit comments

Comments
 (0)