Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using multiprocessing with sympy

I have a big equation and I use Sympy library for solving it, in case of symblolic variables and big amount of it Python spends a lot of time for solving. How can I use multiprocessing in this problem? UPD: Yes, I have to solve it nearly 1000~10000 times with different variables.

>>> price = Symbol('price')
>>> k = delta * np.divide(1., Piecewise((0, z <= 0), (z, z > 0)))
>>> wantedEstate = (wealth / (price + self.q / self.rate)) * Piecewise((1, k > 1), (k, k <= 1)) - realEstate
>>> return wantedEstate
like image 787
Kenenbek Arzymatov Avatar asked Oct 28 '25 10:10

Kenenbek Arzymatov


1 Answers

I can't see exactly what you want to do, because you haven't posted a complete code snippet… but I can do something approximate.

You need to build a function, like the one below. And then use a parallel map. (I'm using the pathos library instead of multiprocessing b/c I'm the author, it can be used interactively from the interpreter, and it easily takes multiple arguments).

>>> import sympy 
>>> price = sympy.Symbol('price')
>>> def estate(x, y, z):
...   k = 2*price + x  
...   return z*k**2 + y*k 
... 
>>> estate(1,2,3)
4*price + 3*(2*price + 1)**2 + 2
>>> 
>>> x = range(10)  
>>> y = range(-5,5)
>>> z = range(20,0,-2)
>>>
>>> from pathos.pools import ProcessPool   
>>> pool = ProcessPool()
>>> pool.map(estate, x, y, z)
[80*price**2 - 10*price, -8*price + 18*(2*price + 1)**2 - 4, -6*price + 16*(2*price + 2)**2 - 6, -4*price + 14*(2*price + 3)**2 - 6, -2*price + 12*(2*price + 4)**2 - 4, 10*(2*price + 5)**2, 2*price + 8*(2*price + 6)**2 + 6, 4*price + 6*(2*price + 7)**2 + 14, 6*price + 4*(2*price + 8)**2 + 24, 8*price + 2*(2*price + 9)**2 + 36]

Or, if you want a non-blocking map:

>>> res = pool.imap(estate, x, y, z)
>>> res.next()
80*price**2 - 10*price
>>> res.next()
-8*price + 18*(2*price + 1)**2 - 4
>>> res.next()
-6*price + 16*(2*price + 2)**2 - 6
>>> list(res)      
[-4*price + 14*(2*price + 3)**2 - 6, -2*price + 12*(2*price + 4)**2 - 4, 10*(2*price + 5)**2, 2*price + 8*(2*price + 6)**2 + 6, 4*price + 6*(2*price + 7)**2 + 14, 6*price + 4*(2*price + 8)**2 + 24, 8*price + 2*(2*price + 9)**2 + 36]

And when you are done, don't forget to shut things down:

>>> pool.close()
>>> pool.join()

However, if you want to use traditional multiprocessing (say you have only one argument for your map), it would look like this:

>>> def doit(x):
...   return x+2*x
... 
>>> # use the standard multiprocessing interface
>>> import pathos.multiprocessing as mp
>>> pool = mp.Pool()
>>> pool.map(doit, x)
[0, 3, 6, 9, 12, 15, 18, 21, 24, 27]
>>> pool.close()
>>> pool.join()

There's also a thread pool (same is available in multiprocessing.dummy), to use threads instead of processes:

>>> from pathos.pools import ThreadPool 
>>> pool = ThreadPool()
>>> pool.map(doit, x)
[0, 3, 6, 9, 12, 15, 18, 21, 24, 27]
>>> pool.close()
>>> pool.join()

There are other flavors of map (e.g. if the ordering of results doesn't matter, use uimap from pathos, which is the same as imap_unordered from multiprocessing).

like image 174
Mike McKerns Avatar answered Oct 30 '25 12:10

Mike McKerns