forked from coralabo/E2EMap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph_embedding.py
405 lines (374 loc) · 21.6 KB
/
graph_embedding.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
import numpy as np
class Graph_cgra:
def __init__(self, pea_width, ii, dfg_data, reward_mode):
self.total_node = (pea_width**2)*ii
self.graph = None
self.adj_m = None
self.normalized_adj = None
self.feature_m = None
self.net_input = None
self.dfg_data = dfg_data
self.ii = ii
self.pea_width = pea_width
self.reward_mode = reward_mode
self.gen_graph(pea_width, ii)
self.gen_adj()
self.gen_feature_m(self.total_node, pea_width, ii)
self.gen_net_input()
def gen_graph(self,pea_width, ii):
graph = np.zeros([self.total_node, 6], dtype=int)
for i in range(self.total_node):
graph[i][0] = i+1
if pea_width == 2:
for i in range(ii):
for j in range(4):
temp = [m for m in range(((i+1)*4+1)%self.total_node,((i+1)*4+1)%self.total_node+4)]
graph[i*4+j][1] = temp[j-1]
graph[i*4+j][2] = temp[j]
graph[i*4+j][3] = temp[(j+1)%4]
else:
pea_size = pea_width*pea_width
if self.reward_mode == 2:
# torus
for i in range(ii):
for j in range(pea_size):
temp = [m for m in range(((i+1)*pea_size+1)%self.total_node,((i+1)*pea_size+1)%self.total_node+pea_size)]
#print("temp:")
#print(temp)
graph[i*pea_size+j][1] = temp[(j-pea_width+pea_size)%pea_size]
graph[i*pea_size+j][2] = temp[(j-1+pea_size)%pea_size if j%pea_width != 0 else (j-1+pea_width+pea_size)%pea_size]
graph[i*pea_size+j][3] = temp[j]
graph[i*pea_size+j][4] = temp[(j+1+pea_size)%pea_size if (j+1)%pea_width != 0 else (j+1-pea_width+pea_size)%pea_size]
graph[i*pea_size+j][5] = temp[(j+pea_width+pea_size)%pea_size]
#print(graph[i*pea_size+j])
#print(1/0)
elif self.reward_mode == 1:
# mesh
for i in range(ii):
for j in range(pea_size):
temp = [m for m in range(((i+1)*pea_size+1)%self.total_node,((i+1)*pea_size+1)%self.total_node+pea_size)]
if j not in [_ for _ in range(pea_width)]:
graph[i*pea_size+j][1] = temp[j-pea_width]
if j not in [pea_width*_ for _ in range(pea_width)]:
graph[i*pea_size+j][2] = temp[j-1]
graph[i*pea_size+j][3] = temp[j]
if j not in [pea_width*(_+1)-1 for _ in range(pea_width)]:
graph[i*pea_size+j][4] = temp[j+1]
if j not in [(pea_width-1)*pea_width+_ for _ in range(pea_width)]:
graph[i*pea_size+j][5] = temp[j+pea_width]
elif self.reward_mode == 3:
# diagonal+mesh
graph = np.zeros([self.total_node, 10], dtype=int)
for i in range(self.total_node):
graph[i][0] = i+1
for i in range(ii):
temp = [m for m in range(((i+1)*pea_size+1)%self.total_node,((i+1)*pea_size+1)%self.total_node+pea_size)]
#print("temp:")
#print(temp)
for j in range(pea_size):
if j not in [_ for _ in range(pea_width)] and j not in [pea_width*_ for _ in range(pea_width)]:
graph[i*pea_size+j][1] = temp[j-pea_width-1]
if j not in [_ for _ in range(pea_width)]:
graph[i*pea_size+j][2] = temp[j-pea_width]
if j not in [_ for _ in range(pea_width)] and j not in [pea_width*(_+1)-1 for _ in range(pea_width)]:
graph[i*pea_size+j][3] = temp[j-pea_width+1]
if j not in [pea_width*_ for _ in range(pea_width)]:
graph[i*pea_size+j][4] = temp[j-1]
graph[i*pea_size+j][5] = temp[j]
if j not in [pea_width*(_+1)-1 for _ in range(pea_width)]:
graph[i*pea_size+j][6] = temp[j+1]
if j not in [pea_width*_ for _ in range(pea_width)] and j not in [(pea_width-1)*pea_width+_ for _ in range(pea_width)]:
graph[i*pea_size+j][7] = temp[j+pea_width-1]
if j not in [(pea_width-1)*pea_width+_ for _ in range(pea_width)]:
graph[i*pea_size+j][8] = temp[j+pea_width]
if j not in [pea_width*(_+1)-1 for _ in range(pea_width)] and j not in [(pea_width-1)*pea_width+_ for _ in range(pea_width)]:
graph[i*pea_size+j][9] = temp[j+pea_width+1]
#print(graph[i*pea_size+j])
#print(1/0)
elif self.reward_mode == 4:
# 1-hop+mesh
graph = np.zeros([self.total_node, 10], dtype=int)
for i in range(self.total_node):
graph[i][0] = i+1
for i in range(ii):
temp = [m for m in range(((i+1)*pea_size+1)%self.total_node,((i+1)*pea_size+1)%self.total_node+pea_size)]
#print("temp:")
#print(temp)
#print(1/0)
for j in range(pea_size):
if j not in [_ for _ in range(pea_width*2)]:
graph[i*pea_size+j][1] = temp[j-pea_width*2]
if j not in [_ for _ in range(pea_width)]:
graph[i*pea_size+j][2] = temp[j-pea_width]
if j not in [pea_width*_ for _ in range(pea_width)] and j not in [pea_width*_+1 for _ in range(pea_width)]:
graph[i*pea_size+j][3] = temp[j-2]
if j not in [pea_width*_ for _ in range(pea_width)]:
graph[i*pea_size+j][4] = temp[j-1]
graph[i*pea_size+j][5] = temp[j]
if j not in [pea_width*(_+1)-1 for _ in range(pea_width)]:
graph[i*pea_size+j][6] = temp[j+1]
if j not in [pea_width*(_+1)-1 for _ in range(pea_width)] and j not in [pea_width*(_+1)-2 for _ in range(pea_width)]:
graph[i*pea_size+j][7] = temp[j+2]
if j not in [(pea_width-1)*pea_width+_ for _ in range(pea_width)]:
graph[i*pea_size+j][8] = temp[j+pea_width]
if j not in [(pea_width-1)*pea_width+_ for _ in range(pea_width)] and j not in [(pea_width-2)*pea_width+_ for _ in range(pea_width)]:
graph[i*pea_size+j][9] = temp[j+pea_width*2]
#print(graph[i*pea_size+j])
#print(1/0)
elif self.reward_mode == 5:
graph = np.zeros([self.total_node, 14], dtype=int)
for i in range(self.total_node):
graph[i][0] = i+1
for i in range(ii):
temp = [m for m in range(((i+1)*pea_size+1)%self.total_node,((i+1)*pea_size+1)%self.total_node+pea_size)]
#print("temp:")
#print(temp)
for j in range(pea_size):
# 1-hop+mesh
if j not in [_ for _ in range(pea_width*2)]:
graph[i*pea_size+j][1] = temp[j-pea_width*2]
if j not in [_ for _ in range(pea_width)]:
graph[i*pea_size+j][2] = temp[j-pea_width]
if j not in [pea_width*_ for _ in range(pea_width)] and j not in [pea_width*_+1 for _ in range(pea_width)]:
graph[i*pea_size+j][3] = temp[j-2]
if j not in [pea_width*_ for _ in range(pea_width)]:
graph[i*pea_size+j][4] = temp[j-1]
graph[i*pea_size+j][5] = temp[j]
if j not in [pea_width*(_+1)-1 for _ in range(pea_width)]:
graph[i*pea_size+j][6] = temp[j+1]
if j not in [pea_width*(_+1)-1 for _ in range(pea_width)] and j not in [pea_width*(_+1)-2 for _ in range(pea_width)]:
graph[i*pea_size+j][7] = temp[j+2]
if j not in [(pea_width-1)*pea_width+_ for _ in range(pea_width)]:
graph[i*pea_size+j][8] = temp[j+pea_width]
if j not in [(pea_width-1)*pea_width+_ for _ in range(pea_width)] and j not in [(pea_width-2)*pea_width+_ for _ in range(pea_width)]:
graph[i*pea_size+j][9] = temp[j+pea_width*2]
# dia
if j not in [_ for _ in range(pea_width)] and j not in [pea_width*_ for _ in range(pea_width)]:
graph[i*pea_size+j][10] = temp[j-pea_width-1]
if j not in [_ for _ in range(pea_width)] and j not in [pea_width*(_+1)-1 for _ in range(pea_width)]:
graph[i*pea_size+j][11] = temp[j-pea_width+1]
if j not in [pea_width*_ for _ in range(pea_width)] and j not in [(pea_width-1)*pea_width+_ for _ in range(pea_width)]:
graph[i*pea_size+j][12] = temp[j+pea_width-1]
if j not in [pea_width*(_+1)-1 for _ in range(pea_width)] and j not in [(pea_width-1)*pea_width+_ for _ in range(pea_width)]:
graph[i*pea_size+j][13] = temp[j+pea_width+1]
# torus
if j%pea_width == 0:
graph[i*pea_size+j][4] = temp[(j-1+pea_width+pea_size)%pea_size]
if (j+1)%pea_width == 0:
graph[i*pea_size+j][6] = temp[(j+1-pea_width+pea_size)%pea_size]
if j in range(pea_width):
graph[i*pea_size+j][2] = temp[(j-pea_width+pea_size)%pea_size]
if j in range(pea_width*(pea_width-1),pea_width*pea_width):
graph[i*pea_size+j][8] = temp[(j+pea_width+pea_size)%pea_size]
#print(graph[i*pea_size+j])
#print(1/0)
elif self.reward_mode == 6:
# 1-hop+mesh+tours
graph = np.zeros([self.total_node, 10], dtype=int)
for i in range(self.total_node):
graph[i][0] = i+1
for i in range(ii):
temp = [m for m in range(((i+1)*pea_size+1)%self.total_node,((i+1)*pea_size+1)%self.total_node+pea_size)]
#print("temp:")
#print(temp)
#print(1/0)
for j in range(pea_size):
# 1-hop+mesh
if j not in [_ for _ in range(pea_width*2)]:
graph[i*pea_size+j][1] = temp[j-pea_width*2]
if j not in [_ for _ in range(pea_width)]:
graph[i*pea_size+j][2] = temp[j-pea_width]
if j not in [pea_width*_ for _ in range(pea_width)] and j not in [pea_width*_+1 for _ in range(pea_width)]:
graph[i*pea_size+j][3] = temp[j-2]
if j not in [pea_width*_ for _ in range(pea_width)]:
graph[i*pea_size+j][4] = temp[j-1]
graph[i*pea_size+j][5] = temp[j]
if j not in [pea_width*(_+1)-1 for _ in range(pea_width)]:
graph[i*pea_size+j][6] = temp[j+1]
if j not in [pea_width*(_+1)-1 for _ in range(pea_width)] and j not in [pea_width*(_+1)-2 for _ in range(pea_width)]:
graph[i*pea_size+j][7] = temp[j+2]
if j not in [(pea_width-1)*pea_width+_ for _ in range(pea_width)]:
graph[i*pea_size+j][8] = temp[j+pea_width]
if j not in [(pea_width-1)*pea_width+_ for _ in range(pea_width)] and j not in [(pea_width-2)*pea_width+_ for _ in range(pea_width)]:
graph[i*pea_size+j][9] = temp[j+pea_width*2]
# torus
if j%pea_width == 0:
graph[i*pea_size+j][4] = temp[(j-1+pea_width+pea_size)%pea_size]
if (j+1)%pea_width == 0:
graph[i*pea_size+j][6] = temp[(j+1-pea_width+pea_size)%pea_size]
if j in range(pea_width):
graph[i*pea_size+j][2] = temp[(j-pea_width+pea_size)%pea_size]
if j in range(pea_width*(pea_width-1),pea_width*pea_width):
graph[i*pea_size+j][8] = temp[(j+pea_width+pea_size)%pea_size]
else:
print("not support this structure")
#print("graph:")
#print(graph)
#print(1/0)
self.graph = graph
def gen_net_input(self):
embedding = self.feature_m.copy()
#"""
# The first part of net_input uses one-hot to represent the node number of each node
#node_number = np.identity(len(embedding))
node_number = np.identity(self.pea_width**2)
node_number = np.tile(node_number,(len(embedding)//(self.pea_width**2),1))
# The second part of net_input uses one-hot to represent the time steps of each node
#node_timestep = np.zeros([len(embedding), max(self.dfg_data[:, -3]) + 1], dtype=np.int)
node_timestep = np.zeros([len(embedding), self.ii], dtype=np.int)
node_timestep[range(len(embedding)), embedding[range(len(embedding)), 1]] = 1
# The third part of net_input uses one-hot to represent the PE number of each node
node_pe_num = np.zeros([len(embedding), max(embedding[:, 2]) + 1], dtype=np.int)
node_pe_num[range(len(embedding)), embedding[range(len(embedding)), 2]] = 1
net_input = np.concatenate([node_number, node_timestep, node_pe_num], axis=1)
#"""
#print("net_input:")
#print(net_input.shape)
"""
# The first part of net_input represents the node number of each node
node_number = np.zeros([len(embedding),1], dtype=np.int)
node_number[range(len(embedding)),0] = embedding[range(len(embedding)), 0]
# The second part of net_input uses one-hot to represent the time steps of each node
node_timestep = np.zeros([len(embedding), 1], dtype=np.int)
node_timestep[range(len(embedding)),0] = embedding[range(len(embedding)), 1]
# The third part of net_input uses one-hot to represent the time steps of each node
node_timestep2 = np.zeros([len(embedding), 1], dtype=np.int)
node_timestep2 = node_timestep
# The fourth part of net_input represents the number of adjacent nodes for each node
neighbor_number = np.zeros([len(embedding), 1], dtype=np.int)
neighbor_number[range(len(embedding)),0] = np.sum(np.count_nonzero([embedding[:,3:]], axis=0),axis=1)[range(len(embedding))]
net_input = np.concatenate([node_number, node_timestep, node_timestep2, neighbor_number], axis=1)
"""
self.net_input = net_input
def gen_adj(self):
graph = self.graph
node_num = len(graph)
adj = np.zeros([node_num, node_num], dtype=int)
for i in range(node_num):
for j in range(node_num):
if i == j:
adj[i][j] = 0
elif j + 1 in graph[i] or i + 1 in graph[j]:
adj[i][j] = 1
"""
elif j + 1 in graph[i]:
adj[i][j] = 1
elif i + 1 in graph[j]:
adj[j][i] = 1
"""
self.adj_m = adj
#print("self.adj_m:")
#print(self.adj_m)
# normalization
adj = adj + np.identity(len(adj))
rowsum = np.sum(adj, axis=1)
d_inv_sqrt = np.power(rowsum, -0.5).flatten()
d_inv_sqrt[np.isinf(d_inv_sqrt)] = 0.
d_mat_inv_sqrt = np.diag(d_inv_sqrt)
normalized_adj = np.dot(np.dot(d_mat_inv_sqrt, adj), d_mat_inv_sqrt)
self.normalized_adj = normalized_adj
def gen_feature_m(self, node_num, pea_width, ii):
#print("self.graph:")
#print(self.graph)
fea = np.zeros([node_num, 2+len(self.graph[0])], dtype=int)
for i in range(node_num):
# The first three columns are node serial number, time step, and PE number, respectively
fea[i][0] = i+1
fea[i][1] = i//(pea_width**2)
fea[i][2] = i%(pea_width**2)
# The last few columns are child nodes
for j in range(3,len(fea[0])):
fea[i][j] = self.graph[i][j-2]
self.feature_m = fea
#print("self.feature_m:")
#print(self.feature_m)
def get_graph_adj_feature_input(self):
return self.graph,self.normalized_adj,self.feature_m,self.net_input
def get_grf_size(self):
# This function is used to provide feedback on how many nodes there are
return len(self.graph)
def get_grf_input_size(self):
# This function is used to provide feedback on the feature_size of each node
_, feature_size = self.net_input.shape
return feature_size
class Graph_dfg:
def __init__(self, origin_embedding, pea_width, ii):
self.total_node = len(origin_embedding)
self.net_input = None
self.graph = None
self.adj_m = None
self.normalized_adj = None
self.pea_width = pea_width
self.ii = ii
self.gen_graph(origin_embedding)
self.gen_net_input(origin_embedding)
self.gen_adj()
self.normalize_adj(self.adj_m)
def gen_graph(self,origin_embedding):
self.graph = origin_embedding[:,:-4]
def gen_net_input(self,origin_embedding):
embedding = origin_embedding.copy()
#"""
# The first part of net_input uses one-hot to represent the node number of each node
#node_number = np.identity(self.pea_width*self.pea_width*self.ii)[:len(embedding)]
node_number = np.identity(self.pea_width*self.pea_width*self.ii)[:len(embedding)]
# The second part of net_input uses one-hot to represent the asap of each node
#node_timestep = np.zeros([len(embedding), self.ii], dtype=np.int)
#node_timestep[range(len(embedding)), (embedding[range(len(embedding)), -4])%self.ii] = 1
node_timestep = np.zeros([len(embedding), max(embedding[:, -4]) + 1], dtype=np.int)
node_timestep[range(len(embedding)), embedding[range(len(embedding)), -4]] = 1
# The third part of net_input uses one-hot to represent the alap of each node
#node_timestep2 = np.zeros([len(embedding), self.ii], dtype=np.int)
#node_timestep2[range(len(embedding)), (embedding[range(len(embedding)), -3])%self.ii] = 1
node_timestep2 = np.zeros([len(embedding), max(embedding[:, -3]) + 1], dtype=np.int)
node_timestep2[range(len(embedding)), embedding[range(len(embedding)), -3]] = 1
net_input = np.concatenate([node_number, node_timestep, node_timestep2], axis=1)
#print(1/0)
#"""
#print("embedding:")
#print(embedding)
"""
node_number = np.zeros([len(embedding),1], dtype=np.int)
node_number[range(len(embedding)),0] = embedding[range(len(embedding)), 0]
# The second part of net_input represent the asap of each node
node_timestep = np.zeros([len(embedding), 1], dtype=np.int)
node_timestep[range(len(embedding)),0] = embedding[range(len(embedding)), -4]
# The third part of net_input represent the alap of each node
node_timestep2 = np.zeros([len(embedding), 1], dtype=np.int)
node_timestep2[range(len(embedding)),0] = embedding[range(len(embedding)), -3]
# The fourth part of net_input represents the number of adjacent nodes for each node
neighbor_number = np.zeros([len(embedding), 1], dtype=np.int)
neighbor_number[range(len(embedding)),0] = np.sum(np.count_nonzero([embedding[:,1:-4]], axis=0),axis=1)[range(len(embedding))]
net_input = np.concatenate([node_number, node_timestep, node_timestep2, neighbor_number], axis=1)
"""
#print("net_input:")
#print(net_input)
#print(1/0)
self.net_input = net_input
def gen_adj(self):
graph = self.graph
node_num = len(graph)
adj = np.zeros([node_num, node_num], dtype=int)
for i in range(node_num):
for j in range(node_num):
if i == j:
adj[i][j] = 0
elif j + 1 in graph[i] or i + 1 in graph[j]:
adj[i][j] = 1
self.adj_m = adj
def normalize_adj(self, adj):
# normalization
adj = adj + np.identity(len(adj))
rowsum = np.sum(adj, axis=1)
d_inv_sqrt = np.power(rowsum, -0.5).flatten()
d_inv_sqrt[np.isinf(d_inv_sqrt)] = 0.
d_mat_inv_sqrt = np.diag(d_inv_sqrt)
normalized_adj = np.dot(np.dot(d_mat_inv_sqrt, adj), d_mat_inv_sqrt)
self.normalized_adj = normalized_adj
def get_grf_size(self):
# This function is used to provide feedback on how many nodes there are
return len(self.graph)
def get_grf_input_size(self):
# This function is used to provide feedback on the feature_size of each node
_, feature_size = self.net_input.shape
return feature_size