Skip to content

Commit

Permalink
mirai map index to error
Browse files Browse the repository at this point in the history
  • Loading branch information
shikokuchuo committed Jan 12, 2025
1 parent c0d6ecd commit a37b73a
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 50 deletions.
2 changes: 1 addition & 1 deletion R/map.R
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ print.mirai_map <- function(x, ...) {
#' @export
#'
.stop <- compiler::compile(
quote(if (is_error_value(xi)) { stop_mirai(x); stop(xi, call. = FALSE) })
quote(if (is_error_value(xi)) { stop_mirai(x); stop(sprintf("In index %d:\n%s", i, xi), call. = FALSE) })
)

# internals --------------------------------------------------------------------
Expand Down
16 changes: 13 additions & 3 deletions R/mirai.R
Original file line number Diff line number Diff line change
Expand Up @@ -634,13 +634,23 @@ mk_mirai_error <- function(e, sc) {
msg <- if (is.null(call) || call == "eval(._mirai_.[[\".expr\"]], envir = ._mirai_., enclos = .GlobalEnv)")
sprintf("Error: %s", .subset2(e, "message")) else
sprintf("Error in %s: %s", call, .subset2(e, "message"))
attributes(msg) <- e
idx <- which(as.logical(lapply(sc, identical, quote(eval(._mirai_.[[".expr"]], envir = ._mirai_., enclos = .GlobalEnv)))))
idx <- which(
as.logical(
lapply(
sc,
identical,
quote(eval(._mirai_.[[".expr"]], envir = ._mirai_., enclos = .GlobalEnv))
)
)
)
sc <- sc[(length(sc) - 1L):(idx + 1L)]
if (sc[[1L]][[1L]] == ".handleSimpleError")
sc <- sc[-1L]
sc <- lapply(sc, deparse_call)
`class<-`(`attr<-`(msg, "stack.trace", sc), c("miraiError", "errorValue", "try-error"))
`class<-`(
`attributes<-`(msg, `[[<-`(e, "stack.trace", sc)),
c("miraiError", "errorValue", "try-error")
)
}

.miraiInterrupt <- `class<-`("", c("miraiInterrupt", "errorValue", "try-error"))
Expand Down
93 changes: 47 additions & 46 deletions vignettes/mirai.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ To wait for and collect the return value, use the mirai's `[]` method:

``` r
m[]
#> [1] 4.002576 1.751233 3.226821 5.200427 3.284885
#> [1] 3.056059 4.812153 3.097510 3.259264 2.456041
```
As a mirai represents an async operation, it is never necessary to wait for it. Other code can continue to be run. Once it completes, the return value automatically becomes available at `$data`.

``` r
m
#> < mirai [$data] >
m$data
#> [1] 4.002576 1.751233 3.226821 5.200427 3.284885
#> [1] 3.056059 4.812153 3.097510 3.259264 2.456041
```
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:

Expand All @@ -82,7 +82,7 @@ args <- list(time = x$time, mean = x$mean)

m <- mirai(.expr = expr, .args = args)
m[]
#> [1] 3.916442 2.951214 2.044501 2.688432 5.520616
#> [1] 4.502500 2.994814 4.201137 4.459780 4.333615
```

[&laquo; Back to ToC](#table-of-contents)
Expand Down Expand Up @@ -162,8 +162,8 @@ for (i in 1:10) {
#> iteration 4 successful
#> iteration 5 successful
#> iteration 6 successful
#> iteration 7 successful
#> Error: random error
#> iteration 7 successful
#> iteration 8 successful
#> iteration 9 successful
#> iteration 10 successful
Expand Down Expand Up @@ -203,7 +203,7 @@ status()
#> [1] 6
#>
#> $daemons
#> [1] "abstract://2591aab600fb24a816361f11"
#> [1] "abstract://1df87ba0b972d00b998c5662"
#>
#> $mirai
#> awaiting executing completed
Expand Down Expand Up @@ -237,7 +237,7 @@ status()
#> [1] 6
#>
#> $daemons
#> [1] "abstract://42971685f607db97affae685"
#> [1] "abstract://17e480896594c8d6a3dfe2d3"
```
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.

Expand Down Expand Up @@ -265,11 +265,11 @@ m <- mirai(capture.output(str(con)))
m[]
#> [1] "Formal class 'SQLiteConnection' [package \"RSQLite\"] with 8 slots"
#> [2] " ..@ ptr :<externalptr> "
#> [3] " ..@ dbname : chr \"/tmp/RtmpcbGRUv/filec92f21c2d747\""
#> [3] " ..@ dbname : chr \"/tmp/Rtmp8U3u4H/file2d0b569baa28\""
#> [4] " ..@ loadable.extensions: logi TRUE"
#> [5] " ..@ flags : int 70"
#> [6] " ..@ vfs : chr \"\""
#> [7] " ..@ ref :<environment: 0x56a7cebba7d8> "
#> [7] " ..@ ref :<environment: 0x627deada26b0> "
#> [8] " ..@ bigint : chr \"integer64\""
#> [9] " ..@ extended_types : logi FALSE"
```
Expand Down Expand Up @@ -321,7 +321,7 @@ status()
#> [1] 0
#>
#> $daemons
#> [1] "tcp://hostname:34975"
#> [1] "tcp://hostname:38197"
#>
#> $mirai
#> awaiting executing completed
Expand Down Expand Up @@ -415,10 +415,10 @@ daemons(url = host_url())
#> [1] 0
launch_remote(2)
#> [1]
#> Rscript -e 'mirai::daemon("tcp://hostname:42961",rs=c(10407,90778926,-642612265,-394494420,-62569155,-851866918,-839171437),dispatcher=TRUE)'
#> Rscript -e 'mirai::daemon("tcp://hostname:36003",rs=c(10407,941025515,-48794832,1339075953,811245694,-214504729,2010043900),dispatcher=TRUE)'
#>
#> [2]
#> Rscript -e 'mirai::daemon("tcp://hostname:42961",rs=c(10407,-216293777,1029180408,-534177028,-534431600,821325985,425956901),dispatcher=TRUE)'
#> Rscript -e 'mirai::daemon("tcp://hostname:36003",rs=c(10407,1267504531,944175427,-1352574462,-1177718520,1238617886,81797111),dispatcher=TRUE)'
daemons(0)
#> [1] 0
```
Expand All @@ -445,37 +445,37 @@ The generated self-signed certificate is available via `launch_remote()`. This f
``` r
launch_remote(1)
#> [1]
#> Rscript -e 'mirai::daemon("tls+tcp://hostname:42777",tls=c("-----BEGIN CERTIFICATE-----
#> Rscript -e 'mirai::daemon("tls+tcp://hostname:38181",tls=c("-----BEGIN CERTIFICATE-----
#> MIIFNzCCAx+gAwIBAgIBATANBgkqhkiG9w0BAQsFADAzMREwDwYDVQQDDAhrdW1h
#> bW90bzERMA8GA1UECgwITmFub25leH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RYEJIOf3y5J/i9sNknYUhXhWc8tzyA58XMXJayiiutRxFo2gzf+InRU
#> eLTnU2ot11Edp1r9N/bdSq3AoN3u3R3oruW288CQbU1XJ1F1Uu+03uu5lX6BsAkk
#> eZEydbc9yx/fXExw1hKGNPmXBn4ISvKESVS7vEn9jPXa72wVj8hxpfCQjA==
#> -----END CERTIFICATE-----
#> ",""),rs=c(10407,-801997837,-995387240,-859772487,-1408070106,1245299311,563041764),dispatcher=TRUE)'
#> ",""),rs=c(10407,1334791256,1224252025,-1453008922,-1667395281,-71588444,-1034880939),dispatcher=TRUE)'
```
The printed value may be deployed directly on a remote machine.

Expand Down Expand Up @@ -711,10 +711,10 @@ daemons(4)
vec <- c(1, 1, 4, 4, 1, 1, 1, 1)
system.time(mirai_map(vec, Sys.sleep)[])
#> user system elapsed
#> 0.005 0.006 4.007
#> 0.005 0.003 4.007
system.time(parLapply(cl, vec, Sys.sleep))
#> user system elapsed
#> 0.009 0.003 8.012
#> 0.018 0.092 8.105
```
`.args` is used to specify further constant arguments to `.f` - the 'mean' and 'sd' in the example below:

Expand All @@ -724,13 +724,13 @@ with(
mirai_map(1:3, rnorm, .args = list(mean = 20, sd = 2))[]
)
#> [[1]]
#> [1] 19.14144
#> [1] 21.11077
#>
#> [[2]]
#> [1] 19.78728 21.73872
#> [1] 18.61698 17.94189
#>
#> [[3]]
#> [1] 18.96823 16.64821 16.74725
#> [1] 21.47360 19.24614 17.86429
```
Use `...` to further specify objects referenced but not defined in `.f` - the 'do' in the anonymous function below:

Expand All @@ -745,13 +745,13 @@ ml
#> < mirai map [3/3] >
ml[]
#> $a
#> [1] "1e"
#> [1] "eb"
#>
#> $b
#> [1] af f5
#> [1] 51 18
#>
#> $c
#> [1] "786cf0"
#> [1] "09562b"
```
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.

Expand All @@ -771,7 +771,8 @@ tryCatch(
mirai_map(list(a = 1, b = "a", c = 3), sum)[.stop],
error = identity
)
#> <simpleError: Error in .Primitive("sum")("a"): invalid 'type' (character) of argument>
#> <simpleError: In index 2:
#> Error in .Primitive("sum")("a"): invalid 'type' (character) of argument>

with(
daemons(4, dispatcher = FALSE),
Expand Down

0 comments on commit a37b73a

Please sign in to comment.