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
. - Always enclose code in functions because functions are compiled
- Don’t use global mutable variables
- 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
- standard functions like
rand
,zero
and operators can be extended by libraries to work with new types - functions (not objects) are central to programming Julia
- don’t over-specify argument types: Julia is dynamically typed, embrace it
-
eachindex
and relatives are good ways iterate collections
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
- You can use the
BestieTemplate
to generate a new package structure complete with tooling for best practices in Julia development. - 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.
Type Stability
- Type instabilities are the bane of efficient Julia
- We can discover type instability using
@profview
, and analyze further using@code_warntype
. - Don’t use mutable global variables.
- Write your code inside functions.
- Specify element types for containers and structs.
Reducing allocations on the Logistic Map
- Allocations are slow.
- Growing arrays dynamically induces allocations.
Value types: game of life
- Value types are a useful and efficient abstraction
- Using StaticArrays can have a dramatic impact on performance
Threads, ASync and Tasks
- Basic parallel for-loops are done with the
@threads
macro. - Julia has built-in support for atomics.
- Channels are primary means of asynchronous communication.
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.
Recap and Recommended libraries
- SciML toolkit is dominant but not always the best choice.
- Search for libraries that specify your needs.
Appendix: Iterators and type stability
- When things are slow, try adding more types.
- Writing abstract code that is also performant is not easy.