Skip to content

Commit

Permalink
more minimal print methods; faster promises method
Browse files Browse the repository at this point in the history
  • Loading branch information
shikokuchuo committed Jan 8, 2024
1 parent 5d9bd0d commit 5b18251
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 106 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: mirai
Type: Package
Title: Minimalist Async Evaluation Framework for R
Version: 0.11.3.9003
Version: 0.11.3.9004
Description: Lightweight parallel code execution and distributed computing.
Designed for simplicity, a 'mirai' evaluates an R expression asynchronously,
on local or network resources, resolving automatically upon completion.
Expand All @@ -22,7 +22,7 @@ Encoding: UTF-8
Depends:
R (>= 3.5)
Imports:
nanonext (>= 0.11.0.9001)
nanonext (>= 0.11.0.9010)
Enhances:
parallel,
promises
Expand Down
5 changes: 3 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# mirai 0.11.3.9003 (development)
# mirai 0.11.3.9004 (development)

* More minimal print methods for 'mirai' and 'miraiCluster'.
* Adds `local_url()` helper to construct a random inter-process communications URL for local daemons (thanks @noamross #90).
* `daemon()` argument 'autoexit' now accepts a signal value such as `tools::SIGINT` in order to raise it upon exit.
* `daemon()` now records the state of initial global environment objects (e.g. those created in .Rprofile) for cleanup purposes (thanks @noamross #91).
* Eliminates potential memory leaks along certain error paths.
* Requires nanonext >= [0.11.0.9001].
* Requires nanonext >= [0.11.0.9010].

# mirai 0.11.3

Expand Down
6 changes: 3 additions & 3 deletions R/daemons.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2022-2023 Hibiki AI Limited <[email protected]>
# Copyright (C) 2022-2024 Hibiki AI Limited <[email protected]>
#
# This file is part of mirai.
#
Expand Down Expand Up @@ -454,9 +454,9 @@ serialization <- function(refhook = list()) {

if (register) {
if (is.list(refhook) && length(refhook) == 2L && is.function(refhook[[1L]]) && is.function(refhook[[2L]]))
cat("[ mirai ] serialization functions registered\n", file = stdout()) else
cat("mirai serialization functions registered\n", file = stdout()) else
if (is.null(refhook))
cat("[ mirai ] serialization functions cancelled\n", file = stdout()) else
cat("mirai serialization functions cancelled\n", file = stdout()) else
stop(._[["refhook_invalid"]])
register_everywhere(refhook)
}
Expand Down
3 changes: 2 additions & 1 deletion R/mirai-package.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2022-2023 Hibiki AI Limited <[email protected]>
# Copyright (C) 2022-2024 Hibiki AI Limited <[email protected]>
#
# This file is part of mirai.
#
Expand Down Expand Up @@ -119,6 +119,7 @@ registerPromisesMethods <- function(pkgname, pkgpath) {
regs <- rbind(ns[[".__NAMESPACE__."]][["S3methods"]],
c("as.promise", "mirai", "as.promise.mirai", NA_character_))
`[[<-`(ns[[".__NAMESPACE__."]], "S3methods", regs)
`[[<-`(., "later", .getNamespace("later")[["later"]])
}

}
Expand Down
4 changes: 2 additions & 2 deletions R/mirai.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2022-2023 Hibiki AI Limited <[email protected]>
# Copyright (C) 2022-2024 Hibiki AI Limited <[email protected]>
#
# This file is part of mirai.
#
Expand Down Expand Up @@ -423,7 +423,7 @@ is_error_value <- is_error_value
#'
print.mirai <- function(x, ...) {

cat("< mirai >\n - $data for evaluated result\n", file = stdout())
cat("< mirai | $data >\n", file = stdout())
invisible(x)

}
Expand Down
6 changes: 3 additions & 3 deletions R/parallel.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2023 Hibiki AI Limited <[email protected]>
# Copyright (C) 2023-2024 Hibiki AI Limited <[email protected]>
#
# This file is part of mirai.
#
Expand Down Expand Up @@ -188,7 +188,7 @@ recvOneData.miraiCluster <- function(cl) {
print.miraiCluster <- function(x, ...) {

id <- attr(.subset2(x, 1L), "id")
cat(sprintf("< miraiCluster >\n - cluster ID: %s\n - nodes: %d\n - active: %s\n", id, length(x), as.logical(length(..[[id]]))), file = stdout())
cat(sprintf("< miraiCluster | ID: %s nodes: %d active: %s >\n", id, length(x), as.logical(length(..[[id]]))), file = stdout())
invisible(x)

}
Expand All @@ -197,7 +197,7 @@ print.miraiCluster <- function(x, ...) {
#'
print.miraiNode <- function(x, ...) {

cat(sprintf("< miraiNode >\n - node: %d\n - cluster ID: %s\n", attr(x, "node"), attr(x, "id")), file = stdout())
cat(sprintf("< miraiNode | node: %d cluster ID: %s >\n", attr(x, "node"), attr(x, "id")), file = stdout())
invisible(x)

}
Expand Down
5 changes: 2 additions & 3 deletions R/promises.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2023 Hibiki AI Limited <[email protected]>
# Copyright (C) 2023-2024 Hibiki AI Limited <[email protected]>
#
# This file is part of mirai.
#
Expand Down Expand Up @@ -53,10 +53,9 @@
as.promise.mirai <- function(x)
promises::promise(
function(resolve, reject) {
later <- .getNamespace("later")[["later"]]
query <- function()
if (unresolved(x))
later(query, delay = 0.1) else
.[["later"]](query, delay = 0.1) else
if (is_error_value(value <- .subset2(x, "value"))) reject(value) else resolve(value)
query()
}
Expand Down
18 changes: 7 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ m <- mirai(
)

m
#> < mirai >
#> - $data for evaluated result
#> < mirai | $data >
```

Above, all specified `name = value` pairs are passed through to the
Expand All @@ -88,17 +87,17 @@ result.

``` r
m$data
#> [1] -0.67343605 -0.20497828 50.37872905 1.48134108 0.37001452 2.70259664
#> [7] 0.67506398 0.01984965 -4.87856556 -1.48492199
#> [1] -0.4556118 1.3715484 -8.0198664 1.5971891 -2.0078760 -0.4980387
#> [7] 0.6260999 -0.1246904 0.7291030 -2.1948512
```

Alternatively, explicitly call and wait for the result using
`call_mirai()`.

``` r
call_mirai(m)$data
#> [1] -0.67343605 -0.20497828 50.37872905 1.48134108 0.37001452 2.70259664
#> [7] 0.67506398 0.01984965 -4.87856556 -1.48492199
#> [1] -0.4556118 1.3715484 -8.0198664 1.5971891 -2.0078760 -0.4980387
#> [7] 0.6260999 -0.1246904 0.7291030 -2.1948512
```

### Vignette
Expand Down Expand Up @@ -135,10 +134,7 @@ package.
``` r
cl <- make_cluster(4)
cl
#> < miraiCluster >
#> - cluster ID: `0`
#> - nodes: 4
#> - active: TRUE
#> < miraiCluster | ID: `0` nodes: 4 active: TRUE >
```

`make_cluster()` creates a ‘miraiCluster’, a cluster fully compatible
Expand Down Expand Up @@ -215,7 +211,7 @@ simply make the following call once at the start of your session:

``` r
serialization(refhook = list(torch::torch_serialize, torch::torch_load))
#> [ mirai ] serialization functions registered
#> mirai serialization functions registered
```

This allows tensors, including more complex objects such as models,
Expand Down
2 changes: 1 addition & 1 deletion inst/CITATION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
bibentry(bibtype = "Manual",
title = "mirai: Minimalist Async Evaluation Framework for R",
author = person("Charlie", "Gao"),
year = 2023,
year = 2024,
note = sprintf("R package version %s", meta$Version),
url = "https://doi.org/10.5281/zenodo.7912722")
Loading

0 comments on commit 5b18251

Please sign in to comment.