我对Python非常陌生,对多处理也完全陌生,我在网上找到了一些教程来帮助我理解多处理软件包
我的代码有一组使用RungeKutta4方法的微分方程,我需要在不同的起始条件下运行大量的计算
代码本身可以工作,但没有多处理,需要很长时间才能完成,我认为使用并行化可能是理想的,因为计算是独立的
我正在使用Python作为IDE顺便说一句
所以我用
import multiprocessing as mp
iterations = np.arange(start,end,step)
pool = mp.Pool(mp.cpu_count()) # Step 1: Init multiprocessing.Pool()
results = pool.map(rungeKutta4, [k for k in iterations]) # Step 2: apply pool map
pool.close() # Step 3: close
当我在Anaconda中运行它时,我没有得到一个错误,它开始计算,但从未停止。。。。 我哪里出错了
提前谢谢你的帮助
最好的
编辑:我在这里添加了全部代码
# Python program to implement Runge Kutta method
# Markus Schmid
# 2020 Appalachian State University
# jupyter nbconvert --to python FILENAME.ipynb
# y" + 2*beta*y' + w0*sin(y) = A + B*cos(w*t)
# Import used libraries
import numpy as np
import math
import matplotlib.pyplot as plt
import time
from matplotlib import rc
rc('font',**{'family':'sans-serif','sans-serif':['Helvetica']})
rc('text', usetex=True)
import multiprocessing as mp
print("Number of processors: ", mp.cpu_count())
# list for time (x axis) and result (y axis)
result = []
w0 = 10 # undamped angular frequency of the oscillator
beta = 1
B = 1
A = 0
B = 10
w = 4
theta_init = 0
theta_end = 20
theta_step = 0.1
# initial conditions
t0 = 0
y0 = 0
z0 = 0
t_final = 20 # final time
h = 0.01 # fixed step size
n = (int)((t_final - t0)/h) # datapoints per RK4 iteration
# define functions
def funcf(t, y, z):
return (z)
def funcg(t, y, z):
return (A+B*math.cos(w*t) - 2*beta*z - w0*math.sin(y))
# Finds value of y for a given x using step size h
# and initial value y0 at x0.
def rungeKutta4(y):
# Count number of iterations using step size or
# step height h
t = t0
z = z0
n = (int)((t_final - t)/h)
for i in range(1, n + 1):
# Apply Runge Kutta to find next value of y
k1 = h * funcf(t, y, z)
l1 = h * funcg(t, y, z)
k2 = h * funcf(t + 0.5 * h, y + 0.5 * k1, z + 0.5 * l1)
l2 = h * funcg(t + 0.5 * h, y + 0.5 * k1, z + 0.5 * l1)
k3 = h * funcf(t + 0.5 * h, y + 0.5 * k2, z + 0.5 * l2)
l3 = h * funcg(t + 0.5 * h, y + 0.5 * k2, z + 0.5 * l2)
k4 = h * funcf(t + h, y + k3, z + l3)
l4 = h * funcg(t + h, y + k3, z + l3)
# Update next value of y
y = y + (1.0 / 6.0)*(k1 + 2 * k2 + 2 * k3 + k4)
z = z + (1.0 / 6.0)*(l1 + 2 * l2 + 2 * l3 + l4)
#result.append(y)
t = t + h # Update next value of t
return y
iterations = np.arange(theta_init,theta_end+theta_step,theta_step) # number iterations for omega sweep
start_time = time.time()
#for k in iterations: # for serial calculation
# rungeKutta4(k)
pool = mp.Pool(mp.cpu_count()) # Step 1: Init multiprocessing.Pool()
results = pool.map(rungeKutta4, [k for k in iterations]) # Step 2: apply pool map
end_time = time.time()
pool.close() # Step 3: close
print ("The program took", end_time - start_time, "s to run")
#table = np.array(result).reshape(len(iterations),n) # rearrange array, 1 row is const. theta0
timer = np.arange(t0,t_final,h) # time array
我的初步意见是:
你回答说:
要使
multiprocessing
在ms windows上正常工作,您必须能够在脚本中导入代码,而不会产生副作用,例如启动新进程这意味着您必须将除导入和函数/类定义之外的所有内容都放在一个主块中,如受文档启发的示例所示:
基本上,ms windows缺少类UNIX系统上存在的漂亮的
fork()
系统调用。 因此,Python开发人员必须想出一种聪明的方法,使multiprocessing
在ms windows上工作。 有关血淋淋的详细信息,请参见例如this answer编辑在ms windows上,似乎还需要将辅助函数放入自己的文件中并导入。例如,见here
您的代码似乎很好,对我来说也很有效(Ipython和python3.6.9),我已经在下面附上了结果和配置
您是否尝试在不同的python virtual environment(Anaconda之外)中运行它? 如前所述(在Ronald的评论中)other IDE'spython的多处理有缺陷,请尝试使用命令行/Ipython运行它。我以前甚至在vscode也遇到过一次
配置:
结果:
我找到了解决办法,让它在Python身上工作
除了
调用的函数(worker)必须位于其自己的.py文件中,该文件必须在开始时导入。
我将RK4放在一个RK4.py文件中,我在开始时导入了该文件,但它不起作用,但在我的情况下,它会在来回传递值时出现新问题
相关问题 更多 >
编程相关推荐