[R-sig-hpc] doMC - compiler - concatenate an expression vector into a single expression?

Renaud Gaujoux renaud at mancala.cbio.uct.ac.za
Wed Jun 29 09:03:52 CEST 2011


Thank you very much Steve.
Your suggestion works perfectly -- at least with doSEQ, doMC and doMPI.

Bests,
Renaud


On 28/06/2011 15:35, Stephen Weston wrote:
> I think that the result of the concatenation should be a call object,
> rather than an expression object.  How about something along the
> lines of:
>
> '%dopar2%'<- function(obj, ex) {
>    ex<- as.call(list(as.name('{'),
>                            quote({ a<- i; message("Custom ", a) }),
>                            substitute(ex)))
>    do.call('%dopar%', list(obj, ex), envir=parent.frame())
> }
>
> - Steve
>
>
> On Tue, Jun 28, 2011 at 8:26 AM, Renaud Gaujoux
> <renaud at mancala.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
>> })
>>
>>
>>
>>
>>
>> ###
>> UNIVERSITY OF CAPE TOWN
>> This e-mail is subject to the UCT ICT policies and e-mai...{{dropped:5}}
>>
>> _______________________________________________
>> R-sig-hpc mailing list
>> R-sig-hpc at r-project.org
>> https://stat.ethz.ch/mailman/listinfo/r-sig-hpc
>>

 

###
UNIVERSITY OF CAPE TOWN 

This e-mail is subject to the UCT ICT policies and e-mai...{{dropped:5}}



More information about the R-sig-hpc mailing list