bscpkgs/garlic/stages/sbatch.nix

104 lines
2.5 KiB
Nix
Raw Normal View History

2020-08-01 00:47:33 +08:00
{
stdenv
2020-08-04 17:51:09 +08:00
, numactl
2020-10-05 18:33:44 +08:00
, slurm
2020-10-09 21:55:37 +08:00
, garlicTools
2020-08-01 00:47:33 +08:00
}:
{
2020-10-09 21:55:37 +08:00
nextStage
2020-09-02 23:07:09 +08:00
, jobName
2020-10-13 19:00:59 +08:00
, chdir ? "."
, nixPrefix ? ""
2020-08-01 00:47:33 +08:00
, binary ? "/bin/run"
, ntasks ? null
2020-08-19 00:28:30 +08:00
, ntasksPerNode ? null
2020-09-21 23:30:24 +08:00
, ntasksPerSocket ? null
, cpusPerTask ? null
2020-08-18 00:50:18 +08:00
, nodes ? null
2020-08-01 00:47:33 +08:00
, exclusive ? true # By default we run in exclusive mode
, qos ? null
2020-08-25 00:07:09 +08:00
, reservation ? null
2020-08-01 00:47:33 +08:00
, time ? null
2020-09-23 00:38:37 +08:00
, output ? "stdout.log"
, error ? "stderr.log"
2020-08-01 00:47:33 +08:00
, extra ? null
2020-09-03 22:19:19 +08:00
, acctgFreq ? null
2020-08-01 00:47:33 +08:00
}:
with stdenv.lib;
2020-10-09 21:55:37 +08:00
with garlicTools;
# sbatch fails silently if we pass garbage, so we assert the types here to avoid
# sending `nodes = [ 1 2 ]` by mistake.
assert (jobName != null) -> isString jobName;
assert (chdir != null) -> isString chdir;
assert (nixPrefix != null) -> isString nixPrefix;
assert (ntasks != null) -> isInt ntasks;
assert (ntasksPerNode != null) -> isInt ntasksPerNode;
assert (ntasksPerSocket != null) -> isInt ntasksPerSocket;
assert (cpusPerTask != null) -> isInt cpusPerTask;
assert (nodes != null) -> isInt nodes;
assert (exclusive != null) -> isBool exclusive;
assert (qos != null) -> isString qos;
assert (reservation != null) -> isString reservation;
assert (time != null) -> isString time;
assert (output != null) -> isString output;
assert (error != null) -> isString error;
assert (extra != null) -> isString extra;
2020-08-01 00:47:33 +08:00
let
sbatchOpt = name: value: optionalString (value!=null)
"#SBATCH --${name}=${toString value}\n";
2020-08-01 00:47:33 +08:00
sbatchEnable = name: value: optionalString (value!=null)
"#SBATCH --${name}\n";
in
stdenv.mkDerivation rec {
2020-09-02 23:07:09 +08:00
name = "sbatch";
2020-08-01 00:47:33 +08:00
preferLocalBuild = true;
2020-09-02 16:44:13 +08:00
phases = [ "installPhase" ];
2020-08-01 00:47:33 +08:00
#SBATCH --tasks-per-node=48
#SBATCH --ntasks-per-socket=24
#SBATCH --cpus-per-task=1
dontBuild = true;
dontPatchShebangs = true;
2020-10-13 19:00:59 +08:00
programPath = "/run";
2020-08-01 00:47:33 +08:00
installPhase = ''
2020-08-05 00:38:33 +08:00
mkdir -p $out
cat > $out/job <<EOF
2020-10-14 22:29:22 +08:00
#!/bin/sh -e
2020-09-02 23:07:09 +08:00
#SBATCH --job-name="${jobName}"
2020-08-01 00:47:33 +08:00
''
+ sbatchOpt "ntasks" ntasks
2020-08-19 00:28:30 +08:00
+ sbatchOpt "ntasks-per-node" ntasksPerNode
2020-09-21 23:30:24 +08:00
+ sbatchOpt "ntasks-per-socket" ntasksPerSocket
+ sbatchOpt "cpus-per-task" cpusPerTask
2020-08-18 00:50:18 +08:00
+ sbatchOpt "nodes" nodes
2020-10-13 19:00:59 +08:00
+ sbatchOpt "chdir" chdir
2020-08-01 00:47:33 +08:00
+ sbatchOpt "output" output
+ sbatchOpt "error" error
+ sbatchEnable "exclusive" exclusive
+ sbatchOpt "time" time
+ sbatchOpt "qos" qos
2020-08-25 00:07:09 +08:00
+ sbatchOpt "reservation" reservation
2020-09-03 22:19:19 +08:00
+ sbatchOpt "acctg-freq" acctgFreq
2020-08-01 00:47:33 +08:00
+ optionalString (extra!=null) extra
+
''
2020-10-09 21:55:37 +08:00
exec ${nixPrefix}${stageProgram nextStage}
2020-08-01 00:47:33 +08:00
EOF
2020-10-13 19:00:59 +08:00
cat > $out/run <<EOF
2020-10-14 22:29:22 +08:00
#!/bin/sh -e
2020-10-05 18:33:44 +08:00
${slurm}/bin/sbatch ${nixPrefix}$out/job
EOF
2020-10-13 19:00:59 +08:00
chmod +x $out/run
2020-08-01 00:47:33 +08:00
'';
}