From 26a5ccdeb11d0e1b926e0b272cc2a2ffa1adc31d Mon Sep 17 00:00:00 2001 From: Scott Gasch Date: Fri, 23 Apr 2021 17:13:59 -0700 Subject: Move parallelize_test to tests dir. --- parallelize_test.py | 94 ----------------------------------------------- tests/parallelize_test.py | 94 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+), 94 deletions(-) delete mode 100755 parallelize_test.py create mode 100755 tests/parallelize_test.py diff --git a/parallelize_test.py b/parallelize_test.py deleted file mode 100755 index 051ec5d..0000000 --- a/parallelize_test.py +++ /dev/null @@ -1,94 +0,0 @@ -#!/usr/bin/env python3 - -import random -import sys - -import bootstrap -import parallelize as p -import decorator_utils -import executors -import math_utils - - -@p.parallelize(method=p.Method.REMOTE) -def list_primes(n): - """Calculates sum of all primes below given integer n""" - ret = [] - for x in range(2, n): - ret.append(math_utils.is_prime(x)) - return ret - - -@decorator_utils.timed -def driver() -> None: - results = {} - for _ in range(200): - n = random.randint(0, 100000) - results[n] = list_primes(n) - tot = 0 - for _ in results[n]: - tot += _ - print(tot) - - -@bootstrap.initialize -def main() -> None: - print(driver()) - pexecutor = executors.DefaultExecutors().process_pool() - pexecutor.shutdown() - texecutor = executors.DefaultExecutors().thread_pool() - texecutor.shutdown() - rexecutor = executors.DefaultExecutors().remote_pool() - rexecutor.shutdown() - sys.exit(0) - - -if __name__ == '__main__': - main() - -# print """Usage: python sum_primes.py [ncpus] -# [ncpus] - the number of workers to run in parallel, -# if omitted it will be set to the number of processors in the system -# """ - -# # tuple of all parallel python servers to connect with -# ppservers = () -# #ppservers = ("10.0.0.1",) - -# if len(sys.argv) > 1: -# ncpus = int(sys.argv[1]) -# # Creates jobserver with ncpus workers -# job_server = pp.Server(ncpus, ppservers=ppservers) -# else: -# # Creates jobserver with automatically detected number of workers -# job_server = pp.Server(ppservers=ppservers) - -# print "Starting pp with", job_server.get_ncpus(), "workers" - -# # Submit a job of calulating sum_primes(100) for execution. -# # sum_primes - the function -# # (100,) - tuple with arguments for sum_primes -# # (isprime,) - tuple with functions on which function sum_primes depends -# # ("math",) - tuple with module names which must be imported before sum_primes execution -# # Execution starts as soon as one of the workers will become available -# job1 = job_server.submit(sum_primes, (100,), (isprime,), ("math",)) - -# # Retrieves the result calculated by job1 -# # The value of job1() is the same as sum_primes(100) -# # If the job has not been finished yet, execution will wait here until result is available -# result = job1() - -# print "Sum of primes below 100 is", result - -# start_time = time.time() - -# # The following submits 8 jobs and then retrieves the results -# inputs = (100000, 100100, 100200, 100300, 100400, 100500, 100600, 100700) -# jobs = [(input, job_server.submit(sum_primes,(input,), (isprime,), ("math",))) for input in inputs] -# for input, job in jobs: -# print "Sum of primes below", input, "is", job() - -# print "Time elapsed: ", time.time() - start_time, "s" -# job_server.print_stats() - -# # Parallel Python Software: http://www.parallelpython.com diff --git a/tests/parallelize_test.py b/tests/parallelize_test.py new file mode 100755 index 0000000..051ec5d --- /dev/null +++ b/tests/parallelize_test.py @@ -0,0 +1,94 @@ +#!/usr/bin/env python3 + +import random +import sys + +import bootstrap +import parallelize as p +import decorator_utils +import executors +import math_utils + + +@p.parallelize(method=p.Method.REMOTE) +def list_primes(n): + """Calculates sum of all primes below given integer n""" + ret = [] + for x in range(2, n): + ret.append(math_utils.is_prime(x)) + return ret + + +@decorator_utils.timed +def driver() -> None: + results = {} + for _ in range(200): + n = random.randint(0, 100000) + results[n] = list_primes(n) + tot = 0 + for _ in results[n]: + tot += _ + print(tot) + + +@bootstrap.initialize +def main() -> None: + print(driver()) + pexecutor = executors.DefaultExecutors().process_pool() + pexecutor.shutdown() + texecutor = executors.DefaultExecutors().thread_pool() + texecutor.shutdown() + rexecutor = executors.DefaultExecutors().remote_pool() + rexecutor.shutdown() + sys.exit(0) + + +if __name__ == '__main__': + main() + +# print """Usage: python sum_primes.py [ncpus] +# [ncpus] - the number of workers to run in parallel, +# if omitted it will be set to the number of processors in the system +# """ + +# # tuple of all parallel python servers to connect with +# ppservers = () +# #ppservers = ("10.0.0.1",) + +# if len(sys.argv) > 1: +# ncpus = int(sys.argv[1]) +# # Creates jobserver with ncpus workers +# job_server = pp.Server(ncpus, ppservers=ppservers) +# else: +# # Creates jobserver with automatically detected number of workers +# job_server = pp.Server(ppservers=ppservers) + +# print "Starting pp with", job_server.get_ncpus(), "workers" + +# # Submit a job of calulating sum_primes(100) for execution. +# # sum_primes - the function +# # (100,) - tuple with arguments for sum_primes +# # (isprime,) - tuple with functions on which function sum_primes depends +# # ("math",) - tuple with module names which must be imported before sum_primes execution +# # Execution starts as soon as one of the workers will become available +# job1 = job_server.submit(sum_primes, (100,), (isprime,), ("math",)) + +# # Retrieves the result calculated by job1 +# # The value of job1() is the same as sum_primes(100) +# # If the job has not been finished yet, execution will wait here until result is available +# result = job1() + +# print "Sum of primes below 100 is", result + +# start_time = time.time() + +# # The following submits 8 jobs and then retrieves the results +# inputs = (100000, 100100, 100200, 100300, 100400, 100500, 100600, 100700) +# jobs = [(input, job_server.submit(sum_primes,(input,), (isprime,), ("math",))) for input in inputs] +# for input, job in jobs: +# print "Sum of primes below", input, "is", job() + +# print "Time elapsed: ", time.time() - start_time, "s" +# job_server.print_stats() + +# # Parallel Python Software: http://www.parallelpython.com -- cgit v1.3