| 1 |
<!DOCTYPE html> |
| 2 |
<html lang="en"> |
| 3 |
<head> |
| 4 |
<meta charset="UTF-8"> |
| 5 |
<title>Parallel Workers on the Local Machine</title> |
| 6 |
<style> |
| 7 |
body { |
| 8 |
font-family: sans-serif; |
| 9 |
line-height: 1.6; |
| 10 |
padding-left: 3ex; |
| 11 |
padding-right: 3ex; |
| 12 |
background-color: white; |
| 13 |
color: black; |
| 14 |
} |
| 15 |
|
| 16 |
a { |
| 17 |
color: #4183C4; |
| 18 |
text-decoration: none; |
| 19 |
} |
| 20 |
|
| 21 |
h1, h2, h3 { |
| 22 |
margin: 2ex 0 1ex; |
| 23 |
padding: 0; |
| 24 |
font-weight: bold; |
| 25 |
-webkit-font-smoothing: antialiased; |
| 26 |
cursor: text; |
| 27 |
position: relative; |
| 28 |
} |
| 29 |
|
| 30 |
h2 { |
| 31 |
border-bottom: 1px solid #cccccc; |
| 32 |
} |
| 33 |
|
| 34 |
code { |
| 35 |
margin: 0 2px; |
| 36 |
padding: 0 5px; |
| 37 |
white-space: nowrap; |
| 38 |
border: 1px solid #eaeaea; |
| 39 |
background-color: #f8f8f8; |
| 40 |
border-radius: 3px; |
| 41 |
} |
| 42 |
|
| 43 |
pre code { |
| 44 |
margin: 0; |
| 45 |
padding: 0; |
| 46 |
white-space: pre; |
| 47 |
border: none; |
| 48 |
background: transparent; |
| 49 |
} |
| 50 |
|
| 51 |
pre { |
| 52 |
background-color: #f8f8f8; |
| 53 |
border: 1px solid #cccccc; |
| 54 |
line-height: 2.5x; |
| 55 |
overflow: auto; |
| 56 |
padding: 0.6ex 1ex; |
| 57 |
border-radius: 3px; |
| 58 |
} |
| 59 |
|
| 60 |
pre code { |
| 61 |
background-color: transparent; |
| 62 |
border: none; |
| 63 |
} |
| 64 |
</style> |
| 65 |
</head> |
| 66 |
<body> |
| 67 |
<h1>Parallel Workers on the Local Machine</h1> |
| 68 |
<!-- |
| 69 |
%\VignetteIndexEntry{Parallel Workers on the Local Machine} |
| 70 |
%\VignetteAuthor{Henrik Bengtsson} |
| 71 |
%\VignetteKeyword{R} |
| 72 |
%\VignetteKeyword{package} |
| 73 |
%\VignetteKeyword{vignette} |
| 74 |
%\VignetteEngine{parallelly::selfonly} |
| 75 |
--> |
| 76 |
<h1>Introduction</h1> |
| 77 |
<p>This vignette illustrates how to launch parallel workers on the |
| 78 |
current, local machine. This works the same on all operating systems |
| 79 |
where R is supported, e.g. Linux, macOS, and MS Windows.</p> |
| 80 |
<h1>Examples</h1> |
| 81 |
<h2>Example: Launching two parallel workers</h2> |
| 82 |
<p>The below illustrates how to launch a cluster of two parallel workers |
| 83 |
on the current machine, run some basic calculations in parallel, and |
| 84 |
then shut down the cluster.</p> |
| 85 |
<pre><code class="language-r">library(parallelly) |
| 86 |
library(parallel) |
| 87 |
|
| 88 |
cl <- makeClusterPSOCK(2) |
| 89 |
print(cl) |
| 90 |
#> Socket cluster with 2 nodes on host 'localhost' (R version 4.5.2 |
| 91 |
#> (2025-10-31), platform x86_64-pc-linux-gnu) |
| 92 |
|
| 93 |
y <- parLapply(cl, X = 1:100, fun = sqrt) |
| 94 |
y <- unlist(y) |
| 95 |
z <- sum(y) |
| 96 |
print(z) |
| 97 |
#> [1] 671.4629 |
| 98 |
|
| 99 |
parallel::stopCluster(cl) |
| 100 |
</code></pre> |
| 101 |
<p><em>Comment</em>: In the <strong>parallel</strong> package, a parallel worker is referred |
| 102 |
to a parallel node, or short <em>node</em>, which is why we use the same term |
| 103 |
in the <strong>parallelly</strong> package.</p> |
| 104 |
<p>An alternative to specifying the <em>number</em> of parallel workers is to |
| 105 |
specify a character vector with that number of <code>"localhost"</code> entries, |
| 106 |
e.g.</p> |
| 107 |
<pre><code class="language-r">cl <- makeClusterPSOCK(c("localhost", "localhost")) |
| 108 |
</code></pre> |
| 109 |
<h2>Example: Launching as many parallel workers as allotted</h2> |
| 110 |
<p>The <code>availableCores()</code> function will return the number of workers that |
| 111 |
the system allows. It respects many common settings that control the |
| 112 |
number of CPU cores that the current R process is allotted, e.g. R |
| 113 |
options, environment variables, and CGroups settings. For details, see |
| 114 |
<code>help("availableCores")</code>. For example,</p> |
| 115 |
<pre><code class="language-r">library(parallelly) |
| 116 |
cl <- makeClusterPSOCK(availableCores()) |
| 117 |
print(cl) |
| 118 |
#> Socket cluster with 8 nodes on host 'localhost' (R version 4.5.2 |
| 119 |
#> (2025-10-31), platform x86_64-pc-linux-gnu) |
| 120 |
</code></pre> |
| 121 |
</body> |
| 122 |
</html> |