Push utility to build and bench all engines using default networks

This commit is contained in:
Andrew Grant committed 2022-06-20 07:30:05 -04:00
1 parent 8cba4b9572
commit 9aa4c608a5
4 files changed
+190 -100

No files matched your search

+190
View File
@@ -0,0 +1,190 @@
import argparse
import hashlib
import multiprocessing
import os
import re
import requests
import subprocess
import sys
sys.path.append(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))
from OpenBench.config import OPENBENCH_CONFIG
USE_OLD_BINARIES = True
OPENBENCH_USERNAME = None
OPENBENCH_PASSWORD = None
OPENBENCH_SERVER = 'http://chess.grantnet.us'
BENCHMARK_THREADS = None
BENCHMARK_SETS = None
def download_network(engine, sha256):
if not os.path.isfile(sha256):
print ('Downloading %s for %s...' % (sha256, engine))
target = '%s/clientGetNetwork/%s/' % (OPENBENCH_SERVER, sha256)
payload = { 'username' : OPENBENCH_USERNAME, 'password' : OPENBENCH_PASSWORD }
request = requests.post(data=payload, url=target)
with open(sha256, 'wb') as fout:
for chunk in request.iter_content(chunk_size=1024):
if chunk: fout.write(chunk)
fout.flush()
with open(sha256, 'rb') as network:
assert sha256 == hashlib.sha256(network.read()).hexdigest()[:8].upper()
def download_default_networks():
target = '%s/networks/' % (OPENBENCH_SERVER)
information = requests.get(url=target)
pattern = r'class="fas fa-star">.*?<a href="/networks/(?P<eng>.*?)/".*?<a href="/networks/download/(?P<sha>.{8})'
default_networks = list(re.findall(pattern, information.text))
for engine, network in default_networks:
download_network(engine, network)
return { engine : network for engine, network in default_networks }
def update_repositories():
if not os.path.isdir('Repositories'):
os.mkdir('Repositories')
for engine, config in OPENBENCH_CONFIG['engines'].items():
if not os.path.isdir('Repositories/' + config['source'].split('/')[-1]):
os.system('cd Repositories && git clone %s && cd ../' % (config['source']))
if not USE_OLD_BINARIES:
os.system('cd Repositories/%s && git pull && cd ../../' % config['source'].split('/')[-1])
def build_engine(engine, config, defaults):
git_path = 'Repositories/' + config['source'].split('/')[-1]
base_path = os.path.dirname(os.path.realpath(__file__))
build_path = os.path.join(base_path, git_path, config['build']['path'])
command = 'make -j EXE=%s' % (engine)
if engine in defaults:
command += ' EVALFILE=%s' % (os.path.join(base_path, defaults[engine]).replace('\\', '/'))
print ('=' * 120)
print ('Attempting to build %s...' % (engine))
subprocess.Popen(command.split(), cwd=build_path).wait()
if os.path.isfile(os.path.join(build_path, engine)):
os.rename(os.path.join(build_path, engine), os.path.join('Binaries', engine))
print ('Succesfully built %s' % (engine))
if os.path.isfile(os.path.join(build_path, engine + '.exe')):
os.rename(os.path.join(build_path, engine + '.exe'), os.path.join('Binaries', engine))
print ('Succesfully built %s' % (engine))
print ('=' * 120)
print ()
def build_all_engines():
update_repositories()
defaults = download_default_networks()
if not os.path.isdir('Binaries'):
os.mkdir('Binaries')
for engine, config in OPENBENCH_CONFIG['engines'].items():
if USE_OLD_BINARIES and os.path.isfile('Binaries/' + engine):
continue
build_engine(engine, config, defaults)
def parse_stream_output(stream):
nps = bench = None # Search through output Stream
for line in stream.decode('ascii').strip().split('\n')[::-1]:
# Try to match a wide array of patterns
line = re.sub(r'[^a-zA-Z0-9 ]+', ' ', line)
nps_pattern = r'([0-9]+ NPS)|(NPS[ ]+[0-9]+)'
bench_pattern = r'([0-9]+ NODES)|(NODES[ ]+[0-9]+)'
re_nps = re.search(nps_pattern, line.upper())
re_bench = re.search(bench_pattern, line.upper())
# Replace only if not already found earlier
if not nps and re_nps: nps = re_nps.group()
if not bench and re_bench: bench = re_bench.group()
# Parse out the integer portion from our matches
nps = int(re.search(r'[0-9]+', nps).group()) if nps else None
bench = int(re.search(r'[0-9]+', bench).group()) if bench else None
return (bench, nps)
def single_core_bench(engine, outqueue):
# Launch the bench and wait for results
stdout, stderr = subprocess.Popen(
"./{0} bench".format(engine).split(),
stdout=subprocess.PIPE, stderr=subprocess.STDOUT).communicate()
# Parse output streams for the benchmark data
bench, speed = parse_stream_output(stdout)
if bench is None or speed is None:
bench, speed = parse_stream_output(stderr)
outqueue.put((int(bench), int(speed)))
def multi_core_bench(engine, threads):
outqueue = multiprocessing.Queue()
processes = [
multiprocessing.Process(
target=single_core_bench,
args=(engine, outqueue)
) for ii in range(threads)
]
for process in processes: process.start()
return [outqueue.get() for ii in range(threads)]
def run_benchmark(engine, threads, sets):
benches, speeds = [], []
for ii in range(sets):
for bench, speed in multi_core_bench(engine, threads):
benches.append(bench); speeds.append(speed)
if len(set(benches)) != 1:
print("Error: Non-Deterministic Results!")
sys.exit()
return int(sum(speeds) / len(speeds))
def bench_built_engines():
for engine in sorted(os.listdir('Binaries')):
nps = run_benchmark('./Binaries/%s' % (engine), BENCHMARK_THREADS, BENCHMARK_SETS)
print ('%-15s | %d' % (engine, nps))
if __name__ == '__main__':
p = argparse.ArgumentParser()
p.add_argument('-U', '--username', help='OpenBench Username', required=True)
p.add_argument('-P', '--password', help='OpenBench Password', required=True)
p.add_argument('-T', '--threads' , help='Concurrent Benchmarks', required=True)
p.add_argument('-S', '--sets' , help='Benchmark Sample Count', required=True)
p.add_argument('-R', '--rebuild' , help='Rebuild Binaries', action='store_true')
arguments = p.parse_args()
USE_OLD_BINARIES = not arguments.rebuild
OPENBENCH_USERNAME = arguments.username
OPENBENCH_PASSWORD = arguments.password
BENCHMARK_THREADS = int(arguments.threads)
BENCHMARK_SETS = int(arguments.sets)
build_all_engines()
bench_built_engines()
Regular → Executable
View File
File mode changed.
Regular → Executable
View File
File mode changed.
-100
View File
@@ -1,100 +0,0 @@
import re, multiprocessing, os, subprocess, sys, time
def parseStreamOutput(output):
# None unless found
bench = None; speed = None
# Split the output line by line and look backwards
for line in output.decode('ascii').strip().split('\n')[::-1]:
# Convert all non-alpha numerics to spaces
line = re.sub(r'[^a-zA-Z0-9 ]+', ' ', line)
# Search for node or speed counters
bench1 = re.search(r'[0-9]+ NODES', line.upper())
bench2 = re.search(r'NODES[ ]+[0-9]+', line.upper())
speed1 = re.search(r'[0-9]+ NPS' , line.upper())
speed2 = re.search(r'NPS[ ]+[0-9]+' , line.upper())
# A line with no parsable information was found
if not bench1 and not bench2 and not speed1 and not speed2:
break
# A Bench value was found
if not bench and (bench1 or bench2):
bench = bench1.group() if bench1 else bench2.group()
# A Speed value was found
if not speed and (speed1 or speed2):
speed = speed1.group() if speed1 else speed2.group()
# Parse out the integer portion from our matches
bench = int(re.search(r'[0-9]+', bench).group()) if bench else None
speed = int(re.search(r'[0-9]+', speed).group()) if speed else None
return (bench, speed)
def singleCoreBench(engine, outqueue):
# Launch the bench and wait for results
stdout, stderr = subprocess.Popen(
"./{0} bench".format(engine).split(),
stdout=subprocess.PIPE, stderr=subprocess.STDOUT).communicate()
# Parse output streams for the benchmark data
bench, speed = parseStreamOutput(stdout)
if bench is None or speed is None:
bench, speed = parseStreamOutput(stderr)
outqueue.put((int(bench), int(speed)))
def multiCoreBench(engine, threads):
# Give time for any previous run to finish
time.sleep(2)
# Dump results into a Queue
outqueue = multiprocessing.Queue()
# Spawn each singleCoreBench()
processes = [
multiprocessing.Process(
target=singleCoreBench,
args=(engine, outqueue)
) for ii in range(threads)
]
# Launch each singleCoreBench()
for process in processes:
process.start()
# Wait for each thread and collect data
return [outqueue.get() for ii in range(threads)]
if __name__ == "__main__":
# Check for arguments
if len(sys.argv) < 4:
print("Usage: python3 bench.py <Engine> <Threads> <Sets>")
sys.exit()
# Run each parrallel test
benches = []; speeds = []
for ii in range(int(sys.argv[3])):
for bench, speed in multiCoreBench(sys.argv[1], int(sys.argv[2])):
benches.append(bench); speeds.append(speed)
# Log per-thread and per-set data
for ii in range(int(sys.argv[3])):
print("\nBenchmark Run #{0:>2}".format(ii))
for jj in range(int(sys.argv[2])):
index = ii * int(sys.argv[3]) + jj
print("Thread #{0:>2} NPS = {1}".format(jj, speeds[index]))
# All Benchmarks should match
if len(set(benches)) != 1:
print("Error: Non-Deterministic Results!")
sys.exit()
# Final reporting of bench and average speeds
print("\nBench for {0} is {1}".format(sys.argv[1], benches[0]))
print("Speed for {0} is {1}".format(sys.argv[1], int(sum(speeds) / len(speeds))))