Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if stopCluster (R) worked

When I try to remove a cluster from my workspace with stopCluster, it does not seem to work. Below is the code I am using.

> cl <- makeCluster(3)
> cl
socket cluster with 3 nodes on host ‘localhost’
> stopCluster(cl)
> cl
socket cluster with 3 nodes on host ‘localhost’

Note that the command cl still is called a socket cluster with 3 nodes after I have supposedly removed it. Shouldn't I get an error that object cl is not found? How do I know that my cluster has actually been removed? A related question: if I close R, is the cluster terminated and my computer returned to its normal state of being able to use all of its cores?

like image 846
BioBroo Avatar asked Jan 05 '23 17:01

BioBroo


2 Answers

You shouldn't get an error that cl is not found, until you run rm(cl). Stopping a cluster doesn't remove the object from your environment.

Use showConnections to see that no connections are active:

> require(parallel)
Loading required package: parallel
> cl <- makeCluster(3)
> cl
socket cluster with 3 nodes on host ‘localhost’
> showConnections()
  description         class      mode  text     isopen   can read can write
3 "<-localhost:11129" "sockconn" "a+b" "binary" "opened" "yes"    "yes"    
4 "<-localhost:11129" "sockconn" "a+b" "binary" "opened" "yes"    "yes"    
5 "<-localhost:11129" "sockconn" "a+b" "binary" "opened" "yes"    "yes"    
> stopCluster(cl)
> showConnections()
     description class mode text isopen can read can write
> 

Whether or not your computer is "returned to its normal state" depends on the type of cluster you create. If it's just a simple socket or fork cluster, then gracefully stopping the parent process should cause all the child processes to terminate. If it's a more complicated cluster, it's possible terminating R will not stop all the jobs it started on the nodes.

like image 54
Joshua Ulrich Avatar answered Jan 08 '23 07:01

Joshua Ulrich


Unfortunately, the print.SOCKcluster method doesn't tell you if the cluster object is usable. However, you can find out if it's usable by printing the elements of the cluster object, thus using the print.SOCKnode method. For example:

> library(parallel)
> cl <- makeCluster(3)
> for (node in cl) try(print(node))
node of a socket cluster on host ‘localhost’ with pid 29607
node of a socket cluster on host ‘localhost’ with pid 29615
node of a socket cluster on host ‘localhost’ with pid 29623
> stopCluster(cl)
> for (node in cl) try(print(node))
Error in summary.connection(connection) : invalid connection
Error in summary.connection(connection) : invalid connection
Error in summary.connection(connection) : invalid connection

Note that print.SOCKnode actually sends a message via the socket connection in order to get the process ID of the corresponding worker, as seen in the source code:

> parallel:::print.SOCKnode
function (x, ...) 
{
    sendCall(x, eval, list(quote(Sys.getpid())))
    pid <- recvResult(x)
    msg <- gettextf("node of a socket cluster on host %s with pid %d", 
        sQuote(x[["host"]]), pid)
    cat(msg, "\n", sep = "")
    invisible(x)
}
<bytecode: 0x2f0efc8>
<environment: namespace:parallel>

Thus, if you've called stopCluster on the cluster object, you'll get errors trying to use the socket connections.

like image 27
Steve Weston Avatar answered Jan 08 '23 05:01

Steve Weston