-
Notifications
You must be signed in to change notification settings - Fork 0
/
ray_example.py
84 lines (67 loc) · 2.39 KB
/
ray_example.py
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import argparse
import logging
import os
from utils.ray_test import compute_reciprocals
from execution_time import ExecutionTime
import ray
import ray.util
import numpy as np
np.random.seed(0)
import time
e = ExecutionTime()
def dir_path(string):
if os.path.isdir(string):
return string
else:
raise NotADirectoryError(string)
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('--cpu', default=False, action="store_true", help="Run computation on CPU")
parser.add_argument('--ray', default=False, action="store_true", help="Run computation on CPU")
args = parser.parse_args()
return args
@ray.remote(num_cpus=4)
class Counter(object):
def __init__(self):
self.n = 0
def increment(self):
self.n += 2
def read(self):
return self.n
@ray.remote
def compute_reciprocals_ray(values):
return compute_reciprocals(values)
@e.timeit
def main():
# set TF_XLA_FLAGS env variable to increase GPU utilization
# os.environ['TF_XLA_FLAGS'] = '--tf_xla_enable_xla_devices'
# if args.cpu:
# os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
print("This runs on the VM")
if __name__ == '__main__':
args = parse_args()
repeater = 10
if args.ray:
if ray.is_initialized() == False:
# ray.init(local_mode=True) # for debug purposes
ray.init(address='192.168.1.11:6379', _redis_password='5241590000000000', logging_level=logging.DEBUG)
# ray.util.connect("192.168.1.11:10001") # replace with the appropriate host and port
# call main function as ray.remote
# future = main.remote()
# print("This runs locally")
# ray.get(future)
# counters = [Counter.remote() for i in range(4)]
# [c.increment.remote() for c in counters]
# futures = [c.read.remote() for c in counters]
big_array = np.random.randint(1, 100, size=2000000)
start = time.time()
results = [compute_reciprocals_ray.remote(big_array) for i in range(repeater)]
print(len(ray.get(results)))
print("duration =", time.time() - start)
else:
big_array = np.random.randint(1, 100, size=2000000)
start = time.time()
results = [compute_reciprocals(big_array) for _ in range(repeater)]
print(len(results))
# print(e.logtime_data)
print("duration =", time.time() - start)