the julia technical computing language - an introduction · 15-03-2017  · aviksengupta. april...

25
Intro to Julia Justin Krometis Overview Julia Basics Structure Parallel Julia Conclusions www.arc.vt.edu The Julia Technical Computing Language An Introduction Justin Krometis [email protected] Advanced Research Computing Virginia Tech March 15, 2017 Intro to Julia An Introduction (March 15, 2017) 1

Upload: others

Post on 03-Feb-2021

2 views

Category:

Documents


0 download

TRANSCRIPT

  • Intro to Julia

    Justin Krometis

    Overview

    Julia Basics

    Structure

    Parallel Julia

    Conclusions

    www.arc.vt.edu

    The Julia Technical ComputingLanguage

    An Introduction

    Justin [email protected]

    Advanced Research ComputingVirginia Tech

    March 15, 2017

    Intro to Julia An Introduction (March 15, 2017) 1

    mailto:[email protected]

  • Intro to Julia

    Justin Krometis

    Overview

    Julia Basics

    Structure

    Parallel Julia

    Conclusions

    www.arc.vt.edu

    Overview

    Intro to Julia An Introduction (March 15, 2017) 2

  • Intro to Julia

    Justin Krometis

    Overview

    Julia Basics

    Structure

    Parallel Julia

    Conclusions

    www.arc.vt.eduWhat is Julia?

    I Technical computing languageI Lots of built-in functionalityI FastI Free (MIT License)I http://julialang.org/I Entered Top 50 languages by TIOBE index

    September 2016

    Intro to Julia An Introduction (March 15, 2017) 3

    http://julialang.org/http://www.tiobe.com/tiobe-index/

  • Intro to Julia

    Justin Krometis

    Overview

    Julia Basics

    Structure

    Parallel Julia

    Conclusions

    www.arc.vt.eduAccess

    I ARC (e.g. NewRiver)I Load it

    $ module load gcc atlas python julia/0.5.0$ which julia/opt/apps/gcc5_2/julia/0.5.0/bin/julia

    I Build your own:$ module purge; module load gcc cmake$ make -j

    I JuliaBox: http://www.juliabox.orgI Download to your machine (.dmg, .exe)

    Intro to Julia An Introduction (March 15, 2017) 4

    http://www.juliabox.org

  • Intro to Julia

    Justin Krometis

    Overview

    Julia Basics

    Structure

    Parallel Julia

    Conclusions

    www.arc.vt.eduLimitations

    I New: First released in 2012, Latest version is 0.5.0I Still changing - this error appeared in v0.5:

    A = rand (10 ,10); y = zeros(100 ,10);

    y[1 ,:] = rand (10);y[2 ,:] = y[1 ,:] * A.’;ERROR: DimensionMismatch ("A has

    dimensions (10 ,1) but B hasdimensions (10 ,10)")

    I Basic documentation is extensive, but outsidedocumentation can be sparse

    Intro to Julia An Introduction (March 15, 2017) 5

  • Intro to Julia

    Justin Krometis

    Overview

    Julia Basics

    Structure

    Parallel Julia

    Conclusions

    www.arc.vt.edu

    Julia Basics

    Intro to Julia An Introduction (March 15, 2017) 6

  • Intro to Julia

    Justin Krometis

    Overview

    Julia Basics

    Structure

    Parallel Julia

    Conclusions

    www.arc.vt.eduPackages

    I Julia contains a built-in package manager similarto R

    I Easy to add and remove third-party packagesI Examples: HDF5 for file I/O, Distributions for

    probability distributionsI To add a package: Pkg.add("HDF5")I To update packages: Pkg.update()I To use a package: using HDF5I Package listing: http://pkg.julialang.org

    Intro to Julia An Introduction (March 15, 2017) 7

    http://pkg.julialang.org

  • Intro to Julia

    Justin Krometis

    Overview

    Julia Basics

    Structure

    Parallel Julia

    Conclusions

    www.arc.vt.eduPlotting

    I Julia does not come with any built-in plottingcapabilities

    I However, plotting is easily added with twopackages:

    I Gadfly: Based on R’s ggplot2 packagePkg.add(" Gadfly ")using Gadfly

    I PyPlot: Uses Python’s matplotlib library.(Requires that Python, Matplotlib be installed.)

    Pkg.add(" PyPlot ")using PyPlot

    I ARC installations come with these pre-loadedI Plots package provides wrappers

    Intro to Julia An Introduction (March 15, 2017) 8

  • Intro to Julia

    Justin Krometis

    Overview

    Julia Basics

    Structure

    Parallel Julia

    Conclusions

    www.arc.vt.eduPlot Examples

    1.0 0.5 0.0 0.5 1.0x

    1.0

    0.5

    0.0

    0.5

    1.0y

    PyPlot Example

    x

    -1.0 -0.5 0.0 0.5 1.0

    -1.0

    -0.5

    0.0

    0.5

    1.0

    y

    Gadfly Example

    Intro to Julia An Introduction (March 15, 2017) 9

  • Intro to Julia

    Justin Krometis

    Overview

    Julia Basics

    Structure

    Parallel Julia

    Conclusions

    www.arc.vt.eduDevelopment and Editing

    I Juno: Integrated Development Environment (IDE)for Julia built on Atom editor.

    I IJulia: Provides Jupyter notebook forbrowser-based development

    I Download julia-vim to add Julia syntax to vimtext editor

    I Debugging via the Gallium package

    Intro to Julia An Introduction (March 15, 2017) 10

    http://junolab.orghttps://atom.iohttps://github.com/JuliaEditorSupport/julia-vim

  • Intro to Julia

    Justin Krometis

    Overview

    Julia Basics

    Structure

    Parallel Julia

    Conclusions

    www.arc.vt.eduPerformance

    Performance is one of the main draws of Julia:I Just-in-time (JIT) Compilation: Compiles at

    run-time, when more optimizations are availableI Performance comparable to C

    Figure : Run-time / C Run-time

    Intro to Julia An Introduction (March 15, 2017) 11

  • Intro to Julia

    Justin Krometis

    Overview

    Julia Basics

    Structure

    Parallel Julia

    Conclusions

    www.arc.vt.edu

    Structure

    Intro to Julia An Introduction (March 15, 2017) 12

  • Intro to Julia

    Justin Krometis

    Overview

    Julia Basics

    Structure

    Parallel Julia

    Conclusions

    www.arc.vt.eduComparison with Matlab

    Much Matlab syntax is valid in Julia, but there aredifferences:

    I Strings with “string” rather than ’string’I Comments are #, not %I Arrays passed by reference (think C)I Array indexing with [i,j], not (i,j)I Types: Integers, tuples, rangesI return statementI Some functions (e.g. max()) operate element-wiseI No need for semicolons at end of statements

    See documentation for full list

    Intro to Julia An Introduction (March 15, 2017) 13

    http://docs.julialang.org/en/release-0.4/manual/noteworthy-differences/#noteworthy-differences-from-matlab

  • Intro to Julia

    Justin Krometis

    Overview

    Julia Basics

    Structure

    Parallel Julia

    Conclusions

    www.arc.vt.eduComparison with R

    Differences are more extensive than for MatlabI Strings with “string” rather than ’string’I Arrays passed by reference (think C)I

  • Intro to Julia

    Justin Krometis

    Overview

    Julia Basics

    Structure

    Parallel Julia

    Conclusions

    www.arc.vt.eduComparison with Python

    I end to end a block (if, for, while, etc)I Array indexing is 1-based (not 0-based)I Arrays are column-major (reorder indices for best

    performance)

    See documentation for full list

    Intro to Julia An Introduction (March 15, 2017) 15

    http://docs.julialang.org/en/release-0.4/manual/noteworthy-differences/#noteworthy-differences-from-python

  • Intro to Julia

    Justin Krometis

    Overview

    Julia Basics

    Structure

    Parallel Julia

    Conclusions

    www.arc.vt.eduTypes

    I Integers: Int8, Int16, Int32, Int64, Int128I Floats: Float16, Float32, Float64I Range: rng=0:0.1:1 returns a

    FloatRange{Float64}I Many more

    Enables speed but can cause headaches, e.g.

    function recip(x); return x^(-1); endrecip(2)

    yieldsERROR: DomainError: Cannot raise aninteger x to a negative power -n.

    Intro to Julia An Introduction (March 15, 2017) 16

  • Intro to Julia

    Justin Krometis

    Overview

    Julia Basics

    Structure

    Parallel Julia

    Conclusions

    www.arc.vt.eduMultiple Dispatch

    Allows multiple functions to be defined with the samename, e.g.:

    function recip(x::Int)return float(x)^(-1);

    endfunction recip(x::Float64)

    return x^(-1);end

    Then recip(2) yields 0.5.You can also define functions with optional arguments.

    Intro to Julia An Introduction (March 15, 2017) 17

  • Intro to Julia

    Justin Krometis

    Overview

    Julia Basics

    Structure

    Parallel Julia

    Conclusions

    www.arc.vt.edu

    Parallel Julia

    Intro to Julia An Introduction (March 15, 2017) 18

  • Intro to Julia

    Justin Krometis

    Overview

    Julia Basics

    Structure

    Parallel Julia

    Conclusions

    www.arc.vt.eduThreaded Linea Algebra

    I Julia built with Basic Linear Algebra Subroutines(BLAS). The default is OpenBLAS.

    I BLAS are threaded, leveraging multiple cores on asingle computer, so Julia is parallel when doinglarge-scale linear algebra.

    I To control the number of threads used, use theOPENBLAS_NUM_THREADS environment variable.export OPENBLAS_NUM_THREADS=24

    Intro to Julia An Introduction (March 15, 2017) 19

  • Intro to Julia

    Justin Krometis

    Overview

    Julia Basics

    Structure

    Parallel Julia

    Conclusions

    www.arc.vt.eduStarting Julia in Parallel

    You can also manually start multiple instances of Juliausing julia -p, e.g.

    julia -p 24 ’include("script.jl")’

    to run the script script.jl on 24 cores.You can also start them on multiple machines:

    julia --machinefile hosts.txt’include("script.jl")’

    Intro to Julia An Introduction (March 15, 2017) 20

  • Intro to Julia

    Justin Krometis

    Overview

    Julia Basics

    Structure

    Parallel Julia

    Conclusions

    www.arc.vt.eduUsing Parallel Julia

    To use multiple workers, the program must tell theworker processes what to do, e.g.

    I Load a script on all processes: @everywhereinclude("count_heads.jl")

    I Get a process’s ID: myid()I Push a set of jobs to workers: pmap()I Created a shared array: SharedArray()

    There is a lot more. See here and here.

    Intro to Julia An Introduction (March 15, 2017) 21

    http://docs.julialang.org/en/stable/manual/parallel-computing/http://docs.julialang.org/en/release-0.5/stdlib/parallel/

  • Intro to Julia

    Justin Krometis

    Overview

    Julia Basics

    Structure

    Parallel Julia

    Conclusions

    www.arc.vt.eduParallel For

    If a for loop has independent iterations, it can beconverted into a parallel for loop:

    total = @parallel (+) for i=1:n

    Iterations are split across all available processes andtotal summed across them to yield the answer.This is similar to MATLAB’s parfor functionality.

    Intro to Julia An Introduction (March 15, 2017) 22

  • Intro to Julia

    Justin Krometis

    Overview

    Julia Basics

    Structure

    Parallel Julia

    Conclusions

    www.arc.vt.edu

    Conclusions

    Intro to Julia An Introduction (March 15, 2017) 23

  • Intro to Julia

    Justin Krometis

    Overview

    Julia Basics

    Structure

    Parallel Julia

    Conclusions

    www.arc.vt.eduReferences

    I Julia WebsiteI “Julia: A Fresh Approach to Numerical

    Computing.” Jeff Bezanson, Alan Edelman, StefanKarpinski, and Viral B. Shah. SIAM Rev., 59(1),65-98. (SIAM, Early version on arXiv)

    I “Julia for R Programmers.” Douglas Bates. (link)I “The Julia Express.” Bogumil Kamiński. (link)I “Julia High Performance.” Avik Sengupta. April

    26, 2016. (Amazon)

    Intro to Julia An Introduction (March 15, 2017) 24

    http://julialang.org/http://epubs.siam.org/doi/abs/10.1137/141000671https://arxiv.org/pdf/1411.1607.pdfhttp://www.stat.wisc.edu/~bates/JuliaForRProgrammers.pdfhttps://github.com/bkamins/The-Julia-Expresshttps://www.amazon.com/Julia-High-performance-Avik-Sengupta/dp/1785880918

  • Intro to Julia

    Justin Krometis

    Overview

    Julia Basics

    Structure

    Parallel Julia

    Conclusions

    www.arc.vt.eduQuestions?

    Justin [email protected] Research ComputingVirginia Tech

    Intro to Julia An Introduction (March 15, 2017) 25

    mailto:[email protected]

    OverviewJulia BasicsStructureParallel JuliaConclusions