+ All Categories
Home > Documents > Introduc)on to Julia: Why are we doing this to you? · Julia code can actually be faster than...

Introduc)on to Julia: Why are we doing this to you? · Julia code can actually be faster than...

Date post: 27-Sep-2020
Category:
Upload: others
View: 1 times
Download: 0 times
Share this document with a friend
17
Introduc)on to Julia: Why are we doing this to you? (Spring 2017) Steven G. Johnson, MIT Applied Math MIT classes 18.06, 18.303, 18.330, 18.08[56], 18.335, 18.337, …
Transcript
Page 1: Introduc)on to Julia: Why are we doing this to you? · Julia code can actually be faster than typical “op)mized” C/Fortran code, by using techniques [metaprogramming/ code generaon

Introduc)ontoJulia:Whyarewedoingthistoyou?

(Spring2017)

StevenG.Johnson,MITAppliedMath

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

Page 2: Introduc)on to Julia: Why are we doing this to you? · Julia code can actually be faster than typical “op)mized” C/Fortran code, by using techniques [metaprogramming/ code generaon

Whatlanguageforteachingscien)ficcompu)ng?

Forthemostpart,thesearenothard-coreprogrammingcourses,andweonlyneedliSle“throw-away”scriptsandtoynumericalexperiments.

Almostanyhigh-level,interac)ve(dynamic)languagewitheasyfacili)esforlinearalgebra(Ax=b,Ax=λx),ploZng,mathema)calfunc)ons,andworkingwithlargearraysofdatawouldbefine.

Andtherearelotsofchoices…

Page 3: Introduc)on to Julia: Why are we doing this to you? · Julia code can actually be faster than typical “op)mized” C/Fortran code, by using techniques [metaprogramming/ code generaon

Lotsofchoicesforinterac)vemath…

[image:ViralShah]

Page 4: Introduc)on to Julia: Why are we doing this to you? · Julia code can actually be faster than typical “op)mized” C/Fortran code, by using techniques [metaprogramming/ code generaon

Justpickthemostpopular?MatlaborPythonorR?

Wefeelguiltypushingalanguageonyouthatwe

arestar;ngtoabandonourselves.

Tradi)onalHLcompu)nglanguageshitaperformancewallin“real”work…eventuallyforceyoutoC,Cython,…

Page 5: Introduc)on to Julia: Why are we doing this to you? · Julia code can actually be faster than typical “op)mized” C/Fortran code, by using techniques [metaprogramming/ code generaon

Anewprogramminglanguage?

julialang.org

[begun2009,“0.1”in2013,~35kcommits,“0.5”releaseinFall2016]

AlanEdelmanJeffBezanson

ViralShah

StefanKarpinski

[30+developerswith100+commits,1000+externalpackages,3rdJuliaConin2016]

[MIT]

Ashigh-levelandinterac)veasMatlaborPython+IPython,asgeneral-purposeasPython,

asproduc)vefortechnicalworkasMatlaborPython+SciPy,butasfastasC.

Page 6: Introduc)on to Julia: Why are we doing this to you? · Julia code can actually be faster than typical “op)mized” C/Fortran code, by using techniques [metaprogramming/ code generaon

Performanceonsynthe)cbenchmarks[loops,recursion,etc.,implementedinmoststraighjorwardstyle]

(normalizedsothatCspeed=1)

Page 7: Introduc)on to Julia: Why are we doing this to you? · Julia code can actually be faster than typical “op)mized” C/Fortran code, by using techniques [metaprogramming/ code generaon

SpecialFunc)onsinJuliaSpecialfunc)onss(x):classiccasethatcannotbevectorizedwell

…switchbetweenvariouspolynomialsdependingonx

ManyofJulia’sspecialfunc)onscomefromtheusualC/Fortranlibraries,butsomearewriSeninpureJuliacode.

PureJuliaerfinv(x)[=erf–1(x)] 3–4×fasterthanMatlab’sand2–3×fasterthanSciPy’s(FortranCephes).

PureJuliapolygamma(m,z)[=(m+1)thderiva)veofthelnΓfunc)on]

~2×fasterthanSciPy’s(C/Fortran)forrealz…andunlikeSciPy’s,samecodesupportscomplexargumentz

Juliacodecanactuallybefasterthantypical“op)mized”C/Fortrancode,byusingtechniques[metaprogramming/codegenera)on]thatarehardinalow-levellanguage.

Page 8: Introduc)on to Julia: Why are we doing this to you? · Julia code can actually be faster than typical “op)mized” C/Fortran code, by using techniques [metaprogramming/ code generaon

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[probablysometweakstoinliningwillmakeitbeSer]

FFTW1.0-likecodegenera)on+recursioninJulia~1/3linesofcodecomparedtoFFTPACK,morefunc)onality

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

