Skip to content
Snippets Groups Projects
Commit ef4b3ec9 authored by xiyehu2's avatar xiyehu2
Browse files

Merge remote-tracking branch 'origin/master'

parents c2aaea24 cf50da3a
No related branches found
No related tags found
No related merge requests found
import numpy as np
from waveform import *
import cupy as cp
from cupyx.profiler import benchmark
import cupy as cp
def test(f_idx, t_idx):
paths, off = get_rearrange_paths(f_idx, t_idx)
create_moving_array_reduced(_sig, _table_cp, paths, off)
def count(paths, path_table, off):
counter = 0
for i, j in paths:
if i == j:
continue
if (i, j) in path_table:
counter += 1
# else:
# print((i, j), "not in path table")
for i in off:
counter += 1
return counter
# data = np.load("data/rearrange_table_11.npz", allow_pickle=True)
# table = data['path_table']
# twzr = data['wfm'].item()
# t = np.zeros(11)
# t[:5] = 1
# t_idx = np.nonzero(t)[0]
data = np.load("data/table-half_31.npz", allow_pickle=True)
table = data['path_table'].item()
twzr = data['wfm'].item()
static_sig = data['static_sig']
target = data['target']
t_idx = np.nonzero(target)[0]
nt = twzr.omega.size
# f = np.zeros(11)
# f[:6] = 1
_table_cp = {}
# table_cp = cp.array(table)
# for i in range(1000):
# np.random.shuffle(f)
# f_idx = np.nonzero(f)[0]
# paths = cp.array(get_rearrange_paths(t_idx, f_idx))
# b = benchmark(create_moving_array_gpu, (table_cp, paths), n_repeat=1)
for key in table:
_table_cp[key] = cp.array(table[key])
# print(b.gpu_times)
n_repeat = 500
_sig = cp.array(static_sig)
times = np.zeros((nt+1,2))
filling_ratio = np.zeros((nt+1,2))
n_move = np.zeros((nt+1,n_repeat))
for i in range(nt+1):
f_prob = i/nt
calc_t = np.zeros(n_repeat)
ratio = np.zeros(n_repeat)
nm = np.zeros(n_repeat)
print(i, f_prob)
for j in range(n_repeat):
filled = np.random.rand(nt)
tr = filled < f_prob
fa = filled >= f_prob
filled[tr] = 1
filled[fa] = 0
f_idx = np.nonzero(filled)[0]
b = benchmark(
test,
(f_idx, t_idx),
n_repeat=1
)
# stuff to save
ratio[j] = f_idx.size / nt
calc_t[j] = b.gpu_times + b.cpu_times
paths, off = get_rearrange_paths(f_idx, t_idx)
nm[j] = count(paths, _table_cp, off)
t = np.array([1,1,0,1,0])
f = np.array([0,1,0,1,1])
t = np.nonzero(t)[0]
f = np.nonzero(f)[0]
print(get_rearrange_paths(t, f))
# n_move[i,0] = np.mean(nm)
# n_move[i,1] = np.var(nm)
n_move[i] = nm
times[i,0] = np.mean(calc_t)
times[i,1] = np.var(calc_t)
filling_ratio[i,0] = np.mean(ratio)
filling_ratio[i,1] = np.var(ratio)
np.savez(
f"data/reduced-benchmark_{nt}-half.npz",
wfm=twzr,
target=target,
filling_ratio=filling_ratio,
times=times,
n_move=n_move
)
......@@ -53,7 +53,7 @@ def create_static_array(wfm: Waveform) -> np.ndarray:
return sig.astype(np.int16)
def create_path_table(wfm: Waveform) -> np.ndarray:
def create_path_table(wfm: Waveform) -> any:
"""
create a dim-3 look up table where the table[i,j] contains a sine wave to move tweezer i to tweezer j
:param wfm: waveform object already initialized with basic parameters.
......@@ -168,7 +168,6 @@ def create_path_table_reduced(
vmax = KILO(20) * MEGA(1) # convert units, 20 kHz/us -> 20e3 * 1e6 Hz/s
t_max = 2 * dw_max / vmax # Longest move sets the maximum moving time
a_max = vmax * 2 / t_max # maximum acceleration, negative sign because of magic
# get number of samples required for longest move,this sets the size of lookup table
sample_len = int(np.ceil(t_max * wfm.sample_rate))
# sample_len += (512 - sample_len % 512) # make overall length a multiple of 512 so AWG doesn't freak out
......@@ -184,7 +183,9 @@ def create_path_table_reduced(
for j in moves[i]: # j is the target position, i is starting position
omega_j = wfm.omega[j]
if i == j:
path = wfm.amplitude[i] * np.sin(omega_i * t + wfm.phi[i])
path = (
wfm.amplitude[i] * np.sin(omega_i * t + wfm.phi[i])
).astype(np.int16)
# path = omega_i * t + wfm.phi[i]
path_table[(i, i)] = path
static_sig += path
......@@ -248,13 +249,13 @@ def create_path_table_reduced(
a / 2 * t_tot / 2 * t2 ** 2 + \
a / 6 * t2 ** 3 # t>=T/2
path[end:] = path[end-1] + omega_j * (t[end:] - t[end-1])
path = amps * np.sin(path)
path = (amps * np.sin(path)).astype(np.int16)
path_table[(i, j)] = path
for key in path_table:
if key[0] != key[1]:
path_table[key] -= path_table[(key[1], key[1])] # for fast real-time generation
path_table[key] = path_table[key].astype(np.int16)
# path_table[key] = path_table[key].astype(np.int16)
return path_table, static_sig.astype(np.int16)
......@@ -281,7 +282,7 @@ def get_rearrange_paths(
while i < f_size:
if j == t_size: break
if filled_idx[i] == target_idx[j]:
paths.append((filled_idx[i], filled_idx[i]))
# paths.append((filled_idx[i], filled_idx[i]))
j += 1
i = j
elif (reserve > 0
......@@ -327,8 +328,8 @@ def create_moving_array_reduced(
continue
if (i, j) in path_table:
sig += path_table[(i, j)]
else:
print((i, j), "not in path table")
# else:
# print((i, j), "not in path table")
for i in off:
sig -= path_table[(i, i)]
pass
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment