EVOLUTION-MANAGER
Edit File: cluster.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>R: Use a multi-core cluster</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="R.css" /> </head><body> <table width="100%" summary="page for cluster {raster}"><tr><td>cluster {raster}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Use a multi-core cluster</h2> <h3>Description</h3> <p><code>beginCluster</code> creates, and <code>endCluster</code> deletes a 'snow' cluster object. This object can be used for multi-core computing with those 'raster' functions that support it. </p> <p><code>beginCluster</code> determines the number of nodes (cores) that are available and uses all of them (unless the argument <code>n</code> is used). </p> <p>NOTE: beginCluster may fail when the package 'nws' is installed. You can fix that by removing the 'nws' package, or by setting the cluster type manually, e.g. <code>beginCluster(type="SOCK")</code> </p> <p>endCluster closes the cluster and removes the object. </p> <p>The use of the cluster is automatic in these functions: <code><a href="projectRaster.html">projectRaster</a></code>, <code><a href="resample.html">resample</a></code> and in <code><a href="extract.html">extract</a></code> when using polygons. </p> <p><code>clusterR</code> is a flexible interface for using cluster with other functions. This function only works with functions that have a Raster* object as first argument and that operate on a cell by cell basis (i.e., there is no effect of neigboring cells) and return an object with the same number of cells as the input raster object. The first argument of the function called must be a Raster* object. There can only be one Raster* object argument. For example, it works with <code><a href="calc.html">calc</a></code> and it also works with <code><a href="overlay.html">overlay</a></code> as long as you provide a single RasterStack or RasterBrick as the first argument. </p> <p>This function is particularly useful to speed up computations in functions like predict, interpolate, and perhaps calc. </p> <p>Among other functions, it does _not_ work with merge, crop, mosaic, (dis)aggregate, resample, projectRaster, focal, distance, buffer, direction. But note that projectRaster has a build-in capacity for clustering that is automatically used if beginCluster() has been called. </p> <h3>Usage</h3> <pre> beginCluster(n, type='SOCK', nice, exclude) endCluster() clusterR(x, fun, args=NULL, export=NULL, filename='', cl=NULL, m=2, ...) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>n</code></td> <td> <p>Integer. The number of nodes to be used (optional)</p> </td></tr> <tr valign="top"><td><code>type</code></td> <td> <p>Character. The cluster type to be used</p> </td></tr> <tr valign="top"><td><code>nice</code></td> <td> <p>Integer. To set the prioirty for the workers, between -20 and 20 (UNIX like platforms only)</p> </td></tr> <tr valign="top"><td><code>exclude</code></td> <td> <p>Character. Packages to exclude from loading on the nodes (because they may fail there) but are required/loaded on the master </p> </td></tr> <tr valign="top"><td><code>x</code></td> <td> <p>Raster* object</p> </td></tr> <tr valign="top"><td><code>fun</code></td> <td> <p>function that takes <code>x</code> as its first argument</p> </td></tr> <tr valign="top"><td><code>args</code></td> <td> <p>list with the arguments for the function (excluding <code>x</code>, which should always be the first argument</p> </td></tr> <tr valign="top"><td><code>export</code></td> <td> <p>character. Vector of variable names to export to the cluster nodes such that the are visible to fun (e.g. a parameter that is not passed as an argument)</p> </td></tr> <tr valign="top"><td><code>filename</code></td> <td> <p>character. Output filename (optional)</p> </td></tr> <tr valign="top"><td><code>cl</code></td> <td> <p>cluster object (do not use it if beginCluster() has been called</p> </td></tr> <tr valign="top"><td><code>m</code></td> <td> <p>tuning parameter to determine how many blocks should be used. The number is rounded and multiplied with the number of nodes.</p> </td></tr> <tr valign="top"><td><code>...</code></td> <td> <p>additional arguments as for <code><a href="writeRaster.html">writeRaster</a></code></p> </td></tr> </table> <h3>Value</h3> <p>beginCluster and endCluster: None. The side effect is to create or delete a cluster object. </p> <p>clusterR: as for the function called with argument <code>fun</code> </p> <h3>Note</h3> <p>If you want to write your own cluster-enabled functions see <code><a href="programming.html">getCluster</a>, <a href="programming.html">returnCluster</a></code>, and the vignette about writing functions. </p> <h3>Author(s)</h3> <p>Matteo Mattiuzzi and Robert J. Hijmans</p> <h3>Examples</h3> <pre> ## Not run: # set up the cluster object for parallel computing beginCluster() r <- raster() values(r) <- 1:ncell(r) x <- clusterR(r, sqrt, verbose=T) f1 <- function(x) calc(x, sqrt) y <- clusterR(r, f1) s <- stack(r, r*2, r*3) f2 <- function(d,e,f) (d + e) / (f * param) param <- 122 ov <- clusterR(s, overlay, args=list(fun=f2), export='param') pts <- matrix(c(0,0, 45,45), ncol=2, byrow=T) d <- clusterR(r, distanceFromPoints, args=list(xy=pts)) values(r) <- runif(ncell(r)) m <- c(0, 0.25, 1, 0.25, 0.5, 2, 0.5, 1, 3) m <- matrix(m, ncol=3, byrow=TRUE) rc1 <- clusterR(r, reclassify, args=list(rcl=m, right=FALSE), filename=rasterTmpFile(), datatype='INT2S', overwrite=TRUE) # equivalent to: rc2 <- reclassify(r, rcl=m, right=FALSE, filename=rasterTmpFile(), datatype='INT2S', overwrite=TRUE) # example with the calc function a <- 10 f3 <- function(x) sum(x)+a z1 <- clusterR(s, calc, args=list(fun=f3), export='a') # for some raster functions that use another function as an argument # you can write your own parallel function instead of using clusterR # get cluster object created with beginCluster cl <- getCluster() library(parallel) clusterExport(cl, "a") z2 <- calc(s, fun=function(x){ parApply(cl, x, 1, f3)} ) # set flag that cluster is available again returnCluster() # # done with cluster object endCluster() ## End(Not run) </pre> <hr /><div style="text-align: center;">[Package <em>raster</em> version 3.3-13 <a href="00Index.html">Index</a>]</div> </body></html>