Seminar Fortran and Julia
-
Upload
krissia-zawadzki -
Category
Software
-
view
1.058 -
download
1
description
Transcript of Seminar Fortran and Julia
Modern Fortran & JuliaArquiteturas Avancadas de Computadores
Krissia de Zawadzki
Instituto de Fısica de Sao Carlos - Universidade de Sao Paulo
10 de junho 2014
Krissia de Zawadzki Modern Fortran & Julia 1 / 38
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
Motivation Modern Fortran Julia Programming with Julia Example of Julia code Summary
Motivation
Krissia de Zawadzki Modern Fortran & Julia 3 / 38
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, proceduralfunctional, meta
3 typing discipline: dynamic withoptional type annotations, andtype inference
3 J. Bezanson, S. Karpinski,V.B.Shah, A. Edelman, 2012
Krissia de Zawadzki Modern Fortran & Julia 4 / 38
Motivation Modern Fortran Julia Programming with Julia Example of Julia code Summary
Motivation
Krissia de Zawadzki Modern Fortran & Julia 5 / 38
Motivation Modern Fortran Julia Programming with Julia Example of Julia code Summary
Fortran - a historical review
FORTRAN
FORmula TRANslator
1953 - John Backus: alternative to assemblylanguage on IBM-704 mainframe
1954 - IBM Mathematical FormulaTranslating System
1956 - 1st FORTRAN manual
1957 - 1st FORTRAN compiler
”Much of my work has come frombeing lazy. I didn’t like writing
programs, and so, when I was workingon the IBM 701, writing programs for
computing missile trajectories, Istarted work on a programming systemto make it easier to write programs.”
John Backus
Krissia de Zawadzki Modern Fortran & Julia 6 / 38
Motivation Modern Fortran Julia Programming with Julia Example of Julia code Summary
Fortran - a historical review
3 scientific computing
3 high-level programming language
3 reduced number of statements
3 compilers able to generate faster andefficient codes
Krissia de Zawadzki Modern Fortran & Julia 6 / 38
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 genericprogramming
3 95 → high performance Fortran
3 2003 → object-oriented programming
3 2008 → concurrent programming
Krissia de Zawadzki Modern Fortran & Julia 6 / 38
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
Motivation Modern Fortran Julia Programming with Julia Example of Julia code Summary
Fortran features
Fortran features
fortran code
START
SPECIFICATIONS
EXECUTION
END
machineinstructions
executableprogram
run!
compiler
linker
OS
Krissia de Zawadzki Modern Fortran & Julia 7 / 38
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 OUTPUTTAPE, 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
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 forstacks)
FORTRAN III - 1958
3 inline assembly code
7 codes 704 FORTRAN and FORTRAN II, FORTRAN III unportablefrom machine
7 never released
Krissia de Zawadzki Modern Fortran & Julia 8 / 38
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
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 andLOGICAL
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 ENDFILEstatements for sequential I/O
3 FORMAT statement
3 CALL, RETURN, PAUSE, and STOP statements
3 Identifiers of up to six characters in length
3 Comment lines
Krissia de Zawadzki Modern Fortran & Julia 9 / 38
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 tripcounts
3 OPEN, CLOSE, and INQUIRE statements-I/O capability and direct access file
3 IMPLICIT statement
3 CHARACTER data type
3 PARAMETER - constants
3 SAVE - persistent local vars
3 LGE, LGT, LLE, LLT - lexical comparision ofstrings
3 Holletith constants and date
3 Overindexing of array
3 extended range
Krissia de Zawadzki Modern Fortran & Julia 10 / 38
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
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 - orelemental - work on individual elements ofarrays - routines
3 forall statement - enhance capabilities of arrayoperations
Krissia de Zawadzki Modern Fortran & Julia 12 / 38
Motivation Modern Fortran Julia Programming with Julia Example of Julia code Summary
Fortran 2003
Fortran 2003
3 Object-oriented programming
3 procedures( funcs orsubroutines) - get passed to thevariables of a specific types
3 derived types can be extendedinto new types
3 select statement in polymorphic variables(classes)
3 abstract interfaces
3 procedure pointers as ordinary variavles
3 intrinsic modules - rounding mode, fpexceptions, interfacing to C, different callingand naming conventions
3 Memory management enhanced: lenght ofcharacter strings by allocate; arrays can bereallocated to correct size; allocations ofmemory from one variable to anothermovealloc
3 stream access to files - read and writeformatted binary files
3 standard access to system environmentvariables
Krissia de Zawadzki Modern Fortran & Julia 13 / 38
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 files
3 exit statement can be used to jump to the endof if or select blocks
3 do concurrent statement - the code can be runin parallel (thread concept)
3 internal procedures now passed as actual args- access to varables in the routine that containthem
3 new standard functions - Bessel functions andbit inquiry functions
Krissia de Zawadzki Modern Fortran & Julia 14 / 38
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 forallstatements
extended & user-definedoperators
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
Motivation Modern Fortran Julia Programming with Julia Example of Julia code Summary
Performance
Comparison of Data StructuresImplementations
3 what is the performancepenalty of using Fortran 90data abstraction features?
3 test problem solves Maxwell’sEquations using the Yee-scheme
3 style loops and data structures:different in f77 and f90
Krissia de Zawadzki Modern Fortran & Julia 16 / 38
Motivation Modern Fortran Julia Programming with Julia Example of Julia code Summary
Introduction
Introduction to Julia
3 2012, influenced by MATLAB, Scheme,Lisp, C, Fortran, Wolfram, Python,Perl, R, Ruby
3 high-level dynamic programminglanguage
3 parametric types
3 parallel and distributed computing
3 direct calling of C,Fortran and Pythonlibraries
3 libraries forfloating-point, linearalgebra, random numbergeneration, FFT andregular expressionmatching
Krissia de Zawadzki Modern Fortran & Julia 17 / 38
Motivation Modern Fortran Julia Programming with Julia Example of Julia code Summary
Introduction
Introduction to Julia
Krissia de Zawadzki Modern Fortran & Julia 17 / 38
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 ”green”threading
3 user types as fast as built-ins
3 other types - generation, extensible conversions, etc are efficient
3 Unicode support not limited to UTF-8
3 MIT licensed : free and open source
Krissia de Zawadzki Modern Fortran & Julia 18 / 38
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
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
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
Motivation Modern Fortran Julia Programming with Julia Example of Julia code Summary
Getting started
Getting started
Krissia de Zawadzki Modern Fortran & Julia 22 / 38
Motivation Modern Fortran Julia Programming with Julia Example of Julia code Summary
Integer types
Integer types
Krissia de Zawadzki Modern Fortran & Julia 23 / 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
Motivation Modern Fortran Julia Programming with Julia Example of Julia code Summary
Math operations
Math operations
Krissia de Zawadzki Modern Fortran & Julia 25 / 38
Motivation Modern Fortran Julia Programming with Julia Example of Julia code Summary
Strings
Strings
Krissia de Zawadzki Modern Fortran & Julia 26 / 38
Motivation Modern Fortran Julia Programming with Julia Example of Julia code Summary
Functions
Functions
Krissia de Zawadzki Modern Fortran & Julia 27 / 38
Motivation Modern Fortran Julia Programming with Julia Example of Julia code Summary
Control Flow
Control Flow
Krissia de Zawadzki Modern Fortran & Julia 28 / 38
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
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
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
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
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
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
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
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
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’sand since then has been continuosly updated in order to serve newscientific applications
3 Current trends: New languages integrating high performance withgraphics potential and parallelism
3 Julia is the bet of enthusiastic programmers! It combines highperformance with easy syntax and offers a variety of scientificpackages!
Krissia de Zawadzki Modern Fortran & Julia 37 / 38
Motivation Modern Fortran Julia Programming with Julia Example of Julia code Summary
References
3 Markus, Arjen; Modern Fortran in Practice; Cambridge UniversityPress
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
Krissia de Zawadzki Modern Fortran & Julia 1 / 3
Julia around the world
Julia around the world
Krissia de Zawadzki Modern Fortran & Julia 2 / 3
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
Parallel Linear Algebra with Julia
Parallel Linear Algebra with Julia
2 processors, 16 tilesKrissia de Zawadzki Modern Fortran & Julia 3 / 3