-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdriver.py
124 lines (108 loc) · 3.71 KB
/
driver.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import numpy as np
import time
import pickle
from tqdm import trange
from ising import run_ising, exact_solutions
from ising_numba import run_ising_numba
from ising_cython import run_ising_cython
# Set the parameters
J = 1.0
L = [10, 20, 40, 80]
T_low = 0.5
T_high = 4.5
nT = 50
equil_steps = 5000
mc_steps = 10000
skip_steps = 100
# Compute and store exact solutions (L-independent)
T_arr, E_exact, M_exact, T_c = exact_solutions(J, L[0], T_low, T_high, nT)
with open("data/ising_exact_data.pkl", "wb") as f:
pickle.dump((T_arr, E_exact, M_exact, T_c, L), f)
# ------------------------------------------------------------
### Monte Carlo simulation with Python + Numba
# ------------------------------------------------------------
E_numba, M_numba, time_numba, spin_config_numba = [], [], [], []
# Run the simulation
for i in trange(len(L)):
start = time.time()
E, M, spin_config = run_ising_numba(
J, L[i], T_low, T_high, nT, equil_steps, mc_steps, skip_steps
)
end = time.time()
time_elapsed = end - start
E_numba.append(E)
M_numba.append(M)
time_numba.append(time_elapsed)
spin_config_numba.append(spin_config)
# Pickle the data
with open("data/ising_numba_data.pkl", "wb") as f:
pickle.dump((E_numba, M_numba, time_numba, spin_config_numba), f)
# ------------------------------------------------------------
### Monte Carlo simulation with Cython
# ------------------------------------------------------------
E_cython, M_cython, time_cython, spin_config_cython = [], [], [], []
# Run the simulation
for i in trange(len(L)):
start = time.time()
E, M, spin_config = run_ising_cython(
J, L[i], T_low, T_high, nT, equil_steps, mc_steps, skip_steps
)
end = time.time()
time_elapsed = end - start
E_cython.append(E)
M_cython.append(M)
time_cython.append(time_elapsed)
spin_config_cython.append(spin_config)
# Pickle the data
with open("data/ising_cython_data.pkl", "wb") as f:
pickle.dump((E_cython, M_cython, time_cython, spin_config_cython), f)
# ------------------------------------------------------------
### Benchmarking (scaling with system size): Pure Python, Python with Numba, and Cython
# ------------------------------------------------------------
J = 1.0
L = [i for i in range(2, 13, 2)]
T_low = 0.5
T_high = 4.5
nT = 50
equil_steps = 5000
mc_steps = 10000
skip_steps = 100
time_py, time_numba, time_cython = [], [], []
for i in trange(len(L)):
# Python
start = time.time()
_, _, _ = run_ising(J, L[i], T_low, T_high, nT, equil_steps, mc_steps, skip_steps)
end = time.time()
time_py.append(end - start)
# Python + Numba
start = time.time()
_, _, _ = run_ising_numba(
J, L[i], T_low, T_high, nT, equil_steps, mc_steps, skip_steps
)
end = time.time()
time_numba.append(end - start)
# Cython
start = time.time()
_, _, _ = run_ising_cython(
J, L[i], T_low, T_high, nT, equil_steps, mc_steps, skip_steps
)
end = time.time()
time_cython.append(end - start)
# Pickle the data
with open("data/ising_benchmark_data.pkl", "wb") as f:
pickle.dump((time_py, time_numba, time_cython, L), f)
# ------------------------------------------------------------
# E_py, M_py, time_py, spin_config_py = [], [], [], []
# # Run the simulation
# for i in trange(len(L)):
# start = time.time()
# E, M, spin_config = run_ising(J, L[i], T_low, T_high, nT, equil_steps, mc_steps, skip_steps)
# end = time.time()
# time_elapsed = end - start
# E_py.append(E)
# M_py.append(M)
# time_py.append(time_elapsed)
# spin_config_py.append(spin_config)
# # Pickle the data
# with open("data/ising_py_data.pkl", "wb") as f:
# pickle.dump((E_py, M_py, time_py, spin_config_py), f)