#!/usr/bin/env expect
############################################################################
# Purpose: Test of SLURM functionality
#          Test allocating sub-sets of GRES to job steps
#
# Output:  "TEST: #.#" followed by "SUCCESS" if test was successful, OR
#          "FAILURE: ..." otherwise with an explanation of the failure, OR
#          anything else indicates a failure mode that must be investigated.
############################################################################
# Copyright (C) 2018 SchedMD LLC
# Written by Morris Jette
#
# This file is part of SLURM, a resource management program.
# For details, see <https://slurm.schedmd.com/>.
# Please also read the included file: DISCLAIMER.
#
# SLURM is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# SLURM is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along
# with SLURM; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
############################################################################
source ./globals

set test_id        "39.17"
set exit_code      0
set file_in1       "test$test_id.input1"
set file_in2       "test$test_id.input2"
set file_out       "test$test_id.output"
set number_commas  "\[0-9_,\]+"

proc get_node_config {} {
	global cpus_per_socket sockets_per_node test_id
	global exit_code file_in1 max_job_delay number srun scontrol

	set cpus_tot 1
	set sockets_per_node 1
	set cpus_per_socket 1
	make_bash_script $file_in1 "$scontrol show node \$SLURM_JOB_NODENAME"
	set timeout $max_job_delay
	log_user 0
	set srun_pid [spawn $srun -n1 -t1 -J "test$test_id" $file_in1]
	expect {
		-re "CPUTot=($number)" {
			set cpus_tot $expect_out(1,string)
			exp_continue
		}
		-re "Sockets=($number)" {
			set sockets_per_node $expect_out(1,string)
			exp_continue
		}
		timeout {
			send_user "\nFAILURE: srun not responding\n"
			slow_kill $srun_pid
			set exit_code 1
		}
		eof {
			wait
		}
	}
	log_user 1
	set cpus_per_socket [expr $cpus_tot / $sockets_per_node]
}

print_header $test_id

set select_type [test_select_type]
if {![string compare $select_type "cons_tres"] ||
    (![string compare $select_type "cray"] && [test_select_type_params "other_cons_tres"])} {
	send_user "\nValid configuration, using select/cons_tres\n"
} else {
	send_user "\nWARNING: This test is only compatible with select/cons_tres\n"
	exit 0
}
if {[test_front_end]} {
	send_user "\nWARNING: This test is incompatible with front-end systems\n"
	exit $exit_code
}

set def_part_name [default_partition]
set nb_nodes [get_node_cnt_in_part $def_part_name]
if {$nb_nodes > 1} {
	set nb_nodes 2
}
set gpu_cnt [get_gpu_count $nb_nodes]
if {$gpu_cnt < 0} {
	send_user "\nFAILURE: Error getting GPU count\n"
	exit 1
}
if {$gpu_cnt < 2} {
	send_user "\nWARNING: This test requires 2 or more GPUs in the default partition\n"
	exit 0
}

get_node_config
set cpus_per_node [expr $sockets_per_node * $cpus_per_socket]

send_user "\nGPU count is $gpu_cnt\n"
send_user "Sockets per node is $sockets_per_node\n"
send_user "CPUs per socket is $cpus_per_socket\n"
send_user "CPUs per node is $cpus_per_node\n"

if {$cpus_per_node < 3} {
	send_user "\nWARNING: This test requires 3 or more CPUs per node in the default partition\n"
	exit 0
}

#
# Build input script file
#
make_bash_script $file_in1 "
	$scontrol -dd show job \${SLURM_JOBID}
	$srun -n1 --gpus-per-node=1 -n1 --mem=0 ./$file_in2 &
	$srun -n1 --gpus-per-node=1 -n1 --mem=0 ./$file_in2 &
	$srun -n1 --gpus-per-node=1 -n1 --mem=0 ./$file_in2 &
	wait
	exit 0"

make_bash_script $file_in2 "
	echo 'STEP_ID:'\$SLURM_STEP_ID 'CUDA_VISIBLE_DEVICES:'\$CUDA_VISIBLE_DEVICES
	sleep 3
	if \[ \$SLURM_STEP_ID -eq 3 ]; then
		$squeue -s
	fi
	exit 0"

#
# Test --gpus-per-node option by job step
#
send_user "\n\nTEST 1: --gpus-per-node option\n"
exec $bin_rm -f $file_out
set job_id 0
set timeout $max_job_delay
spawn $sbatch --gres=craynetwork:0 --cpus-per-gpu=1 --gpus-per-node=2 -N1 -n3 -t1 -o $file_out -J "test$test_id" ./$file_in1
expect {
	-re "Submitted batch job ($number)" {
		set job_id $expect_out(1,string)
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: sbatch not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}
if {$job_id == 0} {
	send_user "\nFAILURE: job not submitted\n"
	exit 1
}

if {[wait_for_job $job_id "DONE"] != 0} {
	send_user "\nFAILURE: job  did not complete\n"
	cancel_job $job_id
	exit 1
}

if {[wait_for_file $file_out] != 0} {
	send_user "\nFAILURE: no output file\n"
	exit 1
}
set match 0
spawn $bin_cat $file_out
expect {
	-re "STEP_ID:($number) CUDA_VISIBLE_DEVICES:($number)" {
		incr match
		exp_continue
	}
	eof {
		wait
	}
}
if {$match != 3} {
	send_user "\nFAILURE: bad CUDA information about job ($match != 3)\n"
	set exit_code 1
}

log_user 0
set match 0
spawn $bin_grep "step creation temporarily disabled" $file_out
expect {
	-re "step creation temporarily disabled" {
		incr match
		exp_continue
	}
	eof {
		wait
	}
}
log_user 1
if {$match != 1} {
	send_user "\nFAILURE: did not delay step for available GPU\n"
	set exit_code 1
}

if {$exit_code == 0} {
	exec $bin_rm -f $file_in1 $file_in2 $file_out
	send_user "\nSUCCESS\n"
}
exit $exit_code
