src/HOL/Library/Sum_of_Squares/neos_csdp_client
changeset 41474 60d091240485
parent 32542 e37c0ddf257e
child 41475 fe4f0d9f9dbb
equal deleted inserted replaced
41473:3717fc42ebe9 41474:60d091240485
       
     1 #!/usr/bin/env python
       
     2 import sys
       
     3 import signal
       
     4 import xmlrpclib
       
     5 import time
       
     6 import re
       
     7 
       
     8 # Neos server config
       
     9 NEOS_HOST="neos.mcs.anl.gov"
       
    10 NEOS_PORT=3332
       
    11 
       
    12 neos=xmlrpclib.Server("http://%s:%d" % (NEOS_HOST, NEOS_PORT))
       
    13 
       
    14 jobNumber = 0
       
    15 password = ""
       
    16 inputfile = None
       
    17 outputfile = None
       
    18 # interrupt handler
       
    19 def cleanup(signal, frame):
       
    20   sys.stdout.write("Caught interrupt, cleaning up\n")
       
    21   if jobNumber != 0:
       
    22     neos.killJob(jobNumber, password)
       
    23   if inputfile != None:
       
    24     inputfile.close()
       
    25   if outputfile != None:
       
    26     outputfile.close()
       
    27   sys.exit(21)
       
    28 
       
    29 signal.signal(signal.SIGHUP, cleanup)
       
    30 signal.signal(signal.SIGINT, cleanup)
       
    31 signal.signal(signal.SIGQUIT, cleanup)
       
    32 signal.signal(signal.SIGTERM, cleanup)
       
    33 
       
    34 if len(sys.argv) <> 3:
       
    35   sys.stderr.write("Usage: neos_csdp_client <input_filename> <output_filename>\n")
       
    36   sys.exit(19)
       
    37 
       
    38 xml_pre = "<document>\n<category>sdp</category>\n<solver>csdp</solver>\n<inputMethod>SPARSE_SDPA</inputMethod>\n<dat><![CDATA["
       
    39 xml_post = "]]></dat>\n</document>\n"
       
    40 xml = xml_pre
       
    41 inputfile = open(sys.argv[1],"r")
       
    42 buffer = 1
       
    43 while buffer:
       
    44   buffer = inputfile.read()
       
    45   xml += buffer
       
    46 inputfile.close()
       
    47 xml += xml_post
       
    48 
       
    49 (jobNumber,password) = neos.submitJob(xml)
       
    50 
       
    51 if jobNumber == 0:
       
    52   sys.stdout.write("error submitting job: %s" % password)
       
    53   sys.exit(20)
       
    54 else:
       
    55   sys.stdout.write("jobNumber = %d\tpassword = %s\n" % (jobNumber,password))
       
    56 
       
    57 offset=0
       
    58 messages = ""
       
    59 status="Waiting"
       
    60 while status == "Running" or status=="Waiting":
       
    61   time.sleep(1)
       
    62   (msg,offset) = neos.getIntermediateResults(jobNumber,password,offset)
       
    63   messages += msg.data
       
    64   sys.stdout.write(msg.data)
       
    65   status = neos.getJobStatus(jobNumber, password)
       
    66 
       
    67 msg = neos.getFinalResults(jobNumber, password).data
       
    68 sys.stdout.write("---------- Begin CSDP Output -------------\n");
       
    69 sys.stdout.write(msg)
       
    70 
       
    71 # extract solution
       
    72 result = msg.split("Solution:")
       
    73 if len(result) > 1:
       
    74   solution = result[1].strip()
       
    75   if solution != "":
       
    76     outputfile = open(sys.argv[2],"w")
       
    77     outputfile.write(solution)
       
    78     outputfile.close()
       
    79 
       
    80 # extract return code
       
    81 p = re.compile(r"^Error: Command exited with non-zero status (\d+)$", re.MULTILINE)
       
    82 m = p.search(messages)
       
    83 if m:
       
    84   sys.exit(int(m.group(1)))
       
    85 else:
       
    86   sys.exit(0)
       
    87