update
This commit is contained in:
parent
03955990aa
commit
50f44061be
@ -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()
|
@ -4,8 +4,7 @@ import time
|
|||||||
|
|
||||||
def run_proc(name): # 要执行的代码
|
def run_proc(name): # 要执行的代码
|
||||||
start_time = time.perf_counter()
|
start_time = time.perf_counter()
|
||||||
for i in range(300000000):
|
time.sleep(2)
|
||||||
x = 100000^1000000000000
|
|
||||||
end_time = time.perf_counter()
|
end_time = time.perf_counter()
|
||||||
print ('Process id running on %s = %s' % (name, os.getpid()), '; running time = %s' % (end_time-start_time))
|
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.start()
|
||||||
p.join() # join()方法可以等待子进程结束后再继续往下运行
|
p.join() # join()方法可以等待子进程结束后再继续往下运行
|
||||||
end_time = time.perf_counter()
|
end_time = time.perf_counter()
|
||||||
print('CPU执行时间(s)=', (end_time-start_time))
|
print('运行时间(s)=', (end_time-start_time))
|
||||||
|
|
@ -1,6 +1,6 @@
|
|||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
|
|
||||||
for job_index in 0 1 2 3 4 5 6
|
for ((job_index=0; job_index<7; job_index++))
|
||||||
do
|
do
|
||||||
|
|
||||||
cp a.py a${job_index}.py
|
cp a.py a${job_index}.py
|
||||||
|
Loading…
x
Reference in New Issue
Block a user