0
大家好我所做的這個函數,給出arctan的泰勒展開的前n項的總和: 我對此使用mpmath模塊,mpf是任意精度浮點數類型。試圖總結一個泰勒系列與Python多處理
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:不能泡菜:它沒有發現如mc.worker「
有沒有辦法讓它啓動並運行?
我沒有'mpf'但這裏的東西很明顯,以測試:如果你火了一個交互式的Python,'進口mpmath會發生什麼,pickle' ,並打印pickle.dumps(mpmath.mpf(0))'?如果你得到一個PicklingError,那就是問題所在:mpf對象不可pickle-able。 (多進程模塊及其'Queue'類型轉儲和加載對象在進程之間傳輸它們。) – torek
沒有錯誤我的朋友。返回一串字符串: 「>>>打印pickle.dumps(mpmath.mpf(0)) ccopy_reg _reconstructor P0 (cmpmath.ctx_mp_python MPF P1 c__builtin__ 對象 P2 Ntp3 RP4 ( I0 S'0' 。 P5 I0 L0L TP6 b >>>」 – dedalux
OK,這不是問題,那麼不知道什麼是它不是別的,上面奇看來:!'out_q.put( [a])'刪除一個list-ified值int輸出隊列,但是sum + = out_q.get()似乎期望原始值,沒有包含列表。但是,這不會給你看到的錯誤。 – torek