File: zzz.R

package info (click to toggle)
r-cran-multicore 0.1-3-2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 116 kB
  • ctags: 34
  • sloc: ansic: 446; makefile: 1
file content (26 lines) | stat: -rw-r--r-- 1,104 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# this envoronment holda any volatile variables we may want to keep inside the package
volatile <- new.env(TRUE, emptyenv())

# detect the number of [virtual] CPUs (cores)
detectCores <- function(all.tests = FALSE) {
  # feel free to add tests - those are the only ones I could test [SU]
  systems <- list(darwin  = "/usr/sbin/sysctl -n hw.ncpu 2>/dev/null",
                  linux   = "grep processor /proc/cpuinfo 2>/dev/null|wc -l",
		  irix    = c("hinv |grep Processors|sed 's: .*::'", "hinv|grep '^Processor '|wc -l"),
		  solaris = "/usr/sbin/psrinfo -v|grep 'Status of.*processor'|wc -l")
  for (i in seq(systems))
    if(all.tests || length(grep(paste("^", names(systems)[i], sep=''), R.version$os)))
      for (cmd in systems[i]) {
        a <- gsub("^ +","",system(cmd, TRUE)[1])
        if (length(grep("^[1-9]", a))) return(as.integer(a))
      }
  NA
}

.onLoad <- function(libname, pkgname) {
  cores <- detectCores()
  volatile$detectedCoresSuccess <- !is.na(cores)
  if (is.na(cores)) cores <- 8L # a fallback expecting higher-end desktop ...
  volatile$detectedCores <- cores
  TRUE
}