Unlocking the Future of AI Agents with Large Language Models
Seminar Fortran and Julia
1. Modern Fortran & Julia
Arquiteturas Avancadas de Computadores
Krissia de Zawadzki
Instituto de F´ısica de S˜ao Carlos - Universidade de S˜ao Paulo
10 de junho 2014
Krissia de Zawadzki Modern Fortran Julia 1 / 38
2. Motivation Modern Fortran Julia Programming with Julia Example of Julia code Summary
Outline
1 Motivation
2 Modern Fortran
3 Julia
4 Programming with Julia
5 Example of Julia code
6 Summary
Krissia de Zawadzki Modern Fortran Julia 2 / 38
3. Motivation Modern Fortran Julia Programming with Julia Example of Julia code Summary
Motivation
Krissia de Zawadzki Modern Fortran Julia 3 / 38
4. Motivation Modern Fortran Julia Programming with Julia Example of Julia code Summary
Motivation
MODERN FORTRAN
3 multi-paradigm:
structured imperative, generic
3 typing discipline: strong, static,
manifest
3 modern: 90, 95, 2003, 2008
3 John Backus, 1957
JULIA
3 multi-paradigm:
multiple-dispatch, procedural
functional, meta
3 typing discipline: dynamic with
optional type annotations, and
type inference
3 J. Bezanson, S. Karpinski,
V.B.Shah, A. Edelman, 2012
Krissia de Zawadzki Modern Fortran Julia 4 / 38
5. Motivation Modern Fortran Julia Programming with Julia Example of Julia code Summary
Motivation
Krissia de Zawadzki Modern Fortran Julia 5 / 38
6. Motivation Modern Fortran Julia Programming with Julia Example of Julia code Summary
Fortran - a historical review
FORTRAN
FORmula TRANslator
1953 - John Backus: alternative to assembly
language on IBM-704 mainframe
1954 - IBM Mathematical Formula
Translating System
1956 - 1st FORTRAN manual
1957 - 1st FORTRAN compiler
”Much of my work has come from
being lazy. I didn’t like writing
programs, and so, when I was working
on the IBM 701, writing programs for
computing missile trajectories, I
started work on a programming system
to make it easier to write programs.”
John Backus
Krissia de Zawadzki Modern Fortran Julia 6 / 38
7. Motivation Modern Fortran Julia Programming with Julia Example of Julia code Summary
Fortran - a historical review
3 scienti
8. c computing
3 high-level programming language
3 reduced number of statements
3 compilers able to generate faster and
ecient codes
Krissia de Zawadzki Modern Fortran Julia 6 / 38
9. Motivation Modern Fortran Julia Programming with Julia Example of Julia code Summary
Fortran - a historical review
FORTRAN versions
3 FORTRAN, I, II, IBM1401, IV
3 66 → ANSI industry-standard
3 77 → structured programming,
character-based data
3 90 → array, modular and generic
programming
3 95 → high performance Fortran
3 2003 → object-oriented programming
3 2008 → concurrent programming
Krissia de Zawadzki Modern Fortran Julia 6 / 38
10. Motivation Modern Fortran Julia Programming with Julia Example of Julia code Summary
Fortran features
Fortran features
Character set and layout
Letters (A to Z and a to z) and digits (0 to 9)
names : started with letters, up to 31 letters, digits and underscores
case-sensitive
special characters = + − */(), .′ :!”%$;?, space
any character allowed in comments
tab and form-feed not allowed, no other positioning options
lines - sequences of 132 characters
comments with !
Krissia de Zawadzki Modern Fortran Julia 7 / 38
11. Motivation Modern Fortran Julia Programming with Julia Example of Julia code Summary
Fortran features
Fortran features
fortran code
START
SPECIFICATIONS
EXECUTION
END
machine
instructions
executable
program
run!
compiler
linker OS
Krissia de Zawadzki Modern Fortran Julia 7 / 38
12. Motivation Modern Fortran Julia Programming with Julia Example of Julia code Summary
Early Fortran versions
Early Fortran versions
Statements:
3 DIMENSION and EQUIVALENCE statements
3 assignment statements
3 arithmetic IF
3 IF for exceptions
3 GOTO, ASSIGN
3 DO loops
3 FORMAT, READ, READ INPUT TAPE, WRITE, WRITE OUTPUT
TAPE, PRINT, and PUNCH
3 READ TAPE, READ DRUM, WRITE TAPE, and WRITE DRUM
3 END FILE, REWIND, and BACKSPACE
3 PAUSE, STOP, and CONTINUE
3 FREQUENCY statement
Krissia de Zawadzki Modern Fortran Julia 8 / 38
13. Motivation Modern Fortran Julia Programming with Julia Example of Julia code Summary
Early Fortran versions
Early Fortran versions
FORTRAN II - 1958
3 SUBROUTINE, FUNCTION and END
3 CALL and RETURN
3 COMMON
7 recursion not allowed in subroutines (arch without support for
stacks)
FORTRAN III - 1958
3 inline assembly code
7 codes 704 FORTRAN and FORTRAN II, FORTRAN III unportable
from machine
7 never released
Krissia de Zawadzki Modern Fortran Julia 8 / 38
14. Motivation Modern Fortran Julia Programming with Julia Example of Julia code Summary
Early Fortran versions
Early Fortran versions
IBM 1401 FORTRAN
3 program in memory
3 executable form not machine language - early Pascal code
FORTRAN IV - 1961
3 no machine-dependent features
3 + LOGICAL data type and boolean expressions
3 important educational tool
3 WATFIV simplifying compile and link
Krissia de Zawadzki Modern Fortran Julia 8 / 38
15. Motivation Modern Fortran Julia Programming with Julia Example of Julia code Summary
FORTRAN 66
FORTRAN 66
3 American Standard Fortran
3 Main, SUBROUTINE, FUNCTION and BLOCK DATA prog.
units
3 INTEGER, REAL, DOUBLE PRECISION, COMPLEX and
LOGICAL
3 COMMON, DIMENSION, and EQUIVALENCE statements
3 GOTO, assigned GOTO, and computed GOTO statements
3 Logical IF and arithmetic (three-way) IF statements
3 READ, WRITE, BACKSPACE, REWIND, and ENDFILE
statements for sequential I/O
3 FORMAT statement
3 CALL, RETURN, PAUSE, and STOP statements
3 Identi
16. ers of up to six characters in length
3 Comment lines
Krissia de Zawadzki Modern Fortran Julia 9 / 38
17. Motivation Modern Fortran Julia Programming with Julia Example of Julia code Summary
FORTRAN 77
FORTRAN 77
3 Block IF- END IF - ELSE - ELSE IF
3 DO loop: negative increments, zero trip
counts
3 OPEN, CLOSE, and INQUIRE statements-
I/O capability and direct access
18. le
3 IMPLICIT statement
3 CHARACTER data type
3 PARAMETER - constants
3 SAVE - persistent local vars
3 LGE, LGT, LLE, LLT - lexical comparision of
strings
3 Holletith constants and date
3 Overindexing of array
3 extended range
Krissia de Zawadzki Modern Fortran Julia 10 / 38
19. Motivation Modern Fortran Julia Programming with Julia Example of Julia code Summary
Fortran 90
Fortran 90
3 free form source code - columns
1 to 6 no longer special
3 implicit none - check all variables
declared with explicit type
3 modules - partioning the code in
small units
3 internal routines - compiles
check the correctness of args
3 no need for GOTO - replaced by exit
3 recursive routines
3 allocate and deallocate - changing size of arrays
3 array operations supported in arithmetic expressions
and can be automatic
3 derived types - used the same way of basic tpes
3 overboarding - extend the language operators to
derived types
3 optional args in funcs and subroutines - args in
arbitrary order
3 kinds -determining the characteristics of basic types
3 select/case - you can use a name for all control
structures
3 enhancements of I/O system - program can read or
write a part of a record in one statement and the rest
in another one
Krissia de Zawadzki Modern Fortran Julia 11 / 38
20. Motivation Modern Fortran Julia Programming with Julia Example of Julia code Summary
Fortran 95
Fortran 95
3 High Performance Fortran
3 null() to initialize pointers explicity
3 allocatable variables without save attribute -
automatically deallocated
3 technical reports
3 pure - more opportunities of optimization - or
elemental - work on individual elements of
arrays - routines
3 forall statement - enhance capabilities of array
operations
Krissia de Zawadzki Modern Fortran Julia 12 / 38
21. Motivation Modern Fortran Julia Programming with Julia Example of Julia code Summary
Fortran 2003
Fortran 2003
3 Object-oriented programming
3 procedures( funcs or
subroutines) - get passed to the
variables of a speci
22. c types
3 derived types can be extended
into new types
3 select statement in polymorphic variables
(classes)
3 abstract interfaces
3 procedure pointers as ordinary variavles
3 intrinsic modules - rounding mode, fp
exceptions, interfacing to C, dierent calling
and naming conventions
3 Memory management enhanced: lenght of
character strings by allocate; arrays can be
reallocated to correct size; allocations of
memory from one variable to another
movealloc
3 stream access to
24. les
3 standard access to system environment
variables
Krissia de Zawadzki Modern Fortran Julia 13 / 38
25. Motivation Modern Fortran Julia Programming with Julia Example of Julia code Summary
Fortran 2008
Fortran 2008
3 coarrays - data avaliable on various copies
3 contiguous arrays
3 blocs - end blocks : local scope
3 submodules and import - very large source
26. les
3 exit statement can be used to jump to the end
of if or select blocks
3 do concurrent statement - the code can be run
in parallel (thread concept)
3 internal procedures now passed as actual args
- access to varables in the routine that contain
them
3 new standard functions - Bessel functions and
bit inquiry functions
Krissia de Zawadzki Modern Fortran Julia 14 / 38
27. Motivation Modern Fortran Julia Programming with Julia Example of Julia code Summary
Modern Fortran features
Modern Fortran features
Modern style
Free format, attributes
implicit none
do, exit, cycle, case
single and double precision
Dynamic data types
allocatable arrays
structures, derived types
Module-oriented programming
internal subprograms
private, public, protected
contains
use
Optional arguments intent
FORmula TRANSlation
array syntax, where and forall
statements
extended user-de
28. ned
operators
Functions: elemental, inquiry,
mathematical
Improvements
Fortran pointers
command line arguments
Environment variables
Interoperability with C
3 performance considerations
3 objected-oriented programming
Krissia de Zawadzki Modern Fortran Julia 15 / 38
29. Motivation Modern Fortran Julia Programming with Julia Example of Julia code Summary
Performance
Comparison of Data Structures
Implementations
3 what is the performance
penalty of using Fortran 90
data abstraction features?
3 test problem solves Maxwell's
Equations using the Yee-scheme
3 style loops and data structures:
dierent in f77 and f90
Krissia de Zawadzki Modern Fortran Julia 16 / 38
30. Motivation Modern Fortran Julia Programming with Julia Example of Julia code Summary
Introduction
Introduction to Julia
3 2012, in
uenced by MATLAB, Scheme,
Lisp, C, Fortran, Wolfram, Python,
Perl, R, Ruby
3 high-level dynamic programming
language
3 parametric types
3 parallel and distributed computing
3 direct calling of C,
Fortran and Python
libraries
3 libraries for
oating-point, linear
algebra, random number
generation, FFT and
regular expression
matching
Krissia de Zawadzki Modern Fortran Julia 17 / 38
31. Motivation Modern Fortran Julia Programming with Julia Example of Julia code Summary
Introduction
Introduction to Julia
Krissia de Zawadzki Modern Fortran Julia 17 / 38
32. Motivation Modern Fortran Julia Programming with Julia Example of Julia code Summary
Julia features
Julia Features
3 Multiple dispatch
3 Dynamic type system
3 Good performance
3 Built-in package manager
3 Lisp-like macros and metaprogramming facilities
3 PyCall package - call python function
3 Call C functions directly
3 shell-like capabilities for managing other process
3 paralellism and distributed computation
3 Coroutines: lightweight greenthreading
3 user types as fast as built-ins
3 other types - generation, extensible conversions, etc are ecient
3 Unicode support not limited to UTF-8
3 MIT licensed : free and open source
Krissia de Zawadzki Modern Fortran Julia 18 / 38
33. Motivation Modern Fortran Julia Programming with Julia Example of Julia code Summary
High Performance JIT Compiler
High Performance JIT Compiler
LLVM-based just-in-time
3 compilation during the execution
3 ahead of time compilation (AOT) and
interpretation
3 optimized to the CPU and OS
3 statistics about the program - rearrange
and recompile for optimal performance
3 global code and cache use optimizations
3 performance often matches C!
Krissia de Zawadzki Modern Fortran Julia 19 / 38
34. Motivation Modern Fortran Julia Programming with Julia Example of Julia code Summary
Julia packages
Julia packages
3 219 avaliable packages!
3 mature package ecosystem
3 easy install -
Pkg.add(mypackage)
PyCall, PyPlot, PySide, SymPy
Calculus, Cartesian, Elliptic,
Polynomial, TimeSeries
RdRand, RandomMatrices
BioSeq, ChemicalKinetics,
GeneticAlgorithms, Phylogenetics
MarketTechnicals
Clustering, DecisionTree,
DimensionalityReduction,
SortingAlgorithms
TextAnalysis
Krissia de Zawadzki Modern Fortran Julia 20 / 38
35. Motivation Modern Fortran Julia Programming with Julia Example of Julia code Summary
Parallel and cloud computing with Julia
Parallel and cloud computing with Julia
3 worker processes - key building blocks for
distributed computation
3 message-passing ”one-side”!= from MPI
3 remote references and remote calls
3 data management
3 code editing and sharing, data exploration
and visualization
3 networks and streams
Krissia de Zawadzki Modern Fortran Julia 21 / 38
36. Motivation Modern Fortran Julia Programming with Julia Example of Julia code Summary
Getting started
Getting started
Krissia de Zawadzki Modern Fortran Julia 22 / 38
37. Motivation Modern Fortran Julia Programming with Julia Example of Julia code Summary
Integer types
Integer types
Krissia de Zawadzki Modern Fortran Julia 23 / 38
38. Motivation Modern Fortran Julia Programming with Julia Example of Julia code Summary
Float types
Float types
Krissia de Zawadzki Modern Fortran Julia 24 / 38
39. Motivation Modern Fortran Julia Programming with Julia Example of Julia code Summary
Math operations
Math operations
Krissia de Zawadzki Modern Fortran Julia 25 / 38
40. Motivation Modern Fortran Julia Programming with Julia Example of Julia code Summary
Strings
Strings
Krissia de Zawadzki Modern Fortran Julia 26 / 38
41. Motivation Modern Fortran Julia Programming with Julia Example of Julia code Summary
Functions
Functions
Krissia de Zawadzki Modern Fortran Julia 27 / 38
42. Motivation Modern Fortran Julia Programming with Julia Example of Julia code Summary
Control Flow
Control Flow
Krissia de Zawadzki Modern Fortran Julia 28 / 38
43. Motivation Modern Fortran Julia Programming with Julia Example of Julia code Summary
Parallel Julia - beginners
Parallel Julia - beginners
Krissia de Zawadzki Modern Fortran Julia 29 / 38
44. Motivation Modern Fortran Julia Programming with Julia Example of Julia code Summary
Parallel Julia - beginners: adding vectors
Parallel Julia - beginners: adding vectors
Krissia de Zawadzki Modern Fortran Julia 30 / 38
45. Motivation Modern Fortran Julia Programming with Julia Example of Julia code Summary
Laplace equation
Example of Julia code - laplace equation
Laplace Equation ∇2휙 = 0 bondary conditions!
휙(푥 − Δ푥, 푦) − 2휙(푥, 푦) + 휙(푥 + Δ푥, 푦)
Δ푥
+
휙(푥, 푦 − Δ푦) − 2휙(푥, 푦) + 휙(푥, 푦 + Δ푦)
Δ푦
= 0
휙푖푗 =
휙푖−1,푗 + 휙푖+1,푗 + 휙푖,푗−1 + 휙푖,푗+1
4
Krissia de Zawadzki Modern Fortran Julia 31 / 38
46. Motivation Modern Fortran Julia Programming with Julia Example of Julia code Summary
Julia code
Example of Julia code
Julia code: solve lap.jl
Krissia de Zawadzki Modern Fortran Julia 32 / 38
47. Motivation Modern Fortran Julia Programming with Julia Example of Julia code Summary
Julia code
Example of Julia code
C code: laplace.c
Krissia de Zawadzki Modern Fortran Julia 33 / 38
48. Motivation Modern Fortran Julia Programming with Julia Example of Julia code Summary
Tests - bondary conditions:1
Example of Julia code
푚[0, 0 : 푁] = 푚[푁, 0 : 푁] = −1 and 푚[0 : 푁, 0] = 푚[0 : 푁,푁] = 1
Krissia de Zawadzki Modern Fortran Julia 34 / 38
49. Motivation Modern Fortran Julia Programming with Julia Example of Julia code Summary
Tests - bondary conditions:2
Example of Julia code
푚[0,푁/3 : 2푁/3] = 푚[푁,푁/3 : 2푁/3] = −1; 푚[푁/3 : 2푁/3, 0] = 푚[푁/3 : 2푁/3,푁] = 1
Krissia de Zawadzki Modern Fortran Julia 35 / 38
50. Motivation Modern Fortran Julia Programming with Julia Example of Julia code Summary
Tests - bondary conditions:3
Example of Julia code
푚[0, 0 : 푁] = −1; 푚[푁, 0 : 푁] = 0; 푚[0 : 푁, 0] = 1 and 푚[0 : 푁,푁] = 2
Krissia de Zawadzki Modern Fortran Julia 36 / 38
51. Motivation Modern Fortran Julia Programming with Julia Example of Julia code Summary
Summary
3 Programming languages develop basically with applications demand
3 Fortran has released the high-level programming paradigm in 60's
and since then has been continuosly updated in order to serve new
scienti
52. c applications
3 Current trends: New languages integrating high performance with
graphics potential and parallelism
3 Julia is the bet of enthusiastic programmers! It combines high
performance with easy syntax and oers a variety of scienti
54. Motivation Modern Fortran Julia Programming with Julia Example of Julia code Summary
References
3 Markus, Arjen; Modern Fortran in Practice; Cambridge University
Press
3 http://people.ds.cam.ac.uk/nmm1/Fortran/index.html
3 /www.tacc.utexas.edu/documents/13601/162125/fortran class.pdf
3 http://julialang.org/
3 https://github.com/JuliaLang/julia/issues/273
3 http://learnxinyminutes.com/docs/julia/
Krissia de Zawadzki Modern Fortran Julia 38 / 38
56. Julia around the world
Julia around the world
Krissia de Zawadzki Modern Fortran Julia 2 / 3
57. Parallel Linear Algebra with Julia
Parallel Linear Algebra with Julia
Two parallel tiled LA algoritms:
Cholesky decomposition
퐴 symmetric positive matrix →
lower-triangular matrix 퐿: 퐿퐿′ = 퐴
QR decomposition
퐴 being 푚 × 푛 matrix, with 푚 ≥ 푛 →
푚 × 푚 unitary matrix 푄 and 푚 × 푛 upper
triangular matrix 푅 : 푄푅 = 퐴
Krissia de Zawadzki Modern Fortran Julia 3 / 3
58. Parallel Linear Algebra with Julia
Parallel Linear Algebra with Julia
2 processors, 16 tiles
Krissia de Zawadzki Modern Fortran Julia 3 / 3