diff --git a/Client/client.py b/Client/client.py index db3cb39..7ce5cb7 100644 --- a/Client/client.py +++ b/Client/client.py @@ -67,6 +67,17 @@ def has_worker(): except ImportError: return False +def custom_help(default_help): + + print (default_help) + + if has_worker(): + import worker + importlib.reload(worker) + worker.run_openbench_worker(None) + + exit() + def parse_arguments(): # We can use ENV variables for the Username, Passwords, and Servers @@ -74,24 +85,27 @@ def parse_arguments(): req_pass = 'OPENBENCH_PASSWORD' not in os.environ req_server = 'OPENBENCH_SERVER' not in os.environ - help_user = 'Username. May also be passed as OPENBENCH_USERNAME environment variable' - help_pass = 'Password. May also be passed as OPENBENCH_PASSWORD environment variable' - help_server = 'Server URL. May also be passed as OPENBENCH_SERVER environment variable' + help_user = 'Username. May also be provided via OPENBENCH_USERNAME environment variable' + help_pass = 'Password. May also be provided via OPENBENCH_PASSWORD environment variable' + help_server = 'Server URL. May also be provided via OPENBENCH_SERVER environment variable' + + # Pretty formatting + p = argparse.ArgumentParser( + formatter_class=lambda prog: + argparse.ArgumentDefaultsHelpFormatter(prog, max_help_position=10) + ) # Create and parse all arguments into a raw format - p = argparse.ArgumentParser() - p.add_argument('-U', '--username' , help=help_user , required=req_user ) - p.add_argument('-P', '--password' , help=help_pass , required=req_pass ) - p.add_argument('-S', '--server' , help=help_server , required=req_server) - p.add_argument('-T', '--threads' , help='Total Threads' , required=True ) - p.add_argument('-N', '--nsockets' , help='Number of Sockets' , required=True ) - p.add_argument('-I', '--identity' , help='Machine pseudonym' , required=False ) - p.add_argument( '--syzygy' , help='Syzygy WDL' , required=False ) - p.add_argument( '--fleet' , help='Fleet Mode' , action='store_true') - p.add_argument( '--clean' , help='Force New Client' , action='store_true') + p.add_argument('-U', '--username', help=help_user , required=req_user ) + p.add_argument('-P', '--password', help=help_pass , required=req_pass ) + p.add_argument('-S', '--server' , help=help_server , required=req_server) + p.add_argument( '--clean' , help='Force New Client', action='store_true') - args = p.parse_args() + # Override, to possibly print worker.py's help as well as client.py's + p.print_help = lambda: custom_help(p.format_help()) + # Replace with ENV variables if needed + args, unknown = p.parse_known_args() args.username = args.username if args.username else os.environ['OPENBENCH_USERNAME'] args.password = args.password if args.password else os.environ['OPENBENCH_PASSWORD'] args.server = args.server if args.server else os.environ['OPENBENCH_SERVER' ] diff --git a/Client/worker.py b/Client/worker.py index 1006775..e6a51a5 100644 --- a/Client/worker.py +++ b/Client/worker.py @@ -50,7 +50,7 @@ from pgn_util import compress_list_of_pgns ## Basic configuration of the Client. These timeouts can be changed at will -CLIENT_VERSION = 23 # Client version to send to the Server +CLIENT_VERSION = 24 # Client version to send to the Server TIMEOUT_HTTP = 30 # Timeout in seconds for HTTP requests TIMEOUT_ERROR = 10 # Timeout in seconds when any errors are thrown TIMEOUT_WORKLOAD = 30 # Timeout in seconds between workload requests @@ -100,6 +100,7 @@ class Configuration: self.identity = args.identity if args.identity else 'None' self.syzygy_path = args.syzygy if args.syzygy else None self.fleet = args.fleet if args.fleet else False + self.focus = args.focus if args.focus else [] def init_client(self): @@ -976,6 +977,7 @@ def server_configure_worker(config): 'concurrency' : config.threads, # Threads to use to play games 'sockets' : config.sockets, # Cutechess copies, usually equal to Socket count 'syzygy_max' : config.syzygy_max, # Whether or not the machine has Syzygy support + 'focus' : config.focus, # List of engines we have a preference to help 'client_ver' : CLIENT_VERSION, # Version of the Client, which the server may reject } @@ -1375,9 +1377,32 @@ def run_and_parse_cutechess(config, command, cutechess_idx, results_queue, abort # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # -def run_openbench_worker(args): +def parse_arguments(client_args): - config = Configuration(args) # System info, Cmdline arguments, and Workload + # Pretty formatting + p = argparse.ArgumentParser( + formatter_class=lambda prog: + argparse.ArgumentDefaultsHelpFormatter(prog, max_help_position=10) + ) + + # Arguments specific to worker.py + p.add_argument('-T', '--threads' , help='Total Threads' , required=True ) + p.add_argument('-N', '--nsockets', help='Number of Sockets' , required=True ) + p.add_argument('-I', '--identity', help='Machine pseudonym' , required=False ) + p.add_argument( '--syzygy' , help='Syzygy WDL' , required=False ) + p.add_argument( '--fleet' , help='Fleet Mode' , action='store_true') + p.add_argument( '--focus' , help='Prefer certain engine(s)', nargs='+' ) + + # Ignore unknown arguments ( from client ) + worker_args, unknown = p.parse_known_args() + + # Add the client args (Username, Password, and Server) to the worker args + return argparse.Namespace(**{ **vars(client_args), **vars(worker_args) }) + +def run_openbench_worker(client_args): + + args = parse_arguments(client_args) # Merge client.py and worker.py args + config = Configuration(args) # Holds System info, args, and Workload info setup_error = '[Note] Unable to establish initial connection with the Server!' connection_error = '[Note] Unable to reach the server to request a workload!' diff --git a/Config/config.json b/Config/config.json index f26bbb7..10bb939 100644 --- a/Config/config.json +++ b/Config/config.json @@ -1,5 +1,5 @@ { - "client_version" : 23, + "client_version" : 24, "client_repo_url" : "https://github.com/AndyGrant/OpenBench", "client_repo_ref" : "master", diff --git a/OpenBench/workloads/get_workload.py b/OpenBench/workloads/get_workload.py index 97d8f19..c4dfd85 100644 --- a/OpenBench/workloads/get_workload.py +++ b/OpenBench/workloads/get_workload.py @@ -55,77 +55,73 @@ def get_workload(machine): def select_workload(machine): - # Find valid workloads, given the current distribution of workers - active, distribution = worker_distribution(machine) - if not (tests := filter_valid_workloads(active, machine, distribution)): - return {} + # Step 1: Refine active workloads to the candidate assignments + candidates, has_focus = filter_valid_workloads(machine) - # Find the tests most deserving of resources currently - test_ratios = effective_allocation(tests, distribution) - lowest_indices = [i for i, r in enumerate(test_ratios) if r == min(test_ratios)] + # Step 2: Count relevant threads on each candidate test + worker_dist, engine_freq = compute_resource_distribution(candidates, machine, has_focus) - # Machine is out of date; or there is an unassigned test - if machine.workload not in distribution or min(test_ratios) == 0: - return tests[random.choice(lowest_indices)] + # Step 3: Determine the effective-throughput for each workload + if OPENBENCH_CONFIG['balance_engine_throughputs']: + for id, data in worker_dist.items(): + data['throughput'] = data['throughput'] / engine_dist[data['engine']] - # Determine the "fair" ratio given the total threads and total throughput of tests - total_threads = sum([distribution[x.id]['threads'] for x in tests]) - total_throughput = sum([x.throughput for x in tests]) - fair_ratio = total_threads / total_throughput + # Step 4: Compute the Resource Ratios for each of the workloads + for id, data in worker_dist.items(): + data['ratio'] = data['threads'] / data['throughput'] - # Repeat the same test if the distribution, is fair, and has the previous test - if min(test_ratios) / fair_ratio > 0.75: - if (test := Test.objects.get(id=machine.workload)) in tests: - return test + # Step 5: Compute the idealized "Fair-Ratio" + min_ratio = min(x['ratio'] for x in worker_dist.values()) + thread_sum = sum(x['threads'] for x in worker_dist.values()) + throughput_sum = sum(x['throughput'] for x in worker_dist.values()) + fair_ratio = thread_sum / throughput_sum - # Fallback to simply doing the least attention given test - return tests[random.choice(lowest_indices)] + # Step 6: Repeat the same machine, if we are still at least 75% fair across the board + if machine.workload in worker_dist.keys(): + if thread_sum > 0 and min_ratio / fair_ratio > 0.75: + return Test.objects.get(machine.workload) -def worker_distribution(machine): + # Step 7: Pick a random test, amongst those who share the min_ratio, weighted by throughput + choices = [id for id, data in worker_dist.items() if data['ratio'] == min_ratio] + weights = [data['throughput'] for id, data in worker_dist.items() if data['ratio'] == min_ratio] + return Test.objects.get(id=random.choices(choices, weights=weights)[0]) - # For each test ( which some worker claims to be running ), return - # a dict with the # of workers and # of threads actively on the test +def filter_valid_workloads(machine): - tests = OpenBench.utils.get_active_tests() - - distribution = { - test.id : { 'workers' : 0, 'threads' : 0 } for test in tests - } - - # Don't count ourselves; and don't include non-active tests - for x in OpenBench.utils.getRecentMachines(1200): - if machine != x and x.workload in distribution: - distribution[x.workload]['workers'] += 1 - distribution[x.workload]['threads'] += x.info['concurrency'] - - return tests, distribution - -def filter_valid_workloads(tests, machine, distribution): + workloads = OpenBench.utils.get_active_tests() # Skip engines that the Machine cannot handle for engine in OPENBENCH_CONFIG['engines'].keys(): if engine not in machine.info['supported']: - tests = tests.exclude(dev_engine=engine) - tests = tests.exclude(base_engine=engine) + workloads = workloads.exclude(dev_engine=engine) + workloads = workloads.exclude(base_engine=engine) - # Skip tests with unmet Syzygy requirements + # Skip workloads with unmet Syzygy requirements for K in range(machine.info['syzygy_max'] + 1, 10): - tests = tests.exclude(syzygy_adj='%d-MAN' % (K)) - tests = tests.exclude(syzygy_wdl='%d-MAN' % (K)) + workloads = workloads.exclude(syzygy_adj='%d-MAN' % (K)) + workloads = workloads.exclude(syzygy_wdl='%d-MAN' % (K)) - # Skip tests that would waste available Threads or exceed them - options = [x for x in tests if valid_assignment(machine, x, distribution)] + # Skip workloads that we have insufficient threads to play + options = [x for x in workloads if valid_hardware_assignment(x, machine)] - # Finally refine for tests of the highest priority - if not options: return [] - highest_prio = max(options, key=lambda x: x.priority).priority - return [test for test in options if test.priority == highest_prio] + # Refine to workloads of the highest priority + priorities = [x.priority for x in options] + candidates = [x for x in options if x.priority == max(priorities)] -def valid_assignment(machine, test, distribution): + # Refine to workloads that match our focus, if applicable + focuses = machine.info.get('focus', []) + has_focus = any(x.dev_engine in focuses for x in candidates) - # Extract thread requirements from the test itself - dev_threads = int(OpenBench.utils.extract_option(test.dev_options, 'Threads')) - base_threads = int(OpenBench.utils.extract_option(test.base_options, 'Threads')) + if has_focus: + candidates = list(filter(lambda x: x.dev_engine in focuses, candidates)) + + return candidates, has_focus + +def valid_hardware_assignment(workload, machine): + + # Extract thread requirements from the workload itself + dev_threads = int(OpenBench.utils.extract_option(workload.dev_options, 'Threads')) + base_threads = int(OpenBench.utils.extract_option(workload.base_options, 'Threads')) # Extract the information from our machine threads = machine.info['concurrency'] @@ -136,7 +132,7 @@ def valid_assignment(machine, test, distribution): threads = threads // 2 # SPSA plays a pair at a time, not a game at a time - is_spsa = test.test_mode == 'SPSA' + is_spsa = workload.test_mode == 'SPSA' # Refuse if there are not enough threads for the test if (1 + is_spsa) * max(dev_threads, base_threads) > threads: @@ -145,6 +141,33 @@ def valid_assignment(machine, test, distribution): # All Criteria have been met return True +def compute_resource_distribution(workloads, machine, has_focus): + + # Return a thread count, and engine name for each workload, as well as the throughput. + # The throughput may be scaled down later, due to balance_engine_throughputs + + worker_dist = { + workload.id : { 'threads' : 0, 'engine' : workload.dev_engine, 'throughput' : workload.throughput } + for workload in workloads + } + + # Ignore our own machine; + # Ignore machines working on non-candidates; + # Ignore focus-assigned machines when has_focus is false + + for x in OpenBench.utils.getRecentMachines(): + if x != machine and x.workload in worker_dist: + if has_focus or worker_dist[x.workload]['engine'] not in x.info.get('focus', []): + worker_dist[x.workload]['threads'] += x.info['concurrency'] + + # Count of tests that exist for a particular dev_engine + + engine_freq = {} + for workload in workloads: + engine_freq[workload.dev_engine] = engine_freq.get(workload.dev_engine, 0) + 1 + + return worker_dist, engine_freq + def workload_to_dictionary(test, result, machine): workload = {}