Renaud Gaujoux
2011-Jun-28 09:58 UTC
[Rd] doMC - compiler - concatenate an expression vector into a single expression?
Hi, this post is about foreach operators, the compiler package and the last update of doMC that includes support for the compiler functionality. I am using a home-made %dopar%-like operator that adds some custom expression to be executed before the foreach loop expression itself (see sample code below). It used to work perfectly with doMC 1.2.1, but with the introduction of the compiler functionality, things do not work properly. The change in the doMC package consists in evaluating a compiled expression instead of the original R expression: # from doMC:::doMC ... c.expr <- comp(expr, env = envir, options = list(suppressUndefined = TRUE)) and for R >= 2.13.0 comp is defined as compiler::compile: function (e, env = .GlobalEnv, options = NULL) { cenv <- makeCenv(env) cntxt <- make.toplevelContext(cenv, options) cntxt$env <- addCenvVars(cenv, findLocals(e, cntxt)) genCode(e, cntxt) } <environment: namespace:compiler> My guess is that the function findLocals or genCode can not handle a 2-length expression vector. Maybe somebody who knows the internals of these functions could explain better this behaviour? How can I concatenate two expressions into a single one? Thank you, Renaud ################################################## # Sample code ################################################## `%dopar2%` <- function(obj, ex){ # append custom code to the expression ex <- c(expression({ a <- i; message("Custom ", a);}), substitute(ex)) # call the standard %dopar% operator do.call(`%dopar%`, list(obj, ex), envir=parent.frame() ) } res <- foreach(i=1:3) %dopar2% { print(i); i*2; } res ######################### # Output with doSEQ or doMC 1.2.1 ######################### Custom 1 [1] 1 Custom 2 [1] 2 Custom 3 [1] 3 > res [[1]] [1] 2 [[2]] [1] 4 [[3]] [1] 6 ######################### # Output with doMC 1.2.2 ######################### [[1]] expression({ a <- i message("Custom ", a) }, { print(i) i * 2 }) [[2]] expression({ a <- i message("Custom ", a) }, { print(i) i * 2 }) [[3]] expression({ a <- i message("Custom ", a) }, { print(i) i * 2 }) -- Renaud Gaujoux Computational Biology - University of Cape Town South Africa ### UNIVERSITY OF CAPE TOWN This e-mail is subject to the UCT ICT policies and e-mai...{{dropped:5}}
Rich Calaway
2011-Jun-28 15:30 UTC
[Rd] doMC - compiler - concatenate an expression vector into a single expression?
Dear Renaud, Thank you for the report. I see that Steve Weston has responded to your cross-posting on r-sig-hpc; I hope this helps with your immediate problem. We will investigate whether changes to doMC are warranted. Regards, Rich Calaway On Tue, Jun 28, 2011 at 2:58 AM, Renaud Gaujoux <renaud@cbio.uct.ac.za>wrote:> Hi, > > this post is about foreach operators, the compiler package and the last > update of doMC that includes support for the compiler functionality. > > I am using a home-made %dopar%-like operator that adds some custom > expression to be executed before the foreach loop expression itself (see > sample code below). > It used to work perfectly with doMC 1.2.1, but with the introduction of the > compiler functionality, things do not work properly. > The change in the doMC package consists in evaluating a compiled expression > instead of the original R expression: > > # from doMC:::doMC ... > c.expr <- comp(expr, env = envir, options = list(suppressUndefined = TRUE)) > > and for R >= 2.13.0 comp is defined as compiler::compile: > function (e, env = .GlobalEnv, options = NULL) > { > cenv <- makeCenv(env) > cntxt <- make.toplevelContext(cenv, options) > cntxt$env <- addCenvVars(cenv, findLocals(e, cntxt)) > genCode(e, cntxt) > } > <environment: namespace:compiler> > > My guess is that the function findLocals or genCode can not handle a > 2-length expression vector. > > Maybe somebody who knows the internals of these functions could explain > better this behaviour? > How can I concatenate two expressions into a single one? > > Thank you, > Renaud > > > ##############################**#################### > # Sample code > ##############################**#################### > > `%dopar2%` <- function(obj, ex){ > > # append custom code to the expression > ex <- c(expression({ a <- i; message("Custom ", a);}), substitute(ex)) > > # call the standard %dopar% operator > do.call(`%dopar%`, list(obj, ex), envir=parent.frame() ) > } > res <- foreach(i=1:3) %dopar2% { print(i); i*2; } > res > > > ######################### > # Output with doSEQ or doMC 1.2.1 > ######################### > Custom 1 > [1] 1 > Custom 2 > [1] 2 > Custom 3 > [1] 3 > > res > [[1]] > [1] 2 > > [[2]] > [1] 4 > > [[3]] > [1] 6 > > > ######################### > # Output with doMC 1.2.2 > ######################### > > [[1]] > expression({ > a <- i > message("Custom ", a) > }, { > print(i) > i * 2 > }) > > [[2]] > expression({ > a <- i > message("Custom ", a) > }, { > print(i) > i * 2 > }) > > [[3]] > expression({ > a <- i > message("Custom ", a) > }, { > print(i) > i * 2 > }) > > > > -- > Renaud Gaujoux > Computational Biology - University of Cape Town > South Africa > > > > > ### > UNIVERSITY OF CAPE TOWN > This e-mail is subject to the UCT ICT policies and e-mail disclaimer > published on our website at http://www.uct.ac.za/about/** > policies/emaildisclaimer/<http://www.uct.ac.za/about/policies/emaildisclaimer/>or obtainable from +27 21 650 9111. This e-mail is intended only for the > person(s) to whom it is addressed. If the e-mail has reached you in error, > please notify the author. If you are not the intended recipient of the > e-mail you may not use, disclose, copy, redirect or print the content. If > this e-mail is not related to the business of UCT it is sent by the sender > in the sender's individual capacity. > > ### > > >-- Rich Calaway Documentation Manager Revolution Analytics, Inc. 1505 Westlake Ave North Suite 300 Seattle, WA 98109 richcalaway@revolutionanalytics.com 206-456-6086 (Direct Phone) 855-GET-REVO x6086 (Toll-free) [[alternative HTML version deleted]]