大家都做了这个函数,给出了arctan的泰勒展开的前n个项的总和: 我正在使用mpmath模块,mpf是任意精度浮点类型。
def atantaylor(x,n):
#Taylor series expansion of arctan about 0 for n terms, evaluated at x
sum = 0
xt = x
xsq = x**2
for j in range(n):
nterm = ((-1)**j * xt) /(2.0*j+1)
xt = xt * xsq
sum += nterm
return sum
我想在几个内核上运行,所以我创建了另一个从term n到n + cs相加的函数,理想情况下我可以并行运行其中一些以使其更快:
def taylor1(x,n,cs):
#Sum of Taylor series of atan from n to n + cs - 1
sum = mpf(0)
#Find term n
xt = mpf(x **(2*n + 1))
xsq = mpf(x**2)
#Loop from n to n + cs - 1
for j in range(n, n+cs):
nterm = ((-1)**j * xt) /(2.0*j+1)
xt = xt * xsq
sum += nterm
print "term %d is %r" % (j, nterm)
print sum
return sum
这里的想法是我可以运行一些间隔为[0,cs] [cs,cs * 2] [cs * 2,cs * 3]的进程。
我是多处理的新手,我从本教程中模仿了以下代码here
def mp_atan(x, n, nprocs):
#nprocs must be a factor of n so each worker can receive the same number of tasks that are integer value
if n % nprocs != 0:
print "please give an n that is a multiple of nprocs"
return 0
def worker(x, n, chunksize, out_q):
a = tan_n(x, n, chunksize)
out_q.put([a])
out_q = mp.Queue()
chunksize = int(n / nprocs)
procs = []
for i in range(nprocs):
p = mp.Process(
target = worker,
args=(x, chunksize * i, chunksize, out_q,)
)
procs.append(p)
p.start()
#sum results
sum = 0
for i in range(nprocs):
sum += out_q.get()
#wait for all workers to finish
for p in procs:
p.join()
return sum
我得到了一个EOFError和“pickle.PicklingError:不能发痒:它找不到mc.worker”
无论如何都要启动并运行吗?
答案 0 :(得分:0)
对于任意函数传递问题 - >
我发现自己通过创建传递函数的深层副本来解决99%的问题。
查看此帖子以制作深层复制功能: How can I make a deepcopy of a function in Python?
如果内存不是瓶颈,而你只是想让它起作用,那么它可能值得一试。
import types
def copy_func(f, name=None):
return types.FunctionType(f.func_code, f.func_globals, name or f.func_name,
f.func_defaults, f.func_closure)
所以现在你可以尝试:
for i in range(nprocs):
workercopy = copy_func(worker)
p = mp.Process(
target = workercopy,
args=(x, chunksize * i, chunksize, out_q,)
)
procs.append(p)
p.start()