#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Simple CUDA MPS test
############################################################################
# Copyright (C) 2019 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 file_out    "test$test_id.output"
set file_prog   "test$test_id.prog"
set job_id      0

proc cleanup {} {
	global bin_rm file_in file_out file_prog

	exec $bin_rm -f $file_in $file_out $file_prog
}

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

if { [llength [get_nodes_by_request "--gres=mps:100 -t1"]] == 0} {
	skip "This test requires being able to submit job with --gres=mps:100"
}

#
# Build input script file
#
exec $bin_rm -f $file_in $file_out $file_prog
make_bash_script $file_in "
env | grep CUDA_VISIBLE_DEVICES
env | grep CUDA_MPS_ACTIVE_THREAD_PERCENTAGE
unset CUDA_VISIBLE_DEVICES
ls $nvcc
$nvcc ${file_prog}.cu -g -o $file_prog
./$file_prog &
./$file_prog &
wait"

#
# Spawn a batch job to build and run CUDA job
#
spawn $sbatch --output=$file_out -N1 --gres=mps:10 -t1 ./$file_in
expect {
	-re "Submitted batch job ($number)" {
		set job_id $expect_out(1,string)
		exp_continue
	}
	timeout {
		fail "sbatch not responding"
	}
	eof {
		wait
	}
}
if {$job_id == 0} {
	fail "Batch not submitted"
}

#
# Wait for job to complete and check for file
#
if {[wait_for_job $job_id "DONE"] != 0} {
	cancel_job $job_id
	fail "Waiting for job to complete"
}
if {[wait_for_file $file_out] == 0} {
	set matches 0
	set no_nvcc 0
	set run_time1 0
	set run_time2 0
	spawn $bin_cat $file_out
	expect {
		-re "Couldn't allocate memory" {
			skip "This means the gpu selected doesn't support this test"
		}
		-re "No such file" {
			incr no_nvcc
			exp_continue
		}
		-re "Could not find" {
			incr no_nvcc
			exp_continue
		}
		-re "CUDA_VISIBLE_DEVICES" {
			incr matches
			exp_continue
		}
		-re "CUDA_MPS_ACTIVE_THREAD_PERCENTAGE" {
			incr matches
			exp_continue
		}
		-re "Max error: 0" {
			incr matches
			exp_continue
		}
		-re "Run Time (usec): ($number)" {
			if {$run_time1 == 0} {
				set run_time1 $expect_out(1,string)
			} else {
				set run_time2 $expect_out(1,string)
			}
			incr matches
			exp_continue
		}
		eof {
			wait
		}
	}
	log_user 0
	spawn $bin_cat $file_out
	expect {
		-re "Run Time .usec.: ($number)" {
			if {$run_time1 == 0} {
				set run_time1 $expect_out(1,string)
			} else {
				set run_time2 $expect_out(1,string)
			}
			incr matches
			exp_continue
		}
		eof {
			wait
		}
	}
	log_user 1
	if {$no_nvcc != 0} {
		log_warn "Could not find program nvcc (CUDA compiler)"
	} elseif {$matches != 6} {
		log_error "CUDA output not as expected ($matches != 6)"
		set exit_code 1
	}
	if {$run_time1 > 0} {
		set delta_t [expr abs($run_time1 - $run_time2)]
		set percent_time_diff [expr ($delta_t * 100) / $run_time1]
		if {$percent_time_diff > 20} {
			log_error "CUDA MPS jobs appear to have not run in parallel. Run time difference was $percent_time_diff percent"
			set exit_code 1
		} else {
			log_debug "CUDA MPS jobs do appear to have not run in parallel"
			log_debug "Run time difference was $percent_time_diff percent"
		}
	}
} else {
	fail "Output file failed to appear ($file_out)"
}

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