Skip to content
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

Add missing Fortran documentation (1/2) #41

Merged
merged 27 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
62eed6a
PWR004: Add Fortran code to `README.md`
alvrogd Oct 14, 2024
5adeca7
PWR005: Add Fortran code to `README.md`
alvrogd Oct 14, 2024
80fceaf
PWR006: Add Fortran code to `README.md`
alvrogd Oct 14, 2024
76f6ba3
PWR009: Add Fortran code to `README.md`
alvrogd Oct 14, 2024
0a2833c
PWR012: Add Fortran code to `README.md`
alvrogd Oct 14, 2024
b63d787
PWR013: Add Fortran code to `README.md`
alvrogd Oct 14, 2024
639fa8a
PWR015: Add Fortran code to `README.md`
alvrogd Oct 14, 2024
365879a
PWR016: Add Fortran code to `README.md`
alvrogd Oct 14, 2024
20c0cbc
PWR018: Add Fortran code to `README.md`
alvrogd Oct 14, 2024
9d99717
PWR019: Add Fortran code to `README.md`
alvrogd Oct 14, 2024
865c5f2
PWR022: Add Fortran code to `README.md`
alvrogd Oct 14, 2024
88d6efe
PWR029: Add Fortran code to `README.md`
alvrogd Oct 14, 2024
a290f0a
PWR034: Add Fortran code to `README.md`
alvrogd Oct 14, 2024
6b17a3d
PWR035: Add Fortran code to `README.md`
alvrogd Oct 14, 2024
8ec8e7a
PWR036: Add Fortran code to `README.md`
alvrogd Oct 14, 2024
87951b9
PWR039: Add Fortran code to `README.md`
alvrogd Oct 14, 2024
6fdc3b2
PWR040: Add Fortran code to `README.md`
alvrogd Oct 15, 2024
91d9259
PWR043: Add Fortran code to `README.md`
alvrogd Oct 15, 2024
bce9f9f
PWR049: Add Fortran code to `README.md`
alvrogd Oct 15, 2024
3b5215d
PWR050: Add Fortran code to `README.md`
alvrogd Oct 15, 2024
80bfe5e
PWR051: Add Fortran code to `README.md`
alvrogd Oct 15, 2024
ae104ee
PWR052: Add Fortran code to `README.md`
alvrogd Oct 15, 2024
2ba68f2
PWR053: Add Fortran code to `README.md`
alvrogd Oct 15, 2024
71e1e5d
PWR054: Add Fortran code to `README.md`
alvrogd Oct 15, 2024
40d6f1c
PWR055: Add Fortran code to `README.md`
alvrogd Oct 15, 2024
71a59bb
PWR056: Add Fortran code to `README.md`
alvrogd Oct 15, 2024
1ad58ea
PWR057: Add Fortran code to `README.md`
alvrogd Oct 15, 2024
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
45 changes: 40 additions & 5 deletions Checks/PWR004/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ improves code readability.

### Code example

In the following code, a variable `factor` is used in each iteration of the loop
to initialize the array `result`.
#### C

In the following code, a variable `factor` is used in each iteration of the
loop to initialize the array `result`:

```c
void example() {
Expand All @@ -36,22 +38,55 @@ void example() {
}
```

Having the scope declared explicitly for each variable improves readability
Having the scope declared explicitly for each variable improves readability,
since it makes explicit the scope of all the variables within the parallel
region.
region:

```c
void example() {
int factor = 42;
int result[10];

#pragma omp parallel for shared(result, factor)
#pragma omp parallel for default(none) shared(result, factor) private(i)
for (int i = 0; i < 10; i++) {
result[i] = factor * i;
}
}
```

#### Fortran

In the following code, a variable `factor` is used in each iteration of the
loop to initialize the array `result`:

```f90
subroutine example()
integer :: factor = 42
integer :: result(10)

!$omp parallel do
do i = 1, 10
result(i) = factor * i
end do
end subroutine example
```

Having the scope declared explicitly for each variable improves readability,
since it makes explicit the scope of all the variables within the parallel
region:

