0

编辑:这是一个同情错误。我已将讨论移至https://github.com/sympy/sympy/issues/7457

我有一个 Python 程序,用于sympy执行一些核心功能,这些功能涉及将线和形状相交。此操作需要执行数千次,并且在使用默认的sympy纯 Python 模块时非常慢。

我试图通过安装来加快速度gmpy 2.0.3(我也尝试过gmpy 1.5)。这确实会导致代码在一定程度上加速,但是当使用multiprocessing它来获得进一步的加速时,程序会以TypeError.

Exception in thread Thread-3:
Traceback (most recent call last):
  File "C:\python27\lib\threading.py", line 810, in __bootstrap_inner
    self.run()
  File "C:\python27\lib\threading.py", line 763, in run
    self.__target(*self.__args, **self.__kwargs)
  File "C:\python27\lib\multiprocessing\pool.py", line 376, in _handle_results
    task = get()
  File "C:\python27\lib\site-packages\sympy\geometry\point.py", line 91, in __new__
    for f in coords.atoms(Float)]))
  File "C:\python27\lib\site-packages\sympy\simplify\simplify.py", line 3839, in nsimplify
    return _real_to_rational(expr, tolerance)
  File "C:\python27\lib\site-packages\sympy\simplify\simplify.py", line 3781, in _real_to_rational
    r = nsimplify(float, rational=False)
  File "C:\python27\lib\site-packages\sympy\simplify\simplify.py", line 3861, in nsimplify
    exprval = expr.evalf(prec, chop=True)
  File "C:\python27\lib\site-packages\sympy\core\evalf.py", line 1300, in evalf
    re = C.Float._new(re, p)
  File "C:\python27\lib\site-packages\sympy\core\numbers.py", line 673, in _new
    obj._mpf_ = mpf_norm(_mpf_, _prec)
  File "C:\python27\lib\site-packages\sympy\core\numbers.py", line 56, in mpf_norm
    rv = mpf_normalize(sign, man, expt, bc, prec, rnd)
TypeError: ('argument is not an mpz', <class 'sympy.geometry.point.Point'>, (-7.07106781186548, -7.07106781186548))

该程序在使用单个进程gmpy运行和不gmpy使用运行时运行良好multiprocessing.Pool

有没有人遇到过这种问题?下面的程序重现了这个问题:

import sympy
import multiprocessing
import numpy

def thread_function(func, data, output_progress=True, extra_kwargs=None, num_procs=None):
    if extra_kwargs:
        func = functools.partial(func, **extra_kwargs)

    if not num_procs:
        num_procs = multiprocessing.cpu_count()
    pool = multiprocessing.Pool(processes=num_procs)
    results = pool.map_async(func, data.T)
    pool.close()

    pool.join()
    return results.get()

def test_fn(data):
    x = data[0]
    y = data[1]
    circle = sympy.Circle((0,0), 10)
    line = sympy.Line(sympy.Point(0,0), sympy.Point(x,y))
    return line.intersection(circle)[0].evalf()

if __name__ == '__main__':
    data = numpy.vstack((numpy.arange(1, 100), numpy.arange(1, 100)))

    print thread_function(test_fn, data) #<--- this line causes the problem
#    print [test_fn(data[:,i]) for i in xrange(data.shape[1])] #<--- this one runs without errors
4

1 回答 1

1

我已经验证了gmpy对象是可腌制的,并且mpmath.mpf使用gmpy的对象也是可腌制的。

man参数 tompf_normalize()不是gmpy对象时会发生错误。如果我强制man成为一个mpz,那么我不再收到错误。但答案与单进程版本不同。

单进程版本:

点(-223606797749979/50000000000000,-223606797749979/25000000000000)

多进程版本:

点(-7.07106781186548,-7.07106781186548)

Point() 中使用的两种类型都不同(有理与浮点)并且值不同(-223606797749979/50000000000000 为 -4.47213595499958)。

我仍在研究,如果我发现根本原因,我会更新这个答案。

更新 #1:不同的值是由示例代码中的错误引起的。线程函数传递的值与非线程版本不同。

我仍在追查为什么多处理会触发异常。我已将问题简化为以下示例:

import sympy
import multiprocessing
import numpy

def thread_function(func, data, output_progress=True, extra_kwargs=None, num_procs=None):
    if extra_kwargs:
        func = functools.partial(func, **extra_kwargs)

    if not num_procs:
        num_procs = multiprocessing.cpu_count()
    pool = multiprocessing.Pool(processes=num_procs)
    results = pool.map_async(func, data)
    pool.close()

    pool.join()
    return results.get()

def test_fn(data):
    return sympy.Point(0,1).evalf()

if __name__ == '__main__':
    test_size = 10
    print [test_fn(None) for i in xrange(1, test_size)] #<--- this one runs without errors
    print thread_function(test_fn, [None] * (test_size - 1)) #<--- this line causes the problem
于 2014-05-04T02:19:34.170 回答