| 1 | #!/usr/bin/env perl | ||||||||||||||||||||||||||||
| 2 | #ABSTRACT: Start an interactive session on the cluster | ||||||||||||||||||||||||||||
| 3 | #PODNAME: session | ||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use v5.16; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use Getopt::Long; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use File::Basename; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use Carp qw(confess); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use Term::ANSIColor qw(:constants); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use FindBin qw($RealBin); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | if (-e "$RealBin/../dist.ini") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | say STDERR "[dev mode] Using local lib" if ($ENV{"DEBUG"}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use lib "$RealBin/../lib"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | } | ||||||||||||||||||||||||||||
| 14 | 2 | ▶ | use NBI::Slurm; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 1 | my $BIN = basename($0); | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 1 | ▶ | main() unless caller; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 18 | 1; | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 1 | ▶ | sub main { | ||||||||||||||||||||||||||
|
Subroutine
main: called CC=15 SCAR=44.6
| |||||||||||||||||||||||||||||
| 21 | 1 | my $USER = $ENV{USER}; | |||||||||||||||||||||||||||
| 22 | 1 | my $config = NBI::Slurm::load_config("$USER/.nbislurm.config"); | |||||||||||||||||||||||||||
| 23 | 1 | ▶ | my $DEFAULT_SESSION = $config->{'session'} // ""; | ||||||||||||||||||||||||||
|
Condition50%1/2
$config->{"session"} // ""
MC/DC0%0/1
$config->{"session"} // ""
$config->{"session"}
Truth table50%1/2
A
A= $config->{"session"}
| |||||||||||||||||||||||||||||
| 24 | 1 | ▶ | my $SPECIAL_SESSION = $config->{'special_session'} // ""; | ||||||||||||||||||||||||||
|
Condition50%1/2
$config->{"special_session"} // ""
MC/DC0%0/1
$config->{"special_session"} // ""
$config->{"special_session"}
Truth table50%1/2
A
A= $config->{"special_session"}
| |||||||||||||||||||||||||||||
| 25 | 1 | ▶ | my $DEFAULT_HOURS = $config->{'session_hours'} // 4; | ||||||||||||||||||||||||||
|
Condition50%1/2
$config->{"session_hours"} // 4
MC/DC0%0/1
$config->{"session_hours"} // 4
$config->{"session_hours"}
Truth table50%1/2
A
A= $config->{"session_hours"}
| |||||||||||||||||||||||||||||
| 26 | 1 | my $NAME = "session"; | |||||||||||||||||||||||||||
| 27 | 1 | ▶ | my $MEM_MB = $config->{'session_memory'} // 1000; | ||||||||||||||||||||||||||
|
Condition50%1/2
$config->{"session_memory"} // 1000
MC/DC0%0/1
$config->{"session_memory"} // 1000
$config->{"session_memory"}
Truth table50%1/2
A
A= $config->{"session_memory"}
| |||||||||||||||||||||||||||||
| 28 | 1 | ▶ | my $CORES = $config->{'session_cpus'} // 1; | ||||||||||||||||||||||||||
|
Condition50%1/2
$config->{"session_cpus"} // 1
MC/DC0%0/1
$config->{"session_cpus"} // 1
$config->{"session_cpus"}
Truth table50%1/2
A
A= $config->{"session_cpus"}
| |||||||||||||||||||||||||||||
| 29 | 1 | my $DAYS = 0; | |||||||||||||||||||||||||||
| 30 | 1 | my $OPT_HOURS; | |||||||||||||||||||||||||||
| 31 | my $OPT_TIME; | ||||||||||||||||||||||||||||
| 32 | 1 | my $OPT_INTEL; | |||||||||||||||||||||||||||
| 33 | 1 | my $OPT_PARTITION; | |||||||||||||||||||||||||||
| 34 | 1 | my $OPT_SPECIAL; | |||||||||||||||||||||||||||
| 35 | 1 | my $OPT_VERBOSE; | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | GetOptions( | ||||||||||||||||||||||||||||
| 38 | 'c|threads=i' => \$CORES, | ||||||||||||||||||||||||||||
| 39 | 'm|mem=s' => \$MEM_MB, | ||||||||||||||||||||||||||||
| 40 | 'd|days=i' => \$DAYS, | ||||||||||||||||||||||||||||
| 41 | 'h|hours=i' => \$OPT_HOURS, | ||||||||||||||||||||||||||||
| 42 | 'T|t|time=s' => \$OPT_TIME, | ||||||||||||||||||||||||||||
| 43 | 'n|name=s' => \$NAME, | ||||||||||||||||||||||||||||
| 44 | 'q|queue=s' => \$OPT_PARTITION, | ||||||||||||||||||||||||||||
| 45 | 's|special' => \$OPT_SPECIAL, | ||||||||||||||||||||||||||||
| 46 | 'i|intel' => \$OPT_INTEL, | ||||||||||||||||||||||||||||
| 47 | 'verbose' => \$OPT_VERBOSE, | ||||||||||||||||||||||||||||
| 48 | 1 | ▶ | 'version' => sub { say "session v", $NBI::Slurm::VERSION; exit(0); }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 49 | 0 | ▶ | 'help' => sub { usage(); exit(0); }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 50 | 1 | ▶ | ) or usage(1); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | # Get memory | ||||||||||||||||||||||||||||
| 53 | 0 | $MEM_MB = parse_memory($MEM_MB); | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 0 | ▶ | if ($MEM_MB < 350) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 0 | say STDERR RED, "[WARNING]", RESET, "Memory set to $MEM_MB Mb, automatically setting to $MEM_MB Gb", RESET; | |||||||||||||||||||||||||||
| 57 | 0 | $MEM_MB *= 1000; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 0 | my $time = session_resolve_time( | |||||||||||||||||||||||||||
| 61 | hours => $OPT_HOURS, | ||||||||||||||||||||||||||||
| 62 | time_string => $OPT_TIME, | ||||||||||||||||||||||||||||
| 63 | default_hours => $DEFAULT_HOURS, | ||||||||||||||||||||||||||||
| 64 | days => $DAYS, | ||||||||||||||||||||||||||||
| 65 | ); | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | # Get queue | ||||||||||||||||||||||||||||
| 68 | 0 | ▶ | if (not $OPT_PARTITION) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 0 | ▶ | if ($config->{'queue'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 0 | $OPT_PARTITION = $config->{'queue'}; | |||||||||||||||||||||||||||
| 71 | } else { | ||||||||||||||||||||||||||||
| 72 | 0 | $OPT_PARTITION = partition_tag($time->{total_hours}); | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 0 | my $PARAMS = $DEFAULT_SESSION; | |||||||||||||||||||||||||||
| 77 | 0 | ▶ | $PARAMS .= $OPT_INTEL ? " --constraint=intel " : ""; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 0 | ▶ | $PARAMS .= $OPT_SPECIAL ? " $SPECIAL_SESSION " : ""; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 0 | ▶ | my $PARTITION //= $OPT_PARTITION; | ||||||||||||||||||||||||||
|
Condition0%0/3
my $PARTITION //= $OPT_PARTITION
MC/DC0%0/2
my $PARTITION //= $OPT_PARTITION
my $PARTITION
$OPT_PARTITION
Truth table0%0/3
A //= B
A= my $PARTITIONB= $OPT_PARTITION
| |||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 0 | say STDERR "-" x 40; | |||||||||||||||||||||||||||
| 83 | print STDERR YELLOW, | ||||||||||||||||||||||||||||
| 84 | "job name ", RESET, $NAME, "\n", | ||||||||||||||||||||||||||||
| 85 | YELLOW, "memory ", RESET, $MEM_MB, " Mb\n", | ||||||||||||||||||||||||||||
| 86 | YELLOW, "cores ", RESET, $CORES, "\n", | ||||||||||||||||||||||||||||
| 87 | 0 | YELLOW, "time ", RESET, $time->{slurm}, "\n", | |||||||||||||||||||||||||||
| 88 | YELLOW, "partition ", RESET, $PARTITION, "\n"; | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 0 | say STDERR "-" x 40; | |||||||||||||||||||||||||||
| 91 | 0 | my $cmd = qq(srun --job-name=$NAME --mem=$MEM_MB -c $CORES --time=$time->{slurm} -n 1 --disable-status -s --pty --partition=$PARTITION $PARAMS -u bash -li); | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 0 | ▶ | say STDERR $cmd if ($OPT_VERBOSE); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 0 | ▶ | if (has_srun() == 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 0 | say STDERR RED, "[ERROR] srun not found... Are you on the cluster?", RESET; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | 0 | exec($cmd); | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 0 | ▶ | sub partition_tag { | ||||||||||||||||||||||||||
|
Subroutine
partition_tag: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 102 | 0 | my $hours = shift; | |||||||||||||||||||||||||||
| 103 | 0 | ▶ | if ($hours <= 2) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 104 | 0 | return "qib-short,nbi-short"; | |||||||||||||||||||||||||||
| 105 | } elsif ($hours <= 8) { | ||||||||||||||||||||||||||||
| 106 | 0 | return "qib-medium,nbi-medium"; | |||||||||||||||||||||||||||
| 107 | } else { | ||||||||||||||||||||||||||||
| 108 | 0 | return "qib-long,nbi-long"; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 5 | ▶ | sub session_resolve_time { | ||||||||||||||||||||||||||
|
Subroutine
session_resolve_time: called CC=6 SCAR=18.0
| |||||||||||||||||||||||||||||
| 113 | 5 | my %args = @_; | |||||||||||||||||||||||||||
| 114 | confess "ERROR: --hours and --time are mutually exclusive\n" | ||||||||||||||||||||||||||||
| 115 | 5 | ▶ | if defined $args{hours} && defined $args{time_string}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $args{"hours"} and defined $args{"time_string"}
MC/DC100%2/2
defined $args{"hours"} and defined $args{"time_string"}
defined $args{"hours"}
defined $args{"time_string"}
Truth table100%3/3
A and B
A= defined $args{"hours"}B= defined $args{"time_string"}
| |||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | my $hours = defined $args{time_string} | ||||||||||||||||||||||||||||
| 118 | ? NBI::Opts::_time_to_hour($args{time_string}) | ||||||||||||||||||||||||||||
| 119 | : defined $args{hours} | ||||||||||||||||||||||||||||
| 120 | ? $args{hours} | ||||||||||||||||||||||||||||
| 121 | 4 | ▶ | : $args{default_hours}; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 4 | ▶ | $hours += ($args{days} // 0) * 24; | ||||||||||||||||||||||||||
|
Condition50%1/2
$args{"days"} // 0
MC/DC0%0/1
$args{"days"} // 0
$args{"days"}
Truth table50%1/2
A
A= $args{"days"}
| |||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | return { | ||||||||||||||||||||||||||||
| 126 | 4 | total_hours => $hours, | |||||||||||||||||||||||||||
| 127 | slurm => session_hours_to_slurm($hours), | ||||||||||||||||||||||||||||
| 128 | }; | ||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 0 | ▶ | sub hours_to_days_hours { | ||||||||||||||||||||||||||
|
Subroutine
hours_to_days_hours: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 132 | 0 | my $hours = shift; | |||||||||||||||||||||||||||
| 133 | 0 | my $days = int($hours / 24); | |||||||||||||||||||||||||||
| 134 | 0 | my $hours = $hours % 24; | |||||||||||||||||||||||||||
| 135 | 0 | return ($days, $hours); | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 6 | ▶ | sub session_hours_to_slurm { | ||||||||||||||||||||||||||
|
Subroutine
session_hours_to_slurm: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 139 | 6 | my $hours = shift; | |||||||||||||||||||||||||||
| 140 | 6 | my $seconds = int($hours * 3600); | |||||||||||||||||||||||||||
| 141 | 6 | my $days = int($seconds / 86400); | |||||||||||||||||||||||||||
| 142 | 6 | $seconds %= 86400; | |||||||||||||||||||||||||||
| 143 | 6 | my $hh = int($seconds / 3600); | |||||||||||||||||||||||||||
| 144 | 6 | $seconds %= 3600; | |||||||||||||||||||||||||||
| 145 | 6 | my $mm = int($seconds / 60); | |||||||||||||||||||||||||||
| 146 | 6 | my $ss = $seconds % 60; | |||||||||||||||||||||||||||
| 147 | 6 | return sprintf("%d-%02d:%02d:%02d", $days, $hh, $mm, $ss); | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 0 | ▶ | sub parse_memory { | ||||||||||||||||||||||||||
|
Subroutine
parse_memory: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 152 | # Parse string return mb | ||||||||||||||||||||||||||||
| 153 | 0 | my $mem = shift; | |||||||||||||||||||||||||||
| 154 | 0 | my $mb = 0; | |||||||||||||||||||||||||||
| 155 | 0 | ▶ | if ($mem =~ /^(\d+)$/) { | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 156 | 0 | $mb = $1; | |||||||||||||||||||||||||||
| 157 | } elsif ($mem =~ /(\d+)\s?Gb?/i) { | ||||||||||||||||||||||||||||
| 158 | 0 | $mb = $1 * 1000; | |||||||||||||||||||||||||||
| 159 | } elsif ($mem =~ /(\d+)\s?Mb?/i) { | ||||||||||||||||||||||||||||
| 160 | 0 | $mb = $1; | |||||||||||||||||||||||||||
| 161 | } elsif ($mem =~ /(\d+)\s?Kb?/i) { | ||||||||||||||||||||||||||||
| 162 | 0 | $mb = int($1 / 1000); | |||||||||||||||||||||||||||
| 163 | } else { | ||||||||||||||||||||||||||||
| 164 | 0 | $mb = $mem; | |||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | 0 | return $mb; | |||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | 0 | ▶ | sub usage { | ||||||||||||||||||||||||||
|
Subroutine
usage: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 169 | 0 | print <<"EOF"; | |||||||||||||||||||||||||||
| 170 | Usage: $BIN [options] | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | Options: | ||||||||||||||||||||||||||||
| 173 | -c, --threads Number of CPU cores (default: 1) | ||||||||||||||||||||||||||||
| 174 | -m, --mem Memory size in MB (default: 1000) | ||||||||||||||||||||||||||||
| 175 | -h, --hours Number of hours (default: 4; mutually exclusive with --time) | ||||||||||||||||||||||||||||
| 176 | -t, --time Time string like 12h, 1d, 2h30m | ||||||||||||||||||||||||||||
| 177 | -n, --name Job name (default: session) | ||||||||||||||||||||||||||||
| 178 | -q, --queue Queue/partition name (default: determined by hours) | ||||||||||||||||||||||||||||
| 179 | -s, --special Enable the special parameter string from the config file (default: off) | ||||||||||||||||||||||||||||
| 180 | -i, --intel Use Intel constraint (default: off) | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | EOF | ||||||||||||||||||||||||||||
| 183 | 0 | ▶ | exit() if ($_[0]); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | 0 | ▶ | sub has_srun { | ||||||||||||||||||||||||||
|
Subroutine
has_srun: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 187 | # Check if srun is available | ||||||||||||||||||||||||||||
| 188 | 0 | my $srun = `srun --version 2> /dev/null`; | |||||||||||||||||||||||||||
| 189 | 0 | chomp $srun; | |||||||||||||||||||||||||||
| 190 | 0 | ▶ | if ($srun eq "") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 191 | 0 | return 0; | |||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||
| 193 | 0 | return 1; | |||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | __END__ | ||||||||||||||||||||||||||||