```f90
subroutine example()
integer :: factor = 42
integer :: result(10)

!$omp parallel do default(none) shared(factor, result) private(i)
do i = 1, 10
result(i) = factor * i
end do
end subroutine example
```

### Related resources

* [PWR004 examples](../PWR004)
Expand Down
68 changes: 61 additions & 7 deletions Checks/PWR005/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ variable.

### Code example

#### C

In the following code, a variable `t` is used in each iteration of the loop to
hold a value that is then assigned to the array `result`. Since no data scoping
is declared for those variables the default will be used. This makes the
variable `t` shared which is incorrect since it introduces a race condition.
is declared for those variables, the default will be used. This makes the
variable `t` shared, which is incorrect since it introduces a race condition:

```c
void example() {
Expand All @@ -39,8 +41,8 @@ void example() {
}
```

The following code disables the default scoping which will make the compiler
raise an error due to unspecified scopes.
The following code disables the default scoping, which will make the compiler
raise an error due to unspecified scopes:

```c
void example() {
Expand All @@ -55,22 +57,74 @@ void example() {
}
```

To fix the code the scope of each variable must be specified. The variable `t`
must be made private to prevent the race condition.
To fix the code, the scope of each variable must be specified. The variable `t`
must be made private to prevent the race condition:

```c
void example() {
int t;
int result[10];

#pragma omp parallel for default(none) shared(result) private(t)
#pragma omp parallel for default(none) shared(result) private(i, t)
for (int i = 0; i < 10; i++) {
t = i + 1;
result[i] = t;
}
}
```

#### Fortran

In the following code, a variable `t` is used in each iteration of the loop to
hold a value that is then assigned to the array `result`. Since no data scoping
is declared for those variables, the default will be used. This makes the
variable `t` shared, which is incorrect since it introduces a race condition:

```f90
subroutine example()
integer :: i, t
integer :: result(10)

!$omp parallel do
do i = 1, 10
t = i + 1;
result(i) = t;
end do
end subroutine example
```

The following code disables the default scoping, which will make the compiler
raise an error due to unspecified scopes:

```f90
subroutine example()
integer :: i, t
integer :: result(10)

!$omp parallel do default(none)
do i = 1, 10
t = i + 1;
result(i) = t;
end do
end subroutine example
```

To fix the code, the scope of each variable must be specified. The variable `t`
must be made private to prevent the race condition:

```f90
subroutine example()
integer :: i, t
integer :: result(10)

!$omp parallel do default(none) shared(result) private(i, t)
do i = 1, 10
t = i + 1;
result(i) = t;
end do
end subroutine example
```

### Related resources

* [PWR005 examples](../PWR005)
Expand Down
52 changes: 48 additions & 4 deletions Checks/PWR006/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ possible.

### Code example

#### C

In the following code, arrays `A` and `B` are never written to. However, they
are privatized and thus each thread will hold a copy of each array, effectively
using more memory and taking more time to create private copies.
using more memory and taking more time to create private copies:

```c
#define SIZE 5
Expand All @@ -30,7 +32,7 @@ void example() {
int B[SIZE] = {5, 4, 3, 2, 1};
int sum[SIZE];

#pragma omp parallel for shared(sum) firstprivate(A, B)
#pragma omp parallel for shared(sum) firstprivate(A, B) private(i)
for (int i = 0; i < SIZE; i++) {
sum[i] = A[i] + B[i];
}
Expand All @@ -39,7 +41,7 @@ void example() {

To save memory, change their scope to shared. This may also prevent memory
issues when using arrays, as codes may easily run out of memory for a high
number of threads.
number of threads:

```c
#define SIZE 5
Expand All @@ -49,13 +51,55 @@ void example() {
int B[SIZE] = {5, 4, 3, 2, 1};
int sum[SIZE];

#pragma omp parallel for shared(sum, A, B)
#pragma omp parallel for shared(sum, A, B) private(i)
for (int i = 0; i < SIZE; i++) {
sum[i] = A[i] + B[i];
}
}
```

#### Fortran

In the following code, arrays `A` and `B` are never written to. However, they
are privatized and thus each thread will hold a copy of each array, effectively
using more memory and taking more time to create private copies:

```f90
subroutine example()
implicit none
integer :: i
integer :: a(5) = [1, 2, 3, 4, 5]
integer :: b(5) = [6, 7, 8, 9, 10]
integer :: sum(5)

