[Vegan-commits] r2066 - in pkg/vegan: R inst man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon Feb 6 17:40:47 CET 2012


Author: jarioksa
Date: 2012-02-06 17:40:47 +0100 (Mon, 06 Feb 2012)
New Revision: 2066

Modified:
   pkg/vegan/R/bioenv.default.R
   pkg/vegan/inst/ChangeLog
   pkg/vegan/man/bioenv.Rd
Log:
parallel processing in bioenv

Modified: pkg/vegan/R/bioenv.default.R
===================================================================
--- pkg/vegan/R/bioenv.default.R	2012-02-05 16:27:16 UTC (rev 2065)
+++ pkg/vegan/R/bioenv.default.R	2012-02-06 16:40:47 UTC (rev 2066)
@@ -1,6 +1,7 @@
 `bioenv.default` <-
 function (comm, env, method = "spearman", index = "bray", upto = ncol(env), 
-              trace = FALSE, partial = NULL, ...) 
+              trace = FALSE, partial = NULL, parallel = getOption("mc.cores"),
+          ...) 
 {
     if (is.null(partial)) {
         corfun <- function(dx, dy, dz, method) {
@@ -34,6 +35,24 @@
     x <- scale(env)
     best <- list()
     comdis <- vegdist(comm, method = index)
+    ## Prepare for parallel processing
+    if (is.null(parallel) && getRversion() >= "2.15.0")
+        parallel <- get("default", envir = parallel:::.reg)
+    if (is.null(parallel) || getRversion() < "2.14.0")
+        parallel <- 1
+    hasClus <- inherits(parallel, "cluster")
+    isParal <- (hasClus || parallel > 1) && require(parallel)
+    isMulticore <- .Platform$OS.type == "unix" && !hasClus
+    if (isParal && !isMulticore && !hasClus) {
+        parallel <- makeCluster(parallel)
+    }
+    ## get the number of clusters
+    if (inherits(parallel, "cluster"))
+        nclus <- length(parallel)
+    else
+        nclus <- parallel
+    CLUSLIM <- 8
+    ## The proper loop
     for (i in 1:upto) {
         if (trace) {
             nvar <- choose(n, i)
@@ -44,9 +63,22 @@
         sets <- t(combn(1:n, i))
         if (!is.matrix(sets)) 
             sets <- as.matrix(t(sets))
-        est <- numeric(nrow(sets))
-        for (j in 1:nrow(sets)) est[j] <- corfun(comdis, dist(x[, 
-                                                                sets[j, ]]), partial, method = method)
+        if (isParal && nrow(sets) >= CLUSLIM*nclus) {
+            if (isMulticore) {
+                est <- unlist(mclapply(1:nrow(sets), function(j)
+                                       corfun(comdis, dist(x[,sets[j, ]]), partial,
+                                              method = method),
+                                       mc.cores = parallel))
+            } else {
+                est <- parSapply(parallel, 1:nrow(sets), function(j)
+                                  corfun(comdis, dist(x[,sets[j, ]]), partial,
+                                         method = method))
+            }
+        } else {
+            est <- sapply(1:nrow(sets), function(j) 
+                          corfun(comdis, dist(x[,sets[j, ]]), partial,
+                                 method = method))
+        }
         best[[i]] <- list(best = sets[which.max(est), ], est = max(est))
         if (trace) {
             ndone <- ndone + nvar

Modified: pkg/vegan/inst/ChangeLog
===================================================================
--- pkg/vegan/inst/ChangeLog	2012-02-05 16:27:16 UTC (rev 2065)
+++ pkg/vegan/inst/ChangeLog	2012-02-06 16:40:47 UTC (rev 2066)
@@ -8,6 +8,8 @@
 	not need clusterEvalQ(parallel, library(vegan)) for socket
 	clusters. 
 
+	* bioenv: implemented parallel processing.
+
 	* nesteddisc: new argument 'niter' to give the number of
 	iterations to reorder tied columns.
 	

Modified: pkg/vegan/man/bioenv.Rd
===================================================================
--- pkg/vegan/man/bioenv.Rd	2012-02-05 16:27:16 UTC (rev 2065)
+++ pkg/vegan/man/bioenv.Rd	2012-02-06 16:40:47 UTC (rev 2066)
@@ -13,7 +13,8 @@
 }
 \usage{
 \method{bioenv}{default}(comm, env, method = "spearman", index = "bray",
-       upto = ncol(env), trace = FALSE, partial = NULL, ...)
+       upto = ncol(env), trace = FALSE, partial = NULL, 
+       parallel = getOption("mc.cores"), ...)
 \method{bioenv}{formula}(formula, data, ...)
 }
 
@@ -28,6 +29,10 @@
   \item{trace}{Trace the advance of calculations }
   \item{partial}{Dissimilarities partialled out when inspecting
     variables in \code{env}.}
+  \item{parallel}{Number of parallel processes or a predefined socket
+    cluster.  With \code{parallel = 1} uses ordinary, non-parallel
+    processing. The parallel processing is done with \pkg{parallel}
+    package which is available only for \R 2.14.0 and later.}
   \item{...}{Other arguments passed to \code{\link{cor}}.}
 }
 \details{



More information about the Vegan-commits mailing list