-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfunction_mp_ray.py
More file actions
66 lines (51 loc) · 1.97 KB
/
function_mp_ray.py
File metadata and controls
66 lines (51 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import ray
import time
import os
total_iters = 10000
sleep_time = 0.01
#########################################################
# Parallel Execution of test function #
#########################################################
# Get the number of cpus or manually define how many cpus you want to use
num_cpus = os.cpu_count()
print (" Using {} CPUs for processing.".format(num_cpus))
# Manually define the number of workers the default is set to number of CPUs
num_workers = min(total_iters, 20)
print ("Initializing {} workers for processing.".format(num_workers))
# Initialize ray
ray.init(num_cpus = num_cpus)
# Define a test function which should be executed parallely, default num of cpus is 1, change it as required
@ray.remote
def test_function_parallel(ii, a, b):
time.sleep(sleep_time)
return a + b
# Initialize the workers
workers = [test_function_parallel for _ in range(num_workers)]
sums = 0
max_iters = min(10000, total_iters - 1)
object_ids = []
start = time.time()
for ii in range(total_iters):
a = 1 # args, change
b = 1 # args, change
object_id = workers[ii%num_workers].remote(ii, a, b) # call, change
object_ids.append(object_id)
# after max_iters the objects are fetched before processing further iterations
if ii % max_iters == 0 or ii == len(range(total_iters)):
sums += sum(ray.get(object_ids))
object_ids = []
# get remaining objects
sums += sum(ray.get(object_ids)) # return values, change
print("Time taken by Parallel Execution in seconds : ", time.time() - start, sums)
#########################################################
# Serial Execution of test function #
#########################################################
# def test_function_serial(a, b):
# time.sleep(sleep_time)
# return a + b
# a = 1
# b = 1
# start = time.time()
# for ii in range(total_iters):
# test_function_serial(a,b)
# print("Time taken by Serial Execution in seconds : ", time.time() - start)