ParallelMap[f,expr]
将 f 并行作用在 expr 第一层的元素上.
ParallelMap[f,expr,levelspec]
将 f 并行作用在 levelspec 指定 expr 的部分元素上.
ParallelMap
ParallelMap[f,expr]
将 f 并行作用在 expr 第一层的元素上.
ParallelMap[f,expr,levelspec]
将 f 并行作用在 levelspec 指定 expr 的部分元素上.
更多信息和选项
- ParallelMap 是一个并行的 Map,将 f 作用在不同的内核和处理器之间.
- ParallelMap 将给出和 Map 相同的结果,除了计算过程中的副作用.
- ParallelMap 用和 Map 相同的层指定,并非所有指定可以并行化.
- Parallelize[Map[f,expr]] 等价于 ParallelMap[f,expr].
- 对于不能并行的 ParallelMap,用 Map 执行计算.
- 可以给出下列选项:
-
Method Automatic 并行化粒度 DistributedContexts $DistributedContexts 用于将符号分配到并行计算的上下文 ProgressReporting $ProgressReporting 是否报告计算进度 - Method 选项指定要使用的并行化方法. 可能的设置包括:
-
"CoarsestGrained" 将计算分成与可用内核数量相同的段 "FinestGrained" 将计算分成最小的子单元 "EvaluationsPerKernel"->e 将计算分成每个内核最多 e 段 "ItemsPerEvaluation"->m 将计算分成每个内核最多 m 个子单元 Automatic 在总开销和负载平衡之间的折中 - Method->"CoarsestGrained" 适用于涉及多个子单元的计算,所有子单元采用相同的时间计算. 将总开销降到最低,不提供任何负载均衡.
- Method->"FinestGrained" 适用于少量子单元的计算,子单元采用不同的时间计算. 它提高总开销,但最大化了负载均衡.
- DistributedContexts 选项指定出现在 expr 中的哪些符号在计算之前将其定义自动分发到所有可用内核.
- 设置 $DistributedContexts:=$Context,默认值为 DistributedContexts:>$DistributedContexts,该值分发当前上下文中所有符号的定义,但不分发程序包中的符号定义.
- ProgressReporting 选项指定是否报告并行计算的进度.
- 默认值为 ProgressReporting:>$ProgressReporting.
范例
打开所有单元 关闭所有单元基本范例 (4)
ParallelMap 与 Map 工作方式相似,不同之处在于并行:
Map[(Pause[1];f[#])&, {a, b, c, d}]//AbsoluteTimingParallelMap[(Pause[1];f[#])&, {a, b, c, d}]//AbsoluteTimingParallelMap 对于任何函数都适用:
ParallelMap[Composition[Framed, FactorInteger], {1, 11, 111, 1111, 11111, 111111}]ParallelMap[N[#, 50]&, {E, Pi, Catalan}]f1[n_] := Length[FactorInteger[(10 ^ n - 1) / 9]]ParallelMap[f1, Range[50, 60]]res = ParallelMap[PrimeQ[2 ^ # - 1]&, Range[9601, 12000]];
| |
范围 (6)
ParallelMap[f, {{a, b}, {c, d}}]ParallelMap[f, {{a, b}, {c, d}}, 2]ParallelMap[f, {{a, b}, {c, d}}, {2}]ParallelMap[f, {{{{{a}}}}}, 3]ParallelMap[f, {{{{{a, b}}}}}, Infinity]ParallelMap[f, {{{{{a, b}}}}}, -1]ParallelMap[f, {{{{{a, b}}}}}, -2]ParallelMap[f, {{{{{a, b}}}}}, -3]ParallelMap[f, {{{{{a}}}}}, {2, -3}]ParallelMap[f, h0[h1[h2[h3[h4[a]]]]], {2, -3}]ParallelMap[f, {{{{{a}}}}}, {0, 2}]推广和延伸 (2)
ParallelMap 可以用于具有任意头部的表达式中:
ParallelMap[f, a + b + c + d]ParallelMap[f, x ^ 2 + y ^ 2, 2]具有属性 Listable 的函数被自动映射:
Parallelize[Sqrt[{1, 2, 3, 4}]]ParallelMap[Sqrt, {1, 2, 3, 4}]选项 (13)
DistributedContexts (5)
remote[x_] := {$KernelID, x ^ 3}ParallelMap[remote, {1, 2, 3, 4}]local[x_] := {$KernelID, x ^ 2}ParallelMap[local, {1, 2, 3, 4}, DistributedContexts -> None]a`f[x_] := {$KernelID, x}ParallelMap[a`f, {1, 2, 3, 4}, DistributedContexts -> Automatic]b`g[x_] := {$KernelID, -x}ParallelMap[b`g, {1, 2, 3, 4}, DistributedContexts -> {"a`"}]恢复 DistributedContexts 选项的值为默认值:
SetOptions[ParallelMap, DistributedContexts :> $DistributedContexts]Method (6)
ParallelMap[Labeled[Framed[#], $KernelID]&, Range[10], Method -> "FinestGrained"]ParallelMap[Labeled[Framed[#], $KernelID]&, Range[10], Method -> "CoarsestGrained"]ParallelMap[Labeled[Framed[#], $KernelID]&, Range[12], Method -> "EvaluationsPerKernel" -> 2]ParallelMap[Labeled[Framed[#], $KernelID]&, Range[18], Method -> "ItemsPerEvaluation" -> 5]ParallelMap[Labeled[Framed[#], $KernelID]&, Range[20], Method -> Automatic]ParallelMap[PrimeQ[2 ^ # - 1]&, Range[30000, 30031], Method -> "FinestGrained"]BinCounts[ParallelMap[Mod[Floor[# * Pi], 10]&, Range[10000], Method -> "CoarsestGrained"], {0, 10}]ProgressReporting (2)
res = ParallelMap[PrimeQ[2 ^ # - 1]&, Range[9601, 12000], ProgressReporting -> False];使用 Method"FinestGrained" 可得到准确度最高的进度报告:
res = ParallelMap[{#, Total[FactorInteger[10 ^ 60 + #][[All, 2]]]}&, Range[1000], Method -> "FinestGrained", ProgressReporting -> True];
| |
应用 (2)
SetSharedVariable[primes]Monitor[primes = {};
ParallelMap[If[PrimeQ[2 ^ # - 1], AppendTo[primes, #]]&, Range[1000, 5000]];primes,
primes]Framed[Grid[{{"SEND"}, {"+MORE"}, {"MONEY"}}, Dividers -> {None, {False, False, True, False}}, Alignment -> {Automatic, Right}, BaseStyle -> "Text"], FrameMargins -> 5, FrameStyle -> None]eq = word[s, e, n, d] + word[m, o, r, e] == word[m, o, n, e, y]eq /. Thread[{d, e, m, n, o, r, s, y} -> {7, 5, 1, 6, 0, 8, 9, 2}]% /. word[l__] :> FromDigits[{l}]check[eq_, letters_, digits_] := eq /. Thread[letters -> digits] /. word[args___] :> FromDigits[{args}]check[eq, {d, e, m, n, o, r, s, y}, {7, 5, 1, 6, 0, 8, 9, 2}]alpha = Union[Reap[eq /. word[args___] :> Sow /@ {args}][[2, 1]]]Join@@Map[Select[Permutations[#], check[eq, alpha, #]&]&, Subsets[Range[0, 9], {Length[alpha]}]]Join@@ParallelMap[Select[Permutations[#], check[eq, alpha, #]&]&, Subsets[Range[0, 9], {Length[alpha]}]]Select[%, #[[3]] ≠ 0&]一旦使用 ParallelTry 找到一个非平凡解,搜索也可以停止:
ParallelTry[Catch[Scan[If[#[[3]] ≠ 0 && check[eq, alpha, #], Throw[#]]&, Permutations[#]];$Failed]&, Subsets[Range[0, 9], {Length[alpha]}]]属性和关系 (10)
ParallelMap[Labeled[Framed[#], $KernelID]&, {a, b, c, d}, 2]ParallelMap[Labeled[Framed[#], $KernelID]&, {{a, b}, {c, d}}, 2]Map 实际使用 ParallelMap,可以被自动并行化:
Parallelize[Map[PrimeQ[2 ^ # - 1]&, Range[100, 110]]]ParallelMap[PrimeQ[2 ^ # - 1]&, Range[100, 110]]times = Range[0, 1, 0.04]AbsoluteTiming[ParallelMap[Pause, times, Method -> "FinestGrained"];]AbsoluteTiming[ParallelMap[Pause, times, Method -> "CoarsestGrained"];]tasks = Range[0.1, 1, 0.05];seq = Total[tasks]t1 = First[AbsoluteTiming[ParallelMap[Pause, tasks]]]seq / t1最精颗粒度的调用(Finest-grained scheduling)给出更好的负载平衡和更高的加速:
t2 = First[AbsoluteTiming[ParallelMap[Pause, tasks, Method -> "FinestGrained"]]]seq / t2t3 = First[AbsoluteTiming[ParallelMap[Pause, Reverse[tasks], Method -> "FinestGrained"]]]seq / t3关于一些变量的函数可以使用 MapThread 映射:
MapThread[f, {{1, 2, 3}, {a, b, c}}]通过使用 Parallelize 获取一个并行版本:
Parallelize[MapThread[Labeled[Framed[f[##]], $KernelID]&, {{1, 2, 3}, {a, b, c}}]]MapIndexed 把一个元素的索引传递给被映射的函数:
MapIndexed[f, {a, b, c}]通过使用 Parallelize 获取一个并行版本:
Parallelize[MapIndexed[Labeled[Framed[f[##]], $KernelID]&, {a, b, c}]]Parallelize[Map[Print, {"a", "b"}]]Parallelize[Scan[Print, {"a", "b"}]]ftest[n_] := Labeled[Framed[PrimeQ[2 ^ n - 1]], $KernelID]ParallelMap[ftest, Range[1275, 1285]]gtest[n_] := Labeled[Framed[PrimeQ[2 ^ n - 1]], $KernelID]DistributeDefinitions[gtest];ParallelMap[gtest, Range[1275, 1285], DistributedContexts -> None]对于来自程序包的函数,使用 ParallelNeeds 而不是 DistributeDefinitions:
Needs["FiniteFields`"]Map[FieldSize, Table[GF[2, i], {i, 10}]]ParallelNeeds["FiniteFields`"]ParallelMap[FieldSize, Table[GF[2, i], {i, 10}]]并行获取 Mathematica 安装中所有 Wolfram 语言文件的大小:
ParallelMap[FileByteCount, FileNames["*.m", $InstallationDirectory, Infinity]]//ShortTotal[%]可能存在的问题 (7)
如果一个层次指定阻止了并行化的运行,ParallelMap 的计算类似 Map:
ParallelMap[f, {a, b, c}, 0]primes = {};
ParallelMap[If[PrimeQ[2 ^ # - 1], AppendTo[primes, #];#]&, Range[1000, 2000]];primesSetSharedVariable[sprimes]sprimes = {};
ParallelMap[If[PrimeQ[2 ^ # - 1], AppendTo[sprimes, #]]&, Range[1000, 2000]];sprimesftest[n_] := Labeled[Framed[PrimeQ[2 ^ n - 1]], $KernelID]ParallelMap[ftest, Range[1275, 1285], DistributedContexts -> None]DistributeDefinitions[ftest]ParallelMap[ftest, Range[1275, 1285], DistributedContexts -> None]gtest[n_] := Labeled[Framed[PrimeQ[2 ^ n - 1]], $KernelID]ParallelMap[gtest, Range[1275, 1285]]ctx`mtest[n_] := Labeled[Framed[PrimeQ[2 ^ n - 1]], $KernelID]ParallelMap[ctx`mtest, Range[1275, 1285]]使用 DistributeDefinitions 分配该类定义:
DistributeDefinitions[ctx`mtest];ParallelMap[ctx`mtest, Range[1275, 1285]]或者,设置 DistributedContexts 选项包括所有上下文:
cty`mtest[n_] := Labeled[Framed[PrimeQ[2 ^ n - 1]], $KernelID]ParallelMap[cty`mtest, Range[1275, 1285], DistributedContexts -> Automatic]AbsoluteTiming[ParallelMap[Composition[N, Sin], Range[1000]];]AbsoluteTiming[Map[Composition[N, Sin], Range[1000]];]AbsoluteTiming[Do[ParallelEvaluate[Null, First[Kernels[]]], {100}]]AbsoluteTiming[Do[N[Sin[1]], {100}]]{Majority[a], Majority[b, c], Majority[d, e, f]}ParallelMap[fun, Majority[a, b, c, d, e, f]]Majority@@ParallelMap[fun, {a, b, c, d, e, f}]ParallelMap[Timing[Inverse[RandomReal[{-1, 1}, {#, #}]];#]&, 2 ^ Range[13]]
| |
文本
Wolfram Research (2008),ParallelMap,Wolfram 语言函数,https://reference.wolfram.com/language/ref/ParallelMap.html (更新于 2021 年).
CMS
Wolfram 语言. 2008. "ParallelMap." Wolfram 语言与系统参考资料中心. Wolfram Research. 最新版本 2021. https://reference.wolfram.com/language/ref/ParallelMap.html.
APA
Wolfram 语言. (2008). ParallelMap. Wolfram 语言与系统参考资料中心. 追溯自 https://reference.wolfram.com/language/ref/ParallelMap.html 年
BibTeX
@misc{reference.wolfram_2026_parallelmap, author="Wolfram Research", title="{ParallelMap}", year="2021", howpublished="\url{https://reference.wolfram.com/language/ref/ParallelMap.html}", note=[Accessed: 13-November-2026]}
BibLaTeX
@online{reference.wolfram_2026_parallelmap, organization={Wolfram Research}, title={ParallelMap}, year={2021}, url={https://reference.wolfram.com/language/ref/ParallelMap.html}, note=[Accessed: 13-November-2026]}