【发布时间】:2017-10-16 03:28:26
【问题描述】:
我正在尝试并行化一个令人尴尬的并行 for 循环 (previously asked here) 并选择适合我的参数的 this implementation:
with Manager() as proxy_manager:
shared_inputs = proxy_manager.list([datasets, train_size_common, feat_sel_size, train_perc,
total_test_samples, num_classes, num_features, label_set,
method_names, pos_class_index, out_results_dir, exhaustive_search])
partial_func_holdout = partial(holdout_trial_compare_datasets, *shared_inputs)
with Pool(processes=num_procs) as pool:
cv_results = pool.map(partial_func_holdout, range(num_repetitions))
我需要使用proxy object(在进程之间共享)的原因是共享代理列表datasets 中的第一个元素,它是一个大对象列表(每个大约200-300MB)。这个datasets 列表通常有 5-25 个元素。我通常需要在 HPC 集群上运行这个程序。
问题是,当我使用 32 个进程和 50GB 内存(num_repetitions=200,数据集是 10 个对象的列表,每个 250MB)运行这个程序时,我看不到加速 16 倍(具有 32 个并行进程)。我不明白为什么 - 任何线索?有什么明显的错误或错误的选择吗?我在哪里可以改进这个实现?有其他选择吗?
我相信这个问题之前已经讨论过,原因可能多种多样,并且非常具体到实施 - 因此我请求你提供你的 2 美分。谢谢。
更新:我使用 cProfile 进行了一些分析以获得更好的想法 - 这是一些按累积时间排序的信息。
In [19]: p.sort_stats('cumulative').print_stats(50)
Mon Oct 16 16:43:59 2017 profiling_log.txt
555404 function calls (543552 primitive calls) in 662.201 seconds
Ordered by: cumulative time
List reduced from 4510 to 50 due to restriction <50>
ncalls tottime percall cumtime percall filename:lineno(function)
897/1 0.044 0.000 662.202 662.202 {built-in method builtins.exec}
1 0.000 0.000 662.202 662.202 test_rhst.py:2(<module>)
1 0.001 0.001 661.341 661.341 test_rhst.py:70(test_chance_classifier_binary)
1 0.000 0.000 661.336 661.336 /Users/Reddy/dev/neuropredict/neuropredict/rhst.py:677(run)
4 0.000 0.000 661.233 165.308 /Users/Reddy/anaconda/envs/py36/lib/python3.6/threading.py:533(wait)
4 0.000 0.000 661.233 165.308 /Users/Reddy/anaconda/envs/py36/lib/python3.6/threading.py:263(wait)
23 661.233 28.749 661.233 28.749 {method 'acquire' of '_thread.lock' objects}
1 0.000 0.000 661.233 661.233 /Users/Reddy/anaconda/envs/py36/lib/python3.6/multiprocessing/pool.py:261(map)
1 0.000 0.000 661.233 661.233 /Users/Reddy/anaconda/envs/py36/lib/python3.6/multiprocessing/pool.py:637(get)
1 0.000 0.000 661.233 661.233 /Users/Reddy/anaconda/envs/py36/lib/python3.6/multiprocessing/pool.py:634(wait)
866/8 0.004 0.000 0.868 0.108 <frozen importlib._bootstrap>:958(_find_and_load)
866/8 0.003 0.000 0.867 0.108 <frozen importlib._bootstrap>:931(_find_and_load_unlocked)
720/8 0.003 0.000 0.865 0.108 <frozen importlib._bootstrap>:641(_load_unlocked)
596/8 0.002 0.000 0.865 0.108 <frozen importlib._bootstrap_external>:672(exec_module)
1017/8 0.001 0.000 0.863 0.108 <frozen importlib._bootstrap>:197(_call_with_frames_removed)
522/51 0.001 0.000 0.765 0.015 {built-in method builtins.__import__}
分析信息现在按time排序
In [20]: p.sort_stats('time').print_stats(20)
Mon Oct 16 16:43:59 2017 profiling_log.txt
555404 function calls (543552 primitive calls) in 662.201 seconds
Ordered by: internal time
List reduced from 4510 to 20 due to restriction <20>
ncalls tottime percall cumtime percall filename:lineno(function)
23 661.233 28.749 661.233 28.749 {method 'acquire' of '_thread.lock' objects}
115/80 0.177 0.002 0.211 0.003 {built-in method _imp.create_dynamic}
595 0.072 0.000 0.072 0.000 {built-in method marshal.loads}
1 0.045 0.045 0.045 0.045 {method 'acquire' of '_multiprocessing.SemLock' objects}
897/1 0.044 0.000 662.202 662.202 {built-in method builtins.exec}
3 0.042 0.014 0.042 0.014 {method 'read' of '_io.BufferedReader' objects}
2037/1974 0.037 0.000 0.082 0.000 {built-in method builtins.__build_class__}
286 0.022 0.000 0.061 0.000 /Users/Reddy/anaconda/envs/py36/lib/python3.6/site-packages/scipy/misc/doccer.py:12(docformat)
2886 0.021 0.000 0.021 0.000 {built-in method posix.stat}
79 0.016 0.000 0.016 0.000 {built-in method posix.read}
597 0.013 0.000 0.021 0.000 <frozen importlib._bootstrap_external>:830(get_data)
276 0.011 0.000 0.013 0.000 /Users/Reddy/anaconda/envs/py36/lib/python3.6/sre_compile.py:250(_optimize_charset)
108 0.011 0.000 0.038 0.000 /Users/Reddy/anaconda/envs/py36/lib/python3.6/site-packages/scipy/stats/_distn_infrastructure.py:626(_construct_argparser)
1225 0.011 0.000 0.050 0.000 <frozen importlib._bootstrap_external>:1233(find_spec)
7179 0.009 0.000 0.009 0.000 {method 'splitlines' of 'str' objects}
33 0.008 0.000 0.008 0.000 {built-in method posix.waitpid}
283 0.008 0.000 0.015 0.000 /Users/Reddy/anaconda/envs/py36/lib/python3.6/site-packages/scipy/misc/doccer.py:128(indentcount_lines)
3 0.008 0.003 0.008 0.003 {method 'poll' of 'select.poll' objects}
7178 0.008 0.000 0.008 0.000 {method 'expandtabs' of 'str' objects}
597 0.007 0.000 0.007 0.000 {method 'read' of '_io.FileIO' objects}
更新 2
我之前提到的大列表datasets 中的元素通常没有那么大——它们通常每个10-25MB。但是根据使用的浮点精度、样本数量和特征,每个元素也可以轻松增长到 500MB-1GB。因此我更喜欢可以扩展的解决方案。
更新 3:
holdout_trial_compare_datasets 中的代码使用 scikit-learn 的 GridSearchCV 方法,如果我们设置 n_jobs > 1(或者无论何时设置它),它都会在内部使用 joblib 库。这可能会导致多处理和 joblib 之间的一些不良交互。所以尝试另一个我根本没有设置 n_jobs 的配置(这应该默认在 scikit-learn 中没有并行性)。会及时通知您。
【问题讨论】:
-
你做过分析吗?
-
还没有,因为我想测试它的参数(16-32 个进程,10-15 个数据集)要求我在集群上运行它,我不知道如何在命令行上配置 python 程序。我会尽快调查的。
-
我的 2¢:如果你的大数据对象只从父级传递给子级,
Manager似乎有点矫枉过正,你可以将它加载到父级的全局变量中,然后它将与子级共享fork(). -
您似乎在分析父进程,而不是子工作者?
-
你可以使用a decorator to profile你的
partial_func_holdout函数。或者使用这个:gist.github.com/nealtodd/2489618
标签: python parallel-processing scikit-learn multiprocessing