-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththroughput-da-periodic.py
More file actions
186 lines (163 loc) · 7.48 KB
/
throughput-da-periodic.py
File metadata and controls
186 lines (163 loc) · 7.48 KB
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
#%%
# Import Statements
import gurobipy as gp
from gurobipy import GRB
import numpy as np
import sys
import networkx as nx
import random
#%%
# Set the number of vertices
N = int(sys.argv[1])
# N=16
#%%
# Set the degree for each vertex
# degree = sys.argv[2] # Define the degree variable
degree=4
#%%
# List of matrices
workdir="/home/vamsi/src/phd/codebase/rdcn-throughput/matrices/"
matrices16 = [
"chessboard-16",
"uniform-16",
"permutation-16",
"skew-16-0.0",
"skew-16-0.1",
"skew-16-0.2",
"skew-16-0.3",
"skew-16-0.4",
"skew-16-0.5",
"skew-16-0.6",
"skew-16-0.7",
"skew-16-0.8",
"skew-16-0.9",
"skew-16-1.0",
"data-parallelism","hybrid-parallelism","heatmap2","heatmap3"]
matrices64 = ["chessboard-64",
"uniform-64",
"permutation-64",
"skew-64-0.0",
"skew-64-0.1",
"skew-64-0.2",
"skew-64-0.3",
"skew-64-0.4",
"skew-64-0.5",
"skew-64-0.6",
"skew-64-0.7",
"skew-64-0.8",
"skew-64-0.9",
"skew-64-1.0",]
degrees = [16, 14, 12, 10, 8, 6, 4]
matrices=matrices16
if N==16:
matrices=matrices16
degrees = [16, 14, 12, 10, 8, 6, 4]
elif N==64:
matrices=matrices64
degrees = [4, 8, 16, 32, 64]
else:
print("Set N=16 or N=64")
exit
#%%
print("mygrep,N,matrix,maxValue,networkType,degree,throughput")
iterations=[1-i*0.01 for i in range(50)]
for degree in degrees:
for matrixfile in matrices:
demandMatrix = np.loadtxt(workdir+matrixfile+".mat", usecols=range(N))
demandMatrix=demandMatrix*degree # The original matrix is normalized. We scale the matrix to for the specified degree
maxValue = np.ceil(np.max(demandMatrix))
print("################")
print(matrixfile, "demand-aware-periodic")
print("################")
finalIteration = 0.5
for iteration in iterations:
print("################")
print(iteration)
print("################")
demand = demandMatrix*iteration
# Create a new Gurobi model
model = gp.Model()
demandFloor = np.floor(demand*N/degree)
# Create a dictionary to store capacity variables for vertex pairs
capacity = {}
for i in range(N):
for j in range(N):
capacity[(i, j)] = demandFloor[i,j]
outDegree = [0]*N
inDegree = [0]*N
for row in range(N):
outDegree[row] = int(N-1 - np.sum(demandFloor[row]))
for column in range(N):
inDegree[column] = int(N-1 - np.sum(demandFloor[:,column]))
minimumDegree = np.min([N-1, np.min([outDegree,inDegree])])
if minimumDegree>=1:
G = nx.random_regular_graph(minimumDegree,N)
for i in range(N):
for j in range(N):
if (i,j) in G.edges:
capacity[(i,j)] = capacity[(i,j)] + 1
# Create a dictionary to store flow variables for each node pair (i, j) corresponding to all node pairs (s, d)
flow_variables = {}
for i in range(N):
for j in range(N):
if i != j:
flow_variables[(i, j)] = {}
for s in range(N):
for d in range(N):
if s != d:
# Create a flow variable for each node pair (i, j) corresponding to node pairs (s, d)
flow_variables[(i, j)][(s, d)] = model.addVar(vtype=GRB.CONTINUOUS, name=f'flow_{i}_{j}_{s}_{d}', lb=0)
# Create a variable for throughput with lower bound 0
throughput = model.addVar(vtype=GRB.CONTINUOUS, name='throughput', lb=0, ub=1)
# Update the model to include these variables
model.update()
# Implement the source demand constraints for all s in N and all d in N
for s in range(N):
for d in range(N):
if s != d:
# Define the source demand constraint
source_demand_constraint_expr = gp.quicksum(flow_variables[(s, i)][(s, d)] for i in range(N) if i != s) >= throughput * demand[s, d]
model.addConstr(source_demand_constraint_expr, f'source_demand_constraint_{s}_{d}')
# Implement the destination demand constraints for all s in N and all d in N
for s in range(N):
for d in range(N):
if s != d:
# Define the destination demand constraint
dest_demand_constraint_expr = gp.quicksum(flow_variables[(i, d)][(s, d)] for i in range(N) if i != d) >= throughput * demand[s, d]
model.addConstr(dest_demand_constraint_expr, f'dest_demand_constraint_{s}_{d}')
# Implement the flow conservation constraints for all j in N (excluding s and d), s in N, and d in N
for j in range(N):
for s in range(N):
for d in range(N):
if j != s and j != d and s != d: # Ensuring s, j, and d are all different
# Define the flow conservation constraint
flow_conservation_constraint_expr = gp.quicksum(flow_variables[(i, j)][(s, d)] for i in range(N) if i != j) - gp.quicksum(flow_variables[(j, k)][(s, d)] for k in range(N) if k != j) == 0
model.addConstr(flow_conservation_constraint_expr, f'flow_conservation_constraint_{j}_{s}_{d}')
# Implement the capacity constraints for every i in N, for every j in N
for i in range(N):
for j in range(N):
if i != j:
capacity_constraint_expr = gp.quicksum(flow_variables[(i, j)][(s, d)] for s in range(N) for d in range(N) if s != d) <= (degree/N)*capacity[(i, j)]
model.addConstr(capacity_constraint_expr, f'capacity_constraint_{i}_{j}')
# Set the objective to maximize throughput
model.setObjective(throughput, GRB.MAXIMIZE)
# Optimize the model
model.optimize()
if model.objVal == 1:
finalIteration = iteration
break
# Check the optimization status
if model.status == GRB.OPTIMAL:
print("mygrep"+str(",")+str(N)+str(",")+str(matrixfile)+str(",")+str(maxValue)+str(",")+str("demand-aware-periodic")+str(",")+str(degree)+str(",")+str(finalIteration))
else:
print("mygrep"+str(",")+str(N)+str(",")+str(matrixfile)+str(",")+str(maxValue)+str(",")+str("demand-aware-periodic")+str(",")+str(degree)+str(",")+"NULL")
# # Print capacity values (commented out)
# '''
# print("Capacity Values:")
# for i in range(N):
# for j in range(N):
# if i != j:
# print(f'Capacity_{i}_{j} = {capacity[(i, j)].x}')
# '''
# You can also access the variable values if needed, e.g., flow_values = model.getAttr('x', flow_variables)
#%%