Introduction to Julia Language

TAMIDS Workshop

Shao-Ting Chiu

Texas A&M Univ.

10/25/22

Session

Time Content
25 min Introduction to the Julia Programming Language
25 min The Julia SciML Ecosystem
10 min Break
30 min Hands-on session with Julia basics
30 min Hands-on session with SciML application

Figure 1: Tutorial Website1

What is Julia?

  1. Julia is a generic high-performance programming language1
    • Just-In-Time (JIT) compilation
    • Multiple dispatch creates type-stability
  2. Via multiple dispatch, different programming patterns can be adapted to the application.
  3. Julia supports high-level syntax that is approachable.

A quick look at multiple dispatch

A Generic function

julia> function multiply(a,b)
           return a*b
       end
multiply (generic function with 1 method)

A quick look at multiple dispatch

  • Type inference in Julia solves the optimized data type2.

Integer Input

julia> @code_llvm multiply(1, 1)
;  @ REPL[1]:1 within `multiply`
define i64 @julia_multiply_660(i64 signext %0, i64 signext %1) #0 {
top:
;  @ REPL[1]:2 within `multiply`
; ┌ @ int.jl:88 within `*`
   %2 = mul i64 %1, %0
; └
  ret i64 %2

A quick look at multiple dispatch

julia> @code_llvm multiply(2.0,2)
;  @ REPL[1]:1 within `multiply`
define double @julia_multiply_208(double %0, i64 signext %1) #0 {
top:
;  @ REPL[1]:2 within `multiply`
; ┌ @ promotion.jl:389 within `*`
; │┌ @ promotion.jl:359 within `promote`
; ││┌ @ promotion.jl:336 within `_promote`
; │││┌ @ number.jl:7 within `convert`
; ││││┌ @ float.jl:146 within `Float64`
       %2 = sitofp i64 %1 to double
# Skip
  ret double %3
}

Significant Features

  1. Julia Base and standard library are written in Julia itself.
  1. Julia is always Just-in-Time (JIT) compiled
  1. Multiple dispatch allows many conbinatoins of argument types
    • Approaching the speed of statically-compiled language like C/Fortran3

Why Julia is fast?4

  • JIT
  • Type inference
  • Type specialization in functions

Why Julia?

  • Syntax is simple5

    for i in 1:N
      # do something
    end
  • Extensibility

    • Collaboration of packages can be achieved by multiple dispatch
  • Fast

    • No need to worry about the unstable for-loop
  • Julia is written mostly by Julia

Generating Vandermonde Matrices1

Given \(x=[\alpha_1, \dots, \alpha_m]\). Derive \[\begin{equation} V_{m\times n} = \begin{bmatrix} 1 & \alpha_{1}^{1} & \dots & \alpha_{1}^{n-1}\\ 1 & \alpha_{2}^{1} & \dots & \alpha_{2}^{n-1}\\ \vdots & \vdots & \ddots & \vdots\\ 1 & \alpha_{m}^{1} & \dots & \alpha_{m}^{n-1} \end{bmatrix} \end{equation}\]

  1. Mining standard library is a must to Write efficient algorithm in Python/Matlab
  2. Type-generic at high-level, but only fitted to limited predefined types.

The Two-Language Problem

Language Flexibility Performance
High-level (e.g. Python/Matlab) Great Bad
Low-level (e.g. C/Fortan) Bad Great

To develope an algorithm,

  1. Write in Python
  2. Speed up in C++/C
  3. Write a wrapper

Generating Vandermonde Matrics

Given \(x=[\alpha_1, \dots, \alpha_m]\). Derive \[\begin{equation} V_{m\times n} = \begin{bmatrix} 1 & \alpha_{1}^{1} & \dots & \alpha_{1}^{n-1}\\ 1 & \alpha_{2}^{1} & \dots & \alpha_{2}^{n-1}\\ \vdots & \vdots & \ddots & \vdots\\ 1 & \alpha_{m}^{1} & \dots & \alpha_{m}^{n-1} \end{bmatrix} \end{equation}\]

Julia (generic typing)1

function vander(x::AbstractVector{T}, n=length(x)) where T
  m = length(x)
  V = zeros(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 V
end
  1. This code works for any container supports element-wised * method.
  2. The flexibility does not hurt performance.

What if I don’t need performance?

  1. For classical problems, Python/Matlab is good enough
  1. When accounting hard problems, it is possible to implement in Python/Matlab. However, the performance will be sacrificed.
  1. When performance is needed, it is necessary to write the whole project again with C++/C/Fortran1.

Can I still access to familiar libraries?

  1. Libraries of Python, C and Fortran can be easily called from Julia
  2. There are also numerous packages written in native Julia:
Python Julia 1
Pandas DataFrames.jl
Matplotlib Plots.jl
SymPy ModelingToolkit.jl
Scipy DifferentialEquations.jl/JuliaStats/…
Scikit-Learn MLJ.jl
Tensorflow/Pytorch Flux.jl
Optimization JuMP.jl/Optimization.jl
\(\vdots\) \(\vdots\)

How can I get started with Julia?1

References

1.
Bezanson, J., Edelman, A., Karpinski, S., and Shah, V.B. (2017). Julia: A fresh approach to numerical computing. SIAM review 59, 65–98.
2.
Nash, J. (2016). Inference Convergence Algorithm in Julia - Julia Computing.
3.
Julia Documentation \(\cdot\) The Julia Language.
4.
Rackauckas, C. (2022). Parallel computing and scientific machine learning (SciML): Methods and applications 10.5281/zenodo.6917234.
5.
Perkel, J.M. (2019). Julia: Come for the syntax, stay for the speed. Nature 572, 141–143.