#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Test --mem-per-gpu option
#
# 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.10"
set exit_code   0
set file_in     "test$test_id.input"

proc run_gpu_per_job { mem_per_gpu } {
	global exit_code file_in number srun test_id

	set mem_size 0
	set srun_pid [spawn $srun --gpus=1 --mem-per-gpu=$mem_per_gpu -J "test$test_id" -t1 ./$file_in]
	expect {
		-re "TRES=cpu=($number),mem=($number)M" {
			set mem_size $expect_out(2,string)
			exp_continue
		}
		timeout {
			send_user "\nFAILURE: srun not responding\n"
			slow_kill $srun_pid
			set exit_code 1
		}
		eof {
			wait
		}
	}
	if {$mem_size != $mem_per_gpu} {
		send_user "\nFAILURE: srun --mem-per-gpu failure ($mem_size != $mem_per_gpu)\n\n"
		set exit_code 1
	}
}

proc run_gpu_per_node { mem_per_gpu } {
	global exit_code file_in number srun test_id

	set mem_size 0
	set srun_pid [spawn $srun --gpus-per-node=1 -N1 --mem-per-gpu=$mem_per_gpu -J "test$test_id" -t1 ./$file_in]
	expect {
		-re "TRES=cpu=($number),mem=($number)M" {
			set mem_size $expect_out(2,string)
			exp_continue
		}
		timeout {
			send_user "\nFAILURE: srun not responding\n"
			slow_kill $srun_pid
			set exit_code 1
		}
		eof {
			wait
		}
	}
	if {$mem_size != $mem_per_gpu} {
		send_user "\nFAILURE: srun --mem-per-gpu failure ($mem_size != $mem_per_gpu)\n\n"
		set exit_code 1
	}
}

proc run_gpu_per_task { mem_per_gpu gpu_cnt } {
	global exit_code file_in number srun test_id

	set mem_size 0
	set srun_pid [spawn $srun --gpus-per-task=$gpu_cnt -n1 --mem-per-gpu=$mem_per_gpu -J "test$test_id" -t1 ./$file_in]
	expect {
		-re "TRES=cpu=($number),mem=($number)M" {
			set mem_size $expect_out(2,string)
			exp_continue
		}
		timeout {
			send_user "\nFAILURE: srun not responding\n"
			slow_kill $srun_pid
			set exit_code 1
		}
		eof {
			wait
		}
	}
	set mem_target [expr $mem_per_gpu * $gpu_cnt]
	if {$mem_size != $mem_target} {
		send_user "\nFAILURE: srun --mem-per-gpu failure ($mem_size != $mem_target)\n\n"
		set exit_code 1
	}
}

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
}
if {![test_select_type_params "MEMORY"]} {
        send_user "\nWARNING: This test requires memory allocation management\n"
        exit 0
}

set gpu_cnt [get_gpu_count 1]
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 in the default partition\n"
	exit 0
}
get_node_config
set def_part_name [default_partition]
set nb_nodes [get_node_cnt_in_part $def_part_name]
send_user "\nGPU count is $gpu_cnt\n"
send_user "Memory Size is $mem_size\n"
send_user "Node count in partition default parition $def_part_name is $nb_nodes\n\n"

#
# 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
# Increase mem_per_gpu value 10x on each iteration
#
for {set inx 12} {$inx <= $mem_size} {set inx [expr $inx * 10]} {
	run_gpu_per_job $inx
	if {$exit_code != 0} {
		break
	}
}

#
# Run test job with gpus-per-node count
# Increase mem_per_gpu value 10x on each iteration
#
for {set inx 12} {$inx <= $mem_size} {set inx [expr $inx * 10]} {
	run_gpu_per_node $inx
	if {$exit_code != 0} {
		break
	}
}

#
# Run test job with gpus-per-task count and one GPU
# Increase mem_per_gpu value 10x on each iteration
#
for {set inx 12} {$inx <= $mem_size} {set inx [expr $inx * 10]} {
	run_gpu_per_task $inx 1
	if {$exit_code != 0} {
		break
	}
}

#
# Run test job with gpus-per-task count and two GPUs
# Increase mem_per_gpu value 10x on each iteration
#
if {$gpu_cnt > 1} {
	for {set inx 13} {$inx <= $mem_size} {set inx [expr $inx * 10]} {
		run_gpu_per_task $inx 2
		if {$exit_code != 0} {
			break
		}
	}
}

#
# Test heterogeneous GPU allocation (2 GPUs on one node, 1 GPU on another node)
#
if {$gpu_cnt > 1 && $nb_nodes > 1} {
	set mem_alloc 0
	set gpu_target 3
	set mem_spec 13
	set node_count 0
	set srun_pid [spawn $srun --gpus=$gpu_target -N2 --mem-per-gpu=$mem_spec -J "test$test_id" -t1 ./$file_in]
	expect {
		-re "NumNodes=($number)" {
			set node_count $expect_out(1,string)
			exp_continue
		}
		-re "TRES=cpu=($number),mem=($number)M" {
			set mem_alloc $expect_out(2,string)
			exp_continue
		}
		timeout {
			send_user "\nFAILURE: srun not responding\n"
			slow_kill $srun_pid
			set exit_code 1
		}
		eof {
			wait
		}
	}
	set node_target 2
	if {$node_count < $node_target} {
		send_user "\nFAILURE: srun --mem-per-gpu failure, bad node count ($node_count < $node_target)\n\n"
		set exit_code 1
	}

	set mem_target [expr $mem_spec * $gpu_target]
	if {$mem_alloc != $mem_target} {
		send_user "\nFAILURE: srun --mem-per-gpu failure ($mem_alloc != $mem_target)\n"
		set exit_code 1
	}
}

#
# Run test with --gpus=2 and mem_per_gpu value that pushed job to 2 nodes
#
if {$gpu_cnt > 1 && $nb_nodes > 1} {
	set mem_alloc 0
	set node_count 0
	set mem_spec [expr $mem_size / 2 + 1]
	set srun_pid [spawn $srun --gpus=2 --mem-per-gpu=$mem_spec -J "test$test_id" -t1 ./$file_in]
	expect {
		-re "NumNodes=($number)" {
			set node_count $expect_out(1,string)
			exp_continue
		}
		-re "TRES=cpu=($number),mem=($number)M" {
			set mem_alloc $expect_out(2,string)
			exp_continue
		}
		timeout {
			send_user "\nFAILURE: srun not responding\n"
			slow_kill $srun_pid
			set exit_code 1
		}
		eof {
			wait
		}
	}

	set node_target 2
	if {$node_count < $node_target} {
		send_user "\nFAILURE: srun --mem-per-gpu failure, bad node count ($node_count < $node_target)\n\n"
		set exit_code 1
	}

	set mem_target [expr $mem_spec * $node_target]
	if {$mem_alloc != $mem_target} {
		send_user "\nFAILURE: srun --mem-per-gpu failure ($mem_alloc != $mem_target)\n"
		set exit_code 1
	}
}

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