#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Test full set of sbatch --gpu options and scontrol show job.
############################################################################
# Copyright (C) SchedMD LLC.
#
# 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 file_in     "$test_dir/input"
set file_out    "$test_dir/output"
set job_id      0

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

set nb_nodes [get_partition_param [default_partition] "TotalNodes"]
if {$nb_nodes > 1} {
	set nb_nodes 2
}
set gpu_cnt [get_highest_gres_count $nb_nodes "gpu"]
if {$gpu_cnt < 1} {
	skip "This test requires 1 or more GPUs on $nb_nodes nodes 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"
}

proc cleanup {} {
	global job_id

	cancel_job $job_id
}

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]

log_debug "GPUs per node count is $gpu_cnt"
log_debug "Sockets per node is $sockets_per_node"
log_debug "CPUs per socket is $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
#
make_bash_script $file_in "$scontrol show job \$SLURM_JOB_ID"

#
# Spawn a batch job with various --gpu options
#
set job_id [submit_job -fail "--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"]

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

#
# Spawn a batch job with various --gpu options
#
set gpus_per_socket 1
set sockets_per_node 1
set job_id [submit_job -fail "--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"]

set match 0
spawn $scontrol show job $job_id
expect {
	-re "TresPerSocket=gres/gpu:$gpus_per_socket" {
		incr match
		exp_continue
	}
	timeout {
		fail "sbatch not responding"
	}
	eof {
		wait
	}
}
if {$match != 1} {
	fail "sbatch gpu options not fully processed ($match != 8)"
}
