我有一个python程序,可以执行我用mpi4py设置的素数,以便可以在我的树莓派群集上运行,并且通常可以从任何目录运行该命令,但是当我将其放在rc.local中时,不起作用。我可以从命令行正常运行rc.local文件,然后按预期方式启动。
我通过ssh连接到我的Raspberry pi,但目前不支持它,否则我将连接一个监视器,以检查启动时的错误消息。我在家庭wifi上设置了端口转发功能,并更改了ssh的默认端口以使其更加安全。
rc.local文件中的命令:
mpiexec -n 16 -f /home/pi/machinefile python3 /home/pi/clusterprime.py
计算素数的代码:
from mpi4py import MPI
def innit():
#setting gloabl variables:
global comm
global rank
global size
global name
#setting up mpi4py:
comm = MPI.COMM_WORLD
rank = comm.rank
size = comm.size
name = comm.name
#checking if thread is master or slave:
if rank == 0:
mastersetup()
main()
def master(thread):
global n
global nLock
global cLock
global primes
global c
print("Started thread: {}".format(thread))
t = time.time()
while True:
with nLock:
n+=2
comm.send(n, dest=thread)
num, p = comm.recv(source=thread)
if p:
times.append(time.time()-t)
t = time.time()
print(num)
primes.append(num)
with cLock:
c+=1
def mastersetup():
#setting global module variables:
global time
#master setup:
import pickle
import threading
import time
#setting global pickle variables:
global prime
global n
global primes
primes = pickle.load(open("/home/pi/primes/primes.p", "rb"))
n = primes[len(primes)-1]
#setting global threading variables:
global nLock
global cLock
global c
global times
nLock = threading.Lock()
cLock = threading.Lock()
threads = []
c = 0
times = []
t = 0
for x in range(1,size):
threads.append(threading.Thread(target=master, args=[x]))
for x in threads:
x.daemon = True
x.start()
while True:
if c >= size*5:
t = 0
with cLock:
c = 0
pickle.dump(primes,open("/home/pi/primes/primes.p", "wb"))
for x in times:
t+=x
t = t/len(times)/size
print(t)
t = []
while len(primes) >= 200:
primes.remove(primes[0])
def main():
while True:
n = comm.recv(source=0)
p = isprime(n)
comm.send((n, p), dest=0)
def isprime(num):
for n in range(3, round(num/2),2):
if num%n == 0:
return(False)
return(True)
if __name__ == "__main__":
innit()