diff --git a/language_learning/2020.06.01_parallel_calculations_with_python/application_of_parallel_calculations_with_python.py b/language_learning/2020.06.01_parallel_calculations_with_python/application_of_parallel_calculations_with_python.py new file mode 100644 index 0000000..9c4bfa0 --- /dev/null +++ b/language_learning/2020.06.01_parallel_calculations_with_python/application_of_parallel_calculations_with_python.py @@ -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() \ No newline at end of file diff --git a/language_learning/2020.06.01_parallel_calculations_with_python/parallel_calculations_with_python.py b/language_learning/2020.06.01_parallel_calculations_with_python/parallel_calculations_with_python.py index 0430ee4..35e20cf 100755 --- a/language_learning/2020.06.01_parallel_calculations_with_python/parallel_calculations_with_python.py +++ b/language_learning/2020.06.01_parallel_calculations_with_python/parallel_calculations_with_python.py @@ -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)) \ No newline at end of file diff --git a/language_learning/2021.05.10_parallel_calculations_using_sh_files/task.sh b/language_learning/2021.05.10_parallel_calculations_using_sh_files/task.sh index ad6ae07..779b233 100755 --- a/language_learning/2021.05.10_parallel_calculations_using_sh_files/task.sh +++ b/language_learning/2021.05.10_parallel_calculations_using_sh_files/task.sh @@ -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 \ No newline at end of file