From 6d904bfd50ec24b2c7e903e1a4831bc8eab4e11a Mon Sep 17 00:00:00 2001 From: shikokuchuo <53399081+shikokuchuo@users.noreply.github.com> Date: Tue, 4 Jun 2024 22:45:29 +0100 Subject: [PATCH] tweak mirai map print method --- R/map.R | 2 +- vignettes/mirai.Rmd | 106 ++++++++++++++++++++++---------------------- 2 files changed, 54 insertions(+), 54 deletions(-) diff --git a/R/map.R b/R/map.R index b6c836011..f41b87c89 100644 --- a/R/map.R +++ b/R/map.R @@ -173,7 +173,7 @@ mirai_map <- function(.x, .f, ..., .args = list(), .promise = NULL, .compute = " print.mirai_map <- function(x, ...) { xlen <- length(x) - cat(sprintf("< mirai map [ %d / %d ] >\n", xlen - .unresolved(x), xlen), file = stdout()) + cat(sprintf("< mirai map [%d/%d] >\n", xlen - .unresolved(x), xlen), file = stdout()) invisible(x) } diff --git a/vignettes/mirai.Rmd b/vignettes/mirai.Rmd index f9885e019..4971d03f6 100644 --- a/vignettes/mirai.Rmd +++ b/vignettes/mirai.Rmd @@ -71,7 +71,7 @@ To wait for and collect the evaluated result, use the mirai's `[]` method: ``` r m[] -#> [1] 59.11044 +#> [1] 55.65924 ``` It is not necessary to wait, as the mirai resolves automatically whenever the async operation completes, the evaluated result then available at `$data`. @@ -83,7 +83,7 @@ m ``` r m$data -#> [1] 59.11044 +#> [1] 55.65924 ``` For easy programmatic use of `mirai()`, '.expr' accepts a pre-constructed language object, and also a list of named arguments passed via '.args'. So, the following would be equivalent to the above: @@ -99,7 +99,7 @@ args <- list(mean = input$x, sd = input$y) m <- mirai(.expr = expr, .args = args) m[] -#> [1] 56.61692 +#> [1] 56.80728 ``` [« Back to ToC](#table-of-contents) @@ -182,8 +182,8 @@ for (i in 1:10) { #> iteration 5 successful #> iteration 6 successful #> iteration 7 successful -#> iteration 8 successful #> Error: random error +#> iteration 8 successful #> iteration 9 successful #> iteration 10 successful ``` @@ -217,12 +217,12 @@ status() #> #> $daemons #> i online instance assigned complete -#> abstract://2fa7e301693ed575a462e656 1 1 1 0 0 -#> abstract://c5f4df1ca83773c57c91a926 2 1 1 0 0 -#> abstract://c00131de9a467cb4fa9b73d8 3 1 1 0 0 -#> abstract://9ff95a5f902c99cbe4b304b8 4 1 1 0 0 -#> abstract://a0afcdfc83a34c149e4569fa 5 1 1 0 0 -#> abstract://8f2950f76e68520f6bcd5956 6 1 1 0 0 +#> abstract://1796721868fe833a4d79a9d2 1 1 1 0 0 +#> abstract://409093e696175813a422e2d0 2 1 1 0 0 +#> abstract://ed35107d353446c7272b788d 3 1 1 0 0 +#> abstract://0dc671f0526afd9a6f9f23bd 4 1 1 0 0 +#> abstract://436170218593260e359fbe8e 5 1 1 0 0 +#> abstract://0cf06fe429656deb4e68fba3 6 1 1 0 0 ``` The default `dispatcher = TRUE` creates a `dispatcher()` background process that connects to individual daemon processes on the local machine. This ensures that tasks are dispatched efficiently on a first-in first-out (FIFO) basis to daemons for processing. Tasks are queued at the dispatcher and sent to a daemon as soon as it can accept the task for immediate execution. @@ -251,7 +251,7 @@ status() #> [1] 6 #> #> $daemons -#> [1] "abstract://c0e485550dbc31183989689e" +#> [1] "abstract://5c6c6415209ac993f2ffc3bd" ``` This implementation sends tasks immediately, and ensures that tasks are evenly-distributed amongst daemons. This means that optimal scheduling is not guaranteed as the duration of tasks cannot be known *a priori*. As an example, tasks could be queued at a daemon behind a long-running task, whilst other daemons are idle having already completed their tasks. @@ -279,11 +279,11 @@ m <- mirai(capture.output(str(con))) m[] #> [1] "Formal class 'SQLiteConnection' [package \"RSQLite\"] with 8 slots" #> [2] " ..@ ptr : " -#> [3] " ..@ dbname : chr \"/tmp/RtmpxulqWL/file103ad781f9542\"" +#> [3] " ..@ dbname : chr \"/tmp/RtmpW3mY6Z/file11a584826e313\"" #> [4] " ..@ loadable.extensions: logi TRUE" #> [5] " ..@ flags : int 70" #> [6] " ..@ vfs : chr \"\"" -#> [7] " ..@ ref : " +#> [7] " ..@ ref : " #> [8] " ..@ bigint : chr \"integer64\"" #> [9] " ..@ extended_types : logi FALSE" ``` @@ -373,7 +373,7 @@ By specifying `dispatcher = FALSE`, remote daemons connect directly to the host ``` r daemons(url = host_url(), dispatcher = FALSE) -#> [1] "tcp://hostname:38835" +#> [1] "tcp://hostname:32993" ``` Note that above, calling `host_url()` without a port value uses the default of '0'. This is a wildcard value that will automatically cause a free ephemeral port to be assigned. The actual assigned port is provided in the return value of the call, or it may be queried at any time via `status()`. @@ -387,7 +387,7 @@ status() #> [1] 0 #> #> $daemons -#> [1] "tcp://hostname:38835" +#> [1] "tcp://hostname:32993" ``` To reset all connections and revert to default behaviour: @@ -459,10 +459,10 @@ daemons(n = 2, url = host_url()) ``` r launch_remote(1:2) #> [1] -#> Rscript -e "mirai::daemon('tcp://hostname:34595',rs=c(10407,-1126958163,-181054838,1668160131,1359405544,501460681,771501046))" +#> Rscript -e "mirai::daemon('tcp://hostname:43647',rs=c(10407,484960323,-725392216,-1321459063,493984950,-173687873,-604042508))" #> #> [2] -#> Rscript -e "mirai::daemon('tcp://hostname:40241',rs=c(10407,-1553484436,-864207463,-1025663212,-964896492,1682066597,684793135))" +#> Rscript -e "mirai::daemon('tcp://hostname:44795',rs=c(10407,-701915261,1786274565,-1507697296,-1492186855,-1556181348,582193203))" ``` ``` r @@ -492,37 +492,37 @@ The generated self-signed certificate is available via `launch_remote()`. This f ``` r launch_remote(1) #> [1] -#> Rscript -e "mirai::daemon('wss://hostname:45205/1',tls=c('-----BEGIN CERTIFICATE----- +#> Rscript -e "mirai::daemon('wss://hostname:46177/1',tls=c('-----BEGIN CERTIFICATE----- #> MIIFNzCCAx+gAwIBAgIBATANBgkqhkiG9w0BAQsFADAzMREwDwYDVQQDDAhrdW1h #> bW90bzERMA8GA1UECgwITmFub25leHQxCzAJBgNVBAYTAkpQMB4XDTAxMDEwMTAw #> MDAwMFoXDTMwMTIzMTIzNTk1OVowMzERMA8GA1UEAwwIa3VtYW1vdG8xETAPBgNV #> BAoMCE5hbm9uZXh0MQswCQYDVQQGEwJKUDCC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nqUDe9zPJZuGMwt9CtjCsg== #> -----END CERTIFICATE----- -#> ',''),rs=c(10407,-1412019416,-946653431,-2118678730,-1190460353,-1052206732,274843109))" +#> ',''),rs=c(10407,2076945126,615221295,-1686802268,-462977195,-1340362734,506390635))" ``` The printed value may be deployed directly on a remote machine. @@ -776,19 +776,19 @@ daemons(4, retry = FALSE) vec <- c(1, 1, 4, 4, 1, 1, 1, 1) system.time(mirai_map(vec, Sys.sleep)[]) #> user system elapsed -#> 0.009 0.000 4.052 +#> 0.006 0.000 4.040 ``` ``` r system.time(parLapplyLB(cl, vec, Sys.sleep)) #> user system elapsed -#> 0.009 0.002 5.010 +#> 0.006 0.009 5.010 ``` ``` r system.time(parLapply(cl, vec, Sys.sleep)) #> user system elapsed -#> 0.021 0.086 8.102 +#> 0.009 0.001 8.009 ``` `.args` is used to specify further constant arguments to `.f` - the 'mean' and 'sd' in the example below: @@ -798,13 +798,13 @@ with( mirai_map(1:3, rnorm, .args = list(mean = 20, sd = 2))[] ) #> [[1]] -#> [1] 22.53297 +#> [1] 19.72028 #> #> [[2]] -#> [1] 18.57054 18.00322 +#> [1] 20.56137 19.78063 #> #> [[3]] -#> [1] 22.74980 20.00176 16.97306 +#> [1] 21.50056 24.13770 23.32697 ``` Use `...` to further specify objects referenced but not defined in `.f` - the 'do' in the anonymous function below: @@ -820,19 +820,19 @@ ml <- mirai_map( ``` r ml -#> < mirai map [ 3 / 3 ] > +#> < mirai map [3/3] > ``` ``` r ml[] #> $a -#> [1] "f8" +#> [1] "5b" #> #> $b -#> [1] e3 d5 +#> [1] 2d 7c #> #> $c -#> [1] "682fbe" +#> [1] "239565" ``` Use of `mirai_map()` assumes that `daemons()` have previously been set. If not then one (non-dispatcher) daemon is set to allow the function to proceed. This ensures safe behaviour, but is unlikely to be optimal, so please ensure daemons are set beforehand.