From cbe1469a3eaea56edeb944422ced05f179d0fe4c Mon Sep 17 00:00:00 2001 From: dialecticDolt Date: Tue, 17 Jan 2023 21:33:34 -0600 Subject: [PATCH] Adding partitioned threading benchmark --- .../overhead/timestep/parallel_threads.py | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 benchmarks/overhead/timestep/parallel_threads.py diff --git a/benchmarks/overhead/timestep/parallel_threads.py b/benchmarks/overhead/timestep/parallel_threads.py new file mode 100644 index 0000000..aeeb865 --- /dev/null +++ b/benchmarks/overhead/timestep/parallel_threads.py @@ -0,0 +1,78 @@ +import csv +import time +import argparse + +import threading + +from sleep.core import bsleep, sleep_with_gil + +free_sleep = bsleep +lock_sleep = sleep_with_gil + +parser = argparse.ArgumentParser() +parser.add_argument("-workers", type=int, default=1) +parser.add_argument("-n", type=int, default=3) +parser.add_argument("-t", type=int, default=10) +parser.add_argument("-accesses", type=int, default=10) +parser.add_argument("-frac", type=float, default=0) +parser.add_argument('-sweep', type=int, default=0) +parser.add_argument('-barrier', type=int, default=0) +args = parser.parse_args() + + +def task(barrier, n, t, accesses, frac, free_time, lock_time): + for k in range(accesses): + free_sleep(free_time) + lock_sleep(lock_time) + +def thread_task(barrier, n, t, accesses, frac): + + kernel_time = t / accesses + free_time = kernel_time * (1 - frac) + lock_time = kernel_time * frac + for i in range(n): + task(barrier, n, t, accesses, frac, free_time, lock_time) + if args.barrier: + barrier.wait() + +def drange(start, stop): + while start < stop: + yield start + start <<= 1 + +def main(workers, n, task_time, accesses, frac): + barrier = threading.Barrier(workers) + threads = [] + + for i in range(workers): + per = (n//workers + (i < n % workers)) + t = threading.Thread(target=thread_task, args=(barrier, per, + task_time, accesses, frac)) + threads.append(t) + + start_t = time.perf_counter() + + for t in threads: + t.start() + + for t in threads: + t.join() + + end_t = time.perf_counter() + + elapsed_t = end_t - start_t + print(', '.join([str(workers), str(n), str(task_time), str( + accesses), str(frac), str(elapsed_t)]), flush=True) + +if __name__ == "__main__": + print(', '.join([str('workers'), str('n'), str('task_time'), str( + 'accesses'), str('frac'), str('total_time')]), flush=True) + for task_time in [1000, 5000, 10000, 50000, 100000]: + for accesses in [1, 10]: + for nworkers in drange(1, args.workers): + for frac in [0]: + main(nworkers, args.n, task_time, accesses, frac) + + + +