gpt4 book ai didi

python - 看来 multiprocessing.pool 两次接受相同的参数

转载 作者:行者123 更新时间:2023-12-01 00:38:30 26 4
gpt4 key购买 nike

我正在使用 multiprocessing.pool 并行执行多个集成。

在此程序中,我通过生成 dW 3D 数组来积分不同噪声实现的运动方程。程序的第一部分只是参数的定义和计算所需数组的生成。

我在函数外部生成dW,因为我知道否则我必须在每个进程中重新播种才能获得相同的随机序列。

Euler(replica) 函数是必须并行化的函数。这包括在单个进程上进行数值积分的 for 循环。 arg replica 是存储在“replicas”数组中的系统副本编号,它是在 pool.map 中传递的参数。

import numpy as np 
from multiprocessing import Pool

# parameters
N = 30 # number of sites
T = 1 # total time
dt = 0.1 # time step
l = 0 # initially localized state on site l
e = 0.0 # site energy
v = 1.0 # hopping coefficient
mu, sigma = 0, 1.0 # average and variance of the gaussian distribution
num_replicas = 8 # number of replicas of the system
processes=2 # number of processes

# identity vector which represents the diagonal of the Hamiltonian
E = np.ones(N) * e

# vector which represents the upper/lower diagonal terms of the Hopping Matrix and the Hamiltonian
V = np.ones(N-1) * v

# definition of the tight-binding Hamiltonian (tridiagonal)
H = np.diag(E) + np.diag(V, k=1) + np.diag(V, k=-1)

# corner elements of the Hamiltonian
H[0, -1] = v
H[-1, 0] = v

# time array
time_array = np.arange(0, T, dt)

# site array
site_array = np.arange(N)

# initial state
psi_0 = np.zeros((N), dtype=complex)
psi_0[l] = 1. + 0.j

#initialization of the state array
Psi = np.zeros((len(time_array), N), dtype=complex)
Psi[0,:] = psi_0

# replicas 1D array
replicas = np.arange(0, num_replicas)

# random 2D array
dW = np.random.normal(mu, 1.0, (len(time_array), num_replicas, N)) * np.sqrt(dt)

def Euler(replica):
psi_0 = np.zeros((N), dtype=complex)
psi_0[l] = 1. + 0.j
psi = psi_0
for i in np.arange(1, len(time_array)):
psi += -1.j * (H @ psi) * dt - 1.j * sigma * psi * dW[i,replica,:] - 0.5 * (sigma**2) * psi * dt
psi /= np.sqrt(psi @ np.conj(psi))
Psi[i,:] = psi
return Psi

pool = Pool(processes)
Psi = pool.map(Euler, replicas)

Psi = np.asarray(Psi)

Psi = np.swapaxes(Psi,0,1)

print(Psi)

根据经验,我发现如果 num_replicas > 4 * 个进程pool.map 函数中所示,似乎两个进程采用相同的参数,就好像相同的参数一样重复计算两次。相反,从“num_replicas <= 4*processes”我得到了预期的结果:每个进程都与其他进程不同。

这不是由于随机矩阵dW的生​​成造成的,因为每一行都是不相关的,所以我将此行为归因于我对multiprocessing.pool的使用。

最佳答案

我认为你应该在欧拉函数内初始化你的 psi_0 和“Psi”。我尝试重现您的结果,事实上,我发现当 num_replicas > 4 * 个进程时,您会从多个处理器获得相同的结果。但我认为这是因为 Psi,在你的例子中,它是一个全局变量。

修改代码如下,它会为每个 num_replicas 提供不同的结果(顺便说一下,为什么使用 site_array?它没有在任何地方使用)。

import numpy as np 
from multiprocessing import Pool

# parameters
N = 3 # number of sites
T = 1 # total time
dt = 0.1 # time step
l = 0 # initially localized state on site l
e = 0.0 # site energy
v = 1.0 # hopping coefficient
mu, sigma = 0, 1.0 # average and variance of the gaussian distribution
num_replicas = 10 # number of replicas of the system
processes=2 # number of processes

# identity vector which represents the diagonal of the Hamiltonian
E = np.ones(N) * e

# vector which represents the upper/lower diagonal terms of the Hopping Matrix and the Hamiltonian
V = np.ones(N-1) * v

# definition of the tight-binding Hamiltonian (tridiagonal)
H = np.diag(E) + np.diag(V, k=1) + np.diag(V, k=-1)

# corner elements of the Hamiltonian
H[0, -1] = v
H[-1, 0] = v

# time array
time_array = np.arange(0, T, dt)

## site array
#site_array = np.arange(N)

# replicas 1D array
replicas = np.arange(0, num_replicas)

# random 2D array
dW = np.random.normal(mu, 1.0, (len(time_array), num_replicas, N)) * np.sqrt(dt)
#dW = np.random.normal(mu, 1.0, (len(time_array), num_replicas, N)) * np.sqrt(dt)

def Euler(replica):
# initial state
psi_0 = np.zeros((N), dtype=complex)
psi_0[l] = 1. + 0.j

#initialization of the state array
Psi = np.zeros((len(time_array), N), dtype=complex)
Psi[0,:] = psi_0
psi_0 = np.zeros((N), dtype=complex)
psi_0[l] = 1. + 0.j
psi = psi_0

# print(dW[0,replica,0])
for i in np.arange(1, len(time_array)):
psi += -1.j * (H @ psi) * dt - 1.j * sigma * psi * dW[i,replica,:] - 0.5 * (sigma**2) * psi * dt
psi /= np.sqrt(psi @ np.conj(psi))
Psi[i,:] = psi
return Psi


pool = Pool(processes)
Psi = pool.map(Euler, replicas)

Psi = np.asarray(Psi)

Psi = np.swapaxes(Psi,0,1)

print(Psi)

关于python - 看来 multiprocessing.pool 两次接受相同的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57554650/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com