栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

多个scipy.integrate.ode实例

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

多个scipy.integrate.ode实例

一种选择是使用

multiprocessing
(即使用进程而不是线程)。这是一个使用类
map
函数的示例
multiprocessing.Pool
。

该函数

solve
采用一组初始条件,并返回由生成的解
odeint
。主要部分中代码的“串行”版本
solve
反复调用,对于中的每组初始条件一次
ics
。“多重处理”版本使用实例的
map
功能
multiprocessing.Pool
来同时运行多个进程,每个进程都调用
solve
。该
map
函数负责为分配参数
solve
。

我的计算机有四个核心,并且随着我的增加

num_processes
,加速最大达到约3.6。

from __future__ import division, print_functionimport sysimport timeimport multiprocessing as mpimport numpy as npfrom scipy.integrate import odeintdef lorenz(q, t, sigma, rho, beta):    x, y, z = q    return [sigma*(y - x), x*(rho - z) - y, x*y - beta*z]def solve(ic):    t = np.linspace(0, 200, 801)    sigma = 10.0    rho = 28.0    beta = 8/3    sol = odeint(lorenz, ic, t, args=(sigma, rho, beta), rtol=1e-10, atol=1e-12)    return solif __name__ == "__main__":    ics = np.random.randn(100, 3)    print("multiprocessing:", end='')    tstart = time.time()    num_processes = 5    p = mp.Pool(num_processes)    mp_solutions = p.map(solve, ics)    tend = time.time()    tmp = tend - tstart    print(" %8.3f seconds" % tmp)    print("serial:         ", end='')    sys.stdout.flush()    tstart = time.time()    serial_solutions = [solve(ic) for ic in ics]    tend = time.time()    tserial = tend - tstart    print(" %8.3f seconds" % tserial)    print("num_processes = %i, speedup = %.2f" % (num_processes, tserial/tmp))    check = [(sol1 == sol2).all()  for sol1, sol2 in zip(serial_solutions, mp_solutions)]    if not all(check):        print("There was at least one discrepancy in the solutions.")

在我的计算机上,输出为:

multiprocessing:    6.904 secondsserial: 24.756 secondsnum_processes = 5, speedup = 3.59


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/429153.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号