This commit is contained in:
guanjihuan 2022-02-06 18:44:55 +08:00
parent 03955990aa
commit 50f44061be
3 changed files with 35 additions and 6 deletions

View File

@ -0,0 +1,31 @@
from multiprocessing import Process
import os
import time
import numpy as np
import guan
def main(parameter_array, task_index):
print ('Process id = %s' % (os.getpid()))
result_array = []
for parameter in parameter_array:
result = parameter*2
result_array.append(result)
guan.write_one_dimensional_data(parameter_array, result_array, filename='task_index='+str(task_index))
if __name__ == '__main__':
cpus = 4
parameter_array_all = np.arange(0, 17, 1)
start_time = time.perf_counter()
for task_index in range(cpus):
parameter_array = guan.preprocess_for_parallel_calculations(parameter_array_all, cpus, task_index)
p = Process(target=main, args=(parameter_array, task_index))
p.start()
p.join()
end_time = time.perf_counter()
print('运行时间=', (end_time-start_time), '\n')
f = open('result.txt', 'w')
for job_index in range(cpus):
with open('task_index='+str(job_index)+'.txt', 'r') as f0:
text = f0.read()
f.write(text)
f.close()

View File

@ -4,8 +4,7 @@ import time
def run_proc(name): # 要执行的代码
start_time = time.perf_counter()
for i in range(300000000):
x = 100000^1000000000000
time.sleep(2)
end_time = time.perf_counter()
print ('Process id running on %s = %s' % (name, os.getpid()), '; running time = %s' % (end_time-start_time))
@ -37,5 +36,4 @@ if __name__ == '__main__':
p.start()
p.join() # join()方法可以等待子进程结束后再继续往下运行
end_time = time.perf_counter()
print('CPU执行时间(s)=', (end_time-start_time))
print('运行时间(s)=', (end_time-start_time))

View File

@ -1,6 +1,6 @@
#!/bin/sh
for job_index in 0 1 2 3 4 5 6
for ((job_index=0; job_index<7; job_index++))
do
cp a.py a${job_index}.py
@ -11,4 +11,4 @@ cp a.sh a${job_index}.sh
sed -i "s/python a.py/python a${job_index}.py/" a${job_index}.sh
qsub a${job_index}.sh
done
done