Reading — step 1 of 5
Learn
Fortran is the most parallelism-friendly mainstream language. Compilers can auto-vectorize loops; explicit constructs make parallel code clean.
do concurrent (Fortran 2008)
real :: a(1000), b(1000), c(1000)
do concurrent (i = 1:1000)
c(i) = a(i) + b(i)
end do
Semantics: "these iterations have no dependencies; do them in any order, possibly in parallel."
The compiler can:
- Vectorize (SIMD)
- Run on multiple cores (with
-fopenmpin gfortran or-stdparin nvfortran) - Run on GPU (with nvfortran's
-stdpar=gpu)
With nvfortran's -stdpar=gpu, this exact code RUNS ON THE GPU. No CUDA. Just do concurrent.
OpenMP directives
!$omp parallel do
do i = 1, 1000
c(i) = a(i) * b(i)
end do
!$omp end parallel do
Compile with -fopenmp (gfortran) or -qopenmp (ifort). Each thread takes a chunk of iterations.
Reduction:
real :: total = 0.0
!$omp parallel do reduction(+: total)
do i = 1, 1000
total = total + a(i)
end do
!$omp end parallel do
Threads compute partial sums, OpenMP combines at end.
Critical sections:
!$omp critical
write(*, *) "only one thread at a time here"
!$omp end critical
Coarrays (Fortran 2008+)
Fortran's PGAS (Partitioned Global Address Space) parallelism:
real :: a(100)[*] ! a is shared across all images
a(:) = a(:)[1] ! copy from image 1 to all
sync all ! barrier
Used in HPC, replaces older MPI-only approaches.
SIMD intrinsics
Modern Fortran compilers vectorize array ops automatically. To check, compile with -O3 -fopt-info-vec:
real :: a(8), b(8), c(8)
c = a + b ! one SSE/AVX instruction
Why Fortran for parallel work
- Array semantics make data dependencies explicit
- No aliasing problems (with intent declarations)
- Compilers have decades of optimization
do concurrentis portable across CPU/GPU- Coarrays are simpler than MPI for many use cases
Caveats
- OpenMP / GPU semantics need understanding to avoid race conditions
do concurrentcorrectness is YOUR responsibility — write loops that REALLY have no deps- Coarrays adoption is uneven (Cray, Intel, gfortran since 5.0)
For scientific computing on multi-core CPUs and GPUs, modern Fortran is shockingly competitive — and sometimes wins against hand-tuned C++/CUDA.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…