在 Mathematica 中并行评估单元

发布于 2024-11-27 19:31:22 字数 430 浏览 5 评论 0原文

我有数百个单元格长的 Mathematica 文件,我想使用并行计算。我的每台机器都有 2 个处理器 x 4 核,内存为 16 Gb。我的 Mathematica 许可证允许我运行最多 2 个主内核,其中 1 个主内核可以有 4 个从内核(这是我玩了一段时间后的解释)。

我曾经在两个不同笔记本的两个主内核中运行我的代码。为了进一步加快速度,我尝试使用 ParallelEvaluate[] 封装一些单元格,它似乎有效。然后,我还有 4 个代码副本通过主内核之一运行,彼此不知情,这很好。 (我基本上是在尝试并行运行尽可能多的代码/数学内核副本。我还没有追求任何真正并行的东西)。

由于我的代码太长且复杂,我不想再次编辑每个单元格以使它们并行计算。有没有什么神奇的东西可以放在我的笔记本的开头,这样之后评估的每个单元格默认都是ParallelEvaluate[...单元格内容....]

I have this many hundreds of cell long Mathematica file and I want to use parallel evaluation. I have a 2 processor x 4 core each machine with 16 Gb memory. My Mathematica license allows me to run at most 2 master kernels with 1 of the master could have 4 slave kernels (this is my interpretation after I played with it for a while).

I used to run my code in two master kernels in two different notebooks. To speed up things further, I tried to encapsulate a few cells with ParallelEvaluate[] and it seemed to work. Then I also have 4 copies of my code running unaware of each other through one of the master kernels, which is fine. (I am basically trying to run as many copies of my code/mathkernel in parallel as possible. I am not shooting for anything truly parallel yet).

Since my code is too long and complicated, I do not want to edit every cell again to make them evaluate in parallel. Is there anything magical I can put in the beginning of my notebook so every cell evaluated after that will be by default ParallelEvaluate[ ... cell contents.... ]?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

够运 2024-12-04 19:31:22

正如 belisarius 所建议的, $Pre=ParallelEvaluate 正是我想要的。一个问题是,当我执行 $Pre=Identity 将其关闭以便我可以返回主内核时,mathematica 仍然尝试在从内核而不是主内核中对其进行评估,并且失败。我最终解决如下: SetSharedVariable[parallelcontrol];并行控制=并行评估; $Pre := 并行控制; ...这里的一切都是在奴隶中评估的...;并行控制=同一性; ....一切都返回到仅在主机上进行评估...这是在我的笔记本电脑上运行的示例,它有 2 个内核:

LaunchKernels[]

{KernelObject[1, "local"], KernelObject[2, "local"]}

$KernelID

0

ParallelEvaluate[$KernelID]

{ 1, 2}

SetSharedVariable[parcontrol]; $Pre := parcontrol; parcontrol = ParallelEvaluate

Null[ParallelEvaluate]

$KernelID

{1, 2}

parcontrol = Identity

{身份, 身份}

$KernelID

0

parcontrol = ParallelEvaluate

ParallelEvaluate

$KernelID

{1, 2}

As suggested by belisarius, $Pre=ParallelEvaluate does exactly what I want. One problem was when I do $Pre=Identity to turn it off so I can go back to my master kernel, mathematica still tries to evaluate that in slave kernels instead of master and fails. I ended up solving it as follows: SetSharedVariable[parallelcontrol]; parallelcontrol = ParallelEvaluate; $Pre := parallelcontrol; ... everything is evaluated in slaves here ... ; parallelcontrol = Identity; .... everything go back to be evaluated on master only ... Here is a sample run on my laptop which has 2 cores:

LaunchKernels[]

{KernelObject[1, "local"], KernelObject[2, "local"]}

$KernelID

0

ParallelEvaluate[$KernelID]

{1, 2}

SetSharedVariable[parcontrol]; $Pre := parcontrol; parcontrol = ParallelEvaluate

Null[ParallelEvaluate]

$KernelID

{1, 2}

parcontrol = Identity

{Identity, Identity}

$KernelID

0

parcontrol = ParallelEvaluate

ParallelEvaluate

$KernelID

{1, 2}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文