Getting Started
- In Julia, the REPL is much more important than in some other languages.
- Pluto is a reactive environment
- VS Code has the best editor integration for Julia
Introduction to Julia
- Julia has
if-else
,for
,while
,function
andmodule
blocks that are not dissimilar from other languages. - Blocks are all ended with
end
. - …
- Julia variables are not visible outside the block in which they’re defined (unlike Python).
Types and Dispatch
- Julia is fundamentally a dynamically typed language.
- Static types are only ever used for dispatch.
- Multiple dispatch is the most important means of abstraction in Julia.
- Parametric types are important to achieve type stability.
Simulating the Solar System
-
Plots.jl
is in some ways the ‘standard’ plotting package, but it is in fact quite horrible. -
Makie.jl
offers a nice interface, pretty plots, is generally stable and very efficient once things have been compiled.
Packages and environments
- Julia has a built-in package manager called
Pkg
. - The package manager is usually accessed from the REPL, by pressing
]
. -
Project.toml
lists the dependencies of a package. -
Manifest.toml
specifies a completely reproducible environment with exact versions.
Package development
- Currently, the editor with (by far) the best support is VSCode.
- The
Revise.jl
module can automatically reload parts of your code that have changed. - Best practice: file names should reflect module names.
Best practices
- Julia has integrated support for unit testing.
-
Documenter.jl
is the standard package for generating documentation. - The Julia ecosystem is well equiped to help you keep your code in fit shape.
Julia and performance
- The Julia compiler compiles a function once its called with a specific type signature.
Reducing allocations on the Logistic Map
- Allocations are slow.
- Growing arrays dynamically induces allocations.
Performance: do's and don'ts
- Don’t use mutable global variables.
- Write your code inside functions.
- Specify element types for containers and structs.
- Specialize functions instead of doing manual dispatch.
- Write functions that always return the same type (type stability).
- Don’t change the type of variables.
Channels and do
-syntax
- Channels are Julia’s primary means of state management and inter-thread communication.
Getting Started
- Basic parallel for-loops are done with the
@threads
macro. - Julia has built-in support for atomics.
- Channels are primary means of asynchronous communication.
Getting Started
- Julia has built-in support for distributed computing.
Transducers
- Transducers are composable functions.
- Using the
|>
operator can make your code consise, with fewer parenthesis, and more readable. - Overusing the
|>
operator can make your code an absolute mess. - Using Tranducers you can build scalable data pipe lines.
- Many Julia libraries for parallel computing and data processing are built on top of Tranducers.
Recommended libraries
- SciML toolkit is dominant but not always the best choice.
- Search for libraries that specify your needs.
GPU Programming
- We can compile Julia code to GPU backends using
KernelAbstractions
. - Even on smaller laptops, significant speedups can be achieved, given the right problem.
Appendix: Iterators and type stability
- When things are slow, try adding more types.
- Writing abstract code that is also performant is not easy.