こんにちは、arctan のテイラー展開の最初の n 項の合計を与えるこの関数を作成しました。これで mpmath モジュールを使用しています。mpf は任意精度の float 型です。
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
これをいくつかのコアで実行したいので、用語 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: Can't pickle : it's not found as mc.worker" が表示されます
これを起動して実行する方法はありますか?