Debian Codegraph @

inst/testme/test-as.cluster.R

raw
1 library(parallelly)
2
3 stopCluster <- parallel::stopCluster
4 useXDR <- isTRUE(parallelly:::getOption2("parallelly.makeNodePSOCK.useXDR"))
5
6 message("*** cluster operations ...")
7
8 local({
9 cl0 <- makeClusterPSOCK(1L)
10 on.exit(stopCluster(cl0))
11 cl <- cl0
12 print(cl)
13
14 message("*** cluster operations - as.cluster() ...")
15 cl1 <- as.cluster(cl)
16 print(cl1)
17 stopifnot(inherits(cl1, "cluster"), identical(cl1, cl))
18
19 node <- cl[[1]]
20 print(node)
21 cl2 <- as.cluster(node)
22 stopifnot(inherits(cl2, "cluster"), length(cl2) == 1L,
23 identical(cl2[[1]], node))
24
25 node <- cl[[1]]
26 print(node)
27 stopifnot(inherits(node, if (useXDR) "SOCKnode" else "SOCK0node"))
28 nodes <- list(node, node)
29 cl3 <- as.cluster(node)
30 print(cl3)
31 stopifnot(inherits(cl3, "cluster"), length(cl3) == 1L,
32 identical(cl3[[1]], node))
33
34 cl4 <- as.cluster(nodes)
35 print(cl4)
36 stopifnot(inherits(cl4, "cluster"), length(cl4) == 2L,
37 identical(cl4[[1]], node), identical(cl4[[2]], node))
38
39 message("*** cluster operations - as.cluster() ... DONE")
40
41 message("*** cluster operations - c(...) ...")
42 cl2 <- makeClusterPSOCK("localhost")
43 on.exit(stopCluster(cl2), add = TRUE)
44 print(cl2)
45
46 cl <- c(cl1, cl2)
47 print(cl)
48
49 stopifnot(inherits(cl, "cluster"), length(cl) == 2L)
50 stopifnot(identical(cl[1], cl1),
51 identical(cl[2], cl2[1]))
52 message("*** cluster operations - c(...) ... DONE")
53 })
54
55
56 message("*** cluster operations - makeClusterPSOCK(remotes) ...")
57
58 remotes <- Sys.getenv("R_PARALLELLY_TESTS_REMOTES", "")
59 remotes <- gsub(" ", "", unlist(strsplit(remotes, split = ",")))
60 remotes <- remotes[nzchar(remotes)]
61 if (length(remotes) > 0) {
62 message("Remotes: ", paste(sQuote(remotes), collapse = ", "))
63 local({
64 cl <- makeClusterPSOCK(remotes, verbose = TRUE)
65 on.exit(stopCluster(cl))
66 print(cl)
67 })
68 }
69
70 message("*** cluster operations - makeClusterPSOCK(remotes) ... DONE")
71
72 message("*** cluster operations ... DONE")
73
74 message("*** cluster operations - as.cluster(<non-cluster>) ...")
75 res <- tryCatch({
76 as.cluster(1)
77 }, error = function(ex) ex)
78 stopifnot(inherits(res, "error"))
79 message("*** cluster operations - as.cluster(<non-cluster>) ... DONE")
80
81 message("*** cluster operations - c(...) with duplicated nodes ...")
82 cl_dup1 <- makeClusterPSOCK(1L)
83 on.exit(stopCluster(cl_dup1), add = TRUE)
84 cl_combined <- c(cl_dup1, cl_dup1)
85 stopifnot(inherits(cl_combined, "cluster"), length(cl_combined) == 2L)
86 message("*** cluster operations - c(...) with duplicated nodes ... DONE")
87
88 message("*** cluster operations - as.cluster(SOCKnode) ...")
89 cl_base <- parallel::makeCluster(1L, type = "PSOCK")
90 node_base <- cl_base[[1]]
91 res_base <- as.cluster(node_base)
92 stopifnot(inherits(res_base, "cluster"), inherits(res_base[[1]], "SOCKnode"))
93 parallel::stopCluster(cl_base)
94 message("*** cluster operations - as.cluster(SOCKnode) ... DONE")