Skip to content
Snippets Groups Projects
Commit bba13690 authored by Dominik Zobel's avatar Dominik Zobel
Browse files

Add Fortran loop hands-on

parent e58cffc8
No related branches found
No related tags found
1 merge request!74Memory hierarchies lecture
Pipeline #71944 passed
...@@ -192,6 +192,50 @@ $^*$: Lower than one due to caching effects ...@@ -192,6 +192,50 @@ $^*$: Lower than one due to caching effects
## Memory access patterns
Execution speed depends on data layout in memory
```{.fortranfree}
program loop_exchange
implicit none
integer, parameter :: nel = 20000
! Note: Stay below 46000 to prevent overflows below
integer, dimension(nel, nel) :: mat
integer :: i, j
do i = 1, nel
do j = 1, nel
mat(j, i) = (i-1)*nel + j-1
end do
end do
end program loop_exchange
```
Loop order with optimal access of elements (contiguous memory).
## Hands-On {.handson}
1. Compile the Fortran code from the previous slide (also [here](static/loops.f90))
on Levante or your PC. On Levante load the `gcc` module first (`module load gcc`).
Then measure the time needed to run the program, i.e.
```{.Bash}
gfortran loops.f90 -o loops
time ./loops
```
2. Exchange the loop variables `i` and `j` in line 8 and 9 and compile again.
How does it impact the run time?
3. Try different values for `nel` (for the original loop order and the exchanged one).
How does the matrix size relate to the effect of exchanged loops?
# Memory Models # Memory Models
- Study effect of latencies, cache sizes, block sizes, ... - Study effect of latencies, cache sizes, block sizes, ...
......
program loop_exchange
implicit none
integer, parameter :: nel = 20000
! Note: Stay below 46000 to prevent overflows below
integer, dimension(nel, nel) :: mat
integer :: i, j
do i = 1, nel
do j = 1, nel
mat(j, i) = (i-1)*nel + j-1
end do
end do
end program loop_exchange
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment