src/HOL/Library/Sum_of_Squares/neos_csdp_client
author wenzelm
Sun, 13 Mar 2011 16:38:14 +0100
changeset 41947 0b8a13b145e9
parent 41475 fe4f0d9f9dbb
permissions -rwxr-xr-x
prefer qualified ISABELLE_NEOS_SERVER;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
32268
d50f0cb67578 Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff changeset
     1
#!/usr/bin/env python
d50f0cb67578 Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff changeset
     2
import sys
32362
c0c640d86b4e interrupt handler for neos csdp client
Philipp Meyer
parents: 32332
diff changeset
     3
import signal
32268
d50f0cb67578 Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff changeset
     4
import xmlrpclib
d50f0cb67578 Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff changeset
     5
import time
d50f0cb67578 Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff changeset
     6
import re
41475
fe4f0d9f9dbb updated NEOS_SERVER, which is now provided via settings;
wenzelm
parents: 41474
diff changeset
     7
import os
32268
d50f0cb67578 Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff changeset
     8
32332
bc5cec7b2be6 misc changes to SOS by Philipp Meyer:
wenzelm
parents: 32268
diff changeset
     9
# Neos server config
41947
0b8a13b145e9 prefer qualified ISABELLE_NEOS_SERVER;
wenzelm
parents: 41475
diff changeset
    10
neos = xmlrpclib.Server(os.getenv("ISABELLE_NEOS_SERVER"))
32542
e37c0ddf257e fixed cleanup routine in neos csdp script
Philipp Meyer
parents: 32362
diff changeset
    11
32362
c0c640d86b4e interrupt handler for neos csdp client
Philipp Meyer
parents: 32332
diff changeset
    12
jobNumber = 0
c0c640d86b4e interrupt handler for neos csdp client
Philipp Meyer
parents: 32332
diff changeset
    13
password = ""
c0c640d86b4e interrupt handler for neos csdp client
Philipp Meyer
parents: 32332
diff changeset
    14
inputfile = None
c0c640d86b4e interrupt handler for neos csdp client
Philipp Meyer
parents: 32332
diff changeset
    15
outputfile = None
c0c640d86b4e interrupt handler for neos csdp client
Philipp Meyer
parents: 32332
diff changeset
    16
# interrupt handler
c0c640d86b4e interrupt handler for neos csdp client
Philipp Meyer
parents: 32332
diff changeset
    17
def cleanup(signal, frame):
c0c640d86b4e interrupt handler for neos csdp client
Philipp Meyer
parents: 32332
diff changeset
    18
  sys.stdout.write("Caught interrupt, cleaning up\n")
c0c640d86b4e interrupt handler for neos csdp client
Philipp Meyer
parents: 32332
diff changeset
    19
  if jobNumber != 0:
c0c640d86b4e interrupt handler for neos csdp client
Philipp Meyer
parents: 32332
diff changeset
    20
    neos.killJob(jobNumber, password)
c0c640d86b4e interrupt handler for neos csdp client
Philipp Meyer
parents: 32332
diff changeset
    21
  if inputfile != None:
c0c640d86b4e interrupt handler for neos csdp client
Philipp Meyer
parents: 32332
diff changeset
    22
    inputfile.close()
c0c640d86b4e interrupt handler for neos csdp client
Philipp Meyer
parents: 32332
diff changeset
    23
  if outputfile != None:
c0c640d86b4e interrupt handler for neos csdp client
Philipp Meyer
parents: 32332
diff changeset
    24
    outputfile.close()
c0c640d86b4e interrupt handler for neos csdp client
Philipp Meyer
parents: 32332
diff changeset
    25
  sys.exit(21)
c0c640d86b4e interrupt handler for neos csdp client
Philipp Meyer
parents: 32332
diff changeset
    26
c0c640d86b4e interrupt handler for neos csdp client
Philipp Meyer
parents: 32332
diff changeset
    27
signal.signal(signal.SIGHUP, cleanup)
c0c640d86b4e interrupt handler for neos csdp client
Philipp Meyer
parents: 32332
diff changeset
    28
signal.signal(signal.SIGINT, cleanup)
c0c640d86b4e interrupt handler for neos csdp client
Philipp Meyer
parents: 32332
diff changeset
    29
signal.signal(signal.SIGQUIT, cleanup)
c0c640d86b4e interrupt handler for neos csdp client
Philipp Meyer
parents: 32332
diff changeset
    30
signal.signal(signal.SIGTERM, cleanup)
c0c640d86b4e interrupt handler for neos csdp client
Philipp Meyer
parents: 32332
diff changeset
    31
c0c640d86b4e interrupt handler for neos csdp client
Philipp Meyer
parents: 32332
diff changeset
    32
if len(sys.argv) <> 3:
c0c640d86b4e interrupt handler for neos csdp client
Philipp Meyer
parents: 32332
diff changeset
    33
  sys.stderr.write("Usage: neos_csdp_client <input_filename> <output_filename>\n")
c0c640d86b4e interrupt handler for neos csdp client
Philipp Meyer
parents: 32332
diff changeset
    34
  sys.exit(19)
32268
d50f0cb67578 Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff changeset
    35
d50f0cb67578 Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff changeset
    36
xml_pre = "<document>\n<category>sdp</category>\n<solver>csdp</solver>\n<inputMethod>SPARSE_SDPA</inputMethod>\n<dat><![CDATA["
d50f0cb67578 Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff changeset
    37
xml_post = "]]></dat>\n</document>\n"
d50f0cb67578 Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff changeset
    38
xml = xml_pre
32362
c0c640d86b4e interrupt handler for neos csdp client
Philipp Meyer
parents: 32332
diff changeset
    39
inputfile = open(sys.argv[1],"r")
32268
d50f0cb67578 Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff changeset
    40
buffer = 1
d50f0cb67578 Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff changeset
    41
while buffer:
32332
bc5cec7b2be6 misc changes to SOS by Philipp Meyer:
wenzelm
parents: 32268
diff changeset
    42
  buffer = inputfile.read()
32268
d50f0cb67578 Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff changeset
    43
  xml += buffer
32332
bc5cec7b2be6 misc changes to SOS by Philipp Meyer:
wenzelm
parents: 32268
diff changeset
    44
inputfile.close()
32268
d50f0cb67578 Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff changeset
    45
xml += xml_post
d50f0cb67578 Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff changeset
    46
d50f0cb67578 Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff changeset
    47
(jobNumber,password) = neos.submitJob(xml)
d50f0cb67578 Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff changeset
    48
d50f0cb67578 Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff changeset
    49
if jobNumber == 0:
d50f0cb67578 Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff changeset
    50
  sys.stdout.write("error submitting job: %s" % password)
32332
bc5cec7b2be6 misc changes to SOS by Philipp Meyer:
wenzelm
parents: 32268
diff changeset
    51
  sys.exit(20)
32268
d50f0cb67578 Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff changeset
    52
else:
d50f0cb67578 Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff changeset
    53
  sys.stdout.write("jobNumber = %d\tpassword = %s\n" % (jobNumber,password))
d50f0cb67578 Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff changeset
    54
d50f0cb67578 Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff changeset
    55
offset=0
32332
bc5cec7b2be6 misc changes to SOS by Philipp Meyer:
wenzelm
parents: 32268
diff changeset
    56
messages = ""
32268
d50f0cb67578 Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff changeset
    57
status="Waiting"
d50f0cb67578 Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff changeset
    58
while status == "Running" or status=="Waiting":
d50f0cb67578 Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff changeset
    59
  time.sleep(1)
d50f0cb67578 Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff changeset
    60
  (msg,offset) = neos.getIntermediateResults(jobNumber,password,offset)
32332
bc5cec7b2be6 misc changes to SOS by Philipp Meyer:
wenzelm
parents: 32268
diff changeset
    61
  messages += msg.data
32268
d50f0cb67578 Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff changeset
    62
  sys.stdout.write(msg.data)
d50f0cb67578 Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff changeset
    63
  status = neos.getJobStatus(jobNumber, password)
d50f0cb67578 Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff changeset
    64
d50f0cb67578 Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff changeset
    65
msg = neos.getFinalResults(jobNumber, password).data
32332
bc5cec7b2be6 misc changes to SOS by Philipp Meyer:
wenzelm
parents: 32268
diff changeset
    66
sys.stdout.write("---------- Begin CSDP Output -------------\n");
bc5cec7b2be6 misc changes to SOS by Philipp Meyer:
wenzelm
parents: 32268
diff changeset
    67
sys.stdout.write(msg)
32268
d50f0cb67578 Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff changeset
    68
32332
bc5cec7b2be6 misc changes to SOS by Philipp Meyer:
wenzelm
parents: 32268
diff changeset
    69
# extract solution
bc5cec7b2be6 misc changes to SOS by Philipp Meyer:
wenzelm
parents: 32268
diff changeset
    70
result = msg.split("Solution:")
32268
d50f0cb67578 Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff changeset
    71
if len(result) > 1:
32362
c0c640d86b4e interrupt handler for neos csdp client
Philipp Meyer
parents: 32332
diff changeset
    72
  solution = result[1].strip()
c0c640d86b4e interrupt handler for neos csdp client
Philipp Meyer
parents: 32332
diff changeset
    73
  if solution != "":
32542
e37c0ddf257e fixed cleanup routine in neos csdp script
Philipp Meyer
parents: 32362
diff changeset
    74
    outputfile = open(sys.argv[2],"w")
e37c0ddf257e fixed cleanup routine in neos csdp script
Philipp Meyer
parents: 32362
diff changeset
    75
    outputfile.write(solution)
e37c0ddf257e fixed cleanup routine in neos csdp script
Philipp Meyer
parents: 32362
diff changeset
    76
    outputfile.close()
32268
d50f0cb67578 Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff changeset
    77
32332
bc5cec7b2be6 misc changes to SOS by Philipp Meyer:
wenzelm
parents: 32268
diff changeset
    78
# extract return code
bc5cec7b2be6 misc changes to SOS by Philipp Meyer:
wenzelm
parents: 32268
diff changeset
    79
p = re.compile(r"^Error: Command exited with non-zero status (\d+)$", re.MULTILINE)
bc5cec7b2be6 misc changes to SOS by Philipp Meyer:
wenzelm
parents: 32268
diff changeset
    80
m = p.search(messages)
bc5cec7b2be6 misc changes to SOS by Philipp Meyer:
wenzelm
parents: 32268
diff changeset
    81
if m:
bc5cec7b2be6 misc changes to SOS by Philipp Meyer:
wenzelm
parents: 32268
diff changeset
    82
  sys.exit(int(m.group(1)))
bc5cec7b2be6 misc changes to SOS by Philipp Meyer:
wenzelm
parents: 32268
diff changeset
    83
else:
bc5cec7b2be6 misc changes to SOS by Philipp Meyer:
wenzelm
parents: 32268
diff changeset
    84
  sys.exit(0)
32268
d50f0cb67578 Functionality for sum of squares to call a remote csdp prover
Philipp Meyer
parents:
diff changeset
    85