-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsumaEREWproc.py
More file actions
41 lines (29 loc) · 979 Bytes
/
Copy pathsumaEREWproc.py
File metadata and controls
41 lines (29 loc) · 979 Bytes
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
# practica 1: Suma EREW (procesos)
# Autor: Jorge de Jesús Jiménez Servín, Jose Daniel Perez Mejia, Kennet Kael Mendoza Pliego
import multiprocessing
import math
import time
def suma(i, j, A):
if (((2*j) % (math.pow(2, i))) == 0):
A[(2*j)] = A[(2*j)]+A[int((2*j)-(math.pow(2, i-1)))]
time.sleep(0.5)
def main():
A = multiprocessing.Array('i', [0, 1, 1, 1, 1, 1, 1, 1, 1]) # Vector compartido
n = len(A)-1
log = (int)(math.log(n, 2))
processes = []
print(f"\nElementos del vector A {A[:]}")
print(f'Numero de elementos: {n}\n')
print(f'{A[1:n+1]}\n')
for i in range(1, log+1):
print(f'Paso: {i}')
for j in range(1, (int)(n/2)+1):
p1 = multiprocessing.Process(target=suma, args=(i, j, A))
processes.append(p1)
p1.start()
for p in processes:
p.join()
print(f'{A[1:n+1]}\n')
print(f'Suma= {A[n]}')
if __name__ == "__main__":
main()