简体   繁体   English

向量化/并行化 GEKKO 方程

[英]Vectorising / Paralellising GEKKO equations

Using Phython's GEKKO library and following the example to solve the Wave Equation from their original paper , one needs to create a whole lot of equations.使用 Phython 的 GEKKO 库并按照示例求解其原始论文中的波动方程,需要创建大量方程。

m.Equations([u[i].dt() == v[i]for i in range(npx)])
m.Equations([v[i].dt() == c**2*(u[mod(i+1,npx)]-2.0*u[mod(i,npx)]+u[mod(i-1,npx)])/dx**2 for i in arange(0,npx,1)])

(I already reduced it to two lines via mod , incorporating the boundary conditions and let i run from 0 to 99, but it's basically still the same as in the example.) (我已经通过mod将它减少到两行,结合边界条件并让我从 0 运行到 99,但它基本上仍然与示例中的相同。)

It is still using a for -loop, ie v[i]for i in range(npx)] .它仍在使用for循环,即v[i]for i in range(npx)] Can one avoid this and thus make the code more readable or the simulation faster by paralellising / vectorising it like one can do with numpy arrays?是否可以像使用 numpy arrays 一样通过并行化/矢量化来避免这种情况,从而使代码更具可读性或模拟速度更快?

Of course for this problem the computation only takes <18s on my laptop, but bigger problems might be very slow.当然,对于这个问题,我的笔记本电脑上的计算只需要 <18s,但更大的问题可能会非常慢。

There is something about arrays in the documentation, but I don't understand it and I don't know if this is even what I am looking for.文档中有关于 arrays 的内容,但我不明白,我不知道这是否是我正在寻找的。

I attached a MWE, using the above code here .我附加了一个 MWE,在这里使用上面的代码。

If you want to improve the speed, try a sequential solution with m.options.IMODE=7 .如果您想提高速度,请尝试使用m.options.IMODE=7的顺序解决方案。 For simulation, it is generally much faster than IMODE=4 (simultaneous solution).对于仿真,它通常比IMODE=4 (同时解)快得多。

from gekko import GEKKO
import numpy as np
import time
start = time.time()
pi = np.pi
#Initialise model
m = GEKKO(remote = False)
#Discretisations(time and space)
npt = 200; npx = 100
m.time = np.linspace(0, 1, npt)
xpos = np.linspace(0, 2*pi, npx)
dx = xpos[1]-xpos[0]
#Define Variables
c = m.Const(value = 10)
u = [m.Var(value = np.cos(xpos[i])) for i in range(npx)]
v = [m.Var(value = np.sin(2*xpos[i])) for i in range(npx)]
#Automatic discretisation in time and manual discretisation in space
m.Equations([u[i].dt() == v[i] for i in range(npx)])
m.Equations([v[i].dt() == c**2*(u[np.mod(i+1,npx)]-2.0*u[np.mod(i,npx)]\
                                 +u[np.mod(i-1,npx)])/dx**2 \
                          for i in np.arange(0,npx,1)])

#Set options
m.options.imode = 7
m.options.solver = 1
m.options.nodes = 3
m.solve(disp = False, GUI = False)
print('Solve Time: ' + str(m.options.SOLVETIME))
print('Total Time: ' + str(time.time()-start))

With IMODE=4 it takes more time to set up the problem but less time to solve it.使用IMODE=4 ,设置问题需要更多时间,但解决问题需要更少时间。

Solve Time: 2.7666
Total Time: 25.547

With IMODE=7 it takes less time to set up the problem but more time to solve it.使用IMODE=7 ,设置问题所需的时间更少,但解决问题的时间更多。

Solve Time: 8.2271
Total Time: 11.819

As you get to larger problems, IMODE=7 will take much less time than IMODE=4 for simulation.当您遇到更大的问题时, IMODE=7将比IMODE=4花费更少的时间进行仿真。 For parallel options, you may be able to construct your model in parallel but the solution of that model doesn't have many options for a much faster solution besides IMODE=7 .对于并行选项,您可以并行构建 model 但该 model 的解决方案除了IMODE=7之外没有很多选项可以更快地解决问题。 You can create a parallel Gekko application in the following ways:您可以通过以下方式创建并行 Gekko 应用程序:

  • Use parallel linear solvers in IPOPT with ma77, ma97, and others.在 IPOPT 中与 ma77、ma97 等一起使用并行线性求解器。 This is typically only a 20-60% improvement in speed from some of the testing that I've done on large-scale problems.与我对大规模问题所做的一些测试相比,这通常只提高了 20-60% 的速度。 These options aren't available in the IPOPT version that is distributed publicly because the solvers require a license.这些选项在公开分发的 IPOPT 版本中不可用,因为求解器需要许可证。 The linear solver MUMPS is distributed with Gekko but does not include parallel support (although this is potentially coming later).线性求解器 MUMPS 与 Gekko 一起分发,但不包括并行支持(尽管这可能会在以后推出)。 The issue is that the solver is only part of the solution and even if the solver were infinitely fast, the automatic differentiation, objective evaluation, and equation evaluation still takes about 50% of the CPU time.问题是求解器只是解决方案的一部分,即使求解器速度无限快,自动微分、客观评估和方程评估仍然需要大约 50% 的 CPU 时间。

  • Run independently.独立运行。 This is often called "massively parallel" because the processes can be split into separate threads and then the code combines again when all of the sub-processes are complete.这通常被称为“大规模并行”,因为可以将进程拆分为单独的线程,然后在所有子进程完成后再次组合代码。 This instructional material on multi-threading and How to do parallel Python Gekko?这份关于多线程如何并行处理 Python Gekko 的说明材料? show how to compute in parallel.展示如何并行计算。 Your problem is not set up for multi-threading because the equations are solved together.您的问题不是针对多线程设置的,因为方程是一起求解的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM