#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Test --cpus-per-gpu option
############################################################################
# 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 exit_code   0
set file_in     "test$test_id.input"
set number_commas  "\[0-9_,\]+"
set timeout $max_job_delay

proc cleanup {} {
	global bin_rm file_in

	exec $bin_rm -f $file_in
}

proc run_gpu_per_job { cpus_per_gpu } {
	global exit_code file_in number srun test_id timeout

	set cpu_count 0
	set srun_pid [spawn $srun --gpus=1 --cpus-per-gpu=$cpus_per_gpu -J "test$test_id" -t1 ./$file_in]
	expect {
		-re "NumCPUs=($number)" {
			set cpu_count $expect_out(1,string)
			exp_continue
		}
		timeout {
			slow_kill $srun_pid
			fail "srun not responding"
		}
		eof {
			wait
		}
	}
#	Depending upon minimal allocation unit, job can be allocated extra CPUs
	if {$cpu_count < $cpus_per_gpu} {
		log_error "srun --cpus-per-gpu failure ($cpu_count < $cpus_per_gpu)"
		set exit_code 1
	}
}

proc run_gpu_per_node { cpus_per_gpu } {
	global exit_code file_in number srun test_id timeout

	set cpu_count 0
	set srun_pid [spawn $srun --gpus-per-node=1 -N1 --cpus-per-gpu=$cpus_per_gpu -J "test$test_id" -t1 ./$file_in]
	expect {
		-re "NumCPUs=($number)" {
			set cpu_count $expect_out(1,string)
			exp_continue
		}
		timeout {
			slow_kill $srun_pid
			fail "srun not responding"
		}
		eof {
			wait
		}
	}
#	Depending upon minimal allocation unit, job can be allocated extra CPUs
	if {$cpu_count < $cpus_per_gpu} {
		log_error "srun --cpus-per-gpu failure ($cpu_count < $cpus_per_gpu)"
		set exit_code 1
	}
}

proc run_gpu_per_task { cpus_per_gpu } {
	global exit_code file_in number srun test_id timeout

	set cpu_count 0
	set srun_pid [spawn $srun --gpus-per-task=1 -n1 --cpus-per-gpu=$cpus_per_gpu -J "test$test_id" -t1 ./$file_in]
	expect {
		-re "NumCPUs=($number)" {
			set cpu_count $expect_out(1,string)
			exp_continue
		}
		timeout {
			slow_kill $srun_pid
			fail "srun not responding"
		}
		eof {
			wait
		}
	}
#	Depending upon minimal allocation unit, job can be allocated extra CPUs
	if {$cpu_count < $cpus_per_gpu} {
		log_error "srun --cpus-per-gpu failure ($cpu_count < $cpus_per_gpu)"
		set exit_code 1
	}
}

if {![check_config_select "cons_tres"]} {
    skip "This test is only compatible with select/cons_tres"
}

set gpu_cnt [get_highest_gres_count 1 "gpu"]
if {$gpu_cnt < 1} {
	skip "This test requires 1 or more GPUs on 1 node of the default partition"
}

set node_name [get_nodes_by_request "--gres=gpu:1 -n1 -t1"]
if { [llength $node_name] != 1 } {
	skip "This test need to be able to submit jobs with at least --gres=gpu:1"
}
set cpus_per_node    [get_node_param $node_name "CPUTot"]
set sockets_per_node [get_node_param $node_name "Sockets"]
set cpus_per_socket  [expr $cpus_per_node / $sockets_per_node]
set nb_nodes         [get_partition_param [default_partition] "TotalNodes"]

log_debug "GPU count is $gpu_cnt"
log_debug "Node count in default parition is $nb_nodes"
log_debug "Sockets per node is $sockets_per_node"
log_debug "CPUs per socket is $cpus_per_socket"
log_debug "CPUs per node is $cpus_per_node"

if {$cpus_per_node < 1} {
	skip "This test requires 1 or more CPUs per node in the default partition"
}

#
# Build input script file
#
exec $bin_rm -f $file_in
make_bash_script $file_in "echo HOST:\$SLURMD_NODENAME CUDA_VISIBLE_DEVICES:\$CUDA_VISIBLE_DEVICES
$scontrol show job \$SLURM_JOB_ID
exit 0"

#
# Run test job with global GPU count
# Double cpus_per_gpu value on each iteration
#
for {set inx 1} {$inx <= $cpus_per_node} {set inx [expr $inx * 2]} {
	run_gpu_per_job $inx
	if {$exit_code != 0} {
		break
	}
}

#
# Run test job with gpus-per-node count
# Double cpus_per_gpu value on each iteration
#
for {set inx 1} {$inx <= $cpus_per_node} {set inx [expr $inx * 2]} {
	run_gpu_per_node $inx
	if {$exit_code != 0} {
		break
	}
}

#
# Run test job with gpus-per-task count
# Double cpus_per_gpu value on each iteration
#
for {set inx 1} {$inx <= $cpus_per_node} {set inx [expr $inx * 2]} {
	run_gpu_per_task $inx
	if {$exit_code != 0} {
		break
	}
}

#
# Run test with --gpus=2 and cpus_per_gpu value that pushed job to 2 nodes
#
if {$gpu_cnt > 1 && $nb_nodes > 1} {
	set cpu_count 0
	set node_count 0
	set srun_pid [spawn $srun --gpus=2 --cpus-per-gpu=$cpus_per_node -J "test$test_id" -t1 ./$file_in]
	expect {
		-re "NumNodes=($number)" {
			set node_count $expect_out(1,string)
			exp_continue
		}
		-re "NumCPUs=($number)" {
			set cpu_count $expect_out(1,string)
			exp_continue
		}
		timeout {
			slow_kill $srun_pid
			fail "srun not responding"
		}
		eof {
			wait
		}
	}
#	Depending upon minimal allocation unit, job can be allocated extra CPUs
	set cpu_target [expr $cpus_per_node * 2]
	if {$cpu_count < $cpu_target} {
		fail "srun --cpus-per-gpu failure, bad CPU count ($cpu_count < $cpu_target)"
	}
	set node_target 2
	if {$node_count < $node_target} {
		fail "srun --cpus-per-gpu failure, bad node count ($node_count < $node_target)"
	}
}

if {$exit_code != 0} {
	fail "Test failed due to previous errors (\$exit_code = $exit_code)"
}
