-
Notifications
You must be signed in to change notification settings - Fork 4
/
dpe_tcam.py
executable file
·90 lines (74 loc) · 2.48 KB
/
dpe_tcam.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
import numpy as np
def tcam_input(vec, vsearch=1, r_start=0, method='2dpe'):
"""transfer search vector into voltage input"""
vec = np.expand_dims(vec, 1)
if method == '3dpe':
vec = np.tile(vec, 3)
for i in range(np.shape(vec)[0]):
if vec[i][0] == 1:
vec[i] = [1, -1, 0]
elif vec[i][0] == 0:
vec[i] = [0, -1, 1]
else:
vec[i] = [0, 0, 0]
elif method == '2dpe':
vec = np.tile(vec, 2)
for i in range(np.shape(vec)[0]):
if vec[i][0] == 1:
vec[i] = [1, 0]
elif vec[i][0] == 0:
vec[i] = [0, 1]
else:
vec[i] = [0, 0]
vec = np.reshape(vec, -1)
vec = np.expand_dims(vec, 1)
if vec.shape[0]<64:
input_vec = np.zeros((64, 1))
input_vec[r_start:r_start+np.shape(vec)[0], :] = vec*vsearch
return input_vec
elif vec.shape[0]>=64:
return vec*vsearch
def tcam_storage(vec, G_on, G_off, method='2dpe'):
"""store info in memristor array """
# input:
# array(int): which array
# vec(np.ndarray): info stored in TCAM [N, num_bits]
stor_vec = []
numRows = np.shape(vec)[0]
numCols = np.shape(vec)[1]
if method == '3dpe':
for r in range(numRows):
vec_in = []
for c in range(numCols):
if vec[r, c] == 1:
vec_in.extend([G_off, G_off, G_on])
elif vec[r, c] == 0:
vec_in.extend([G_on, G_off, G_off])
else:
vec_in.extend([G_off, G_off, G_off])
stor_vec.append(vec_in)
elif method == '2dpe':
for r in range(numRows):
vec_in = []
for c in range(numCols):
if vec[r, c] == 1:
vec_in.extend([G_off, G_on])
elif vec[r, c] == 0:
vec_in.extend([G_on, G_off])
else:
vec_in.extend([G_off, G_off])
stor_vec.append(vec_in)
stor_vec = np.array(stor_vec).T
return stor_vec
def tcam_search(array, vec, dpe, c_sel=[0, 10]):
"""core part of tcam"""
# Args:
# array(int):which array to search
# vec:the input
# Return
# the current of the output
Iresult = dpe.multiply(array,
vec,
c_sel=c_sel,
r_start=0, mode=0, Tdly=1000)
return Iresult