# Running A Julia Script Through SLURM

**URL:** https://discourse.julialang.org/t/running-a-julia-script-through-slurm/123268
**Category:** General Usage
**Tags:** package, hpc, argument, slurm
**Created:** [November 29, 2024, 11:06pm UTC](https://discourse.julialang.org/t/running-a-julia-script-through-slurm/123268 "2024-11-29T23:06:10Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![leespen1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leespen1/32/221928_2.png) [@leespen1](https://discourse.julialang.org/u/leespen1)
#### Post date: [November 29, 2024, 11:06pm UTC](https://discourse.julialang.org/t/running-a-julia-script-through-slurm/123268/1 "2024-11-29T23:06:10Z")

</div>

I have a julia script that run a physics simulation. The physics simulation takes around 8 hours, and I want to run it with many random initial conditions, so I planned to run the script multiple times using a SLURM job array, using ArgParse.jl to process command line arguments which determines the parameters of the simulation, including the seed to use for the randomization (which is what the job array ID controls). My script does not use multithreading or distributed computing in any way, I just want to run it many time with different initial conditions

Unfortunately, I am somehow getting a segmentation fault from ArgParse.jl

Here is a MWE.

`argparse.jl`:

```julia
using ArgParse

s = ArgParseSettings()
#add_arg_table(s, "arg1", Dict(:nargs=>1, :required=>true))
@add_arg_table s begin
    "arg1"
        help = "First argument"
        required = true
end
p = parse_args(s)
println("Argument 1 is: ", p["arg1"])

```

`argparse.sb`:

```julia
#!/bin/bash --login
#SBATCH --job-name=argparse # Job name
#SBATCH --mail-type=NONE # Mail events (NONE, BEGIN, END, FAIL, ALL)
#SBATCH --nodes=1 # Maximum number of nodes to be allocated
#SBATCH --ntasks-per-node=1 # Maximum number of tasks on each node
#SBATCH --cpus-per-task=1 # Number of processors for each task (want several because the BLAS is multithreaded, even though my Julia code is not)
#SBATCH --mem=2G # Memory (i.e. RAM) per NODE
#SBATCH --export=ALL                
#SBATCH --constraint=intel18         
#SBATCH --time=0-00:05:00 # Wall time limit (days-hrs:min:sec)
#SBATCH --output=argparse_%A.log # Path to the standard output and error files relative to the working directory

echo "Date = $(date)"
echo "Hostname = $(hostname -s)"
echo "Number of Nodes Allocated = $SLURM_JOB_NUM_NODES"
echo "Number of Tasks Per Node = $SLURM_NTASKS_PER_NODE"
echo "Number of CPUs Per Task = $SLURM_CPUS_PER_TASK"
echo ""

which julia
julia ./argparse.jl 1

```

The log generated by running `sbatch argparse.sb`:

```julia
Date = Fri Nov 29 05:59:40 PM EST 2024
Hostname = skl-027
Number of Nodes Allocated = 1
Number of Tasks Per Node = 1
Number of CPUs Per Task = 1

/mnt/home/leespen1/.juliaup/bin/julia
/var/lib/slurmd/job47051755/slurm_script: line 22: 1426934 Segmentation fault (core dumped) julia ./argparse.jl 1

```

I have no idea where to go from here. Any advice on how to fix this (or an alternative workflow which would accomplish the same thing) would be appreciated. All the material I have found for using Julia in HPC have been about how to use multi-threading or distributed computing, which is not what I am trying to do.

PS, the segfault does not happen when I run `argparse.sb` as a bash script using `salloc`:

```julia
eespen1@dev-intel18:~/Research/QuantumGateDesign.jl/cnot3$ salloc --nodes=1 --ntasks=1 --mem=2G --cpus-per-task=1 --constraint=intel18 --time=00:05:00
salloc: Granted job allocation 47051784
salloc: Waiting for resource configuration
salloc: Nodes skl-031 are ready for job
leespen1@skl-031:~/Research/QuantumGateDesign.jl/cnot3$ bash argparse.sb 
Date = Fri Nov 29 06:11:15 PM EST 2024
Hostname = skl-031
Number of Nodes Allocated = 1
Number of Tasks Per Node = 
Number of CPUs Per Task = 1

/mnt/home/leespen1/.juliaup/bin/julia
Argument 1 is: 1

```

---

<div class="post-metadata">

### Author: ![Sevi](https://avatars.discourse-cdn.com/v4/letter/s/c67d28/32.png) [@Sevi](https://discourse.julialang.org/u/Sevi)
#### Post date: [November 30, 2024, 8:09am UTC](https://discourse.julialang.org/t/running-a-julia-script-through-slurm/123268/2 "2024-11-30T08:09:37Z")

</div>

Sounds strange…

I’m using a very similar setup, so the general idea should definitely work (create a Julia script with `ArgParse` and call it from the Slurm script).

- In my experience, when `salloc` and `sbatch` do different things, it might be related to the shell environment, but not sure here since you start `bash` as a login shell and export the user environment explicitly. But perhaps you can check (e.g. by using the bash command `env` to print the whole environment and see if it differs somehow).
- EDIT: Related to the first point: Is there anything in your `.bashrc` that could make the two cases behave differently?
- Another useful step in debugging this problem would be to compare the output of `versioninfo()` and `Pkg.status()` in the Julia scripts. It looks like the same executable is called, but maybe the problem is in a specific version of Julia and/or the packages.
- Are you using the global Julia environment? Have you tried creating a new environment in the script and adding your dependencies (`ArgParse` here) before you call the script. If the problem is related to (pre)compilation, this might help sorting it out.
- Related to the compilation question: Are the machine CPUs identical? You specify `intel18` in both, but I don’t know if there could be different details about the CPUs still?
- Does the issue appear always on the same machine or on all machines (you could request a certain node explicitly to test that)?

Might be something completely unrelated 😅 but maybe checking these things can help narrow down the problem.

---

<div class="post-metadata">

### Author: ![affans](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/affans/32/11911_2.png) [@affans](https://discourse.julialang.org/u/affans)
#### Post date: [November 30, 2024, 3:21pm UTC](https://discourse.julialang.org/t/running-a-julia-script-through-slurm/123268/3 "2024-11-30T15:21:59Z")

</div>

How much access do you have on your cluster? Could you ssh into the allocated node `skl-027` manually and run the script directly to see if it produces a segfault? There could be a hardware error on that node.

---

<div class="post-metadata">

### Author: ![p\_f](https://avatars.discourse-cdn.com/v4/letter/p/45deac/32.png) [@p\_f](https://discourse.julialang.org/u/p_f)
#### Post date: [November 30, 2024, 4:29pm UTC](https://discourse.julialang.org/t/running-a-julia-script-through-slurm/123268/4 "2024-11-30T16:29:10Z")

</div>

Alternative workflow that might work. I don’t use argparse for this, I just do this in Julia:

`id = Base.parse(Int, ENV["SLURM_ARRAY_TASK_ID"])`

which is the job id created by slurm. So if you have `array=1-30` in your slurm submit script, each Julia job will have access to a unique id number to set up parameters etc.

---

<div class="post-metadata">

### Author: ![leespen1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leespen1/32/221928_2.png) [@leespen1](https://discourse.julialang.org/u/leespen1)
#### Post date: [December 15, 2024, 3:26am UTC](https://discourse.julialang.org/t/running-a-julia-script-through-slurm/123268/5 "2024-12-15T03:26:46Z")

</div>

I was on a deadline so I ended up just removing the `ArgParse` dependency and writing a bash script to replace the relevant variables, but when I have time I will try your suggestions. Thank you very much!
