Introduc)ontoJulia:Whyarewedoingthistoyou?
(Spring2017)
StevenG.Johnson,MITAppliedMath
MITclasses18.06,18.303,18.330,18.08[56],18.335,18.337,…
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…
Lotsofchoicesforinterac)vemath…
[image:ViralShah]
Justpickthemostpopular?MatlaborPythonorR?
Wefeelguiltypushingalanguageonyouthatwe
arestar;ngtoabandonourselves.
Tradi)onalHLcompu)nglanguageshitaperformancewallin“real”work…eventuallyforceyoutoC,Cython,…
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.
Performanceonsynthe)cbenchmarks[loops,recursion,etc.,implementedinmoststraighjorwardstyle]
(normalizedsothatCspeed=1)
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.
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
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.
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):
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
ButIdon’t“need”performance!
Forlotsofproblems,especially“toy”problemsincourses,Matlab/Pythonperformanceisgoodenough.Butifusethoselanguagesforallofyour“easy”problems,thenyouwon’tbepreparedtoswitchwhenyouhitahardproblem.Whenyouneedperformance,itistoolate.Youdon’twanttolearnanewlanguageatthesame)methatyouaresolvingyourfirsttrulydifficultcomputa)onalproblem.
Justvectorizeyourcode?=relyonmatureexternallibraries,opera)ngonlargeblocksofdata,forperformance-cri)calcode
Goodadvice!But…
•Someonehastowritethoselibraries.
•Eventuallythatpersonwillbeyou. —someproblemsareimpossibleor justveryawkwardtovectorize.
ButeveryoneelseisusingMatlab/Python/R/…
Juliaiss)llayoung,nichelanguage.Thatimposesrealcosts—lackoffamiliarity,roughedges,con)nuallanguagechanges.Thesearerealobstacles.ButitalsogivesyouadvantagesthatMatlab/Pythonusersdon’thave.
ButIloseaccesstoallthelibrariesavailableforotherlanguages?
VeryeasytocallC/FortranlibrariesfromJulia,andalsotocallPython…
[ipython.org]
viaIPython/Jupyter:Modernmul)mediainterac)venotebooksmixingcode,results,graphics,richtext,equa)ons,interac)on
“IJulia”
JulialeveragesPython…DirectlycallPythonlibraries(PyCallpackage),
e.g.toplotwithMatplotlib(PyPlotpackage)
gotoliveIJulianotebookdemo…
Gotojuliabox.orgforinstall-freeIJuliaontheAmazoncloud
Seealsojulialang.orgformoretutorialmaterials…