introduc)on to julia: why are we doing this to you? -...
TRANSCRIPT
![Page 1: Introduc)on to Julia: Why are we doing this to you? - MITweb.mit.edu/18.06/www/Spring17/Julia-intro.pdf · 2017-09-07 · Julia code can actually be faster than typical “op)mized”](https://reader033.vdocument.in/reader033/viewer/2022050305/5f6da8593f992041ce663ab7/html5/thumbnails/1.jpg)
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? - MITweb.mit.edu/18.06/www/Spring17/Julia-intro.pdf · 2017-09-07 · Julia code can actually be faster than typical “op)mized”](https://reader033.vdocument.in/reader033/viewer/2022050305/5f6da8593f992041ce663ab7/html5/thumbnails/2.jpg)
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? - MITweb.mit.edu/18.06/www/Spring17/Julia-intro.pdf · 2017-09-07 · Julia code can actually be faster than typical “op)mized”](https://reader033.vdocument.in/reader033/viewer/2022050305/5f6da8593f992041ce663ab7/html5/thumbnails/3.jpg)
Lotsofchoicesforinterac)vemath…
[image:ViralShah]
![Page 4: Introduc)on to Julia: Why are we doing this to you? - MITweb.mit.edu/18.06/www/Spring17/Julia-intro.pdf · 2017-09-07 · Julia code can actually be faster than typical “op)mized”](https://reader033.vdocument.in/reader033/viewer/2022050305/5f6da8593f992041ce663ab7/html5/thumbnails/4.jpg)
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? - MITweb.mit.edu/18.06/www/Spring17/Julia-intro.pdf · 2017-09-07 · Julia code can actually be faster than typical “op)mized”](https://reader033.vdocument.in/reader033/viewer/2022050305/5f6da8593f992041ce663ab7/html5/thumbnails/5.jpg)
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? - MITweb.mit.edu/18.06/www/Spring17/Julia-intro.pdf · 2017-09-07 · Julia code can actually be faster than typical “op)mized”](https://reader033.vdocument.in/reader033/viewer/2022050305/5f6da8593f992041ce663ab7/html5/thumbnails/6.jpg)
Performanceonsynthe)cbenchmarks[loops,recursion,etc.,implementedinmoststraighjorwardstyle]
(normalizedsothatCspeed=1)
![Page 7: Introduc)on to Julia: Why are we doing this to you? - MITweb.mit.edu/18.06/www/Spring17/Julia-intro.pdf · 2017-09-07 · Julia code can actually be faster than typical “op)mized”](https://reader033.vdocument.in/reader033/viewer/2022050305/5f6da8593f992041ce663ab7/html5/thumbnails/7.jpg)
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? - MITweb.mit.edu/18.06/www/Spring17/Julia-intro.pdf · 2017-09-07 · Julia code can actually be faster than typical “op)mized”](https://reader033.vdocument.in/reader033/viewer/2022050305/5f6da8593f992041ce663ab7/html5/thumbnails/8.jpg)
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? - MITweb.mit.edu/18.06/www/Spring17/Julia-intro.pdf · 2017-09-07 · Julia code can actually be faster than typical “op)mized”](https://reader033.vdocument.in/reader033/viewer/2022050305/5f6da8593f992041ce663ab7/html5/thumbnails/9.jpg)
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? - MITweb.mit.edu/18.06/www/Spring17/Julia-intro.pdf · 2017-09-07 · Julia code can actually be faster than typical “op)mized”](https://reader033.vdocument.in/reader033/viewer/2022050305/5f6da8593f992041ce663ab7/html5/thumbnails/10.jpg)
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? - MITweb.mit.edu/18.06/www/Spring17/Julia-intro.pdf · 2017-09-07 · Julia code can actually be faster than typical “op)mized”](https://reader033.vdocument.in/reader033/viewer/2022050305/5f6da8593f992041ce663ab7/html5/thumbnails/11.jpg)
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? - MITweb.mit.edu/18.06/www/Spring17/Julia-intro.pdf · 2017-09-07 · Julia code can actually be faster than typical “op)mized”](https://reader033.vdocument.in/reader033/viewer/2022050305/5f6da8593f992041ce663ab7/html5/thumbnails/12.jpg)
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? - MITweb.mit.edu/18.06/www/Spring17/Julia-intro.pdf · 2017-09-07 · Julia code can actually be faster than typical “op)mized”](https://reader033.vdocument.in/reader033/viewer/2022050305/5f6da8593f992041ce663ab7/html5/thumbnails/13.jpg)
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? - MITweb.mit.edu/18.06/www/Spring17/Julia-intro.pdf · 2017-09-07 · Julia code can actually be faster than typical “op)mized”](https://reader033.vdocument.in/reader033/viewer/2022050305/5f6da8593f992041ce663ab7/html5/thumbnails/14.jpg)
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? - MITweb.mit.edu/18.06/www/Spring17/Julia-intro.pdf · 2017-09-07 · Julia code can actually be faster than typical “op)mized”](https://reader033.vdocument.in/reader033/viewer/2022050305/5f6da8593f992041ce663ab7/html5/thumbnails/15.jpg)
ButIloseaccesstoallthelibrariesavailableforotherlanguages?
VeryeasytocallC/FortranlibrariesfromJulia,andalsotocallPython…
![Page 16: Introduc)on to Julia: Why are we doing this to you? - MITweb.mit.edu/18.06/www/Spring17/Julia-intro.pdf · 2017-09-07 · Julia code can actually be faster than typical “op)mized”](https://reader033.vdocument.in/reader033/viewer/2022050305/5f6da8593f992041ce663ab7/html5/thumbnails/16.jpg)
[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? - MITweb.mit.edu/18.06/www/Spring17/Julia-intro.pdf · 2017-09-07 · Julia code can actually be faster than typical “op)mized”](https://reader033.vdocument.in/reader033/viewer/2022050305/5f6da8593f992041ce663ab7/html5/thumbnails/17.jpg)
gotoliveIJulianotebookdemo…
Gotojuliabox.orgforinstall-freeIJuliaontheAmazoncloud
Seealsojulialang.orgformoretutorialmaterials…