introduction to julia: why are we doing this to...

17
Introduction to Julia: Why are we doing this to you? (Fall 2017) Steven G. Johnson, MIT Applied Math MIT classes 18.06, 18.303, 18.330, 18.08[56], 18.335, 18.337, …

Upload: others

Post on 21-Jan-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to Julia: Why are we doing this to you?web.mit.edu/18.06/www/Fall17/1806/julia/Julia-intro.pdf · 2017-12-23 · Pure-Julia FFT performance 2 4 8 16 32 64 128 256 512

IntroductiontoJulia:Whyarewedoingthistoyou?

(Fall2017)

StevenG.Johnson,MITAppliedMath

MITclasses18.06,18.303,18.330,18.08[56],18.335,18.337,…

Page 2: Introduction to Julia: Why are we doing this to you?web.mit.edu/18.06/www/Fall17/1806/julia/Julia-intro.pdf · 2017-12-23 · Pure-Julia FFT performance 2 4 8 16 32 64 128 256 512

Whatlanguageforteachingscientificcomputing?

Forthemostpart,thesearenothard-coreprogrammingcourses,andweonlyneed little“throw-away”scriptsandtoynumericalexperiments.

Almostanyhigh-level,interactive(dynamic)languagewitheasyfacilitiesforlinearalgebra(Ax=b,Ax=λx),plotting,mathematical functions,andworkingwithlargearrays ofdatawouldbefine.

Andtherearelotsofchoices…

Page 3: Introduction to Julia: Why are we doing this to you?web.mit.edu/18.06/www/Fall17/1806/julia/Julia-intro.pdf · 2017-12-23 · Pure-Julia FFT performance 2 4 8 16 32 64 128 256 512

Lotsofchoicesforinteractivemath…

[image:ViralShah]

Page 4: Introduction to Julia: Why are we doing this to you?web.mit.edu/18.06/www/Fall17/1806/julia/Julia-intro.pdf · 2017-12-23 · Pure-Julia FFT performance 2 4 8 16 32 64 128 256 512

Justpickthemostpopular?Matlab orPython orR?

Wefeelguiltypushingalanguageonyouthatwe

arestartingtoabandonourselves.

TraditionalHLcomputinglanguageshitaperformancewallin“real”work…eventuallyforceyoutoC,Cython,…

Page 5: Introduction to Julia: Why are we doing this to you?web.mit.edu/18.06/www/Fall17/1806/julia/Julia-intro.pdf · 2017-12-23 · Pure-Julia FFT performance 2 4 8 16 32 64 128 256 512

Anewprogramminglanguage?

julialang.org

[begun2009,“0.1”in2013,~40kcommits,“0.6”release inJune2017]

AlanEdelmanJeffBezanson

ViralShah

StefanKarpinski

[30+developerswith100+commits,1000+externalpackages,4th JuliaCon in2017]

[MIT]

Ashigh-levelandinteractiveasMatlab orPython+IPython,asgeneral-purposeasPython,

asproductivefortechnicalworkasMatlab orPython+SciPy,butasfastasC.

Page 6: Introduction to Julia: Why are we doing this to you?web.mit.edu/18.06/www/Fall17/1806/julia/Julia-intro.pdf · 2017-12-23 · Pure-Julia FFT performance 2 4 8 16 32 64 128 256 512

Performanceonsyntheticbenchmarks[loops,recursion,etc.,implemented inmoststraightforwardstyle]

(normalizedsothatCspeed=1)

Page 7: Introduction to Julia: Why are we doing this to you?web.mit.edu/18.06/www/Fall17/1806/julia/Julia-intro.pdf · 2017-12-23 · Pure-Julia FFT performance 2 4 8 16 32 64 128 256 512

SpecialFunctionsinJuliaSpecial functions s(x):classic casethatcannotbevectorized well

…switchbetween variouspolynomials depending onx

ManyofJulia’sspecialfunctionscomefromtheusualC/Fortranlibraries,butsomearewritteninpureJuliacode.

PureJuliaerfinv(x) [=erf–1(x) ]3–4× fasterthanMatlab’s and2–3× faster thanSciPy’s (FortranCephes).

PureJuliapolygamma(m,z)[=(m+1)th derivative oftheln Γ function]~2× faster thanSciPy’s (C/Fortran) forrealz

…andunlikeSciPy’s,same code supports complexargumentz

Juliacodecanactuallybefaster thantypical“optimized”C/Fortrancode,byusingtechniques

[metaprogramming/codegen generation]thatarehardinalow-levellanguage.

Page 8: Introduction to Julia: Why are we doing this to you?web.mit.edu/18.06/www/Fall17/1806/julia/Julia-intro.pdf · 2017-12-23 · Pure-Julia FFT performance 2 4 8 16 32 64 128 256 512

Pure-JuliaFFTperformance

2 4 8 16 32 64 128

256

512

1024

2048

4096

8192

16384

32768

65536

131072

262144

0

1000

2000

3000

4000

5000

6000

7000

8000

9000

10000

11000

12000

13000

14000

spee

d (m

flops

)

intel-mkl-dfti in-placeintel-mkl-dfti out-of-placefftw3 out-of-placefftw3 in-placefftw3-no-simd out-of-placefftw3-no-simd in-placedfftpackemayerjuliabloodworthcrosscwplibesrfft

double-precision complex, 1d transformspowers of two

alreadycomparabletoFFTPACK

[probablysometweakstoinlining willmake itbetter]

FFTW1.0-likecodegeneration+recursioninJulia

~1/3lines ofcodecomparedtoFFTPACK,morefunctionality

