| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- # -*- coding: utf-8 -*-
- # reasonLS
- # authors
- #
- # runs n instances of reasonLS in parallel (script code based on code of Norbert Manthey 2014)
- #
- # used libraries
- import os, signal, subprocess, time
- import sys, shutil, random, re
- def check_pid(pid):
- """ Check For the existence of a unix pid. """
- try:
- os.kill(pid, 0)
- except OSError:
- return False
- else:
- return True
- #
- # usage and current PID
- #
- print ("c reasonLS SC18")
- print ("c authors")
- print ("c runs n instances of reasonLS in parallel")
- if (len(sys.argv) < 4):
- print ("c usage: python pmapple.py <input.cnf> <seed> <numCores> [<proofFile>] [<tmpDirectory>]" ) #n is the number of instances to start
- sys.exit(0)
- print ("c running with PID " + str(os.getpid()))
- #
- # Handle parameters
- #
- # get input file
- #
- inputFile = sys.argv[1]
- seed = int(sys.argv[2])
- numCores = int(sys.argv[3])
- if (numCores == 1):
- print ("c does not make sense, please start reasonLS directly")
- sys.exit(0)
- # set proof file
- if (len(sys.argv) > 4):
- tmpProofz = sys.argv[4]
- path_proof = tmpProofz[tmpProofz.find('drup-file=')+10:]
- pathz = os.path.dirname(path_proof)
- proof = os.path.basename(path_proof)
- else:
- proof = "/dev/null"
- #
- # setup tmp files
- #
- tmpDir = "/tmp/"
- print ("c found " + str(len(sys.argv)) + " parameters")
- if (len(sys.argv) > 5):
- tmpDir = sys.argv[5]
- if (tmpDir[-1:] != '/'):
- tmpDir = tmpDir + "/"
- #construction of temporary files for output
- # let user know that everything has been read correctly
- print ("c starting " + str(numCores) + " instances of reasonLS with seed " + str(
- seed) + " on instance " + str(
- inputFile) + " \nc writing temp files to " + tmpDir)
- #
- # start the two solvers, each in its private process group
- #
- random.seed(seed)
- pids = set()
- pidsseed = set()
- seeds = set()
- confl = [0.9, 0.01, 1, 0.5, 0.6, 0.7, 0.8]
- area = [1.01, 1, 1.01, 1.05, 1.04, 1.03, 1.02]
- maxflip = [600, 600, 600, 600, 600, 600, 600]
- initflip = [100, 100, 100, 100, 100, 100, 100]
- raiseflip = [1.02, 1.02, 1.02, 1.02, 1.02, 1.02, 1.02]
- time = [0.35, 1.2, 0.35, 0.5, 0.45, 0.4, 0.35]
- tmpBassename = os.path.basename(inputFile)
- for i in range(0, numCores):
- seed=random.randint(0,2147483647)
- tmpFile = tmpDir + tmpBassename + "reasonLS_" + str(seed)
- print ("c starting instance " + str(
- i) + " of reasonLS started with seed " + str(seed))
- print ("c writing to files " + tmpFile + ".out .err")
- # start reasonLS
- if (proof != "/dev/null"):
- tmpProof = tmpDir + tmpBassename + proof + "reasonLS_" + str(seed)
- else:
- tmpProof = proof
- reasonLSCallString = "./ReasonLS " + inputFile + " -drup-file=" + tmpProof # add CNF file and tmp directory
- reasonLSCallString = reasonLSCallString + " -conflRatio=" + str(confl[i % 7]) + " -areaRatio=" + str(area[i % 7])
- reasonLSCallString = reasonLSCallString + " -maxFlipRatio=" + str(maxflip[i % 7]) + " -initFlipRatio=" + str(initflip[i % 7]) + " -raiseFlipRatio=" + str(raiseflip[i % 7])
- reasonLSCallString = reasonLSCallString + " -timeRatio=" + str(time[i % 7]) + " -switchMode="
- if (i < 7):
- reasonLSCallString = reasonLSCallString + "2500"
- else:
- reasonLSCallString = reasonLSCallString + "0"
- args = reasonLSCallString.split()
- # split the actual command line
- print ("c calling reasonLS with " + str(args))
- reasonLSProcess = subprocess.Popen(
- args,
- stdout=open(tmpFile + ".out", "w"),
- stderr=open(tmpFile + ".err", "w"),
- preexec_fn=os.setpgrp)
- pidsseed.add((reasonLSProcess.pid, seed))
- pids.add(reasonLSProcess.pid)
- seeds.add(seed)
- print ("c started reasonLS instance with PID" + str(reasonLSProcess.pid))
- # wait until the first solver returns
- winner = ""
- winnerCode = 0
- while pids:
- pid, retval = os.wait()
- print ("c finished " + str(pid) + " with return value " + str(retval))
- pids.remove(pid)
- # extract the exit code
- signal = retval & 255
- exitCode = (retval >> 8) & 255
- print ("c signal: " + str(signal) + " exit code: " + str(exitCode))
- # if exit code is nice, select the winner
- winnerseed = -1
- if signal == 0 and (exitCode == 10 or exitCode == 20):
- winnerCode = exitCode # get exit code
- for i in range(numCores):
- proc = pidsseed.pop()
- if proc[0] == pid:
- winnerseed = proc[1]
- break # do not wait for the other process as well, if a solution has been found!
- if winnerseed != -1:
- break
- #
- # output the result
- #
- if winnerCode != 0:
- print ("c winner seed: " + str(winnerseed))
- tmpFile = tmpDir + tmpBassename + "reasonLS_" + str(winnerseed)
- # for more recent python versions:
- # with open(tmpFile + ".out", "r") as f:
- # for the competition 2014 version:
- f = open(tmpFile + ".out", "r")
- if f:
- shutil.copyfileobj(f, sys.stdout)
- if (proof != "/dev/null"):
- tmpProof = tmpBassename + proof + "reasonLS_" + str(winnerseed)
- if( not os.path.exists(pathz) ):
- os.mkdir(pathz)
- if( os.path.exists(tmpDir+tmpProof)):
- os.system("mv " + tmpDir + tmpProof + " " + pathz + '/' +proof)
-
- else:
- print ("s UNKNOWN")
- # kill the other process, and its child processes
- for p in pids:
- try:
- os.kill(p, 15)
- except OSError:
- #do nothing
- else:
- #do nothing
- # clean up the temporary files
- for i in range(numCores):
- seed = seeds.pop()
- tmpFile = tmpDir + tmpBassename + "reasonLS_" + str(seed)
- if(os.path.exists(tmpFile + ".err")):
- os.unlink(tmpFile + ".err")
- if(os.path.exists(tmpFile + ".out")):
- os.unlink(tmpFile + ".out")
- if(proof != "/dev/null"):
- tmpProof = tmpDir + tmpBassename + proof + "reasonLS_" + str(seed)
- #if(os.path.exists(tmpProof)):
- os.unlink(tmpProof)
- # exit with the correct exit code
- sys.exit(winnerCode)
|