Python 达斯克:为什么CPU使用率突然下降?
我正在对一个模型进行蒙特卡罗模拟,并认为Dask在这方面非常有用。在最初的35个小时左右,一切都运行得相当“平稳”(除了风扇的噪音让人感觉到电脑正在起飞)。每个模型运行大约需要2秒钟,有8个分区并行运行。活动监视器显示了8个python3.6实例 然而,计算机已经变得“沉默”,CPU使用率(如Spyder所示)几乎不超过20%。模型运行是按顺序进行的(不是并行的),每次运行大约需要4秒。这发生在今天的某个时候,当时我正在做其他事情。我知道,根据操作的顺序,Dask不会同时使用所有内核。然而,在这种情况下,实际上只需要执行一个任务(请参阅下文),因此可以预期所有分区或多或少同时运行和完成。编辑:整个设置在过去已经成功运行了10000次模拟,现在的区别是有近500000次模拟要运行 编辑2:现在它已经转换为并行执行2个分区(而不是以前的1个分区和原来的8个分区)。似乎有什么东西使它改变了同时处理多少个分区 编辑3:根据建议,我使用了Python 达斯克:为什么CPU使用率突然下降?,python,python-3.x,pandas,spyder,dask,Python,Python 3.x,Pandas,Spyder,Dask,我正在对一个模型进行蒙特卡罗模拟,并认为Dask在这方面非常有用。在最初的35个小时左右,一切都运行得相当“平稳”(除了风扇的噪音让人感觉到电脑正在起飞)。每个模型运行大约需要2秒钟,有8个分区并行运行。活动监视器显示了8个python3.6实例 然而,计算机已经变得“沉默”,CPU使用率(如Spyder所示)几乎不超过20%。模型运行是按顺序进行的(不是并行的),每次运行大约需要4秒。这发生在今天的某个时候,当时我正在做其他事情。我知道,根据操作的顺序,Dask不会同时使用所有内核。然而,在这
dask.distributed.Client
来跟踪正在发生的事情,并在前400行中运行了它。下面是一个关于完成后的外观的图示。我正在努力理解x轴标签,在矩形上盘旋显示大约143s
因此,有些问题是:
# Create a pandas DataFrame where each column is a parameter and each row is a possible parameter combination (cartesian product). At the end of each row some columns to store the respective values of some objective functions are pre-allocated too.
# Generate a dask dataframe that is the DataFrame above split into 8 partitions
# Define a function that takes a partition and, for each row:
# Runs the model with the coefficient values defined in the row
# Retrieves the values of objective functions
# Assigns these values to the respective columns of the current row in the partition (columns have been pre-allocated)
# and then returns the partition with columns for objective functions populated with the calculated values
# map_partitions() to this function in the dask dataframe
有什么想法吗?
这显示了脚本有多简单:
# Create a pandas DataFrame where each column is a parameter and each row is a possible parameter combination (cartesian product). At the end of each row some columns to store the respective values of some objective functions are pre-allocated too.
# Generate a dask dataframe that is the DataFrame above split into 8 partitions
# Define a function that takes a partition and, for each row:
# Runs the model with the coefficient values defined in the row
# Retrieves the values of objective functions
# Assigns these values to the respective columns of the current row in the partition (columns have been pre-allocated)
# and then returns the partition with columns for objective functions populated with the calculated values
# map_partitions() to this function in the dask dataframe
仪表板:
更新:我采取的方法是:
- 设置大量分区(
)。这使我们更容易看到进展。我不确定设置这么多分区是否是一种好的做法,但它在没有太多减速的情况下工作npartitions=nCores*200
- 我没有试图通过
最终获得单个大熊猫数据帧,而是将dask数据帧写入拼花地板(这样每个分区都被写入一个单独的文件)。之后,将所有文件读入DASK数据框和<代码>计算< /CODE> ING到熊猫数据文件并不困难,如果中间出现了错误,至少我不会丢失已经成功处理和写入的分区。.compute()
Dask有许多诊断工具,可以帮助您了解计算中的情况。看 特别是,我建议在本地使用分布式调度器并观察Dask仪表板,以了解计算中发生的情况。看
这是一个您可以访问的网页,它将准确地告诉您所有处理器中正在发生的事情。可能您的访问模式发生了更改,位置被删除,从而导致更多IO。但这只是猜测。由于没有关于您正在运行的算法的信息,我可以假设您遇到了设计上不并行的瓶颈步骤。@EliKorvigo感谢您的输入,我理解没有正确的算法很难猜测。实际上只有一个步骤:在每个分区上运行.iterrows()并返回更新的分区。在过去,这对较少的模拟有效,因此在这种情况下,设计本身可能不是问题所在。我已将此信息添加到描述中以使其更清晰。@sascha还感谢您的输入,坦率地说,这些内容超出了我目前的知识范围,因此我必须在发表任何其他评论之前查看您的意思。感谢您的输入,仪表板的显示效果确实令人印象深刻。我已经将脚本迁移到Jupyter笔记本,实现了一个仪表板并更新了