-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_benchmark.py
73 lines (58 loc) · 2.05 KB
/
run_benchmark.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
from time import time
import numpy as np
import torch
import matplotlib.pyplot as plt
import modern_robotics as mr
from tqdm import tqdm
from mrobo_torch.core import TorchKinematics, get_px150_M_Slist
def benchmark_parallel_iterate(device, n_iterations=20):
M, Slist = get_px150_M_Slist()
kinematics = TorchKinematics(M, Slist, device)
times = []
for i in range(n_iterations):
thetalist = torch.rand(2 ** i, 5, device=kinematics.M.device)
start = time()
_ = kinematics.forward(thetalist)
took = time() - start
times.append(took)
print('Iteration {0} took {1}'.format(i, took))
return times
def benchmark_sequential_iterate(n_iterations=20):
M, Slist = get_px150_M_Slist()
times = []
for i in range(n_iterations):
thetalist = torch.rand(2 ** i, 5)
start = time()
all_r = []
for t in tqdm(thetalist):
all_r.append(mr.FKinSpace(M, Slist, t.cpu().numpy()))
_ = np.stack(all_r, axis=0)
took = time() - start
times.append(took)
print('Iteration {0} took {1}'.format(i, took))
return times
def benchmark_sequential(n_iterations=20):
# Warm up
_ = benchmark_sequential_iterate(5)
return benchmark_sequential_iterate(n_iterations)
def benchmark_parallel(device, n_iterations=20):
# Warm up
_ = benchmark_parallel_iterate(device, 5)
return benchmark_parallel_iterate(device, n_iterations)
def main():
cpu_times = benchmark_sequential()
np.save('cpu_times.npy', cpu_times)
vectorized_times = benchmark_parallel('cpu')
np.save('vectorized_times.npy', vectorized_times)
gpu_times = benchmark_parallel('cuda:0')
np.save('gpu_times.npy', gpu_times)
# plt.plot(cpu_times, label='Modern robotics')
plt.plot(vectorized_times, label='Ours (CPU)')
plt.plot(gpu_times, label='Ours (GPU) ')
plt.legend()
plt.xlabel('Number of input configurations (log scale)')
plt.ylabel('Time [s]')
plt.savefig('benchmark.eps')
plt.show()
if __name__ == '__main__':
main()