Introduction to Julia

Eric Neiva | TurlierLab | 17-02-2022


 


Acknowledgement of EU funding
This material is part of a project that has received funding from the European Research Council (ERC) under the European Union’s Horizon 2020 research and innovation programme (Grant agreement No. 949267).
Eric Neiva | Intro to Julia | TurlierLab | 17-02-2022

Contents

  • Motivation
  • The Julia programming language
  • Getting started with Julia
 
Eric Neiva | Intro to Julia | TurlierLab | 17-02-2022

Motivation

  • An abstract FE framework for cell morphogenesis
    • To accommodate all the FE simulation needs in the lab
    • Easily import or prototype new FE technology 👈
    • In Julia, as a subpackage of Gridap


Borja da Rocha, Bleyer et Turlier, submitted

 
Eric Neiva | Intro to Julia | TurlierLab | 17-02-2022

The Julia programming language

  • Since 2012
  • General purpose
  • Focus in high-performance
  • Well-suited for numerical analysis and computational science
  • v1.7.2 (Feb 8 2022)
  • v1.8 (very soon)

https://julialang.org/

 
Eric Neiva | Intro to Julia | TurlierLab | 17-02-2022

Why Julia?

To answer this, we need to review
high-level compiled and interpreted programming languages

 
Eric Neiva | Intro to Julia | TurlierLab | 17-02-2022

High-level programming languages (almost all)


medium.com, Young Coder, The Difference Between Compiled and Interpreted Languages, Sep 14, 2020

 
Eric Neiva | Intro to Julia | TurlierLab | 17-02-2022

Compiled languages (C, C++, Java, ...) 👈 Performance


medium.com, Young Coder, The Difference Between Compiled and Interpreted Languages, Sep 14, 2020

 
Eric Neiva | Intro to Julia | TurlierLab | 17-02-2022

Interpreted languages (Python, JavaScript, ...) 👈 Productivity


medium.com, Young Coder, The Difference Between Compiled and Interpreted Languages, Sep 14, 2020

 
Eric Neiva | Intro to Julia | TurlierLab | 17-02-2022

Trade-off performance and productivity

For computationally-demanding applications (e.g., FEs):

  • "Hybrid" software applications:
    • E.g., python front-end + c++ back-end
    • two-language problem > limited extensibility and prototyping
  • Julia potentially provides better balance
    • performance and productivity in a single environment
 
Eric Neiva | Intro to Julia | TurlierLab | 17-02-2022

Main features of Julia

  • Optional typing
  • Multiple dispatch (generalisation of polymorphism)
    foo(bar) = ...
    foo(bar::Integer) = ...
    foo(bar::Float64) = ...
    foo(bar::MyType1,baz::MyType2) = ...
  • Multi-paradigm
    • Procedural, functional, meta, object-oriented...
 
Eric Neiva | Intro to Julia | TurlierLab | 17-02-2022

Main features of Julia

  • Performance through
    • type inference (deduce output types from input types)
      • generate fast, optimized code
    • just-in-time (JIT) or run-time compilation
      • user experience as an intepreted one
  • The only JIT-compiled language in the petaflop (1015)\footnotesize{\left(\mathsf{10^{15}}\right)} club
    • Since 2017; along with C, C++ and Fortran
 
Eric Neiva | Intro to Julia | TurlierLab | 17-02-2022

Main features of Julia

  • Time to first plot or compiler latency
    • JIT compilation time on first run
julia> @time using Plots
  4.860129 seconds (6.87 M allocations: 502.634 MiB, 7.64% gc time, 0.14% compilation time)

julia> @time using Plots
  0.000099 seconds (81 allocations: 5.969 KiB)
  • Two minor releases per year (+3-5 patch releases per minor)
    • Beware of breaking changes and compiler latency regressions
 
Eric Neiva | Intro to Julia | TurlierLab | 17-02-2022

