#!/usr/bin/perl 

use work_queue;

my $q = work_queue_create(0);
if (not defined($q)) {
	print "could not instantiate Work Queue master\n";
	exit 1;
}

$port = work_queue_port($q);
print "Listening on port $port.\n"; 

print "Submitting 20 simulation tasks...";
for (my $i = 0; $i < 20; $i++) {
	my $infile = "input.txt";
	my $outfile = sprintf("file.%0.2x", $i);
    my $command = "python simulation.py $i < $infile > $outfile";

    my $t = work_queue_task_create($command);

    work_queue_task_specify_file($t, "simulation.py", "simulation.py", $WORK_QUEUE_INPUT, $WORK_QUEUE_CACHE);
    work_queue_task_specify_file($t, $infile, $infile, $WORK_QUEUE_INPUT, $WORK_QUEUE_CACHE);
    work_queue_task_specify_file($t, $outfile, $outfile, $WORK_QUEUE_OUTPUT, $WORK_QUEUE_NOCACHE);

    my $taskid = work_queue_submit($q, $t);
}
print "done.\n"; 

print "Waiting for tasks to complete...\n";
while (not work_queue_empty($q)) {
    my $t = work_queue_wait($q, 5);

    if (defined($t)) {
		print "task (id# $t->{taskid}) complete: $t->{command_line} (return code $t->{return_status})\n";
		work_queue_task_delete($t);
	}
}

print "done.\n";

work_queue_delete($q);

exit 0;
