The interactive() bash function is designed to simplify the submission of interactive jobs using salloc. It provides default arguments for common resource requests and allows for customization through additional parameters.
interactive ()
{
local DEFAULT_ARGS=(--account="$1" --partition="$2" --nodes=1 --ntasks-per-node=1 --cpus-per-task=4 --mem=64G --time="$3" --gres=gpu:1);
local EXTRA_ARGS=();
local DO_NOTIFY=0;
local num_fixed_args=3;
local args_copy=("$@");
local extra_args_start_index=-1;
local i=0;
while (( i < ${#args_copy[@]} )); do
if [[ "${args_copy[$i]}" == "--notify" ]]; then
DO_NOTIFY=1;
unset 'args_copy[i]';
args_copy=("${args_copy[@]}");
continue;
fi;
if [[ "${args_copy[$i]}" == "--" ]]; then
extra_args_start_index=$((i + 1));
break;
fi;
i=$((i + 1));
done;
if [[ $extra_args_start_index -ne -1 ]]; then
EXTRA_ARGS=("${args_copy[@]:$extra_args_start_index}");
fi;
declare -A arg_map;
for arg in "${DEFAULT_ARGS[@]}";
do
key="${arg%%=*}";
arg_map["$key"]="$arg";
done;
for extra_arg in "${EXTRA_ARGS[@]}";
do
key="${extra_arg%%=*}";
arg_map["$key"]="$extra_arg";
done;
if [[ -v arg_map["--gres"] ]]; then
local gres_value="${arg_map["--gres"]}";
if [[ "$gres_value" == "--gres" || "$gres_value" == "--gres=" ]]; then
unset arg_map["--gres"];
fi;
fi;
local final_args=();
for key in "${!arg_map[@]}";
do
final_args+=("${arg_map[$key]}");
done;
local remote_command="";
if (( DO_NOTIFY == 1 )); then
remote_command="push_to_mobile 'chpc-notification' 'Interactive Queue Ready' & ";
fi;
remote_command+="exec bash";
if [ -z "${DEBUG+x}" ]; then
salloc "${final_args[@]}" srun --pty /bin/bash -l -c "${remote_command}";
else
echo "salloc ${final_args[@]} srun --pty /bin/bash -l -c \"${remote_command}\"";
fi
}To use the interactive() function, call it with the required three arguments:
interactive <account> <partition> <time><account>: Your account or project ID.<partition>: The partition (queue) to submit the job to.<time>: The requested walltime in the formatHH:MM:SS.
Example:
interactive my_account main_partition 01:00:00This will allocate an interactive session with the default resource settings:
- 1 node
- 1 task per node
- 4 CPUs per task
- 64G of memory
- 1 GPU of any available
You can override the default arguments by adding additional parameters after the first three. You must separate the parameters from the first 3 with a --. The new parameters should also start with --.
interactive <account> <partition> <time> -- [--nodes=<num_nodes>] [--ntasks-per-node=<num_tasks>] [--cpus-per-task=<num_cpus>] [--mem=<memory>] [--gres=<resource:type:count>]Example:
interactive my_account main_partition 01:00:00 -- --nodes=2 --mem=128GThis will allocate an interactive session with:
- 2 nodes
- 128G of memory per node
- Other default settings remain unchanged.
To see the generated salloc command without executing it, set the DEBUG variable:
DEBUG=true interactive my_account main_partition 01:00:00This will print the command to the console instead of executing it.
- The function merges default arguments with any additional arguments you provide.
- If you specify an argument that is already in the defaults (e.g.,
--nodes), your custom value will override the default. - The function assumes you have
sallocin your PATH. (If running on the CHPC you do)
This function is particularly useful for quickly submitting interactive jobs to a scheduler while allowing flexibility for custom resource requests.