introduction to julia: why are we doing this to...
TRANSCRIPT
IntroductiontoJulia:Whyarewedoingthistoyou?
(Fall2017)
StevenG.Johnson,MITAppliedMath
MITclasses18.06,18.303,18.330,18.08[56],18.335,18.337,…
Whatlanguageforteachingscientificcomputing?
Forthemostpart,thesearenothard-coreprogrammingcourses,andweonlyneed little“throw-away”scriptsandtoynumericalexperiments.
Almostanyhigh-level,interactive(dynamic)languagewitheasyfacilitiesforlinearalgebra(Ax=b,Ax=λx),plotting,mathematical functions,andworkingwithlargearrays ofdatawouldbefine.
Andtherearelotsofchoices…
Lotsofchoicesforinteractivemath…
[image:ViralShah]
Justpickthemostpopular?Matlab orPython orR?
Wefeelguiltypushingalanguageonyouthatwe
arestartingtoabandonourselves.
TraditionalHLcomputinglanguageshitaperformancewallin“real”work…eventuallyforceyoutoC,Cython,…
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.
Performanceonsyntheticbenchmarks[loops,recursion,etc.,implemented inmoststraightforwardstyle]
(normalizedsothatCspeed=1)
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.
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)
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.
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):
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
ButIdon’t“need” performance!
Forlotsofproblems,especially“toy”problemsincourses,Matlab/Pythonperformanceisgoodenough.
Butifusethoselanguagesforallofyour“easy”problems,thenyouwon’tbepreparedtoswitchwhenyouhitahardproblem.Whenyouneed performance,itistoolate.
Youdon’t wanttolearnanewlanguageatthesametimethatyouaresolvingyourfirsttrulydifficultcomputationalproblem.
Justvectorize yourcode?=relyonmatureexternallibraries,operatingonlargeblocksofdata,forperformance-criticalcode
Goodadvice!But…
•Someone hastowritethoselibraries.
•Eventuallythatpersonwillbeyou.— someproblemsareimpossibleor
justveryawkwardtovectorize.
ButeveryoneelseisusingMatlab/Python/R/…
Juliaisstillayoung,nichelanguage.Thatimposesrealcosts— lackoffamiliarity,rough edges,continuallanguagechanges.Thesearerealobstacles.
ButitalsogivesyouadvantagesthatMatlab/Pythonusersdon’thave.
ButIloseaccesstoallthelibrariesavailableforotherlanguages?
VeryeasytocallC/FortranlibrariesfromJulia,andalsotocallPython…
[jupyter.org ]
viaIPython/Jupyter:
Modernmultimediainteractivenotebooksmixingcode,results,graphics,richtext,equations,interaction
“IJulia”
JulialeveragesPython…DirectlycallPythonlibraries(PyCall package),
e.g.toplotwithMatplotlib (PyPlot package),andalso…
goto liveIJulia notebook demo…
Goto juliabox.org forinstall-freeIJulia ontheAmazoncloud
Seealsojulialang.org formoretutorialmaterials…