d87b5e76bac766a666ce0bc636a1da0cd5c7a2e3
[python_utils.git] / tests / parallelize_test.py
1 #!/usr/bin/env python3
2
3 import random
4 import sys
5
6 import bootstrap
7 import parallelize as p
8 import decorator_utils
9 import executors
10 import math_utils
11 import smart_future
12
13
14 @p.parallelize(method=p.Method.THREAD)
15 def compute_factorial_thread(n):
16     total = 1
17     for x in range(2, n):
18         total *= x
19     return total
20
21
22 @p.parallelize(method=p.Method.PROCESS)
23 def compute_factorial_process(n):
24     total = 1
25     for x in range(2, n):
26         total *= x
27     return total
28
29
30 @p.parallelize(method=p.Method.REMOTE)
31 def list_primes(n):
32     """Calculates sum of all primes below given integer n"""
33     ret = []
34     for x in range(2, n):
35         ret.append(math_utils.is_prime(x))
36     return ret
37
38
39 @decorator_utils.timed
40 def driver() -> None:
41     results = []
42     for _ in range(20):
43         results.append(compute_factorial_process(_))
44     for future in smart_future.wait_any(results):
45         print(f'Process: {future}')
46
47     results = []
48     for _ in range(20):
49         results.append(compute_factorial_thread(_))
50     smart_future.wait_all(results)
51     for future in results:
52         print(f'Thread: {future}')
53
54     results = {}
55     for _ in range(50):
56         n = random.randint(0, 100000)
57         results[n] = list_primes(n)
58     tot = 0
59     for _ in results[n]:
60         tot += _
61     print(tot)
62
63
64 @bootstrap.initialize
65 def main() -> None:
66     print(driver())
67     pexecutor = executors.DefaultExecutors().process_pool()
68     pexecutor.shutdown()
69     texecutor = executors.DefaultExecutors().thread_pool()
70     texecutor.shutdown()
71     rexecutor = executors.DefaultExecutors().remote_pool()
72     rexecutor.shutdown()
73     sys.exit(0)
74
75
76 if __name__ == '__main__':
77     main()
78
79 # print """Usage: python sum_primes.py [ncpus]
80 #     [ncpus] - the number of workers to run in parallel,
81 #     if omitted it will be set to the number of processors in the system
82 # """
83
84 # # tuple of all parallel python servers to connect with
85 # ppservers = ()
86 # #ppservers = ("10.0.0.1",)
87
88 # if len(sys.argv) > 1:
89 #     ncpus = int(sys.argv[1])
90 #     # Creates jobserver with ncpus workers
91 # job_server = pp.Server(ncpus, ppservers=ppservers)
92 # else:
93 #     # Creates jobserver with automatically detected number of workers
94 # job_server = pp.Server(ppservers=ppservers)
95
96 # print "Starting pp with", job_server.get_ncpus(), "workers"
97
98 # # Submit a job of calulating sum_primes(100) for execution.
99 # # sum_primes - the function
100 # # (100,) - tuple with arguments for sum_primes
101 # # (isprime,) - tuple with functions on which function sum_primes depends
102 # # ("math",) - tuple with module names which must be imported before sum_primes execution
103 # # Execution starts as soon as one of the workers will become available
104 # job1 = job_server.submit(sum_primes, (100,), (isprime,), ("math",))
105
106 # # Retrieves the result calculated by job1
107 # # The value of job1() is the same as sum_primes(100)
108 # # If the job has not been finished yet, execution will wait here until result is available
109 # result = job1()
110
111 # print "Sum of primes below 100 is", result
112
113 # start_time = time.time()
114
115 # # The following submits 8 jobs and then retrieves the results
116 # inputs = (100000, 100100, 100200, 100300, 100400, 100500, 100600, 100700)
117 # jobs = [(input, job_server.submit(sum_primes,(input,), (isprime,), ("math",))) for input in inputs]
118 # for input, job in jobs:
119 #     print "Sum of primes below", input, "is", job()
120
121 # print "Time elapsed: ", time.time() - start_time, "s"
122 # job_server.print_stats()
123
124 # # Parallel Python Software: http://www.parallelpython.com