Communication of parallel processes: what are my options?

前端 未结 2 1997
逝去的感伤
逝去的感伤 2021-02-10 06:17

I\'m trying to dig a bit deeper into parallelziation of R routines.

What are my options with respect to the communication of a bunch of \"worker\" processes regarding

2条回答
  •  闹比i
    闹比i (楼主)
    2021-02-10 06:51

    For communication between processes, a kind of fun place to start is the help page ?socketConnections and the code in the chunk marked "## Not run:". So start an R process and run

     con1 <- socketConnection(port = 6011, server=TRUE)
    

    This process is acting as a server, listening on a particular port for some information. Now start a second R process and enter

     con2 <- socketConnection(Sys.info()["nodename"], port = 6011)
    

    con2 in process 2 has made a socket connection with con1 on process 1. Back at con1, write out the R object LETTERS

    writeLines(LETTERS, con1)
    

    and retrieve them on con2.

    readLines(con2)
    

    So you've communicated between processes without writing to disk. Some important concepts are also implicit here, e.g., about blocking vs. non-blocking connections, It is not limited to communication on the same machine, provided the ports are accessible across whatever network the computers are on. This is the basis for makePSOCKcluster in the parallel package, with the addition that process 1 actually uses the system command and a script in the parallel package to start process 2. The object returned by makePSOCKcluster is sub-settable, so that you can dedicate a fraction of your cluster to solving a particular task. In principle you could arrange for the spawned nodes to communicate with one another independent of the node that did the spawning.

    An interesting exercise is to do the same using the fork-like commands in the parallel package (on non-Windows). A high-level version of this is in the help page ?mcparallel, e.g.,

     p <- mcparallel(1:10)
     q <- mcparallel(1:20)
     # wait for both jobs to finish and collect all results
     res <- mccollect(list(p, q))
    

    but this builds on top of lower-level sendMaster and friends (peak at the mcparallel and mccollect source code).

    The Rmpi package takes an approach like the PSOCK example, where the manager uses scripts to spawn workers, and with communication using mpi rather than sockets. But a different approach, worthy of a weekend project if you have a functioning MPI implementation, is to implement a script that does the same calculation on different data, and then collates results onto a single node, using commands like mpi.comm.rank, mpi.barrier, mpi.send.Robj, and mpi.recv.Robj.

    A fun weekend project would use the parallel package to implement a work flow that involved parallel computation but not of the mclapply variety, e.g., where one process harvests data from a web site and then passes it to another process that draws pretty pictures. The input to the first process might well be JSON, but the communication within R is probably much more appropriately R data objects.

提交回复
热议问题