Dear R, I have two R instances running at the same time, say instance A and instance B. Is there a simpler way to pass the data in A to B? More precise, I have a stupid example: In instance A, I am running a function "test1" test1 <- function (x1) { x2 <- sin(x1) return(x2) } In instance B, another function "test2" test2 <- function (x2) { x3 <- cos(x2) return(x3) } where " test2" receives the input from "test1"'s rueslt. "test1" and "test2" could be much more complex. They may take one minute each. Now the whole procedure is instance A is running, while instance B is waiting for the result of instance A. Once instance A is done (instance A goes to run with new data), instance B should detect A is done, and instance B receives the parameter from instance A. B begins to work. While B is done, waiting for A's new results. I want to repeat the loop many times and get x3 in the end. Is it possible to do this job? Thanks ! Feng [[alternative HTML version deleted]]
Thanks your information any how. I am using a Quad CPU. That's why I really want two or more than two instances. On Wed, Feb 4, 2009 at 3:46 PM, <markleeds@verizon.net> wrote:> There's no need to wait as long as you have the econd function fter the > first ( the second won't > start until the first one's finished ) but I think it's better to use > lexical scoping by putting the second function right inside the first. > Something like this: > > test1 <- function (x1) > { > x2 <- sin(x1) > > test2 <- function(x2) { > x3<- cos(x2) > } > > return(x3) > } > > > Then just call test1. > > But wait till someone else responds because I'm not an expeRt and someone > else might > say something more useful or different. > > > > > > > > > In instance B, another function "test2" >> >> test2 <- function (x2) >> { >> x3 <- cos(x2) >> return(x3) >> } >> > > > > On Wed, Feb 4, 2009 at 9:08 AM, Feng Li wrote: > > Dear R, >> >> I have two R instances running at the same time, say instance A and >> instance >> B. Is there a simpler way to pass the data in A to B? >> >> More precise, I have a stupid example: >> >> In instance A, I am running a function "test1" >> >> test1 <- function (x1) >> { >> x2 <- sin(x1) >> return(x2) >> } >> >> In instance B, another function "test2" >> >> test2 <- function (x2) >> { >> x3 <- cos(x2) >> return(x3) >> } >> >> where " test2" receives the input from "test1"'s rueslt. "test1" and >> "test2" could be much more complex. They may take one minute each. >> >> Now the whole procedure is instance A is running, while instance B is >> waiting for the result of instance A. Once instance A is done (instance A >> goes to run with new data), instance B should detect A is done, and >> instance >> B receives the parameter from instance A. B begins to work. While B is >> done, >> waiting for A's new results. >> >> I want to repeat the loop many times and get x3 in the end. >> >> >> Is it possible to do this job? Thanks ! >> >> >> Feng >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> R-help@r-project.org mailing list >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide >> http://www.R-project.org/posting-guide.html >> and provide commented, minimal, self-contained, reproducible code. >> >-- Feng Li Department of Statistics Stockholm University 106 91 Stockholm, Sweden [[alternative HTML version deleted]]
Feng Li wrote:> > I have two R instances running at the same time,On the same computer, or on different computers? Is the number of Rs likely to change, or will it always be just the two? Is this a simple one-off problem, or are you breaking the problem up into pieces so you can throw lots of hardware at it?> Is there a simpler way to pass the data in A to B?Perhaps the simplest option is to write the data structure to a file, using any of the several R ways to do that. When instance 2 sees that a file is available, it slurps its contents in and works on it. The hard part is making the second instance wait until the whole file is written out by the first. You wouldn't want it to read in half the file then hit the end because the first process hasn't finished writing out the file. I don't see any good mechanism in R to fix this. A more robust option is to use sockets. This is suitable even within a single machine. See ?make.socket. This solves the "how do I know when I've got the full data structure problem" because the second process can just keep reading until it gets an error indicating that the remote peer closed the connection. Once you have the data structure in string form, you can eval() it to get an R object suitable for munching on. Figuring out how to pass the data might be the hardest part. deparse() might be the easiest way. If you're hoping to scale this up to lots of processes, look into Rmpi. This provides a very clean way for an R program on one computer to start slaves on other computers and then pass data to them in native R structures. Setting up MPI itself is not trivial, however. It's best when you already have a cluster of computers linked with MPI.
Look at the nws package, it has tools for passing data among multiple instances of R (and waiting for data to be ready). There are other packages that provide some of the same, but from what I remember, nws was fairly simple to set up on a single computer. Hope this helps, -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Feng Li > Sent: Wednesday, February 04, 2009 7:08 AM > To: r-help at r-project.org > Subject: [R] Passing data among multiple instances > > Dear R, > > I have two R instances running at the same time, say instance A and > instance > B. Is there a simpler way to pass the data in A to B? > > More precise, I have a stupid example: > > In instance A, I am running a function "test1" > > test1 <- function (x1) > { > x2 <- sin(x1) > return(x2) > } > > In instance B, another function "test2" > > test2 <- function (x2) > { > x3 <- cos(x2) > return(x3) > } > > where " test2" receives the input from "test1"'s rueslt. "test1" and > "test2" could be much more complex. They may take one minute each. > > Now the whole procedure is instance A is running, while instance B is > waiting for the result of instance A. Once instance A is done (instance > A > goes to run with new data), instance B should detect A is done, and > instance > B receives the parameter from instance A. B begins to work. While B is > done, > waiting for A's new results. > > I want to repeat the loop many times and get x3 in the end. > > > Is it possible to do this job? Thanks ! > > > Feng > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting- > guide.html > and provide commented, minimal, self-contained, reproducible code.