Skip to content

Julia on MeluXina

The MeluXina system environment provides the Julia programming language.

EasyBuild module description

Julia is a high-level, high-performance dynamic programming language for numerical computing.

Julia usage

Interactive

Reserve an interactive session:

salloc -A COMPUTE_ACCOUNT -t 01:00:00 -q dev --res cpudev -p cpu -N 1

The example above will allocate one CPU node in interactive mode (dev QoS with cpudev reservation). Load the Julia (Will load default version if not specified) module as in below script.

module load Julia

#Get in Julia environment
julia
Output
               _
   _       _ _(_)_     |  Documentation: https://docs.julialang.org
  (_)     | (_) (_)    |
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 1.6.2 (2021-07-14)
 _/ |\__'_|_|_|\__'_|  |  Official https://julialang.org/ release
|__/                   |

julia>

It is also possible to execute your Julia script in interactive mode by using julia command.

julia sphere_volume.jl
Or
>julia include(sphere_volume.jl)

Script
# function to calculate the volume of a sphere
function sphere_vol(r)
  # julia allows Unicode names (in UTF-8 encoding)
  # so either "pi" or the symbol π can be used
  return 4/3*pi*r^3
end

# functions can also be defined more succinctly
quadratic(a, sqr_term, b) = (-b + sqr_term) / 2a

# calculates x for 0 = a*x^2+b*x+c, arguments types can be defined in function definitions
function quadratic2(a::Float64, b::Float64, c::Float64)
  # unlike other languages 2a is equivalent to 2*a
  # a^2 is used instead of a**2 or pow(a,2)
  sqr_term = sqrt(b^2-4a*c)
  r1 = quadratic(a, sqr_term, b)
  r2 = quadratic(a, -sqr_term, b)
  # multiple values can be returned from a function using tuples
  # if the return keyword is omitted, the last term is returned
  r1, r2
end
vol = sphere_vol(3)
# @printf allows number formatting but does not automatically append the \n to statements, see below
using Printf
@printf "volume = %0.3f\n" vol 
#> volume = 113.097

quad1, quad2 = quadratic2(2.0, -2.0, -12.0)
println("result 1: ", quad1)
#> result 1: 3.0
println("result 2: ", quad2)
#> result 2: -2.0

Batch

Julia can also be used in a batch job using Slurm. The script below executes a simple Julia script on one CPU node allocated for 5 minutes.

#!/bin/bash -l
#SBATCH -N 1
#SBATCH --ntasks=1
#SBATCH --cpus-per-task=1
#SBATCH -p cpu
#SBATCH -q test
#SBATCH --time 00:05:00

#Load Julia module
module load Julia

#Check Julia version
julia --version

#Execute Julia script
julia julia_script.jl