Python中的多处理:Numpy+;向量求和->;大幅减速
请不要因为这篇长篇大论而气馁。我尽可能多地提供数据,我真的需要帮助解决这个问题:S。如果有新的提示或想法,我会每天更新 问题: 我尝试在并行进程的帮助下在双核机器上并行运行Python代码(以避免GIL),但问题是代码速度明显减慢。例如,在单核计算机上运行每个工作负载需要600秒,但在双核计算机上运行需要1600秒(每个工作负载需要800秒) 我已经尝试过的:Python中的多处理:Numpy+;向量求和->;大幅减速,python,performance,parallel-processing,multiprocessing,slowdown,Python,Performance,Parallel Processing,Multiprocessing,Slowdown,请不要因为这篇长篇大论而气馁。我尽可能多地提供数据,我真的需要帮助解决这个问题:S。如果有新的提示或想法,我会每天更新 问题: 我尝试在并行进程的帮助下在双核机器上并行运行Python代码(以避免GIL),但问题是代码速度明显减慢。例如,在单核计算机上运行每个工作负载需要600秒,但在双核计算机上运行需要1600秒(每个工作负载需要800秒) 我已经尝试过的: 我测量了内存,似乎没有内存问题。 [在高点仅使用20%] 我使用“htop”来检查我是否真的在不同的内核上运行该程序,或者我的内核关联
- 我测量了内存,似乎没有内存问题。 [在高点仅使用20%]
- 我使用“htop”来检查我是否真的在不同的内核上运行该程序,或者我的内核关联是否混乱。但是也不走运,我的程序在我所有的内核上运行
- 这个问题是一个CPU受限的问题,所以我检查并确认我的代码在所有内核上大部分时间都是100%CPU运行的
- 我检查了进程ID,实际上,我正在生成两个不同的进程
- 我将提交到执行器[e.submit(function,[…])中的函数更改为计算饼函数,并观察到了巨大的加速。因此,问题可能出现在我提交给执行器的process_函数(…)中,而不是之前的代码中
- 目前,我正在使用“并发”中的“未来”来并行化任务。但我也尝试了“多处理”中的“池”类。然而,结果仍然是一样的
- 繁殖进程:
result = [None]*psutil.cpu_count() e = futures.ProcessPoolExecutor( max_workers=psutil.cpu_count() ) for i in range(psutil.cpu_count()): result[i] = e.submit(process_function, ...)
- 处理单元功能:
这些文件太长,无法在这里发布,但我相信如果它们包含有价值的信息,那么这些信息可能就是结果文本顶部的信息。因此,我将在这里发布第一行结果: 1核机器,启动了1个进程:from math import floor from math import ceil import numpy import MySQLdb import time db = MySQLdb.connect(...) cursor = db.cursor() query = "SELECT ...." cursor.execute(query) [...] #save db results into the variable db_matrix (30 columns, 5.000 rows) [...] #save db results into the variable bp_vector (3 columns, 500 rows) [...] #save db results into the variable option_vector( 3 columns, 4000 rows) cursor.close() db.close() counter = 0 for i in range(4000): for j in range(500): helper[:] = (1-bp_vector[j,0]-bp_vector[j,1]-bp_vector[j,2])*db_matrix[:,0] + db_matrix[:,option_vector[i,0]] * bp_vector[j,0] + db_matrix[:,option_vector[i,1]] * bp_vector[j,1] + db_matrix[:,option_vector[i,2]] * bp_vector[j,2] result[counter,0] = (helper < -7.55).sum() counter = counter + 1 return result
623158 function calls (622735 primitive calls) in 229.286 seconds Ordered by: cumulative time ncalls tottime percall cumtime percall filename:lineno(function) 1 0.371 0.371 229.287 229.287 20_with_multiprocessing.py:1(<module>) 3 0.000 0.000 225.082 75.027 threading.py:309(wait) 1 0.000 0.000 225.082 225.082 _base.py:378(result) 25 225.082 9.003 225.082 9.003 {method 'acquire' of 'thread.lock' objects} 1 0.598 0.598 3.081 3.081 get_BP_Verteilung_Vektoren.py:1(get_BP_Verteilung_Vektoren) 3 0.000 0.000 2.877 0.959 cursors.py:164(execute) 3 0.000 0.000 2.877 0.959 cursors.py:353(_query) 3 0.000 0.000 1.958 0.653 cursors.py:315(_do_query) 3 0.000 0.000 1.943 0.648 cursors.py:142(_do_get_result) 3 0.000 0.000 1.943 0.648 cursors.py:351(_get_result) 3 1.943 0.648 1.943 0.648 {method 'store_result' of '_mysql.connection' objects} 3 0.001 0.000 0.919 0.306 cursors.py:358(_post_get_result) 3 0.000 0.000 0.917 0.306 cursors.py:324(_fetch_row) 3 0.917 0.306 0.917 0.306 {built-in method fetch_row} 591314 0.161 0.000 0.161 0.000 {range}
626052 function calls (625616 primitive calls) in 578.086 seconds Ordered by: cumulative time ncalls tottime percall cumtime percall filename:lineno(function) 1 0.310 0.310 578.087 578.087 20_with_multiprocessing.py:1(<module>) 30 574.310 19.144 574.310 19.144 {method 'acquire' of 'thread.lock' objects} 2 0.000 0.000 574.310 287.155 _base.py:378(result) 3 0.000 0.000 574.310 191.437 threading.py:309(wait) 1 0.544 0.544 2.854 2.854 get_BP_Verteilung_Vektoren.py:1(get_BP_Verteilung_Vektoren) 3 0.000 0.000 2.563 0.854 cursors.py:164(execute) 3 0.000 0.000 2.563 0.854 cursors.py:353(_query) 3 0.000 0.000 1.715 0.572 cursors.py:315(_do_query) 3 0.000 0.000 1.701 0.567 cursors.py:142(_do_get_result) 3 0.000 0.000 1.701 0.567 cursors.py:351(_get_result) 3 1.701 0.567 1.701 0.567 {method 'store_result' of '_mysql.connection' objects} 3 0.001 0.000 0.848 0.283 cursors.py:358(_post_get_result) 3 0.000 0.000 0.847 0.282 cursors.py:324(_fetch_row) 3 0.847 0.282 0.847 0.282 {built-in method fetch_row} 591343 0.152 0.000 0.152 0.000 {range}
623164 function calls (622741 primitive calls) in 235.954 seconds Ordered by: cumulative time ncalls tottime percall cumtime percall filename:lineno(function) 1 0.246 0.246 235.955 235.955 20_with_multiprocessing.py:1(<module>) 3 0.000 0.000 232.003 77.334 threading.py:309(wait) 25 232.003 9.280 232.003 9.280 {method 'acquire' of 'thread.lock' objects} 1 0.000 0.000 232.003 232.003 _base.py:378(result) 1 0.593 0.593 3.104 3.104 get_BP_Verteilung_Vektoren.py:1(get_BP_Verteilung_Vektoren) 3 0.000 0.000 2.774 0.925 cursors.py:164(execute) 3 0.000 0.000 2.774 0.925 cursors.py:353(_query) 3 0.000 0.000 1.981 0.660 cursors.py:315(_do_query) 3 0.000 0.000 1.970 0.657 cursors.py:142(_do_get_result) 3 0.000 0.000 1.969 0.656 cursors.py:351(_get_result) 3 1.969 0.656 1.969 0.656 {method 'store_result' of '_mysql.connection' objects} 3 0.001 0.000 0.794 0.265 cursors.py:358(_post_get_result) 3 0.000 0.000 0.792 0.264 cursors.py:324(_fetch_row) 3 0.792 0.264 0.792 0.264 {built-in method fetch_row} 591314 0.144 0.000 0.144 0.000 {range}
626072 function calls (625636 primitive calls) in 682.460 seconds Ordered by: cumulative time ncalls tottime percall cumtime percall filename:lineno(function) 1 0.334 0.334 682.461 682.461 20_with_multiprocessing.py:1(<module>) 4 0.000 0.000 678.231 169.558 threading.py:309(wait) 33 678.230 20.552 678.230 20.552 {method 'acquire' of 'thread.lock' objects} 2 0.000 0.000 678.230 339.115 _base.py:378(result) 1 0.527 0.527 2.974 2.974 get_BP_Verteilung_Vektoren.py:1(get_BP_Verteilung_Vektoren) 3 0.000 0.000 2.723 0.908 cursors.py:164(execute) 3 0.000 0.000 2.723 0.908 cursors.py:353(_query) 3 0.000 0.000 1.749 0.583 cursors.py:315(_do_query) 3 0.000 0.000 1.736 0.579 cursors.py:142(_do_get_result) 3 0.000 0.000 1.736 0.579 cursors.py:351(_get_result) 3 1.736 0.579 1.736 0.579 {method 'store_result' of '_mysql.connection' objects} 3 0.001 0.000 0.975 0.325 cursors.py:358(_post_get_result) 3 0.000 0.000 0.973 0.324 cursors.py:324(_fetch_row) 3 0.973 0.324 0.973 0.324 {built-in method fetch_row} 5 0.093 0.019 0.304 0.061 __init__.py:1(<module>) 1 0.017 0.017 0.275 0.275 __init__.py:106(<module>) 1 0.005 0.005 0.198 0.198 add_newdocs.py:10(<module>) 591343 0.148 0.000 0.148 0.000 {range}
1核机器,启动2进程:623158个函数调用(622735个基元调用),耗时229.286秒 排序人:累计时间 ncalls tottime percall cumtime percall文件名:lineno(函数) 1 0.371 0.371 229.287 229.287 20_带_多处理。py:1() 3 0.000 0.000 225.082 75.027螺纹。py:309(等待) 1 0.000 0.000 225.082 225.082 _基准py:378(结果) 25 225.082 9.003 225.082 9.003{“thread.lock”对象的“获取”方法} 1 0.598 0.598 3.081 3.081 get_BP_Verteilung_Vektoren.py:1(get_BP_Verteilung_Vektoren) 3 0.000 0.000 2.877 0.959游标。py:164(执行) 3 0.000 0.000 2.877 0.959游标。py:353(_查询) 3 0.000 0.000 1.958 0.653游标。py:315(_do_query) 3 0.000 0.000 1.943 0.648游标。py:142(\u do\u get\u result) 3 0.000 0.000 1.943 0.648游标。py:351(_get_result) 3 1.943 0.648 1.943 0.648{method'store_result'of'\u mysql.connection'objects} 3 0.001 0.000 0.919 0.306游标。py:358(_post_get_result) 3 0.000 0.000 0.917 0.306游标。py:324(_fetch_行) 3 0.917 0.306 0.917 0.306{内置方法fetch_row} 591314 0.161 0.000 0.161 0.000{范围}
623158 function calls (622735 primitive calls) in 229.286 seconds Ordered by: cumulative time ncalls tottime percall cumtime percall filename:lineno(function) 1 0.371 0.371 229.287 229.287 20_with_multiprocessing.py:1(<module>) 3 0.000 0.000 225.082 75.027 threading.py:309(wait) 1 0.000 0.000 225.082 225.082 _base.py:378(result) 25 225.082 9.003 225.082 9.003 {method 'acquire' of 'thread.lock' objects} 1 0.598 0.598 3.081 3.081 get_BP_Verteilung_Vektoren.py:1(get_BP_Verteilung_Vektoren) 3 0.000 0.000 2.877 0.959 cursors.py:164(execute) 3 0.000 0.000 2.877 0.959 cursors.py:353(_query) 3 0.000 0.000 1.958 0.653 cursors.py:315(_do_query) 3 0.000 0.000 1.943 0.648 cursors.py:142(_do_get_result) 3 0.000 0.000 1.943 0.648 cursors.py:351(_get_result) 3 1.943 0.648 1.943 0.648 {method 'store_result' of '_mysql.connection' objects} 3 0.001 0.000 0.919 0.306 cursors.py:358(_post_get_result) 3 0.000 0.000 0.917 0.306 cursors.py:324(_fetch_row) 3 0.917 0.306 0.917 0.306 {built-in method fetch_row} 591314 0.161 0.000 0.161 0.000 {range}
626052 function calls (625616 primitive calls) in 578.086 seconds Ordered by: cumulative time ncalls tottime percall cumtime percall filename:lineno(function) 1 0.310 0.310 578.087 578.087 20_with_multiprocessing.py:1(<module>) 30 574.310 19.144 574.310 19.144 {method 'acquire' of 'thread.lock' objects} 2 0.000 0.000 574.310 287.155 _base.py:378(result) 3 0.000 0.000 574.310 191.437 threading.py:309(wait) 1 0.544 0.544 2.854 2.854 get_BP_Verteilung_Vektoren.py:1(get_BP_Verteilung_Vektoren) 3 0.000 0.000 2.563 0.854 cursors.py:164(execute) 3 0.000 0.000 2.563 0.854 cursors.py:353(_query) 3 0.000 0.000 1.715 0.572 cursors.py:315(_do_query) 3 0.000 0.000 1.701 0.567 cursors.py:142(_do_get_result) 3 0.000 0.000 1.701 0.567 cursors.py:351(_get_result) 3 1.701 0.567 1.701 0.567 {method 'store_result' of '_mysql.connection' objects} 3 0.001 0.000 0.848 0.283 cursors.py:358(_post_get_result) 3 0.000 0.000 0.847 0.282 cursors.py:324(_fetch_row) 3 0.847 0.282 0.847 0.282 {built-in method fetch_row} 591343 0.152 0.000 0.152 0.000 {range}
623164 function calls (622741 primitive calls) in 235.954 seconds Ordered by: cumulative time ncalls tottime percall cumtime percall filename:lineno(function) 1 0.246 0.246 235.955 235.955 20_with_multiprocessing.py:1(<module>) 3 0.000 0.000 232.003 77.334 threading.py:309(wait) 25 232.003 9.280 232.003 9.280 {method 'acquire' of 'thread.lock' objects} 1 0.000 0.000 232.003 232.003 _base.py:378(result) 1 0.593 0.593 3.104 3.104 get_BP_Verteilung_Vektoren.py:1(get_BP_Verteilung_Vektoren) 3 0.000 0.000 2.774 0.925 cursors.py:164(execute) 3 0.000 0.000 2.774 0.925 cursors.py:353(_query) 3 0.000 0.000 1.981 0.660 cursors.py:315(_do_query) 3 0.000 0.000 1.970 0.657 cursors.py:142(_do_get_result) 3 0.000 0.000 1.969 0.656 cursors.py:351(_get_result) 3 1.969 0.656 1.969 0.656 {method 'store_result' of '_mysql.connection' objects} 3 0.001 0.000 0.794 0.265 cursors.py:358(_post_get_result) 3 0.000 0.000 0.792 0.264 cursors.py:324(_fetch_row) 3 0.792 0.264 0.792 0.264 {built-in method fetch_row} 591314 0.144 0.000 0.144 0.000 {range}
626072 function calls (625636 primitive calls) in 682.460 seconds Ordered by: cumulative time ncalls tottime percall cumtime percall filename:lineno(function) 1 0.334 0.334 682.461 682.461 20_with_multiprocessing.py:1(<module>) 4 0.000 0.000 678.231 169.558 threading.py:309(wait) 33 678.230 20.552 678.230 20.552 {method 'acquire' of 'thread.lock' objects} 2 0.000 0.000 678.230 339.115 _base.py:378(result) 1 0.527 0.527 2.974 2.974 get_BP_Verteilung_Vektoren.py:1(get_BP_Verteilung_Vektoren) 3 0.000 0.000 2.723 0.908 cursors.py:164(execute) 3 0.000 0.000 2.723 0.908 cursors.py:353(_query) 3 0.000 0.000 1.749 0.583 cursors.py:315(_do_query) 3 0.000 0.000 1.736 0.579 cursors.py:142(_do_get_result) 3 0.000 0.000 1.736 0.579 cursors.py:351(_get_result) 3 1.736 0.579 1.736 0.579 {method 'store_result' of '_mysql.connection' objects} 3 0.001 0.000 0.975 0.325 cursors.py:358(_post_get_result) 3 0.000 0.000 0.973 0.324 cursors.py:324(_fetch_row) 3 0.973 0.324 0.973 0.324 {built-in method fetch_row} 5 0.093 0.019 0.304 0.061 __init__.py:1(<module>) 1 0.017 0.017 0.275 0.275 __init__.py:106(<module>) 1 0.005 0.005 0.198 0.198 add_newdocs.py:10(<module>) 591343 0.148 0.000 0.148 0.000 {range}
2核机器,启动1个进程:626052个函数调用(625616个基元调用)578.086秒 排序人:累计时间 ncalls tottime percall cumtime percall文件名:lineno(函数) 1 0.310 0.310 578.087 578.087 20_带_多处理。py:1() 30 574.310 19.144 574.310 19.144{thread.lock对象的'acquire'方法} 2 0.000 0.000 574.310 287.155_基数py:378(结果) 3 0.000 0.000 574.310 191.437螺纹。py:309(等待) 1 0.544 0.544 2.854 2.854 get_BP_Verteilung_Vektoren.py:1(get_BP_Verteilung_Vektoren) 3 0.000 0.000 2.563 0.854游标。py:164(执行) 3 0.000 0.000 2.563 0.854游标。py:353(_查询) 3 0.000 0.000 1.715 0.572游标。py:315(_do_query) 3 0.000 0.000 1.701 0.567游标。py:142(\u do\u get\u result) 3 0.000 0.000 1.701 0.567游标。py:351(\u get\u result) 3 1.701 0.567 1.701 0.567{method'store_result'of'\u mysql.connection'objects} 3 0.001 0.000 0.848 0.283游标。py:358(\u post\u get\u result) 3 0.000 0.000 0.847 0.282游标。py:324(_fetch_行) 3 0.847 0.282 0.847 0.282{内置方法fetch_row} 591343 0.152 0.000 0.152 0.000{范围}
623158 function calls (622735 primitive calls) in 229.286 seconds Ordered by: cumulative time ncalls tottime percall cumtime percall filename:lineno(function) 1 0.371 0.371 229.287 229.287 20_with_multiprocessing.py:1(<module>) 3 0.000 0.000 225.082 75.027 threading.py:309(wait) 1 0.000 0.000 225.082 225.082 _base.py:378(result) 25 225.082 9.003 225.082 9.003 {method 'acquire' of 'thread.lock' objects} 1 0.598 0.598 3.081 3.081 get_BP_Verteilung_Vektoren.py:1(get_BP_Verteilung_Vektoren) 3 0.000 0.000 2.877 0.959 cursors.py:164(execute) 3 0.000 0.000 2.877 0.959 cursors.py:353(_query) 3 0.000 0.000 1.958 0.653 cursors.py:315(_do_query) 3 0.000 0.000 1.943 0.648 cursors.py:142(_do_get_result) 3 0.000 0.000 1.943 0.648 cursors.py:351(_get_result) 3 1.943 0.648 1.943 0.648 {method 'store_result' of '_mysql.connection' objects} 3 0.001 0.000 0.919 0.306 cursors.py:358(_post_get_result) 3 0.000 0.000 0.917 0.306 cursors.py:324(_fetch_row) 3 0.917 0.306 0.917 0.306 {built-in method fetch_row} 591314 0.161 0.000 0.161 0.000 {range}
626052 function calls (625616 primitive calls) in 578.086 seconds Ordered by: cumulative time ncalls tottime percall cumtime percall filename:lineno(function) 1 0.310 0.310 578.087 578.087 20_with_multiprocessing.py:1(<module>) 30 574.310 19.144 574.310 19.144 {method 'acquire' of 'thread.lock' objects} 2 0.000 0.000 574.310 287.155 _base.py:378(result) 3 0.000 0.000 574.310 191.437 threading.py:309(wait) 1 0.544 0.544 2.854 2.854 get_BP_Verteilung_Vektoren.py:1(get_BP_Verteilung_Vektoren) 3 0.000 0.000 2.563 0.854 cursors.py:164(execute) 3 0.000 0.000 2.563 0.854 cursors.py:353(_query) 3 0.000 0.000 1.715 0.572 cursors.py:315(_do_query) 3 0.000 0.000 1.701 0.567 cursors.py:142(_do_get_result) 3 0.000 0.000 1.701 0.567 cursors.py:351(_get_result) 3 1.701 0.567 1.701 0.567 {method 'store_result' of '_mysql.connection' objects} 3 0.001 0.000 0.848 0.283 cursors.py:358(_post_get_result) 3 0.000 0.000 0.847 0.282 cursors.py:324(_fetch_row) 3 0.847 0.282 0.847 0.282 {built-in method fetch_row} 591343 0.152 0.000 0.152 0.000 {range}
623164 function calls (622741 primitive calls) in 235.954 seconds Ordered by: cumulative time ncalls tottime percall cumtime percall filename:lineno(function) 1 0.246 0.246 235.955 235.955 20_with_multiprocessing.py:1(<module>) 3 0.000 0.000 232.003 77.334 threading.py:309(wait) 25 232.003 9.280 232.003 9.280 {method 'acquire' of 'thread.lock' objects} 1 0.000 0.000 232.003 232.003 _base.py:378(result) 1 0.593 0.593 3.104 3.104 get_BP_Verteilung_Vektoren.py:1(get_BP_Verteilung_Vektoren) 3 0.000 0.000 2.774 0.925 cursors.py:164(execute) 3 0.000 0.000 2.774 0.925 cursors.py:353(_query) 3 0.000 0.000 1.981 0.660 cursors.py:315(_do_query) 3 0.000 0.000 1.970 0.657 cursors.py:142(_do_get_result) 3 0.000 0.000 1.969 0.656 cursors.py:351(_get_result) 3 1.969 0.656 1.969 0.656 {method 'store_result' of '_mysql.connection' objects} 3 0.001 0.000 0.794 0.265 cursors.py:358(_post_get_result) 3 0.000 0.000 0.792 0.264 cursors.py:324(_fetch_row) 3 0.792 0.264 0.792 0.264 {built-in method fetch_row} 591314 0.144 0.000 0.144 0.000 {range}
626072 function calls (625636 primitive calls) in 682.460 seconds Ordered by: cumulative time ncalls tottime percall cumtime percall filename:lineno(function) 1 0.334 0.334 682.461 682.461 20_with_multiprocessing.py:1(<module>) 4 0.000 0.000 678.231 169.558 threading.py:309(wait) 33 678.230 20.552 678.230 20.552 {method 'acquire' of 'thread.lock' objects} 2 0.000 0.000 678.230 339.115 _base.py:378(result) 1 0.527 0.527 2.974 2.974 get_BP_Verteilung_Vektoren.py:1(get_BP_Verteilung_Vektoren) 3 0.000 0.000 2.723 0.908 cursors.py:164(execute) 3 0.000 0.000 2.723 0.908 cursors.py:353(_query) 3 0.000 0.000 1.749 0.583 cursors.py:315(_do_query) 3 0.000 0.000 1.736 0.579 cursors.py:142(_do_get_result) 3 0.000 0.000 1.736 0.579 cursors.py:351(_get_result) 3 1.736 0.579 1.736 0.579 {method 'store_result' of '_mysql.connection' objects} 3 0.001 0.000 0.975 0.325 cursors.py:358(_post_get_result) 3 0.000 0.000 0.973 0.324 cursors.py:324(_fetch_row) 3 0.973 0.324 0.973 0.324 {built-in method fetch_row} 5 0.093 0.019 0.304 0.061 __init__.py:1(<module>) 1 0.017 0.017 0.275 0.275 __init__.py:106(<module>) 1 0.005 0.005 0.198 0.198 add_newdocs.py:10(<module>) 591343 0.148 0.000 0.148 0.000 {range}
623164个函数调用(622741个基元调用),时间为235.954秒 排序人:累计时间 ncalls tottime percall cumtime percall文件名:lineno(函数) 1 0.246 0.246 235.955 235.955 20_带_多处理。py:1() 3 0.000 0.000 232.003 77.334螺纹。py:309(等待) 25 232.003 9.280 232.003 9.280{thread.lock对象的'acquire'方法} 1 0.000 0.000 232.003 232.003_base.py:378(结果) 1 0.593 0.593 3.104 3.104 get_BP_Verteilung_Vektoren.py:1(get_BP_Verteilung_Vektoren) 3 0.000 0.000 2.774 0.925游标。py:164(执行) 3 0.000 0.000 2.774 0.925游标。py:353(_查询) 3 0.000 0.000 1.981 0.660游标。py:315(_do_query) 3 0.000 0.000 1.970 0.657个游标。py:142(\u do\u get\u result) 3 0.000 0.000 1.969 0.656个游标。py:351(_get_result) 3 1.969 0.656 1.969 0.656{method'store_result'of''u mysql.connection'objects} 3 0.001 0.000 0.79