|
1 #!/usr/bin/env python |
|
2 import sys |
|
3 import xmlrpclib |
|
4 import time |
|
5 import re |
|
6 |
|
7 # Neos server config |
|
8 NEOS_HOST="neos.mcs.anl.gov" |
|
9 NEOS_PORT=3332 |
|
10 |
|
11 if len(sys.argv) < 3 or len(sys.argv) > 3: |
|
12 sys.stderr.write("Usage: NeosCSDPClient <input_filename> <output_filename>\n") |
|
13 sys.exit(1) |
|
14 |
|
15 neos=xmlrpclib.Server("http://%s:%d" % (NEOS_HOST, NEOS_PORT)) |
|
16 |
|
17 inputfile = open(sys.argv[1],"r") |
|
18 xml_pre = "<document>\n<category>sdp</category>\n<solver>csdp</solver>\n<inputMethod>SPARSE_SDPA</inputMethod>\n<dat><![CDATA[" |
|
19 xml_post = "]]></dat>\n</document>\n" |
|
20 xml = xml_pre |
|
21 buffer = 1 |
|
22 while buffer: |
|
23 buffer = inputfile.read() |
|
24 xml += buffer |
|
25 inputfile.close() |
|
26 xml += xml_post |
|
27 |
|
28 (jobNumber,password) = neos.submitJob(xml) |
|
29 |
|
30 if jobNumber == 0: |
|
31 sys.stdout.write("error submitting job: %s" % password) |
|
32 sys.exit(20) |
|
33 else: |
|
34 sys.stdout.write("jobNumber = %d\tpassword = %s\n" % (jobNumber,password)) |
|
35 |
|
36 offset=0 |
|
37 messages = "" |
|
38 status="Waiting" |
|
39 while status == "Running" or status=="Waiting": |
|
40 time.sleep(1) |
|
41 (msg,offset) = neos.getIntermediateResults(jobNumber,password,offset) |
|
42 messages += msg.data |
|
43 sys.stdout.write(msg.data) |
|
44 status = neos.getJobStatus(jobNumber, password) |
|
45 |
|
46 msg = neos.getFinalResults(jobNumber, password).data |
|
47 sys.stdout.write("---------- Begin CSDP Output -------------\n"); |
|
48 sys.stdout.write(msg) |
|
49 |
|
50 # extract solution |
|
51 result = msg.split("Solution:") |
|
52 if len(result) > 1: |
|
53 output = open(sys.argv[2],"w") |
|
54 output.write(result[1].strip()) |
|
55 output.close() |
|
56 |
|
57 # extract return code |
|
58 p = re.compile(r"^Error: Command exited with non-zero status (\d+)$", re.MULTILINE) |
|
59 m = p.search(messages) |
|
60 if m: |
|
61 sys.exit(int(m.group(1))) |
|
62 else: |
|
63 sys.exit(0) |
|
64 |
|
65 |