#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Test of task distribution support on multi-core systems.
############################################################################
# Copyright (C) 2005-2007 The Regents of the University of California.
# Copyright (C) 2008-2010 Lawrence Livermore National Security.
# Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
# Written by Morris Jette <jette1@llnl.gov>
# CODE-OCEC-09-009. All rights reserved.
#
# 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.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA.
############################################################################
source ./globals

set file_bash   "$test_dir/script"
set job_id      0

if {[get_config_param "FrontendName"] ne "MISSING"} {
        skip "This test is incompatible with front-end systems"
}

# find out if we have enough nodes to test functionality
set node_count [get_partition_param [default_partition] "TotalNodes"]
if { $node_count < 2 } {
	skip "Insufficient nodes in default partition ($node_count < 2)"
}

proc cleanup {} {
	global test_status STATUS_FAIL

	if {$test_status == $STATUS_FAIL} {
		log_warn "This test can fail if the node configuration in slurm.conf (sockets, cores, threads) differs from the actual configuration"
	}
}

make_bash_script $file_bash {
echo nodeid:$SLURM_NODEID taskid:$SLURM_PROCID localid:$SLURM_LOCALID
exit 0
}

#
# Create an allocation
#
set timeout $max_job_delay
spawn $salloc -N2 --ntasks-per-node=2 --verbose -t2 $bin_bash
expect {
	-re "salloc: Granted job allocation ($number)" {
		reset_bash_prompt
		set job_id $expect_out(1,string)
		exp_continue
	}
	-re "(configuration is not available|Unable to submit batch job|Node count specification invalid|More processors requested than permitted)" {
		skip "Can't test srun task distribution"
	}
	-re $test_prompt {
		send "$srun -l -c1 $file_bash | $bin_sort -V\r"
	}
	timeout {
		fail "salloc not responding"
	}
}
if {$job_id == 0} {
	fail "salloc failure"
}


#############################################################################
#
# Run a job step to get allocated processor count
#
set mask 0
set task_cnt 0
set prev_node -1
set node_cnt 0
expect {
	-re "nodeid:($number) taskid:($number)" {
		set this_node $expect_out(1,string)
		set this_tid  $expect_out(2,string)
		incr task_cnt 1
		if {$this_node != $prev_node} {
			incr node_cnt 1
			set prev_node $this_node
		}
		exp_continue
	}
	-re "error" {
		fail "Some error occurred"
	}
	timeout {
		fail "salloc not responding or failure to recognize prompt"
	}
	-re $test_prompt
}

if {$node_cnt != 2} {
	skip "Need 2 nodes to perform test"
}

if {$task_cnt < (2 * $node_cnt)} {
    skip "Need at least 2 CPUs per node, test is not applicable"
}

#############################################################################
#
# Run a job step with block distribution
#
set this_cnt  0
set prev_node -1
set this_node -1
send "$srun -l -n $task_cnt -m block $file_bash | $bin_sort -V\r"
expect {
	-re "nodeid:($number) taskid:($number) localid:($number)" {
		set this_node $expect_out(1,string)
		set this_tid  $expect_out(2,string)
		set this_lid  $expect_out(3,string)
		incr this_cnt 1
		if {$prev_node != $this_node} {
			subtest {$prev_node <= $this_node} "Correct distribution for a job step with block distribution" "$prev_node > $this_node"
			set prev_node $this_node
			set prev_cnt 1
		} else {
			incr prev_cnt 1
		}
		exp_continue
	}
	-re "error" {
		fail "Some error occurred"
	}
	timeout {
		fail "srun not responding or failure to recognize prompt"
	}
	-re $test_prompt
}
subtest {$prev_node <= $this_node} "Correct final distribution or a job step with block distribution" "$prev_node > $this_node"
subtest {$this_cnt == $task_cnt} "Task count is consistent or a job step with block distribution" "$this_cnt != $task_cnt"

#############################################################################
#
# Run a job step with cyclic distribution
#
set block_size 1
set this_cnt  0
set prev_node -1
set this_node -1
set prev_cnt  $block_size
send "$srun -l -n $task_cnt -m cyclic $file_bash | $bin_sort -V\r"
expect {
	-re "nodeid:($number) taskid:($number) localid:($number)" {
		set this_node $expect_out(1,string)
		set this_tid  $expect_out(2,string)
		set this_lid  $expect_out(3,string)
		incr this_cnt 1
		if {$prev_node != $this_node} {
			subtest {$prev_cnt == $block_size } "Correct distribution for a job step with cyclic distribution" "$prev_node != $this_node; $prev_cnt != $block_size"
			set prev_node $this_node
			set prev_cnt 1
		} else {
			incr prev_cnt 1
		}
		exp_continue
	}
	-re "error" {
		fail "Some error occurred"
	}
	timeout {
		fail "srun not responding or failure to recognize prompt"
	}
	-re $test_prompt
}
subtest {$prev_cnt == $block_size} "Correct final distribution for a job step with cyclic distribution" "$prev_cnt != $block_size"
subtest {$this_cnt == $task_cnt} "Task count is consistent for a job step with cyclic distribution" "$this_cnt != $task_cnt"

#############################################################################
#
# Run a job step with plane distribution
#
set block_size 2
set this_cnt  0
set prev_node -1
set this_node -1
set prev_cnt  $block_size
send "$srun -l -n $task_cnt -m plane=$block_size $file_bash | $bin_sort -V\r"
expect {
	-re "nodeid:($number) taskid:($number) localid:($number)" {
		set this_node $expect_out(1,string)
		set this_tid  $expect_out(2,string)
		set this_lid  $expect_out(3,string)
		incr this_cnt 1
		if {$prev_node == $this_node} {
			incr prev_cnt 1
		} else {
			subtest {$prev_node != -1 || $prev_cnt <= $block_size } "Correct distribution for a job step with plane distribution" "nodeid:$prev_node task_cnt:$prev_cnt"
			set prev_node $this_node
			set prev_cnt 1
		}
		exp_continue
	}
	-re "error" {
		fail "Some error occurred"
	}
	timeout {
		fail "srun not responding or failure to recognize prompt"
	}
	-re $test_prompt
}
subtest {$prev_cnt <= $block_size} "Correct final distribution for a job step with plane distribution" "nodeid:$prev_node task_cnt:$prev_cnt"
subtest {$this_cnt == $task_cnt} "Task count is consistent for a job step with plane distribution" "$this_cnt != $task_cnt"

#############################################################################
#
# Terminate the job, free the allocation
#
send "exit\r"
expect {
	-re "error" {
		fail "Some error occurred"
	}
	timeout {
		fail "salloc not responding or failure to recognize prompt"
	}
	eof {
		wait
	}
}