Julia vs Python

  • Python has never been so popular (in Feb 2022)
    • #1 in TIOBE index (search engine results)
    • #1 in IEEE Spectrum index
    • #1 in PYPL index
    • ...
  • Julia way behind (#20-30). Growing, but not emerging
  • Often performance not needed or enough with GPU parallelism
 
Eric Neiva | Intro to Julia | TurlierLab | 17-02-2022

Julia vs Python in machine learning



Numbers speak for themselves....

 
Eric Neiva | Intro to Julia | TurlierLab | 17-02-2022

Julia vs Python in machine learning



Numbers speak for themselves.... BUT this is not the whole story!

TensorFlow PyTorch Flux.jl
 
Eric Neiva | Intro to Julia | TurlierLab | 17-02-2022

My case for Julia

  • It risks becoming a niche language for scientific computing
  • But it could become THE standard in this domain
    • The only petascale-capable JIT-compiled language
    • Improved balance of productivity and performance
      • e.g. in basic research on finite elements
  • Further reading: independent benchmarks
 
Eric Neiva | Intro to Julia | TurlierLab | 17-02-2022

Getting started with Julia

  • Download and install
  • Differences from Python/C/C++
  • The Julia package manager
  • Introductory tutorials
  • Style and performance tips
  • Noteworthy packages
  • VS Code as Julia IDE
  • The Julia Community
 
Eric Neiva | Intro to Julia | TurlierLab | 17-02-2022

Download and install Julia

Download Julia

Check supported platforms

Julia comes with a full-featured interactive command-line REPL (read-eval-print loop) built into the julia executable

 
Eric Neiva | Intro to Julia | TurlierLab | 17-02-2022

Documentation

Julia is well-documented

Julia documentation

It comprises an excellent manual

Manual / Getting started 👈 USE IT!

 
Eric Neiva | Intro to Julia | TurlierLab | 17-02-2022

Fast-track to Julia from Python/C/C++

 
Eric Neiva | Intro to Julia | TurlierLab | 17-02-2022

The Julia package manager, Pkg.jl

  • Integrated into the REPL (Pkg documentation)
  • Manages reproducible environments, not only packages 👈
    • An environment is a record of
      • Package dependencies and compatibilities in Project.toml
      • Package state (pkg version, git revision, ...) in Manifest.toml
  • If a Julia project (\approx library) contains a manifest, this will install the packages in the same state that is given by that manifest
 
Eric Neiva | Intro to Julia | TurlierLab | 17-02-2022

Set up the working environment

  • Work in projects or packages
  • Creating packages
 
Eric Neiva | Intro to Julia | TurlierLab | 17-02-2022

Introductory tutorials

 
Eric Neiva | Intro to Julia | TurlierLab | 17-02-2022

How to develop your code in Julia?

 
Eric Neiva | Intro to Julia | TurlierLab | 17-02-2022

How to write your code in Julia?

  • Style guide
    • Append ! to names of functions that modify their arguments
      • e.g., sort does not mutate, sort! mutates
    • Prefer exported methods over direct field access
      • data members of a type are not hidden
    • Apply naming and argument ordering similar to Julia base
    • ...
 
Eric Neiva | Intro to Julia | TurlierLab | 17-02-2022

How to write your code in Julia?

  • Performance tips
    • Performance critical code should be inside a function 👈
      • Julia's compiler works better on code inside functions
      • Avoid global variables
    • Write "type-stable" functions (results in faster code)
    • Measure performance and memory footprint with @time
 
Eric Neiva | Intro to Julia | TurlierLab | 17-02-2022

More on VS Code as Julia IDE

  • Quite powerful
    • Built-in debugger
    • Native support for Jupyter notebooks (e.g., JuliaTutorials)
    • Embedded results (e.g., plots, profiler, databases)
    • New features every year (announced in JuliaCon)
  • Collaborative sessions
    • A "Google Docs" experience with integrated audio and text chat
 
Eric Neiva | Intro to Julia | TurlierLab | 17-02-2022

Julia for data science and image processing

 
Eric Neiva | Intro to Julia | TurlierLab | 17-02-2022

Parallel computing in Julia

 
Eric Neiva | Intro to Julia | TurlierLab | 17-02-2022

Other interesting packages

 
Eric Neiva | Intro to Julia | TurlierLab | 17-02-2022

Community

 
Eric Neiva | Intro to Julia | TurlierLab | 17-02-2022

Gridap.jl in JuliaCon 2021


 
Eric Neiva | Intro to Julia | TurlierLab | 17-02-2022

Getting help

 
Eric Neiva | Intro to Julia | TurlierLab | 17-02-2022

Announcements

And that's all! Thank you!

 

This material is part of a project that has received funding from the European Research Council (ERC) under the European Union’s Horizon 2020 research and innovation programme (Grant agreement No. 949267).

Except where otherwise noted, this work and its contents (texts and illustrations) are licensed under the Attribution 4.0 International (CC BY 4.0).