[Rd] Multiple cores are used in simple for loop

2016-01-15 Thread Daniel Kaschek

Dear all,

I run different R versions (3.2.1, 3.2.2 and 3.2.3) on different 
platforms (Arch, Ubuntu, Debian) with a different number of available 
cores (24, 4, 24). The following line produces very different behavior 
on the three machines:


for(i in 1:1e6) {n <- 100; M <- matrix(rnorm(n^2), n, n); M %*% M}

On the Ubuntu and Arch machine one core is used, but on the Debian 
machine ALL cores are used with heavy "kernel time" vs. "normal time" 
(red vs. green in htop). It seems that the number of cores used on 
Debian is related to the size of the matrix. Reducing n from 100 to 4 
causes four cores to work.


A similar problem persists with the parallel package and mclapply():

library(parallel)
out <- mclapply(1:1e6, function(i) { n <- 100; M <- matrix(rnorm(n^2), 
n, n); M %*% M }, mc.cores = 24)


On Arch and Debian all 24 cores run and show a high kernel time vs. 
normal time (all CPU bars in htop are 80% red). With mc.cores = 4 on 
the Ubuntu system however, all four cores run at full load with almost 
no kernel time but full normal time (all bars are green).


Have you seen this problem before? Does anybody know how to fix it?

Cheers,
Daniel

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] Multiple cores are used in simple for loop

2016-01-15 Thread Daniel Kaschek

Dear Martyn,


On Fr, Jan 15, 2016 at 4:01 , Martyn Plummer  wrote:


Alternatively, you may be able to control the maximum number of 
threads

by setting and exporting an appropriate environment variable depending
on what backend you are using, e.g. OPENBLAS_NUM_THREADS or
MKL_NUM_THREADS.



Thanks a lot. Running

export OPENBLAS_NUM_THREADS = 1

in the bash before starting R solves both problems!


Cheers,
Daniel

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


[Rd] Assignment in environment

2016-02-07 Thread Daniel Kaschek

Dear all,

I have a function "fn" with its own environment, i.e.

env <- environment(fn)

and env is not .GlobalEnv. And another function

getValue <- function(x) environment(x)$mylist

which returns the list object "mylist" which is in "env". If I want to 
modify "mylist", I could write


'getValue<-' <- function(x, value) { environment(x)$mylist <- value}

which gives not the desired result, e.g.

getValue(fn)[[1]] <- 3

will set the first list entry of "mylist" to 3. But then "fn" will also 
be 3!


environment(fn)$mylist[[1]] <- 3

does set the list entry correctly while keeping "fn". What's the 
difference?


Cheers,
Daniel

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] Assignment in environment

2016-02-07 Thread Daniel Kaschek
On So, Feb 7, 2016 at 2:46 , Duncan Murdoch  
wrote:


An assignment function should return the modified value.  So it looks 
like you would need



'getValue<-' <- function(x, value) {
   environment(x)$mylist <- value
   x
 }


Thanks. I have not thought of a return value. That makes the difference.




but in fact, this doesn't work:

getValue(fn)[[1]] <- 3
Error in getValue(fn)[[1]] <- 3 : could not find function "getValue"



I have a "getValue" function anyway. It was even the other way round. I 
wanted to assign values without a function "getValue<-" and was rebuked 
by R that this function was missing.


Thanks to both of you, Peter and Duncan.

Best regards,
Daniel

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


[Rd] iterated lapply

2015-02-23 Thread Daniel Kaschek
Hi everybody,

with the following code I generate a list of functions. Each function
reflects a "condition". When I evaluate this list of functions by
another lapply/sapply, I get an unexpected result: all values coincide.
However, when I uncomment the print(), it works as expected. Is this a
bug or a feature?

conditions <- 1:4
test <- lapply(conditions, function(mycondition){
  #print(mycondition)
  myfn <- function(i) mycondition*i
  return(myfn)
})

sapply(test, function(myfn) myfn(2))



Cheers,
Daniel

-- 
Daniel Kaschek
Institute of Physics
Freiburg University

Room 210
Phone: +49 761 2038531

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] iterated lapply

2015-02-24 Thread Daniel Kaschek
On Mo, 2015-02-23 at 16:54 -0500, Duncan Murdoch wrote:
> This is a feature:  it allows you to have arguments that are never
> evaluated, because they are never used, or defaults that depend on
> things that are calculated within the function.

I haven't thought about the thing with the default arguments. That's
really a feature.

Thanks,
Daniel

> 
> Duncan Murdoch
> 
> 
> > conditions <- 1:4
> > test <- lapply(conditions, function(mycondition){
> >   #print(mycondition)
> >   myfn <- function(i) mycondition*i
> >   return(myfn)
> > })
> > 
> > sapply(test, function(myfn) myfn(2))
> > 
> > 
> > 
> > Cheers,
> > Daniel
> > 
> 

-- 
Daniel Kaschek
Institute of Physics
Freiburg University

Room 210
Phone: +49 761 2038531

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel