1
2
3 from __future__ import division, absolute_import
4
5 __author__ = "Eduardo dos Santos Pereira"
6 __email__ = "pereira.somoza@gmail.com"
7 __credits__ = ["Eduardo dos Santos Pereira"]
8 __license__ = "GPLV3"
9 __version__ = "1.0.1"
10 __maintainer__ = "Eduardo dos Santos Pereira"
11 __status__ = "Stable"
12 __date__ = '09/12/2013'
13
14 """
15 """
16
17 import multiprocessing as mpg
18 from numpy import array
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
37 """
38 ppvector: Parallel Processing Vector
39 This program is used to calculate, in parallel, by python module
40 multiprocessing, points in vector.
41 """
42
44 '''
45 Dmatiz: The dimension of the vector
46 func: function that will run in parallel
47 '''
48 self.__inputArray = mpg.Array('d', inputArray)
49 self.__sizeArray = len(inputArray)
50 self.__func = func
51 self.__output = mpg.Array('d', inputArray)
52 self.__runProcess()
53
55 return array(self.__output)
56
58 return array(self.__output)
59
61 x = self.__inputArray[k: E + k]
62 self.__output[k: E + k] = [func(xi) for xi in x]
63
65 E = Dmatriz // n_process
66 k = n * E
67 q.put(self.__Calcula(func, k, E, n))
68
70
71 n_process = mpg.cpu_count()
72 subprocess = []
73
74 for i in range(n_process):
75 q = mpg.Queue()
76 p = mpg.Process(target=self.__acaoParalera,
77 args=(i, q, self.__sizeArray, self.__func, n_process))
78 p.start()
79 subprocess.append(p)
80
81 while subprocess:
82 subprocess.pop().join()
83
84
88
89
90 if(__name__ == "__main__"):
91 import time
92 import matplotlib.pyplot as plt
93
94 tP = []
95 tS = []
96 func = lambda x: x ** 2.0
97 for i in range(1, 500):
98 x = array(list(range(0, i * 100)))
99 t1 = time.time()
100 parallel_list(func, x)
101 t2 = time.time()
102 tP.append([i * 100, t2 - t1])
103 t3 = time.time()
104 [func(xi) for xi in x]
105 t4 = time.time()
106 tS.append([i * 100, t4 - t3])
107 tP = array(tP)
108 tS = array(tS)
109 plt.plot(tP[:, 1], tP[:, 0], label="Parallel")
110 plt.plot(tS[:, 1], tS[:, 0], label="Serial")
111 plt.xlabel('time')
112 plt.ylabel('size input array')
113 plt.legend()
114 plt.show()
115