#!/usr/local/bin/perl

#
#  The list, @table, represents a 2-d matrix, sender x recver.  
#  The vertical axis is sending nodes and the horizontal is 
#  recving nodes.  So the value at @table(0,2) indicates that proc 0
#  is sending to proc 2, and proc 2 is recving from proc 0.
#
#  This script just creates separate files for each processor from 
#  the table defined in the script.  Each new file has a processor id 
#  and its send/recv instructions in a suitable format for input to 
#  the C scheduling routine, callschedule.c
#
#  Command line input to this script controls the basename of the output
#  files. 
#

$filebase = $ARGV[0];
@table =( x, 1, 0, 0, 0, 1,
	  1, x, 1, 0, 0, 1,
	  1, 1, x, 1, 0, 1,
	  0, 1, 1, x, 1, 1, 
	  0, 0, 1, 1, x, 1,
	  1, 1, 1, 1, 1, x );

$ncol = sqrt($#table +1);

for ($i = 0; $i < $ncol; $i++) {
  $filehandle = "FILE$i";
  $file = "$filebase.p$i";
  print("file = $file\n");
  open ($filehandle, ">>$file") || warn "couldn't open file $filehandle";
  print($filehandle "$i\n");
}

for ($sender = 0; $sender < $ncol; $sender++) {
  for ($recver = 0; $recver < $ncol; $recver++) {
    $value = $sender * $ncol + $recver;
    $senderfile = "FILE$sender";
    $recverfile = "FILE$recver";
    if ($table[$value] == 1) {
      print $senderfile "$recver 0\n";
      print $recverfile "$sender 1\n";
    }
  }
}
    
