ParallelDo[expr,{imax}]
evaluates expr in parallel imax times.
ParallelDo[expr,{i,imax}]
evaluates expr in parallel with the variable i successively taking on the values 1 through imax (in steps of 1).
ParallelDo[expr,{i,imin,imax}]
starts with i=imin.
ParallelDo[expr,{i,imin,imax,di}]
uses steps di.
ParallelDo[expr,{i,{i1,i2,…}}]
uses the successive values i1, i2, ….
ParallelDo[expr,{i,imin,imax},{j,jmin,jmax},…]
evaluates expr looping in parallel over different values of j, etc. for each i.
ParallelDo
ParallelDo[expr,{imax}]
evaluates expr in parallel imax times.
ParallelDo[expr,{i,imax}]
evaluates expr in parallel with the variable i successively taking on the values 1 through imax (in steps of 1).
ParallelDo[expr,{i,imin,imax}]
starts with i=imin.
ParallelDo[expr,{i,imin,imax,di}]
uses steps di.
ParallelDo[expr,{i,{i1,i2,…}}]
uses the successive values i1, i2, ….
ParallelDo[expr,{i,imin,imax},{j,jmin,jmax},…]
evaluates expr looping in parallel over different values of j, etc. for each i.
Details and Options
- ParallelDo is a parallel version of Do that automatically distributes different evaluations of expr among different kernels and processors.
- If side effects involve unshared variables, they will in general work differently than in Do.
- Parallelize[Do[expr,iter, …]] is equivalent to ParallelDo[expr,iter,…].
- The following options can be given:
-
Method Automatic granularity of parallelization DistributedContexts $DistributedContexts contexts used to distribute symbols to parallel computations ProgressReporting $ProgressReporting whether to report the progress of the computation - The Method option specifies the parallelization method to use. Possible settings include:
-
"CoarsestGrained" break the computation into as many pieces as there are available kernels "FinestGrained" break the computation into the smallest possible subunits "EvaluationsPerKernel"->e break the computation into at most e pieces per kernel "ItemsPerEvaluation"->m break the computation into evaluations of at most m subunits each Automatic compromise between overhead and load balancing - Method->"CoarsestGrained" is suitable for computations involving many subunits, all of which take the same amount of time. It minimizes overhead, but does not provide any load balancing.
- Method->"FinestGrained" is suitable for computations involving few subunits whose evaluations take different amounts of time. It leads to higher overhead, but maximizes load balancing.
- The DistributedContexts option specifies which symbols appearing in expr have their definitions automatically distributed to all available kernels before the computation.
- The default value is DistributedContexts:>$DistributedContexts with $DistributedContexts:=$Context, which distributes definitions of all symbols in the current context, but does not distribute definitions of symbols from packages.
- The ProgressReporting option specifies whether to report the progress of the parallel computation.
- The default value is ProgressReporting:>$ProgressReporting.
Examples
open all close allBasic Examples (3)
ParallelDo works like Do, but in parallel:
Do[Pause[1];f[i], {i, 4}]//AbsoluteTimingParallelDo[Pause[1];f[i], {i, 4}]//AbsoluteTimingNo results are returned by ParallelDo:
ParallelDo[Print[i], {i, 4}]Use a shared variable to communicate results found to the master kernel:
SetSharedVariable[pr]pr = {};
ParallelDo[If[PrimeQ[2 ^ i - 1], AppendTo[pr, i]], {i, 2000, 10000}]
pr
| |
Options (9)
Method (2)
Calculations with vastly differing runtimes should be parallelized as finely as possible:
ParallelDo[If[PrimeQ[2 ^ i - 1], Print[i]], {i, 4000, 5000}, Method -> "FinestGrained"]A large number of simple calculations should be distributed into as few batches as possible:
BinCounts[ParallelMap[Mod[Floor[# * Pi], 10]&, Range[10000], Method -> "CoarsestGrained"], {0, 10}]DistributedContexts (5)
By default, definitions in the current context are distributed automatically:
remote[x_] := {$KernelID, x ^ 3}ParallelDo[Print[remote[i]], {i, 4}]Do not distribute any definitions of functions:
kid := $KernelIDParallelDo[Print[kid], {i, 4}, DistributedContexts -> None]Distribute definitions for all symbols in all contexts appearing in a parallel computation:
a`kid := $KernelIDParallelDo[Print[a`kid], {i, 4}, DistributedContexts -> Automatic]Distribute only definitions in the given contexts:
b`kid := $KernelIDParallelDo[Print[b`kid], {i, 4}, DistributedContexts -> {"a`"}]Restore the value of the DistributedContexts option to its default:
SetOptions[ParallelDo, DistributedContexts :> $DistributedContexts]ProgressReporting (2)
Do not show a temporary progress report:
ParallelDo[If[PrimeQ[2 ^ i - 1], Print[i]], {i, 4000, 5000}, ProgressReporting -> False]Use Method"FinestGrained" for the most accurate progress report:
ParallelDo[If[PrimeQ[2 ^ i - 1], Print[i]], {i, 4000, 10000}, Method -> "FinestGrained", ProgressReporting -> True]
| |
Applications (1)
Generate a number of animation frames and save them to individual files:
With[{frames = 60, R = 12},
ParallelDo[
Export["drum-" <> ToString[PaddedForm[t, 3, NumberPadding -> "0"]] <> ".gif", RevolutionPlot3D[Sin[t / frames * 2 * Pi]BesselJ[0, r], {r, 0, R}, PlotRange -> {{-R, R}, {-R, R}, {-1, 1}}]]
, {t, 0, frames - 1}]
]Import every 5
file and display it:
sample = FileNames["drum-*.gif"][[1 ;; -1 ;; 5]]GraphicsGrid[Partition[Import /@ sample, 4]]Properties & Relations (2)
ParallelDo performs the same iterations as ParallelTable, but does not return the values:
ParallelTable[Print[i];i, {i, 4}]ParallelDo[Print[i];i, {i, 4}]Parallelization happens along the outermost (first) index:
ParallelDo[Print[{$KernelID, i, j}], {i, 2}, {j, 4}, Method -> "FinestGrained"]Possible Issues (2)
A function used that is not known on the parallel kernels has no effect:
fprint[n_] := If[PrimeQ[2 ^ n - 1], Print[{n, $KernelID}]]ParallelDo[fprint[i], {i, 2000, 3000}, DistributedContexts -> None]Define the function on all parallel kernels:
DistributeDefinitions[fprint];The function is now evaluated on the parallel kernels:
ParallelDo[fprint[i], {i, 2000, 3000}, DistributedContexts -> None]Definitions of functions in the current context are distributed automatically:
gprint[n_] := If[PrimeQ[2 ^ n - 1], Print[{n, $KernelID}]]ParallelDo[gprint[i], {i, 2000, 3000}]Side effects are local to each parallel kernel:
i = 0;ParallelDo[i++, {5}];iUse a shared variable to support global side effects:
SetSharedVariable[j]; j = 0;ParallelDo[j++, {5}];jSee Also
Related Guides
History
Introduced in 2008 (7.0) | Updated in 2010 (8.0) ▪ 2021 (13.0)
Text
Wolfram Research (2008), ParallelDo, Wolfram Language function, https://reference.wolfram.com/language/ref/ParallelDo.html (updated 2021).
CMS
Wolfram Language. 2008. "ParallelDo." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2021. https://reference.wolfram.com/language/ref/ParallelDo.html.
APA
Wolfram Language. (2008). ParallelDo. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/ParallelDo.html
BibTeX
@misc{reference.wolfram_2026_paralleldo, author="Wolfram Research", title="{ParallelDo}", year="2021", howpublished="\url{https://reference.wolfram.com/language/ref/ParallelDo.html}", note=[Accessed: 12-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_paralleldo, organization={Wolfram Research}, title={ParallelDo}, year={2021}, url={https://reference.wolfram.com/language/ref/ParallelDo.html}, note=[Accessed: 12-June-2026]}