!"#$%&'()*(+,-.'%"-% /0-,+".%1,-#2,#'34% 56789%#2'3:%1'.:2('% ;(*)6%<:'='-%>6%?*$-3*-% @AB%C&&1"'/%@,:$'+,:".3D%@AB%;$03".3% 1
Dynamic languages for interactive math… The two-language approach: High-level dynamic language for productivity, + low-level language (C, Fortran, Cython, …) for performance-critical code. = Huge jump in complexity, loss of generality. 2
Just vectorize your code? = rely on mature external libraries, operating on large blocks of data, for performance-critical code Good advice! But… • Someone has to write those libraries. • Eventually that person will be you. — some problems are impossible or just very awkward to vectorize. 3
!"#$%"&'()'*++,#)"-*#).*)$/" !-*#"GH$-+*#" L,'*-"MN*N" 2"TUV"F" C$II"J$K*#A(#" 0.-,*-*#)1(')" M@$I*#"O*'&,#A>," 23$).#"45567"8519:",#"459;7"<=5>"?(++,@A7" 2";5P"H$Q$-(&$'A"%,@N"955P"?(++,@A7" 851B:"'$-$*A$",#"C.#$"459D7" 9555P"$R@$'#*-"&*?>*)$A7"= @N" C.-,*S(#",#"459D"F" 915"'$-$*A$",#"!.).A@"459E"F" !A"N,)NW-$Q$-"*#H",#@$'*?@,Q$"*A"T*@-*3"('"XY@N(#PUXY@N(#7" *A")$#$'*-W&.'&(A$"*A"XY@N(#7" *A"&'(H.?@,Q$"I('"@$?N#,?*-"%('>"*A"T*@-*3"('"XY@N(#PM?,XY7" 3.@"*A" !"#$%"#%& 1" 4
!"#"$%&'#()*%#+"$,-#+"),%&$'."/) ('0"#)1)2)34 5 6)4 7 6)896)("#"$%&":) ;<,=>) ? numpy.vander @:) !"#$$#%&$'()*+& =>&A-#).-+") 8B$%C/)D).-+") 8)B$%C/)("#"$%&"+)D).-+") &>C"E("#"$'.)%&)A'(AEF"0"F6)G<&) F-B)F"0"F)F','&"+)&-)/,%FF)/"&)-H)&>C"/I) J$'&'#()H%/&).-+")K'#L)=>&A-#)-$)M%&F%G)2),'#'#()&A")/&%#+%$+)F'G$%$>) H-$)C$"EB$'&&"#)H<#.&'-#/)?',CF","#&"+)'#)D)-$)N-$&$%#@I) OH)&A")C$-GF",)+-"/#P&)K0".&-$'Q"L)'#&-)G<'F&E'#)H<#.&'-#/6) 'H)>-<)A%0")&-)B$'&")>-<$)-B#)'##"$)F--C/)8)/<.R/)H-$)>-<I) 5
!"#"$%&'#()*%#+"$,-#+"),%&$'."/) ('0"#)1)2)34 5 6)4 7 6)896)("#"$%&":) ;<,=>) ? numpy.vander @:) !"#$$#%&$'()*+& =>&A-#).-+") 8B$%C/)D).-+") 8)B$%C/)("#"$%&"+)D).-+") &>C"E("#"$'.)%&)A'(AEF"0"F6)G<&) F-B)F"0"F)F','&"+)&-)/,%FF)/"&)-H)&>C"/I) J<F'%)?&>C"E("#"$'.).-+"@:) function vander(x, n=length(x)) m = length(x) V = Array(eltype(x), 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 V end 6
!"#"$%&'#()*%#+"$,-#+"),%&$'."/) function vander(x, n=length(x)) m = length(x) V = Array(eltype(x), m, n) #-&"0)1-$2/)3-$)%#4)).-#&%'#"$) for j = 1:m -3)%#4)&45")1'&6)789)-5"$%&'-#) V[j,1] = one(x[j]) :)5"$3-$,%#.");)'#3<"='>'<'&4) end for i = 2:n for j = 1:m V[j,i] = x[j] * V[j,i-1] end end return V end 7
Special Functions in Julia Special functions s(x): classic case that cannot be vectorized well … switch between various polynomials depending on x Many of Julia’s special functions come from the usual C/Fortran libraries, but some are written in pure Julia code. Pure Julia erfinv(x) [ = erf –1 (x) ] 3–4× faster than Matlab’s and 2–3× faster than SciPy’s (Fortran Cephes). Pure Julia polygamma(m, z) [ = (m+1) th derivative of the ln Γ function ] ~ 2× faster than SciPy’s (C/Fortran) for real z … and unlike SciPy’s, same code supports complex argument z Julia code can actually be faster than typical “optimized” C/Fortran code, by using techniques [metaprogramming/codegen generation] that are hard in a low-level language. 8
Why can Julia be fast? First need to understand: Why is Python slow? goto Jupyter/IJulia notebooks from 18.S096. 9
MIT OpenCourseWare https://ocw.mit.edu 6.172 Performance Engineering of Software Systems Fall 2018 For information about citing these materials or our Terms of Use, visit: https://ocw.mit.edu/terms. 10
Recommend
More recommend