[Rd] doMC - compiler - concatenate an expression vector into a single expression?

From: Renaud Gaujoux <renaud_at_mancala.cbio.uct.ac.za>
Date: Tue, 28 Jun 2011 11:58:05 +0200


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}}

______________________________________________
R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Received on Tue 28 Jun 2011 - 10:00:28 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 29 Jun 2011 - 09:40:25 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive