From e134c0ed8303e6f7ce9e382cbc0da18322fcf889 Mon Sep 17 00:00:00 2001 From: Viren6 <94880762+Viren6@users.noreply.github.com> Date: Wed, 19 Jun 2024 13:52:09 +0100 Subject: [PATCH] Fix TC Scaling Co-Authored-By: Jamie Whiting <99771266+jw1912@users.noreply.github.com> --- worker/games.py | 114 ++++++++++++++++++++++-------------------------- 1 file changed, 52 insertions(+), 62 deletions(-) diff --git a/worker/games.py b/worker/games.py index 28a1b6265..5976423ef 100644 --- a/worker/games.py +++ b/worker/games.py @@ -387,6 +387,31 @@ def establish_validated_net(remote, testing_dir, net, global_cache): ) time.sleep(waitTime) + +def run_single_bench(engine, queue): + bench_sig = None + bench_nps = None + + try: + p = subprocess.Popen( + [engine, "bench"], + stderr=subprocess.PIPE, + stdout=subprocess.DEVNULL, + universal_newlines=True, + bufsize=1, + close_fds=not IS_WINDOWS, + ) + + for line in iter(p.stderr.readline, ""): + if "Nodes searched" in line: + bench_sig = line.split(": ")[1].strip() + if "Nodes/second" in line: + bench_nps = float(line.split(": ")[1].strip()) + + queue.put((bench_sig, bench_nps)) + except: + queue.put((None, None)) + def verify_signature(engine, signature, active_cores): cpu_features = "?" @@ -408,74 +433,39 @@ def verify_signature(engine, signature, active_cores): ) ) - with ExitStack() as stack: - if active_cores > 1: - busy_process = stack.enter_context( - subprocess.Popen( - [engine], - stdin=subprocess.PIPE, - stdout=subprocess.DEVNULL, - universal_newlines=True, - bufsize=1, - close_fds=not IS_WINDOWS, - ) - ) - busy_process.stdin.write( - "setoption name Threads value {}\n".format(active_cores - 1) - ) - busy_process.stdin.write("go infinite\n") - busy_process.stdin.flush() - time.sleep(1) # wait CPU loading - - bench_sig = None - bench_nps = None - print("Verifying signature of {} ...".format(os.path.basename(engine))) - p = stack.enter_context( - subprocess.Popen( - [engine, "bench"], - stderr=subprocess.PIPE, - stdout=subprocess.DEVNULL, - universal_newlines=True, - bufsize=1, - close_fds=not IS_WINDOWS, - ) - ) - for line in iter(p.stderr.readline, ""): - if "Nodes searched" in line: - bench_sig = line.split(": ")[1].strip() - if "Nodes/second" in line: - bench_nps = float(line.split(": ")[1].strip()) + queue = multiprocessing.Queue() - if active_cores > 1: - busy_process.communicate("quit\n") + processes = [ + multiprocessing.Process( + target=run_single_bench, + args=(engine, queue), + ) for _ in range(active_cores) + ] - if p.returncode != 0: - if p.returncode == 1: # EXIT_FAILURE + for p in processes: + p.start() + + results = [queue.get() for _ in range(active_cores)] + bench_nps = 0.0 + + for sig, nps in results: + + if sig is None or bench_nps is None: raise RunException( - "Bench of {} exited with EXIT_FAILURE".format(os.path.basename(engine)) + "Unable to parse bench output of {}".format(os.path.basename(engine)) ) - else: # Signal? It could be user generated so be careful. - raise WorkerException( - "Bench of {} exited with error code {}".format( - os.path.basename(engine), format_return_code(p.returncode) - ) - ) - - # Now we know that bench finished without error we check that its - # output is correct. - if bench_sig is None or bench_nps is None: - raise RunException( - "Unable to parse bench output of {}".format(os.path.basename(engine)) - ) + if int(sig) != int(signature): + message = "Wrong bench in {}, user expected: {} but worker got: {}".format( + os.path.basename(engine), + signature, + sig, + ) + raise RunException(message) + + bench_nps += nps - if int(bench_sig) != int(signature): - message = "Wrong bench in {}, user expected: {} but worker got: {}".format( - os.path.basename(engine), - signature, - bench_sig, - ) - raise RunException(message) + bench_nps /= active_cores return bench_nps, cpu_features