![](/img/trans.png)
[英]How to run multiple for loops in parallel under a single function in python
[英]How to run multiple python function pairs in parallel with different array as input?
在f0.py
中,我生成了一个参数列表,然后将它们保存到一个 shape- (1000,M,L,N)
数组中:
np.save('../data/seed_S.npy',S)
我必须在另外两个函数f1.py
和f2.py
中使用上述数组S
的一个元素,我使用:
S = np.load('../data/seed_S.npy')
array_x = S[0]
在f1.py
和f2.py
中,
然后我可以用S[0]
进行相关计算。
同样,为了再次运行f1.py
和f2.py
这两个函数,我使用了S
的第二个元素:
S = np.load('../data/seed_S.npy')
array_x = S[1]
在f1.py
和f2.py
中,
然后我可以用S[1]
进行相关计算。
等等。
我的问题是:我想运行函数f1.py
和f2.py
N 次(N=1000),我总是必须更改我的函数f1.py
和f2.py
。 是否可以在不更改功能本身的情况下实现相同的运行? 我想并行运行f1.py
-- f2.py
function 对 1000 次。 此外,我还尝试在没有 function f0.py
的情况下实现这个想法,但是在每次运行时, f1.py
生成并保存array_x
,并且相应的f2.py
读取并使用这个array_x
。 哪种方法可能更好? 谁能给我一个建议? 谢谢!
我尝试了如下实现。
# f1.py
import numpy as np
from modules import *
class system1:
def __init__(self,N,M,L):
# store the parameters in the system1
self.M = int(M)
self.L = int(L)
self.N = int(N)
self.S = init_S(M,L,N)
self.T = 7
# The array
self.S_traj = np.zeros((self.T, self.M, self.L+1, self.N))
# Main
if __name__=='__main__':
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-M', nargs='?', const=10, type=int, default=10, \
help="the number of samples.")
parser.add_argument('-L', nargs='?', const=5, type=int, default=5, \
help="the number of hidden layers.")
parser.add_argument('-N', nargs='?', const=8, type=int, default=8, \
help="the number of nodes per layer.")
args = parser.parse_args()
M,N,L = args.M, args.N, args.L
# Preparing parameters for using in f2.py
parameter_list = np.array([M,L,N])
# Initilize an instance of system1
o = system1(N,M,L)
#=================================
#Save the seed for f2.py
#=================================
np.save('../data/para_list.npy', parameter_list)
np.save('../data/seed_S_M{:d}_L{:d}_N{:d}.npy'.format(M,L,N),o.S)
# Run some calculation ...
o.S_traj[0,:,:,:] = o.S # Note that self.S_traj will independent of self.S from now on.
for index in range(o.T):
print("Runing f1...")
print("Done.")
# f2.py
import numpy as np
from modules import *
class system2:
def __init__(self):
# f2.py accept the same intial configurations as f1.py.
# to obtain the basic parameters: M, L, N
para_list = np.load('../data/para_list.npy')
M = para_list[0]
L = para_list[1]
N = para_list[2]
# then store these parameters
self.M = M
self.L = L
self.N = N
self.T = 7
#arrays
self.S_traj = np.zeros((self.T, self.M, self.L+1, self.N))
self.S = np.load('../data/seed_S_M{:d}_L{:d}_N{:d}.npy'.format(self.M,self.L,self.N))
# Main
if __name__=='__main__':
import argparse
# Initilize an instance of system2.
o = system2()
# run calculation ...
o.S_traj[0,:,:,:] = o.S
for index in range(o.T):
print("Running f2...")
print("Done.")
modules.py
是:
# modules.py
import numpy as np
# Functions
def init_S(M,L,N):
S = np.ones((M,L+1,N))
for i in range(M):
for j in range(L+1):
S[i,j,:] = generate_coord(N)
return S
def generate_coord(N):
"""Randomly set the initial coordinates."""
v = np.ones(N)
list = [-1,0]
for i in range(N):
v[i] = np.random.choice(list)
return v
如果您将它们作为不同的文件运行,那么您需要从脚本外部给它们 arguments。 您可以简单地使用sys.argv
或以更详细的方式使用 argparse 模块来做到这一点。 例如,如果您的 f1.py 具有以下内容:
import sys
print(sys.argv[1])
运行python3 f1.py hello
将打印hello
。 您可能希望在使用之前将输入转换为 int。
这应该允许您使用subprocess.run
从另一个脚本循环运行它
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.