-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker4.py
166 lines (152 loc) · 5.89 KB
/
worker4.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
import zmq
import sys
import json
import numpy
import itertools
import time
from collections import namedtuple
def leermatrizrangos(nombre,rangoA,rangoB):
matrizR = []
with open(nombre,'r') as file:
texto = itertools.islice(file, rangoA, rangoB)
for linea in texto:
a = json.loads(linea)
matrizR.append(a)
return matrizR
def leermatrizcompleta(nombre):
matrizR = []
with open(nombre,'r') as file:
for linea in file.readlines():
a = json.loads(linea)
matrizR.append(a)
return matrizR
def multiplicar(socket,identity,msg):
mensaje_json = json.loads(msg)
Inicial = mensaje_json['indiceInicial']
Final = mensaje_json['indiceFinal']
matrices = mensaje_json['matrices']
size = mensaje_json['size']
matrizA = leermatrizrangos(matrices[0],Inicial,Final)
matrizB = leermatrizcompleta(matrices[1])
matrizR = numpy.zeros((len(matrizA),len(matrizB[0])))
for i in range(len(matrizA)):
for j in range(len(matrizB[0])):
for k in range(len(matrizB)):
matrizR[i][j] += matrizA[i][k] * matrizB[k][j]
matriz = matrizR.tolist()
mensaje = {'operacion':'complete','matrizR':matriz,'indiceInicial':Inicial,'indiceFinal':Final,'size':size}
mensaje_json = json.dumps(mensaje)
socket.send_multipart([identity,mensaje_json.encode('utf8')])
def nWorkers(socket,identity):
msg = {'operacion':'nWorkers'}
msg_json = json.dumps(msg)
socket.send_multipart([identity,msg_json.encode('utf8')])
sender, msg= socket.recv_multipart()
mensaje_json = json.loads(msg)
return mensaje_json['nWorkers']
def workersID(socket,identity):
msg = {'operacion':'workersID'}
msg_json = json.dumps(msg)
socket.send_multipart([identity,msg_json.encode('utf8')])
sender, msg= socket.recv_multipart()
mensaje_json = json.loads(msg)
return mensaje_json['workersID']
def sendMatrizWorkers(socket,identity,matrizA,matrizB):
a = leermatrizcompleta(matrizA)
b = leermatrizcompleta(matrizB)
listWorkers = workersID(socket,identity)
nWorkers = len(listWorkers)
nFilas = int(len(a)/nWorkers)
filas_extra = len(a)%nWorkers
indiceInicial = 0
for x in listWorkers:
worker = x.encode('utf8')
mensaje = {'operacion':'multiplicar','indiceInicial':indiceInicial,'indiceFinal':nFilas,'size':len(a),'matrices':[matrizA,matrizB]}
mensaje_json = json.dumps(mensaje)
socket.send_multipart([worker,mensaje_json.encode('utf8')])
indiceInicial = indiceInicial + nFilas
nFilas = nFilas + nFilas
if filas_extra !=0:
mensaje = {'operacion':'multiplicar','indiceInicial':len(a)-filas_extra,'indiceFinal':len(a),'size':len(a),'matrices':[matrizA,matrizB]}
mensaje_json = json.dumps(mensaje)
socket.send_multipart([listWorkers[0].encode('utf8'),mensaje_json.encode('utf8')])
def recvMatrizWorkers(socket,identity,msg):
mensaje_json = json.loads(msg)
segmento = mensaje_json['matrizR']
indiceFinal = mensaje_json['indiceFinal']
size = mensaje_json['size']
for x in range(int(len(segmento))):
print(x)
if(indiceFinal==size):
return time.time()
else:
return -1
def main():
tiempoInicial = time.time()
tiempoFinal = time.time()
identity = b'a4'
servidortcp = "tcp://localhost:4444"
context = zmq.Context()
socket = context.socket(zmq.DEALER)
socket.identity = identity
socket.connect(servidortcp)
print("Started client with id {}".format(identity))
poller = zmq.Poller()
poller.register(sys.stdin, zmq.POLLIN)
poller.register(socket, zmq.POLLIN)
#registrando worker------------
rWorker = {'operacion':'registrar'}
rWorker_json = json.dumps(rWorker)
socket.send_multipart([identity,rWorker_json.encode('utf8')])
#--------------------------------
while True:
socks = dict(poller.poll())
mensaje = {'operacion':'sin operacion'}
mensaje_json = json.dumps(mensaje)
if socket in socks:
sender, msg = socket.recv_multipart()
mensaje_json = json.loads(msg)
operacion = mensaje_json['operacion']
if(operacion=='multiplicar'):
print('estamos multiplicando')
multiplicar(socket,sender,msg)
elif(operacion=='complete'):
a = recvMatrizWorkers(socket,identity,msg)
if(a!=-1):
tiempoFinal = a
print(a-tiempoInicial)
else:
pass
elif sys.stdin.fileno() in socks:
print("?")
command = input()
op, msg = command.split(' ', 1)
if(op=='m1'):
if (msg=='5x5'):
a = 'matrizA5X5.txt'
b = 'matrizB5X5.txt'
tiempoInicial = time.time()
print(tiempoInicial)
sendMatrizWorkers(socket,identity,a,b)
elif(msg=='10x10'):
a = 'matrizA10X10.txt'
b = 'matrizB10X10.txt'
tiempoInicial = time.time()
print(tiempoInicial)
sendMatrizWorkers(socket,identity,a,b)
elif(msg=='100x100'):
a = 'matrizA100X100.txt'
b = 'matrizB100X100.txt'
tiempoInicial = time.time()
print(tiempoInicial)
sendMatrizWorkers(socket,identity,a,b)
elif(msg=='500x500'):
a = 'matrizA1000X1000.txt'
b = 'matrizB1000X1000.txt'
tiempoInicial = time.time()
print(tiempoInicial)
sendMatrizWorkers(socket,identity,a,b)
else:
socket.send_multipart([identity,mensaje_json.encode('utf8')])
if __name__ == '__main__':
main()