小编mat*_*tos的帖子

如何使用mpi4py并行化这个python脚本?

如果已经有人问过这个问题,我深表歉意,但我已经阅读了大量文档,但仍然不确定如何做我想做的事情。

我想同时在多个内核上运行 Python 脚本。

我在一个目录中有 1800 个 .h5 文件,名称为“snapshots_s1.h5”、“snapshots_s2.h5”等,每个文件的大小约为 30MB。这个 Python 脚本:

  1. 从目录中一次一个地读入 h5py 文件。
  2. 提取和操作 h5py 文件中的数据。
  3. 创建提取数据的绘图。

完成后,脚本然后从目录中读取下一个 h5py 文件并遵循相同的过程。因此,在进行这项工作时,没有一个处理器需要与任何其他处理器通信。

脚本如下:

import h5py
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as colors
import cmocean
import os  

from mpi4py import MPI

de.logging_setup.rootlogger.setLevel('ERROR')

# Plot writes

count = 1
for filename in os.listdir('directory'):  ### [PERF] Applied to ~ 1800 .h5 files
    with h5py.File('directory/{}'.format(filename),'r') as file:

         ### Manipulate 'filename' data.  ### [PERF] Each fileI ~ 0.03 TB in …
Run Code Online (Sandbox Code Playgroud)

python parallel-processing mpi4py

5
推荐指数
1
解决办法
1011
查看次数

在 Jupyter Notebook 中并行化绘图循环

我使用的是 Python 版本 3.5.1。我想并行化一个循环,该循环用于使用 imshow 绘制一组数组。没有任何并行化的最小代码如下

import matplotlib.pyplot as plt
import numpy as np

# Generate data

arrays   = [np.random.rand(3,2) for x in range(10)]
arrays_2 = [np.random.rand(3,2) for x in range(10)]

# Loop and plot sequentially

for i in range(len(arrays)):

    # Plot side by side

    figure = plt.figure(figsize = (20, 12))
    ax_1 = figure.add_subplot(1, 2, 1)
    ax_2 = figure.add_subplot(1, 2, 2)

    ax_1.imshow(arrays[i], interpolation='gaussian', cmap='RdBu', vmin=0.5*np.min(arrays[i]), vmax=0.5*np.max(arrays[i]))
    ax_2.imshow(arrays_2[i], interpolation='gaussian', cmap='YlGn', vmin=0.5*np.min(arrays_2[i]), vmax=0.5*np.max(arrays_2[i]))

    plt.savefig('./Figure_{}'.format(i), bbox_inches='tight')
    plt.close()
Run Code Online (Sandbox Code Playgroud)

该代码目前是在 Jupyter 笔记本中编写的,我想仅通过 Jupyter 笔记本进行所有处理。虽然这种方法效果很好,但实际上我有 2500 …

python parallel-processing

3
推荐指数
1
解决办法
881
查看次数

在Python中增加函数名称

我很抱歉,如果已经有我的问题的答案,我已经搜索了堆栈溢出一段时间,但没有发现我可以使用的任何东西.

我正在学习如何创建类,我已经为显式Runge-Kutta方法1-4构建了类.类的名称是'RK_1','RK_2','RK_3'和'RK_4'.为了测试我的代码,我决定解决Legendre微分方程,我还创建了一个叫做"Legendre"的类.

现在我想解决这个问题,所以我写了一个使用特定RK方案的函数并解决了勒让德问题.我想为我的每一个RK方案做这个,所以我写了4次相同的函数,即

def solve_Legendre_1(p,Tmax,init,dt=0.001):

    f      = Legendre(p)
    solver = RK_1(init,f)

    while solver.now() < Tmax:
        solver(dt)

    return solver.state()

def solve_Legendre_2(p,Tmax,init,dt=0.001):

    f      = Legendre(p)
    solver = RK_2(init,f)

    while solver.now() < Tmax:
        solver(dt)

    return solver.state()

def solve_Legendre_3(p,Tmax,init,dt=0.001):

    f      = Legendre(p)
    solver = RK_3(init,f)

    while solver.now() < Tmax:
        solver(dt)

    return solver.state()

def solve_Legendre_4(p,Tmax,init,dt=0.001):

    f      = Legendre(p)
    solver = RK_4(init,f)

    while solver.now() < Tmax:
        solver(dt)

    return solver.state()
Run Code Online (Sandbox Code Playgroud)

但是,我意识到必须有一种更简单的方法来做到这一点.所以我想我可以使用循环和str.format()来改变函数的名称并让它采用相应的RK方案,类似于

for j in range(4):
    def solve_Legendre_%s(p,Tmax,init,dt=0.001) % (j+1):

        f      = Legendre(p)
        solver = RK_%s(init,f) % (j+1) …
Run Code Online (Sandbox Code Playgroud)

python string

1
推荐指数
1
解决办法
448
查看次数

标签 统计

python ×3

parallel-processing ×2

mpi4py ×1

string ×1