diff --git a/Client/Client.py b/Client/Client.py index 892352a..5012982 100644 --- a/Client/Client.py +++ b/Client/Client.py @@ -52,7 +52,7 @@ TIMEOUT_WORKLOAD = 30 # Timeout in seconds between workload requests CLIENT_VERSION = '2' # Client version to send to the Server SYZYGY_WDL_PATH = None # Pathway to WDL Syzygy Tables -BASE_GAMES_PER_CORE = 16 # Typical games played per-thread +BASE_GAMES_PER_CORE = 32 # Typical games played per-thread FLEET_MODE = False # Exit when there are no workloads CUSTOM_SETTINGS = { @@ -583,10 +583,10 @@ def complete_workload(arguments, workload): download_opening_book(arguments, workload) avg_nps = (dev_nps + base_nps) // 2 - concurrency, command = build_cutechess_command( + concurrency, update_interval, command = build_cutechess_command( arguments, workload, dev_name, base_name, avg_nps) - run_and_parse_cutechess(arguments, workload, concurrency, command) + run_and_parse_cutechess(arguments, workload, concurrency, update_interval, command) def download_opening_book(arguments, workload): @@ -757,10 +757,10 @@ def build_cutechess_command(arguments, workload, dev_name, base_name, nps): if SYZYGY_WDL_PATH and workload['test']['syzygy_adj'] != 'DISABLED': flags += '-tb %s' % (SYZYGY_WDL_PATH.replace('\\', '\\\\')) - throughput = int(workload['test']['throughput']) concurrency = int(arguments.threads) // max(dev_threads, base_threads) + update_interval = 8 if max(dev_threads, base_threads) == 1 else 1 - games = int(concurrency * BASE_GAMES_PER_CORE * throughput / 1000) + games = int(concurrency * BASE_GAMES_PER_CORE) games = max(8, concurrency * 2, games - (games % (2 * concurrency))) time_control = scale_time_control(workload, nps) @@ -775,10 +775,10 @@ def build_cutechess_command(arguments, workload, dev_name, base_name, nps): ) if IS_LINUX: - return concurrency, './cutechess-ob ' + flags % (args) - return concurrency, 'cutechess-ob.exe ' + flags % (args) + return concurrency, update_interval, './cutechess-ob ' + flags % (args) + return concurrency, update_interval, 'cutechess-ob.exe ' + flags % (args) -def run_and_parse_cutechess(arguments, workload, concurrency, command): +def run_and_parse_cutechess(arguments, workload, concurrency, update_interval, command): print('\nLaunching Cutechess...\n%s\n' % (command)) cutechess = Popen(command.split(), stdout=PIPE) @@ -814,7 +814,7 @@ def run_and_parse_cutechess(arguments, workload, concurrency, command): # Only report scores after every eight games score = list(map(int, score_reason.split()[0:5:2])) - if ((sum(score) - sum(sent)) % 8 != 0): continue + if ((sum(score) - sum(sent)) % update_interval != 0): continue # Report to the server but allow failed reports to delay wld = [score[ii] - sent[ii] for ii in range(3)] diff --git a/OpenBench/utils.py b/OpenBench/utils.py index 58bd915..b352d20 100644 --- a/OpenBench/utils.py +++ b/OpenBench/utils.py @@ -318,66 +318,88 @@ def getWorkload(user, request): machine = getMachine(machineid, user, osname, int(threads)) if type(machine) == str: return machine - # Filter only to active tests + # Check to make sure we have a potential workload + tests = get_valid_workloads(machine, request) + if not tests: return 'None' + + # Select from valid workloads and create a Result object + test = select_workload(machine, tests) + result = getResult(test, machine) + + # Success. Update the Machine's status and save everything + machine.workload = test; machine.save(); result.save() + return str(workload_to_dictionary(test, result, machine)) + + +# Purely Helper functions for getWorkload() + +def get_valid_workloads(machine, request): + + # Skip finished tests tests = Test.objects.filter(finished=False) - tests = tests.filter(deleted=False) - tests = tests.filter(approved=True) + tests = tests.filter(deleted=False).filter(approved=True) - # Filter to compilable engines - supported = request.POST['supported'].split() - cpuflags = request.POST['cpuflags'].split() - - # Remove tests that this Machine cannot complete (Compile) + # Skip engines that the Machine cannot build for engine in OPENBENCH_CONFIG['engines'].keys(): - if engine not in supported: + if engine not in request.POST['supported'].split(): tests = tests.exclude(engine=engine) - # Remove tests that this Machine cannot complete (Architecture) + # Skip engines that the Machine cannot run for engine, data in OPENBENCH_CONFIG['engines'].items(): for flag in data['build']['cpuflags']: - if flag not in cpuflags: + if flag not in request.POST['cpuflags'].split(): tests = tests.exclude(engine=engine) - # Remove Syzygy tests if the Machine has no Syzygy Tablebases + # Skip tests with unmet Syzygy requirments if request.POST['syzygy_wdl'] == 'False': tests = tests.exclude(syzygy_adj='REQUIRED') tests = tests.exclude(syzygy_wdl='REQUIRED') - # If we don't get a Test back, there was an error - test = selectWorkload(tests, machine) - if type(test) == str: return test + # Skip tests that would waste available Threads or exceed them + options = [x for x in tests if test_maps_onto_thread_count(machine, x)] - # If we don't get a Result back, there was an error - result = getResult(test, machine) - if type(result) == str: return result + # Finally refine for tests of the highest priority + highest_prio = max(options, key=lambda x: x.priority).priority + return [test for test in options if test.priority == highest_prio] - # Success. Update the Machine's status and save everything - machine.workload = test; machine.save(); result.save() - return str(workloadDictionary(test, result, machine)) +def test_maps_onto_thread_count(machine, test): -def selectWorkload(tests, machine): + # Only assign a workload to a machine if the machine actually has + # enough Threads for the test. Furthermore, ensure that there are + # no left over threads when assigned. An exception is made for the + # case where thread counts differ -- a bad idea to run in general - options = [] # Tests which this Machine may complete + dev_threads = int(extractOption(test.devoptions, 'Threads')) + base_threads = int(extractOption(test.baseoptions, 'Threads')) - for test in tests: + if max(dev_threads, base_threads) > machine.threads: return False + return dev_threads != base_threads or machine.threads % dev_threads == 0 - # Skip tests which the Machine lacks the threads to complete - devthreads = int(extractOption(test.devoptions, 'Threads')) - basethreads = int(extractOption(test.baseoptions, 'Threads')) - if max(devthreads, basethreads) > machine.threads: continue +def select_workload(machine, tests, acceptable_variance=1.25): - # First Test or a higher priority Test - if not options or test.priority > highest: - highest = test.priority; options = [test] + # Determine how many threads are assigned to each workload + table = { test : 0 for test in tests } + for m in getRecentMachines(): + if m.workload in tests and m != machine: + table[m.workload] = table[m.workload] + m.threads - # Another Test with an equal priority - elif options and test.priority == highest: - options.append(test) + # Find the tests most deserving of resources currently + ratios = [table[test] / test.throughput for test in tests] + lowest_idxs = [i for i, r in enumerate(ratios) if r == min(ratios)] - if len(options) == 0: return 'None' - return random.choice(options) + # Machine has not recently been working on the current options + if machine.workload not in tests or min(ratios) == 0: + return tests[random.choice(lowest_idxs)] -def workloadDictionary(test, result, machine): + # Machine can repeat its workload without exceeding distrubtion limits + ideal_ratio = sum(table.values()) / sum([x.throughput for x in tests]) + if ratios[tests.index(machine.workload)] / ideal_ratio < acceptable_variance: + return machine.workload + + # Fallback to simply doing the least attention given test + return tests[random.choice(lowest_idxs)] + +def workload_to_dictionary(test, result, machine): # Convert the workload into a Dictionary to be used by the Client. # The Client must know his Machine ID, his Result ID, and his Test @@ -389,31 +411,32 @@ def workloadDictionary(test, result, machine): 'result' : { 'id' : result.id }, - 'test' : { + 'test' : { - 'id' : test.id, - 'engine' : test.engine, - 'timecontrol' : test.timecontrol, - 'throughput' : test.throughput, - 'syzygy_adj' : test.syzygy_adj, - 'syzygy_wdl' : test.syzygy_wdl, + 'throughput' : 1000, # HACK: Updated Client's no longer need this value - 'nps' : OPENBENCH_CONFIG['engines'][test.engine]['nps'], - 'build' : OPENBENCH_CONFIG['engines'][test.engine]['build'], - 'book' : OPENBENCH_CONFIG['books'][test.bookname], + 'id' : test.id, + 'engine' : test.engine, + 'timecontrol' : test.timecontrol, + 'syzygy_adj' : test.syzygy_adj, + 'syzygy_wdl' : test.syzygy_wdl, - 'dev' : { - 'id' : test.dev.id, 'name' : test.dev.name, - 'source' : test.dev.source, 'sha' : test.dev.sha, - 'bench' : test.dev.bench, 'options' : test.devoptions, - 'network' : test.devnetwork, + 'nps' : OPENBENCH_CONFIG['engines'][test.engine]['nps'], + 'build' : OPENBENCH_CONFIG['engines'][test.engine]['build'], + 'book' : OPENBENCH_CONFIG['books'][test.bookname], + + 'dev' : { + 'id' : test.dev.id, 'name' : test.dev.name, + 'source' : test.dev.source, 'sha' : test.dev.sha, + 'bench' : test.dev.bench, 'options' : test.devoptions, + 'network' : test.devnetwork, }, 'base' : { - 'id' : test.base.id, 'name' : test.base.name, - 'source' : test.base.source, 'sha' : test.base.sha, - 'bench' : test.base.bench, 'options' : test.baseoptions, - 'network' : test.basenetwork, + 'id' : test.base.id, 'name' : test.base.name, + 'source' : test.base.source, 'sha' : test.base.sha, + 'bench' : test.base.bench, 'options' : test.baseoptions, + 'network' : test.basenetwork, }, }, })