#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Ensure job requesting GPUs on multiple sockets gets CPUs on multiple
#          sockets
############################################################################
# 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 number_commas  "\[0-9_,\]+"

proc multi_socket_gres { gres_string } {
	set offset 0
	set multi_found 0
	set sock0_found 0
	set sock1_found 0
	set socks_found 0
	set gres_char "x"

	while {1} {
		set start [string first "gpu" $gres_string $offset]
		if {$start == -1} {
			return 0
		}
		set offset [expr $start + 1]
		set len [string length $gres_string]
		for {set char_inx [expr $start + 3]} {$char_inx < $len} {incr char_inx} {
			set gres_char [string index $gres_string $char_inx]
			if {[string match "," $gres_char]} {
				break
			}
			if {![string match "(" $gres_char]} {
				continue
			}
			incr char_inx
			if {$char_inx >= $len} {
				break
			}
			set gres_char [string index $gres_string $char_inx]
			if {![string match "S" $gres_char]} {
				break
			}
			incr char_inx
			if {$char_inx >= $len} {
				break
			}
			set gres_char [string index $gres_string $char_inx]
			if {![string match ":" $gres_char]} {
				break
			}
			for {incr char_inx} {$char_inx < $len} {incr char_inx} {
				set gres_char [string index $gres_string $char_inx]
				if {[string match ")" $gres_char]} {
					break
				}
				if {[string match "-" $gres_char] || [string match "," $gres_char]} {
					return 1
				}
				if {[string match "0" $gres_char]} {
					if {$sock0_found != 0} {
						incr socks_found
						set sock0_found 1
					}
				}
				if {[string match "1" $gres_char]} {
					if {$sock1_found != 0} {
						incr socks_found
						set sock1_found 1
					}
				}
			}
			if {$socks_found > 1} {
				return 1
			}
		}
	}
	return 0
}

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]

if {$sockets_per_node < 2} {
	skip "This test requires 2 or more sockets per node in the default partition"
}
if {$cpus_per_socket < 2} {
	skip "This test requires 2 or more cores per socket in the default partition"
}

log_debug "GPU count is $gpu_cnt"
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"

#
# Build input script file
#
make_bash_script $file_in "echo HOST:\$SLURMD_NODENAME CUDA_VISIBLE_DEVICES:\$CUDA_VISIBLE_DEVICES
$scontrol show node \$SLURM_JOB_NODENAME | grep Gres
$scontrol -dd show job \$SLURM_JOB_ID | grep CPU_IDs
exit 0"

#
# If $gpu_cnt > $cpus_per_node then assume there is no DefCpusPerGpu configured
#
set match 0
set multi_socket 0
spawn $srun --cpus-per-gpu=1 --gpus-per-node=$gpu_cnt --nodes=1 --ntasks=1 -t1 -l $file_in

expect {
	-re "CUDA_VISIBLE_DEVICES:($number_commas)" {
		incr match [cuda_count $expect_out(1,string)]
		exp_continue
	}
	-re "Gres=(.*)" {
		set multi_socket [multi_socket_gres $expect_out(1,string)]
		exp_continue
	}
	-re "CPU_IDs=($re_word_str)" {
		if {$multi_socket != 0 && [string first , $expect_out(1,string)] == -1} {
			log_warn "Allocated CPUs do not appear to span multiple sockets. This is fine if every GPU is bound to ALL sockets"
		}
		exp_continue
	}
	timeout {
		fail "srun not responding"
	}
	eof {
		wait
	}
}
if {$match != $gpu_cnt} {
	fail "srun --gpus-per-node failure ($match != $gpu_cnt)"
}
