Package pycosmicstar :: Module paralleloverlist
[hide private]
[frames] | no frames]

Source Code for Module pycosmicstar.paralleloverlist

  1  #!/usr/bin/env python 
  2  # -*- coding: utf-8 -*- 
  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  ##@file paralleloverlist.py 
 21  ##@author  Eduardo dos Santos Pereira <pereira.somoza@gmail.com> 
 22  ##@version 1.1 
 23  ##@section LICENSE 
 24  # This program is free software; you can redistribute it and/or 
 25  #modify it under the terms of the GNU General Public License as 
 26  #published by the Free Software Foundation; either version 2 of 
 27  #the License, or (at your option) any later version. 
 28  #This program is distributed in the hope that it will be useful, but 
 29  #WITHOUT ANY WARRANTY; without even the implied warranty of 
 30  #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
 31  #General Public License for more details at 
 32  #http://www.gnu.org/copyleft/gpl.html 
 33  ##@section DESCRIPTION 
 34   
 35   
36 -class paralleloverlist:
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
43 - def __init__(self, func, inputArray):
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
54 - def getResult(self):
55 return array(self.__output)
56
57 - def __call__(self):
58 return array(self.__output)
59
60 - def __Calcula(self, func, k, E, n):
61 x = self.__inputArray[k: E + k] 62 self.__output[k: E + k] = [func(xi) for xi in x]
63
64 - def __acaoParalera(self, n, q, Dmatriz, func, n_process):
65 E = Dmatriz // n_process 66 k = n * E 67 q.put(self.__Calcula(func, k, E, n))
68
69 - def __runProcess(self):
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
85 -def parallel_list(func, x):
86 result = paralleloverlist(func, x) 87 return result()
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