!$omp parallel do default(none) firstprivate(a, b) shared(sum) private(i)
do i = 1, 5
sum(i) = a(i) + b(i)
end do
!$omp end parallel do
end subroutine example
```

To save memory, change their scope to shared. This may also prevent memory
issues when using arrays, as codes may easily run out of memory for a high
number of threads:

```f90
subroutine example()
implicit none
integer :: i
integer :: a(5) = [1, 2, 3, 4, 5]
integer :: b(5) = [6, 7, 8, 9, 10]
integer :: sum(5)

!$omp parallel do default(none) shared(a, b, sum) private(i)
do i = 1, 5
sum(i) = a(i) + b(i)
end do
!$omp end parallel do
end subroutine example
```

### Related resources

* [PWR006 examples](../PWR006)
Expand Down
2 changes: 1 addition & 1 deletion Checks/PWR006/example.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ void example() {
int B[SIZE] = {5, 4, 3, 2, 1};
int sum[SIZE];

#pragma omp parallel for shared(sum) firstprivate(A, B)
#pragma omp parallel for shared(sum) firstprivate(A, B) private(i)
for (int i = 0; i < SIZE; i++) {
sum[i] = A[i] + B[i];
}
Expand Down
8 changes: 4 additions & 4 deletions Checks/PWR006/example.f90
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
! PWR006: Avoid privatization of read-only variables

program example
subroutine example()
implicit none
integer :: i
integer :: a(5) = [1, 2, 3, 4, 5]
integer :: b(5) = [6, 7, 8, 9, 10]
integer :: sum(5)

!$omp parallel do default(none) firstprivate(a, b) shared(sum)
!$omp parallel do default(none) firstprivate(a, b) shared(sum) private(i)
do i = 1, 5
sum(i) = a(i) + b(i);
sum(i) = a(i) + b(i)
end do
!$omp end parallel do
end program example
end subroutine example
48 changes: 46 additions & 2 deletions Checks/PWR009/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ is used.

### Code example

#### C

The following code offloads a matrix multiplication computation through the
`target` construct and then creates a parallel region and distributes the work
through `for` construct (note that the matrices are statically sized arrays):
Expand All @@ -60,9 +62,9 @@ through `for` construct (note that the matrices are statically sized arrays):
} // end target
```

When offloading to the GPU it is recommended to use an additional level of
When offloading to the GPU, it is recommended to use an additional level of
parallelism. This can be achieved by using the `teams` and `distribute`
constructs, in this case in combination with `parallel for`:
constructs; in this case, in combination with `parallel for`:

```c
#pragma omp target teams distribute parallel for \
Expand All @@ -77,6 +79,48 @@ for (size_t i = 0; i < m; i++) {
}
```

#### Fortran

The following code offloads a matrix multiplication computation through the
`target` construct and then creates a parallel region and distributes the work
through the `do` construct:

```f90
!$omp target map(to: A, B) map(tofrom: C)
!$omp parallel default(none) private(i, j, k) shared(A, B, C)
!$omp do
do j = 1, size(C, 2)
do k = 1, size(C, 2)
do i = 1, size(C, 1)
C(i, j) = C(i, j) + A(i, k) * B(k, j)
end do
end do
end do
!$omp end do
!$omp end parallel
!$omp end target
```

When offloading to the GPU, it is recommended to use an additional level of
parallelism. This can be achieved by using the `teams` and `distribute`
constructs; in this case, in combination with `parallel do`:

```f90
!$omp target teams distribute map(to: A, B) map(tofrom: C)
!$omp parallel default(none) private(i, j, k) shared(A, B, C)
!$omp do
do j = 1, size(C, 2)
do k = 1, size(C, 2)
do i = 1, size(C, 1)
C(i, j) = C(i, j) + A(i, k) * B(k, j)
end do
end do
end do
!$omp end do
!$omp end parallel
!$omp end target
```

### Related resources

* [PWR009 examples](../PWR009)
Expand Down
Loading