#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Test full set of sbatch --gpu options and scontrol show job.
#
# 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.1"
set exit_code   0
set file_in     "test$test_id.input"
set file_out    "test$test_id.output"
set job_id      0

print_header $test_id

if {[test_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
}

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 < 1} {
	send_user "\nWARNING: This test requires 1 or more GPUs per node in the default partition\n"
	exit 0
}
get_node_config
send_user "\nGPUs per node 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\n"

set cpus_per_node [expr $sockets_per_node * $cpus_per_socket]
if {$gpu_cnt > $cpus_per_node} {
	set gpu_cnt $cpus_per_node
}

set cpus_per_gpu 1
set gpu_bind "closest"
set gpu_freq "medium"
set tot_gpus $gpu_cnt
if {$nb_nodes > 1} {
	incr tot_gpus $gpu_cnt
}
set gpus_per_node $gpu_cnt
if {$gpus_per_node > 1 && [expr $gpus_per_node % 2] == 0 && ($sockets_per_node > 1 || $cpus_per_socket > 1)} {
	set ntasks [expr $nb_nodes * 2]
} else {
	set ntasks $nb_nodes
}
set gpus_per_task [expr $tot_gpus / $ntasks]
set mem_per_gpu 10

#
# Build input script file
#
exec $bin_rm -f $file_in $file_out
make_bash_script $file_in "$scontrol show job \$SLURM_JOB_ID"

#
# Spawn a batch job with various --gpu options
#
spawn $sbatch --output=/dev/null --cpus-per-gpu=$cpus_per_gpu --gpu-bind=$gpu_bind --gpu-freq=$gpu_freq --gpus=$tot_gpus --gpus-per-node=$gpus_per_node --gpus-per-task=$gpus_per_task --mem-per-gpu=$mem_per_gpu --nodes=$nb_nodes --ntasks=$ntasks -t1 ./$file_in
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: batch not submitted\n"
	exit 1
}

set match 0
spawn $scontrol show job $job_id
expect {
	-re "CpusPerTres=gpu:$cpus_per_gpu" {
		incr match
		exp_continue
	}
	-re "MemPerTres=gpu:$mem_per_gpu" {
		incr match
		exp_continue
	}
	-re "TresBind=gpu:$gpu_bind" {
		incr match
		exp_continue
	}
	-re "TresFreq=gpu:$gpu_freq" {
		incr match
		exp_continue
	}
	-re "TresPerJob=gpu:$tot_gpus" {
		incr match
		exp_continue
	}
	-re "TresPerNode=gpu:$gpus_per_node" {
		incr match
		exp_continue
	}
	-re "TresPerTask=gpu:$gpus_per_task" {
		incr match
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: sbatch not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}
if {$match != 7} {
	send_user "\nFAILURE: sbatch gpu options not fully processed ($match != 7)\n"
	set exit_code 1

}
cancel_job $job_id

#
# Spawn a batch job with various --gpu options
#
set job_id 0
set gpus_per_socket 1
set sockets_per_node 1
spawn $sbatch --output=/dev/null --cpus-per-gpu=$cpus_per_gpu --gpus-per-socket=$gpus_per_socket --sockets-per-node=$sockets_per_node --nodes=$nb_nodes --ntasks=$ntasks -t1 ./$file_in
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: batch not submitted\n"
	exec $bin_rm -f $file_in $file_out
	exit 1
}
set match 0
spawn $scontrol show job $job_id
expect {
	-re "TresPerSocket=gpu:$gpus_per_socket" {
		incr match
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: sbatch not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}
if {$match != 1} {
	send_user "\nFAILURE: sbatch gpu options not fully processed ($match != 8)\n"
	set exit_code 1

}
cancel_job $job_id

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