#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Simple CUDA test
#
# 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) 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 test_id     "39.21"
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

print_header $test_id

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 per node in the default partition\n"
	exit 0
}
send_user "\nGPUs per node count is $gpu_cnt\n"

#
# Build input script file
#
exec $bin_rm -f $file_in $file_out $file_prog
make_bash_script $file_in "
ls $nvcc
$nvcc ${file_prog}.cu -o $file_prog
./$file_prog"

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

#
# Wait for job to complete and check for file
#
if {[wait_for_job $job_id "DONE"] != 0} {
	send_user "\nFAILURE: waiting for job to complete\n"
	cancel_job $job_id
	exit 1
}
if {[wait_for_file $file_out] == 0} {
	set matches 0
	set no_nvcc 0
	spawn $bin_cat $file_out
	expect {
		-re "No such file" {
			incr no_nvcc
			exp_continue
		}
		-re "Could not find" {
			incr no_nvcc
			exp_continue
		}
		-re "Max error: 0" {
			incr matches
			exp_continue
		}
		eof {
			wait
		}
	}
	if {$no_nvcc != 0} {
		send_user "\nWARNING: Could not find program nvcc (CUDA compiler)\n"
	} elseif {$matches != 1} {
		send_user "\nFAILURE: CUDA output not as expected\n"
		set exit_code 1
	}
} else {
	set exit_code 1
}

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