JuliaFFTWw/oSIMD

Page 9: Introduc)on to Julia: Why are we doing this to you? · Julia code can actually be faster than typical “op)mized” C/Fortran code, by using techniques [metaprogramming/ code generaon

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

Pythoncode…wrapsCcode…wrapsgeneratedCcodetype-genericathigh-level,butlowlevellimitedtosmallsetoftypes.

Wri)ngfastcode“in”PythonorMatlab=miningthestandardlibraryforpre-wriSenfunc)ons(implementedinCorFortran).Iftheproblemdoesn’t“vectorize”intobuilt-infunc)ons,ifyouhavetowriteyourowninnerloops…sucksforyou.

Page 10: Introduc)on to Julia: Why are we doing this to you? · Julia code can actually be faster than typical “op)mized” C/Fortran code, by using techniques [metaprogramming/ code generaon

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

Pythoncode…wrapsCcode…wrapsgeneratedCcodetype-genericathigh-level,butlowlevellimitedtosmallsetoftypes.

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]) end for i = 2:n for j = 1:m V[j,i] = x[j] * V[j,i-1] end end return Vend

Julia(type-genericcode):

Page 11: Introduc)on to Julia: Why are we doing this to you? · Julia code can actually be faster than typical “op)mized” C/Fortran code, by using techniques [metaprogramming/ code generaon

Genera)ngVandermondematricesfunction 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]) end for i = 2:n for j = 1:m V[j,i] = x[j] * V[j,i-1] end end return Vend

note:worksforanycontainerofanytypewith“*”opera)on…performance≠inflexibility

Page 12: Introduc)on to Julia: Why are we doing this to you? · Julia code can actually be faster than typical “op)mized” C/Fortran code, by using techniques [metaprogramming/ code generaon

ButIdon’t“need”performance!

Forlotsofproblems,especially“toy”problemsincourses,Matlab/Pythonperformanceisgoodenough.Butifusethoselanguagesforallofyour“easy”problems,thenyouwon’tbepreparedtoswitchwhenyouhitahardproblem.Whenyouneedperformance,itistoolate.Youdon’twanttolearnanewlanguageatthesame)methatyouaresolvingyourfirsttrulydifficultcomputa)onalproblem.

Page 13: Introduc)on to Julia: Why are we doing this to you? · Julia code can actually be faster than typical “op)mized” C/Fortran code, by using techniques [metaprogramming/ code generaon

Justvectorizeyourcode?=relyonmatureexternallibraries,opera)ngonlargeblocksofdata,forperformance-cri)calcode

Goodadvice!But…

•Someonehastowritethoselibraries.

•Eventuallythatpersonwillbeyou. —someproblemsareimpossibleor justveryawkwardtovectorize.

Page 14: Introduc)on to Julia: Why are we doing this to you? · Julia code can actually be faster than typical “op)mized” C/Fortran code, by using techniques [metaprogramming/ code generaon

ButeveryoneelseisusingMatlab/Python/R/…

Juliaiss)llayoung,nichelanguage.Thatimposesrealcosts—lackoffamiliarity,roughedges,con)nuallanguagechanges.Thesearerealobstacles.ButitalsogivesyouadvantagesthatMatlab/Pythonusersdon’thave.

Page 15: Introduc)on to Julia: Why are we doing this to you? · Julia code can actually be faster than typical “op)mized” C/Fortran code, by using techniques [metaprogramming/ code generaon

ButIloseaccesstoallthelibrariesavailableforotherlanguages?

VeryeasytocallC/FortranlibrariesfromJulia,andalsotocallPython…

Page 16: Introduc)on to Julia: Why are we doing this to you? · Julia code can actually be faster than typical “op)mized” C/Fortran code, by using techniques [metaprogramming/ code generaon

[ipython.org]

viaIPython/Jupyter:Modernmul)mediainterac)venotebooksmixingcode,results,graphics,richtext,equa)ons,interac)on

“IJulia”

JulialeveragesPython…DirectlycallPythonlibraries(PyCallpackage),

e.g.toplotwithMatplotlib(PyPlotpackage)

Page 17: Introduc)on to Julia: Why are we doing this to you? · Julia code can actually be faster than typical “op)mized” C/Fortran code, by using techniques [metaprogramming/ code generaon

gotoliveIJulianotebookdemo…

Gotojuliabox.orgforinstall-freeIJuliaontheAmazoncloud

Seealsojulialang.orgformoretutorialmaterials…


Recommended