src/HOL/SMT/lib/scripts/run_smt_solver
author boehmes
Mon, 08 Feb 2010 11:01:47 +0100
changeset 35025 0ea45a4d32f3
parent 34983 src/HOL/SMT/lib/scripts/run_smt_solver.pl@e5cb3a016094
permissions -rwxr-xr-x
split SMT perl script: certificate caching and invokation of remote solvers are now in separate scripts, modernized perl scripts: prefer standalone executables
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
35025
0ea45a4d32f3 split SMT perl script: certificate caching and invokation of remote solvers are now in separate scripts,
boehmes
parents: 34983
diff changeset
     1
#!/usr/bin/env perl
34983
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
     2
#
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
     3
# Author: Sascha Boehme, TU Muenchen
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
     4
#
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
     5
# Cache for prover results, based on discriminating problems using MD5.
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
     6
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
     7
use strict;
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
     8
use warnings;
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
     9
use Digest::MD5;
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    10
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    11
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    12
# arguments
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    13
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    14
my $certs_file = shift @ARGV;
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    15
my $location = shift @ARGV;
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    16
my $result_file = pop @ARGV;
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    17
my $problem_file = $ARGV[-1];
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    18
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    19
my $use_certs = not ($certs_file eq "-");
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    20
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    21
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    22
# create MD5 problem digest
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    23
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    24
my $problem_digest = "";
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    25
if ($use_certs) {
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    26
  my $md5 = Digest::MD5->new;
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    27
  open FILE, "<$problem_file" or
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    28
    die "ERROR: Failed to open '$problem_file' ($!)";
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    29
  $md5->addfile(*FILE);
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    30
  close FILE;
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    31
  $problem_digest = $md5->b64digest;
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    32
}
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    33
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    34
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    35
# lookup problem digest among existing certificates and
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    36
# return a cached result (if there is a certificate for the problem)
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    37
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    38
if ($use_certs and -e $certs_file) {
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    39
  my $cached = 0;
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    40
  open CERTS, "<$certs_file" or die "ERROR: Failed to open '$certs_file' ($!)";
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    41
  while (<CERTS>) {
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    42
    if (m/(\S+) (\d+)/) {
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    43
      if ($1 eq $problem_digest) {
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    44
        my $start = tell CERTS;
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    45
        open FILE, ">$result_file" or
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    46
          die "ERROR: Failed to open '$result_file ($!)";
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    47
        while ((tell CERTS) - $start < $2) {
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    48
          my $line = <CERTS>;
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    49
          print FILE $line;
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    50
        }
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    51
        close FILE;
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    52
        $cached = 1;
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    53
        last;
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    54
      }
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    55
      else { seek CERTS, $2, 1; }
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    56
    }
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    57
    else { die "ERROR: Invalid file format in '$certs_file'"; }
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    58
  }
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    59
  close CERTS;
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    60
  if ($cached) { exit 0; }
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    61
}
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    62
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    63
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    64
# invoke (remote or local) prover
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    65
35025
0ea45a4d32f3 split SMT perl script: certificate caching and invokation of remote solvers are now in separate scripts,
boehmes
parents: 34983
diff changeset
    66
my $result;
34983
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    67
35025
0ea45a4d32f3 split SMT perl script: certificate caching and invokation of remote solvers are now in separate scripts,
boehmes
parents: 34983
diff changeset
    68
if ($location eq "remote") {
0ea45a4d32f3 split SMT perl script: certificate caching and invokation of remote solvers are now in separate scripts,
boehmes
parents: 34983
diff changeset
    69
  $result = system "$ENV{'REMOTE_SMT'} @ARGV >$result_file 2>&1";
34983
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    70
}
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    71
elsif ($location eq "local") {
35025
0ea45a4d32f3 split SMT perl script: certificate caching and invokation of remote solvers are now in separate scripts,
boehmes
parents: 34983
diff changeset
    72
  $result = system "@ARGV >$result_file 2>&1";
34983
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    73
}
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    74
else { die "ERROR: No SMT solver invoked"; }
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    75
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    76
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    77
# cache result
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    78
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    79
if ($use_certs) {
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    80
  open CERTS, ">>$certs_file" or
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    81
    die "ERROR: Failed to open '$certs_file' ($!)";
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    82
  print CERTS
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    83
    ("$problem_digest " . ((-s $result_file) + (length "\n")) . "\n");
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    84
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    85
  open FILE, "<$result_file" or
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    86
    die "ERROR: Failed to open '$result_file' ($!)";
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    87
  foreach (<FILE>) { print CERTS $_; }
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    88
  close FILE; 
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    89
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    90
  print CERTS "\n";
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    91
  close CERTS;
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    92
}
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    93