(FFTW,MKL:“unfair”factorof~2frommanualSIMD)

Page 9: Introduction to Julia: Why are we doing this to you?web.mit.edu/18.06/www/Fall17/1806/julia/Julia-intro.pdf · 2017-12-23 · Pure-Julia FFT performance 2 4 8 16 32 64 128 256 512

GeneratingVandermondematricesgivenx=[α1,α2,…],generate: NumPy (numpy.vander): [followlinks]

Pythoncode …wrapsCcode… wrapsgeneratedCcode

type-generic athigh-level,butlowlevel limited tosmall setoftypes.

Writingfastcode“in”PythonorMatlab =miningthestandardlibraryforpre-writtenfunctions(implementedinCorFortran).

Iftheproblemdoesn’t“vectorize”intobuilt-infunctions,ifyouhavetowriteyourowninnerloops…sucks foryou.

Page 10: Introduction to Julia: Why are we doing this to you?web.mit.edu/18.06/www/Fall17/1806/julia/Julia-intro.pdf · 2017-12-23 · Pure-Julia FFT performance 2 4 8 16 32 64 128 256 512

GeneratingVandermondematricesgivenx=[α1,α2,…],generate: NumPy (numpy.vander): [followlinks]

Pythoncode …wrapsCcode… wrapsgeneratedCcode

type-generic athigh-level,butlowlevel limited tosmall setoftypes.

function vander{T}(x::AbstractVector{T}, n=length(x))m = length(x)V = Array(T, m, n)for j = 1:m

V[j,1] = one(x[j])endfor i = 2:n

for j = 1:mV[j,i] = x[j] * V[j,i-1]

endendreturn V

end

Julia(type-generic code):

Page 11: Introduction to Julia: Why are we doing this to you?web.mit.edu/18.06/www/Fall17/1806/julia/Julia-intro.pdf · 2017-12-23 · Pure-Julia FFT performance 2 4 8 16 32 64 128 256 512

GeneratingVandermondematricesfunction vander{T}(x::AbstractVector{T}, n=length(x))

m = length(x)V = Array(T, m, n)for j = 1:m

V[j,1] = one(x[j])endfor i = 2:n

for j = 1:mV[j,i] = x[j] * V[j,i-1]

endendreturn V

end

note:worksforanycontainerofanytypewith“*”operation…performance≠inflexibility

Page 12: Introduction to Julia: Why are we doing this to you?web.mit.edu/18.06/www/Fall17/1806/julia/Julia-intro.pdf · 2017-12-23 · Pure-Julia FFT performance 2 4 8 16 32 64 128 256 512

ButIdon’t“need” performance!

Forlotsofproblems,especially“toy”problemsincourses,Matlab/Pythonperformanceisgoodenough.

Butifusethoselanguagesforallofyour“easy”problems,thenyouwon’tbepreparedtoswitchwhenyouhitahardproblem.Whenyouneed performance,itistoolate.

Youdon’t wanttolearnanewlanguageatthesametimethatyouaresolvingyourfirsttrulydifficultcomputationalproblem.

Page 13: Introduction to Julia: Why are we doing this to you?web.mit.edu/18.06/www/Fall17/1806/julia/Julia-intro.pdf · 2017-12-23 · Pure-Julia FFT performance 2 4 8 16 32 64 128 256 512

Justvectorize yourcode?=relyonmatureexternallibraries,operatingonlargeblocksofdata,forperformance-criticalcode

Goodadvice!But…

•Someone hastowritethoselibraries.

•Eventuallythatpersonwillbeyou.— someproblemsareimpossibleor

justveryawkwardtovectorize.

Page 14: Introduction to Julia: Why are we doing this to you?web.mit.edu/18.06/www/Fall17/1806/julia/Julia-intro.pdf · 2017-12-23 · Pure-Julia FFT performance 2 4 8 16 32 64 128 256 512

ButeveryoneelseisusingMatlab/Python/R/…

Juliaisstillayoung,nichelanguage.Thatimposesrealcosts— lackoffamiliarity,rough edges,continuallanguagechanges.Thesearerealobstacles.

ButitalsogivesyouadvantagesthatMatlab/Pythonusersdon’thave.

Page 15: Introduction to Julia: Why are we doing this to you?web.mit.edu/18.06/www/Fall17/1806/julia/Julia-intro.pdf · 2017-12-23 · Pure-Julia FFT performance 2 4 8 16 32 64 128 256 512

ButIloseaccesstoallthelibrariesavailableforotherlanguages?

VeryeasytocallC/FortranlibrariesfromJulia,andalsotocallPython…

Page 16: Introduction to Julia: Why are we doing this to you?web.mit.edu/18.06/www/Fall17/1806/julia/Julia-intro.pdf · 2017-12-23 · Pure-Julia FFT performance 2 4 8 16 32 64 128 256 512

[jupyter.org ]

viaIPython/Jupyter:

Modernmultimediainteractivenotebooksmixingcode,results,graphics,richtext,equations,interaction

“IJulia”

JulialeveragesPython…DirectlycallPythonlibraries(PyCall package),

e.g.toplotwithMatplotlib (PyPlot package),andalso…

Page 17: Introduction to Julia: Why are we doing this to you?web.mit.edu/18.06/www/Fall17/1806/julia/Julia-intro.pdf · 2017-12-23 · Pure-Julia FFT performance 2 4 8 16 32 64 128 256 512

goto liveIJulia notebook demo…

Goto juliabox.org forinstall-freeIJulia ontheAmazoncloud

Seealsojulialang.org formoretutorialmaterials…