0

Our school uses SLURM as the queueing system, where one has to specify some "preambles" before other commands. Hence, a shell script in this case usually starts with

#!/bin/bash

#SBATCH -n 10               # Number of cores requested
#SBATCH -N 1                # Ensure that all cores are on one machine
#SBATCH -p general          # Partition to submit to
#SBATCH --mem-per-cpu=20000 # Memory per cpu in MB (see also --mem)
#SBATCH -o out              # Standard out goes to this file 

Now, I wish to make my core number as a constant, which facilitates modifications. I did

#!/bin/bash

ZEROTH_PORT=50000
NO_CORES=10

#SBATCH -n $((NO_CORES))    # Number of cores requested
#SBATCH -N 1                # Ensure that all cores are on one machine
#SBATCH -p general          # Partition to submit to
#SBATCH --mem-per-cpu=20000 # Memory per cpu in MB (see also --mem)
#SBATCH -o out              # Standard out goes to this file 

It fails at #SBATCH -n $((NO_CORES)). As a complete newbie in shell script, I have no idea why $((NO_CORES)) here returns the value of NO_CORES.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
Sibbs Gambling
  • 19,274
  • 42
  • 103
  • 174

1 Answers1

0

$(( … )) is an arithmetic evaluation context. Since NO_CORES is an integer, and no other operation is done on it in the syntax, the result is the value of the expansion of NO_CORES. But if you know the parameter is an integer, you can just use a normal expansion:

NO_CORES=10
SBATCH -n $NO_CORES # -> SBATCH -n 10
kojiro
  • 74,557
  • 19
  • 143
  • 201
  • Strangely, this does not work, either. This is my first intuition, and hence, I tried it at the very beginning. In addition, the `#` before `SBATCH` is not a typo. I do not understand why it has a `#` in front, making it look like a comment. – Sibbs Gambling Jul 04 '14 at 01:39
  • @FarticlePilter The `#` makes it a comment, for certain, in bash. A line of code starting with a comment will not run. – kojiro Jul 04 '14 at 01:40
  • No, it does take some effect. `#SBATCH -n 10` indeed gives me 10 cores, and `#SBATCH -n 20` gives me 20, as per the job information. If it is a comment, shouldn't these two lines yield the same result? – Sibbs Gambling Jul 04 '14 at 01:42
  • How are you running the above script? – kojiro Jul 04 '14 at 01:43
  • `sbatch run.sh`, where `run.sh` is the name of this shell script. – Sibbs Gambling Jul 04 '14 at 01:44
  • 1
    @FarticlePilter ah, then the script is not bash and not invoked by bash. I do not know how `sbatch` interprets its scripts. – kojiro Jul 04 '14 at 01:46
  • What you need to do is post the relevant parts of sbatch. You understand that the code you posted provides nothing but `comments` about how it is supposed to be used and shows nothing about its operation. It could be reassigning or initializing `NO_CORES` later in the script completely overwriting your use of `NO_CORES`. You can get an idea of what lines you need (if it is a huge file) with `grep -n -B5 -A5 NO_CORES sbatch` – David C. Rankin Jul 04 '14 at 02:03