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

From: Renaud Gaujoux <renaud_at_mancala.cbio.uct.ac.za>
Date: Wed, 29 Jun 2011 09:03:52 +0200

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



R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Wed 29 Jun 2011 - 07:06:42 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 - 11:50:24 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