From 38c275a74e1bd8fb93a43293aaed664acc9a8b03 Mon Sep 17 00:00:00 2001 From: Hamed Gorjiara Date: Wed, 3 Jul 2019 16:56:02 -0700 Subject: [PATCH 1/1] Scripts needed for automation of learning process ... Tested on Sypet benchmark ... --- deploy-cs.sh | 5 +- src/{ => Scripts}/autotunerparser.py | 34 +++++--- src/Scripts/learnresultgen.sh | 30 +++++++ src/Scripts/remotelearning.py | 117 +++++++++++++++++++++++++++ src/{ => Scripts}/runbench.sh | 0 src/{ => Scripts}/runinterpreter.sh | 0 src/Scripts/serverstatus.py | 15 ++++ 7 files changed, 189 insertions(+), 12 deletions(-) rename src/{ => Scripts}/autotunerparser.py (76%) create mode 100755 src/Scripts/learnresultgen.sh create mode 100644 src/Scripts/remotelearning.py rename src/{ => Scripts}/runbench.sh (100%) rename src/{ => Scripts}/runinterpreter.sh (100%) create mode 100644 src/Scripts/serverstatus.py diff --git a/deploy-cs.sh b/deploy-cs.sh index 2f41b32..6eafd06 100755 --- a/deploy-cs.sh +++ b/deploy-cs.sh @@ -5,13 +5,14 @@ set -e BASE=../ SERVERS="dc-4.calit2.uci.edu dc-5.calit2.uci.edu dc-6.calit2.uci.edu dc-7.calit2.uci.edu dc-8.calit2.uci.edu dc-9.calit2.uci.edu dc-10.calit2.uci.edu dc-11.calit2.uci.edu" +#SERVERS="dc-1.calit2.uci.edu dc-2.calit2.uci.edu dc-3.calit2.uci.edu" REMOTEDIR="/scratch/hamed/" INFILE="constraint_compiler/" SRC="constraint_compiler/src/" SHAREDDIR=~/ OUTFILE=csolver.tar.gz USER=hamed - +BIN=${REMOTEDIR}${SRC}/bin cd $BASE rm -f $OUTFILE @@ -19,5 +20,5 @@ tar -czvf $OUTFILE $INFILE cp $OUTFILE $SHAREDDIR for SERVER in $SERVERS; do - ssh $USER@$SERVER "cp $SHAREDDIR$OUTFILE $REMOTEDIR; cd $REMOTEDIR; sudo rm -r $SRC; tar -xzvf $OUTFILE; cd $SRC; make clean; ./setup.sh" + ssh $USER@$SERVER "cp $SHAREDDIR$OUTFILE $REMOTEDIR; cd $REMOTEDIR; sudo rm -r $SRC; tar -xzvf $OUTFILE; cd $SRC; make clean; ./setup.sh; find -iname csolver -exec rm '{}' \; -exec ln -s $BIN '{}' \;" done diff --git a/src/autotunerparser.py b/src/Scripts/autotunerparser.py similarity index 76% rename from src/autotunerparser.py rename to src/Scripts/autotunerparser.py index 1632f4b..618fcd6 100644 --- a/src/autotunerparser.py +++ b/src/Scripts/autotunerparser.py @@ -11,7 +11,9 @@ class AutoTunerArgParser: def getFileName(self): return self.args.file[0] - + +HEADER = ["TESTCASE", "SATTIME", "EXECTIME", "PREPROCESS", "ELEMENTOPT", "ELEMENTOPTSETS", "PROXYVARIABLE", "#SubGraph", "NODEENCODING", "EDGEENCODING", "NAIVEENCODER", "ENCODINGGRAPHOPT"] + configs = {"EXECTIME": "-", "SATTIME":"-", "TESTCASE":"-", @@ -39,32 +41,41 @@ REGEXES = {"EXECTIME": "CSOLVER solve time: (.*)", "NAIVEENCODER" : "Param NAIVEENCODER = (.*)range=\[1,3\](.*)", "ENCODINGGRAPHOPT" : "Param ENCODINGGRAPHOPT = (.*)range=\[0,1\]" } +def reorderEntry(entry): + global HEADER + result = [] + for key in HEADER: + result.append(entry[key]) + return result + def printHeader(file): - global configs + global HEADER mystr="" - for config in configs: - mystr+=str(config)+"," + for key in HEADER: + mystr+=key+"," print >>file, mystr def printConfig(file, data): print data mystr="" - for config in data: - mystr+=str(data[config])+"," + for val in data: + mystr+=str(val)+"," print >> file, mystr + def main(): global configs argprocess = AutoTunerArgParser() output = open("tuner.csv", "w") printHeader(output) + result = [] with open(argprocess.getFileName()) as file: for line in file: if line.startswith("Mutating"): - printConfig(output,configs) + result.append(reorderEntry(configs)) elif line.startswith("Best tuner"): - printConfig(output,configs); + result.append(reorderEntry(configs)) else : for regex in REGEXES: p = re.compile(REGEXES[regex]) @@ -75,8 +86,11 @@ def main(): else: configs[regex] = re.findall("\d+\.?\d*", line)[0] - configs["EXECTIME"] = "BEST TUNE:" - printConfig(output, configs) + #configs["EXECTIME"] = "BEST TUNE:" + result.append(reorderEntry(configs)) + result.sort(key = lambda entry: entry[0]) + for entry in result: + printConfig(output, entry) print "Done with parsing " + argprocess.getFileName() if __name__ == "__main__": diff --git a/src/Scripts/learnresultgen.sh b/src/Scripts/learnresultgen.sh new file mode 100755 index 0000000..d34f090 --- /dev/null +++ b/src/Scripts/learnresultgen.sh @@ -0,0 +1,30 @@ +#!/bin/bash +# ./learnresultgen.sh [sypet] [learning set = 1, 2, 3, etc.] [algorithm= 1, 2, 3, 4] +set -e + +if [ "$#" -lt 3 ]; then + echo "Illegal number of argument" + echo "./learnresultgen.sh [sypet] [learning set = 0, 1, 2, 3, etc.] [algorithm = Known Tuner Types: Random Tuner=1, Comp Tuner=2, Kmeans Tuner=3, Simulated Annealing Tuner=4]" + exit 1 +fi + +SATUNEDIR=$PWD +BENCHDIR=$SATUNEDIR/Benchmarks/$1 +BIN=$SATUNEDIR/bin + +source $SATUNEDIR/Benchmarks/common.sh +cd $BENCHDIR +./learn.sh $2 $3 +cd $BIN +./run.sh analyzemultituner +cd $SATUNEDIR +TUNERS=$(find "$BIN" -name "*.tuner") +for T in $TUNERS; do + TUNER=$(basename $T) + echo "Running tuner "$TUNER + ./Scripts/runbench.sh $1 $TIMEOUT $TUNER &> $BIN/$1"-set"$2"-"$TUNER".log" + python ./Scripts/autotunerparser.py -f $BIN/$1"-set"$2"-"$TUNER".log" + mv tuner.csv $1"-set"$2"-"$TUNER".csv" +done + +mv ./bin ./"bin-"$1"-set"$2"-alg"$3 diff --git a/src/Scripts/remotelearning.py b/src/Scripts/remotelearning.py new file mode 100644 index 0000000..dccee9c --- /dev/null +++ b/src/Scripts/remotelearning.py @@ -0,0 +1,117 @@ +import re +import argparse +import sys +from threading import Thread +import subprocess +import os + +# 1) Deploy on all the servers +# 2) Based on the benchmark selection, run each learning set on a server +# 3) After being done with that, it sshould calculate the first best 3 +# 4) Run them indivisually +# 5) Generate the excel sheet! +SRCDIR="/scratch/hamed/constraint_compiler/src" +LOCALSRCDIR="/scratch/satcheck/satproject/constraint_compiler/src" +class ArgParser: + def __init__(self): + self.parser = argparse.ArgumentParser(description='Parsing the output log of the CSolver auto tuner ...') + self.parser.add_argument('--bench', '-b', metavar='sudoku', type=str, nargs=1,help='Benchmark that you want to learn on') + self.args = self.parser.parse_args() + + def getBenchmarkName(self): + return self.args.bench[0] + +def deploy(): + os.system("cd ../; ./deploy-cs.sh") + +def getServerNeeded(benchmark): + variable = "" + with open("./Benchmarks/" + benchmark + "/learn.sh") as f: + line = f.readline() + while "declare -a LearningSet=" not in line: + line = f.readline() + while ")" not in line: + variable = variable + line + line = f.readline() + variable = variable + line + return variable.count("\"")/2 + +def getAvailableServerList(needed): + global SRCDIR + available = [] + for i in range(4,12): + print ("Checking availability for server " + str(i)) + HOST="dc-"+ str(i) + ".calit2.uci.edu" + COMMAND="cd "+SRCDIR+"; python ./Scripts/serverstatus.py" + ssh = subprocess.Popen(["ssh", "%s" % HOST, COMMAND], + shell=False, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE) + result = ssh.stdout.readlines() + if result == []: + error = ssh.stderr.readlines() + print >>sys.stderr, "ERROR: %s" % error + else: + print ("Result of running serverStatus: ") + print result + if "AVAILABLE\n" in result: + available.append(i) + if len(available) >= needed: + break + return available + +def startLearningProcess(benchmark, server, learningSet): + global SRCDIR + HOST="dc-"+ str(server) + ".calit2.uci.edu" + ALGORITHM = "2" + LOGFILE= benchmark + "-" + str(learningSet) + ".log" + print("Running benchmark " + benchmark + "(Set="+ str(learningSet)+") on server") + COMMAND=("cd "+SRCDIR+"; ./Scripts/learnresultgen.sh " + + benchmark + " " + str(learningSet) + " " + ALGORITHM + " &> " + LOGFILE + "; mv *.csv ~/; echo 'SUCCESS'") + print("Calling the following command:\n" + COMMAND) + ssh = subprocess.Popen(["ssh", "%s" % HOST, COMMAND], + shell=False, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE) + result = ssh.stdout.readlines() + if result == []: + error = ssh.stderr.readlines() + print >>sys.stderr, "ERROR: %s" % error + else: + print ("Result of running serverStatus: ") + print result + +def moveCSVFiles(): + global LOCALSRCDIR + os.system("mv ~/*.csv "+ LOCALSRCDIR) + + +def main(): + benchmark = ArgParser().getBenchmarkName() +# print("Deploying on all the servers ...") +# deploy() + serverNumber = getServerNeeded(benchmark) + print("Learning on " + benchmark + " needs " + str(serverNumber) + " servers.") + availableServers = getAvailableServerList(serverNumber) + print ("Available Server:" + str(availableServers)) + if serverNumber > len(availableServers): + print("Servers are busy. We don't have enough server available for learning ...") + sys.exit(1) + try: + threads = [] + for i in range(serverNumber): + t = Thread(target=startLearningProcess, args=(benchmark, availableServers[i], i, )) + t.start() + threads.append(t) + + for t in threads: + t.join() + moveCSVFiles() + except: + print("Exception in creating learning thread ...") + sys.exit(1) + + + +if __name__ == "__main__": + main() diff --git a/src/runbench.sh b/src/Scripts/runbench.sh similarity index 100% rename from src/runbench.sh rename to src/Scripts/runbench.sh diff --git a/src/runinterpreter.sh b/src/Scripts/runinterpreter.sh similarity index 100% rename from src/runinterpreter.sh rename to src/Scripts/runinterpreter.sh diff --git a/src/Scripts/serverstatus.py b/src/Scripts/serverstatus.py new file mode 100644 index 0000000..3f6ffd4 --- /dev/null +++ b/src/Scripts/serverstatus.py @@ -0,0 +1,15 @@ +import psutil +from time import sleep +# gives a single float value +avg = 0.0 +count = 0 +for i in range(350): + avg = (psutil.cpu_percent() + avg*count)/(count+1) + count = count + 1 + sleep(0.1) + + +if avg> 15: + print "BUSY" +else: + print "AVAILABLE" -- 2.34.1