This repository has been archived by the owner on Jun 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
276 lines (244 loc) · 10.7 KB
/
server.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
# python server.py create
import subprocess
import multiprocessing
import numpy as np
import math
import random
import json
import sys
import time
# COMPUTE time --> total.txt
def write_total(correct_count, start_time):
with open("total.txt", "w") as file:
file.write(f"number of correct: {correct_count}\n")
file.write(f"number of incorrect: {incorrect_count}\n")
file.write(f"elapsed time: {time.time() - start_time} seconds\n")
# SEND worker sequence number, F (json) & G (json)
def worker(i, F_json, G_json, M_json, m_json):
subprocess.run(["python", "worker.py", str(i), F_json, G_json, M_json, m_json])
# WRITE file function
def write_to_file(file, content):
with open(file, 'a') as f:
f.write(content + '\n')
# COUNT number of worker (correct) --> total.txt
def get_number_of_correct():
with open("total.txt", "r") as file:
lines = file.readlines()
for line in lines:
if "number of correct:" in line:
return int(line.split(":")[1].strip())
# COUNT number of worker (incorrect) --> total.txt
def get_number_of_incorrect():
with open("total.txt", "r") as file:
lines = file.readlines()
for line in lines:
if "number of incorrect:" in line:
return int(line.split(":")[1].strip())
# CREATE random matrix (X, Y)
def create_matrix(rows, cols):
matrix = np.random.randint(2, size=(rows, cols))
return matrix
# DIVIDE sub matrix 1
def print_sub_matrices_1(matrix, submatrix_rows, submatrix_cols):
num_rows, num_cols = matrix.shape
sub_matrices = {}
for i in range(0, num_rows, submatrix_rows):
for j in range(0, num_cols, submatrix_cols):
sub_matrix = matrix[i : i + submatrix_rows, j : j + submatrix_cols]
sub_matrix_name = f"A_{i//submatrix_rows +1}_{j//submatrix_cols+ 1}"
sub_matrices[sub_matrix_name] = sub_matrix
return sub_matrices
# DIVIDE sub matrix 2
def print_sub_matrices_2(matrix, submatrix_rows, submatrix_cols):
num_rows, num_cols = matrix.shape
sub_matrices = {}
for i in range(0, num_rows, submatrix_rows):
for j in range(0, num_cols, submatrix_cols):
sub_matrix = matrix[i : i + submatrix_rows, j : j + submatrix_cols]
sub_matrix_name = f"B_{i//submatrix_rows +1}_{j//submatrix_cols+ 1}"
sub_matrices[sub_matrix_name] = sub_matrix
return sub_matrices
# GEN additional matrix 1 (row)
def create_additional_matrices_1(submatrix_rows, submatrix_cols, n, delta_pc):
additional_matrices = {}
for i in range(1, delta_pc + 1):
for j in range(1, n + 1):
sub_matrix = create_matrix(submatrix_rows, submatrix_cols)
sub_matrix_name = f"R_{i}_{j}"
additional_matrices[sub_matrix_name] = sub_matrix
return additional_matrices
# GEN additional matrix 2 (col)
def create_additional_matrices_2(submatrix_rows, submatrix_cols, n, delta_pc):
additional_matrices = {}
for i in range(n, 0, -1):
for j in range(1, delta_pc + 1):
sub_matrix = create_matrix(submatrix_rows, submatrix_cols)
sub_matrix_name = f"R'_{i}_{j}"
additional_matrices[sub_matrix_name] = sub_matrix
return additional_matrices
# COMPUTE F(z)
def calc_F(sub_matrices, additional_matrices, z, m, n, delta_pc):
F1 = 0
F2 = 0
for i in range(1, m + 1):
for j in range(1, n + 1):
F1 += sub_matrices[f"A_{i}_{j}"] * pow(z, n * (i - 1) + (j - 1))
for i in range(1, delta_pc + 1):
for j in range(1, n + 1):
F2 += additional_matrices[f"R_{i}_{j}"] * pow(z, n * (i - 1) + (j - 1))
F = F1 + F2
return F
# COMPUTE G(z)
def calc_G(sub_matrices, additional_matrices, z, m, n, p, delta_pc):
G1 = 0
G2 = 0
for k in range(1, n + 1):
for l in range(1, p + 1):
G1 += sub_matrices[f"B_{k}_{l}"] * pow(
z, n - k + (m + delta_pc) * n * (l - 1)
)
for k in range(1, n + 1):
for l in range(1, delta_pc + 1):
G2 += additional_matrices[f"R'_{k}_{l}"] * pow(
z, (m + delta_pc) * n * p + n * l - k
)
G = G1 + G2
return G
# GEN key (random 1 - 20)
def key_gen():
key = random.randint(1, 20)
return key
# COMPUTE recovery threshold Pr
def recovery_threshold(m, n, p, delta_pc, Pc):
if (delta_pc == Pc/n):
return (m + delta_pc) * n * (p + 1) + n * delta_pc - 1
else:
return (m + delta_pc) * n * (p + 1) - n * delta_pc + 2 * Pc - 1
if __name__ == "__main__":
# CREATE process (server when start)
if len(sys.argv) == 1:
total_runtimes = [] # List to store runtimes
with open("graph.txt", "w"):
pass
for run in range(1500): # Run the program 1500 times
start_time = time.time() # start count time
with open("result.txt", "w"):
pass
with open("total.txt", "w"):
pass
# M, N, P, m, n, p, Pc = map(int, input("Enter M, N, P, m, n, p, Pc: ").split())
M = 10
N = 10
P = 10
m = 2
n = 2
p = 2
Pc = 2
delta_pc = math.ceil(Pc / n) # COMPUTE delta_pc
# GEN 2 MATRIX
matrix1 = create_matrix(M, N)
matrix2 = create_matrix(N, P)
# DIVIDE matrix & GEN additional matrix
sub_matrices1 = print_sub_matrices_1(matrix1, M // m, N // n)
additional_matrices1 = create_additional_matrices_1(M // m, N // n, n, delta_pc)
sub_matrices2 = print_sub_matrices_2(matrix2, N // n, P // p)
additional_matrices2 = create_additional_matrices_2(N // n, P // p, n, delta_pc)
# CONVERT key -> numpy type
key = np.int64(key_gen())
key_json = int(key)
with open("result.txt", "w") as f:
f.write("Matrix 1:\n")
np.savetxt(f, matrix1, fmt="%d")
f.write("\nMatrix 2:\n")
np.savetxt(f, matrix2, fmt="%d")
f.write("\nGenerated Key: " + json.dumps(key_json) + '\n')
for i in range(0, 40): # 40 workers (changeable)
F = calc_F(sub_matrices1, additional_matrices1, i, m, n, delta_pc)
G = calc_G(sub_matrices2, additional_matrices2, i, m, n, p, delta_pc)
FxG = np.dot(F, G)
FxG_key = np.dot(FxG, key)
write_to_file("result.txt", f"worker {i+1} (FxG_key):\n{FxG_key}")
F_json = json.dumps(F.tolist())
M_json = json.dumps(M)
m_json = json.dumps(m)
G_json = json.dumps(G.tolist())
r = multiprocessing.Process(target=worker, args=(i, F_json, G_json, M_json, m_json))
r.start()
r.join()
correct_count = get_number_of_correct()
incorrect_count = get_number_of_incorrect()
if correct_count >= recovery_threshold(m, n, p, delta_pc, Pc):
print("[+] Number of correct exceeds recovery threshold. Stopping all workers.")
write_total(correct_count, start_time)
# write time, num of correct & incorrect -> graph.txt
with open("graph.txt", "a") as total_total_file:
total_total_file.write(f"Run {run + 1}: {time.time() - start_time}s, ")
total_total_file.write(f"incorrect: {incorrect_count}, ")
total_total_file.write(f"correct: {correct_count}\n")
with open("total.txt", "w"):
pass
break
# CHECK process (server when receive data from workers)
if len(sys.argv) == 6 and sys.argv[1] == "check":
# receive worker sequence number, F mul G (json)
i = int(sys.argv[2])
FmulG_json = sys.argv[3]
FmulG = np.array(json.loads(FmulG_json))
M = json.loads(sys.argv[4])
m = json.loads(sys.argv[5])
x = int(M/m)
# GET key gen from result.txt
with open("result.txt", "r") as file:
content = file.read()
start_index = content.find("Generated Key:")
if start_index != -1:
end_index = content.find("\nworker", start_index)
key_string = content[start_index + len("Generated Key:"):end_index].strip()
key = int(key_string)
key_int64 = np.int64(key)
# CONVERT F mul G from numpy type -> string
FmulG_check = np.dot(FmulG, key_int64)
FmulG_check_str = str(FmulG_check)
# GET F mul G
with open('result.txt', 'r') as file:
lines = file.readlines()
last_two_lines = lines[-M:-x]
last_two_lines_str = ("".join(last_two_lines))
# with open("result.txt", "a") as file:
# file.write(f"{FmulG_check}\n")
# file.write(f"{last_two_lines_str[:-1]}")
# CHECK server value & workers value
if np.array_equal(FmulG_check_str, last_two_lines_str[:-1]):
with open("result.txt", "a") as file:
file.write("Correct Array\n")
with open("total.txt", "r+") as total_file:
lines = total_file.readlines()
num_correct = 0
num_incorrect = 0
for line in lines:
if "number of correct:" in line:
num_correct = int(line.split(":")[1])
elif "number of incorrect:" in line:
num_incorrect = int(line.split(":")[1])
num_correct += 1
total_file.seek(0)
total_file.truncate()
total_file.write(f"number of correct: {num_correct}\n")
total_file.write(f"number of incorrect: {num_incorrect}\n")
else:
with open("result.txt", "a") as file:
file.write("Incorrect Array\n")
with open("total.txt", "r+") as total_file:
lines = total_file.readlines()
num_correct = 0
num_incorrect = 0
for line in lines:
if "number of correct:" in line:
num_correct = int(line.split(":")[1])
elif "number of incorrect:" in line:
num_incorrect = int(line.split(":")[1])
num_incorrect += 1
total_file.seek(0)
total_file.truncate()
total_file.write(f"number of correct: {num_correct}\n")
total_file.write(f"number of incorrect: {num_incorrect}\n")