From 3723f14f43e1fe758fc9dd107c7a0f8c20ef16b7 Mon Sep 17 00:00:00 2001
From: de Lavenne Alban <alban.de-lavenne@irstea.fr>
Date: Fri, 24 Jan 2025 11:55:21 +0100
Subject: [PATCH] feat: allow to distinguish number of cores for sampling and
 Ghosh distance

---
 R/hdist.R    | 15 +++++++++------
 man/hdist.Rd |  4 +++-
 2 files changed, 12 insertions(+), 7 deletions(-)

diff --git a/R/hdist.R b/R/hdist.R
index 5ccd061..e45b033 100644
--- a/R/hdist.R
+++ b/R/hdist.R
@@ -19,7 +19,9 @@
 #' of length two to distinguish between parallelisation of sampling and Ghosh distance
 #' (because sampling over large areas can be memory intensive)
 #' @param cores the number of cores to use for parallel execution if \code{parallel} is TRUE.
-#' If not specified, the number of cores is set to the value of \code{parallel::detectCores()}
+#' If not specified, the number of cores is set to the value of \code{parallel::detectCores()}.
+#' Similarly to \code{parallel}, it could be a vector of length two to distinguish between
+#' parallelisation of sampling and Ghosh distance
 #' @param verbose boolean indicating if information messages should be written to the console
 #' @param ... further arguments passed to or from other methods
 #' @return A matrix of class units with the catchments of \code{x} organised in rows
@@ -60,8 +62,9 @@ hdist.sfc <- function(x, y, method="rghosh", gres=5, ditself=FALSE, maxsample=2.
     if(verbose) cat("Sampling the catchments at a resolution of about",gres,"pts/km2\n")
     if(length(parallel)==1) parallel[1:2] <- parallel
     if(any(parallel) & (missing(cores)|is.null(cores))) cores <- parallel::detectCores()
+    if(length(cores)==1) cores[1:2] <- cores
     if(parallel[1]){
-      cl <- parallel::makeCluster(cores)
+      cl <- parallel::makeCluster(cores[1])
       doParallel::registerDoParallel(cl=cl)
       on.exit(parallel::stopCluster(cl))
     }
@@ -98,12 +101,12 @@ hdist.sfc <- function(x, y, method="rghosh", gres=5, ditself=FALSE, maxsample=2.
       }
     }
     if(verbose) cat("Computing Ghosh distance between catchments\n")
-    gdist <- loop_gdist(xdisc=xdisc, ydisc=ydisc, proj=proj, intersect=FALSE, parallel=parallel[2], cores=cores)
+    gdist <- loop_gdist(xdisc=xdisc, ydisc=ydisc, proj=proj, intersect=FALSE, parallel=parallel[2], cores=cores[2])
     if(ditself){
       if(verbose) cat("Computing Ghosh distance within catchments\n")
       if(!identical(x,y)){
-        gdist <- cbind(gdist,sapply(xdisc,FUN=function(x){call_gdist(pts1=x, pts2=x, proj=proj, intersect=FALSE, rescale=FALSE, diag=TRUE, parallel=parallel[2], cores=cores)}))
-        gdist <- rbind(gdist,c(sapply(ydisc,FUN=function(x){call_gdist(pts1=x, pts2=x, proj=proj, intersect=FALSE, rescale=FALSE, diag=TRUE, parallel=parallel[2], cores=cores)}),NA))
+        gdist <- cbind(gdist,sapply(xdisc,FUN=function(x){call_gdist(pts1=x, pts2=x, proj=proj, intersect=FALSE, rescale=FALSE, diag=TRUE, parallel=parallel[2], cores=cores[2])}))
+        gdist <- rbind(gdist,c(sapply(ydisc,FUN=function(x){call_gdist(pts1=x, pts2=x, proj=proj, intersect=FALSE, rescale=FALSE, diag=TRUE, parallel=parallel[2], cores=cores[2])}),NA))
       }else{
         gdist <- cbind(gdist,diag(gdist))
         gdist <- rbind(gdist,c(diag(gdist),NA))
@@ -113,7 +116,7 @@ hdist.sfc <- function(x, y, method="rghosh", gres=5, ditself=FALSE, maxsample=2.
     if(method=="rghosh2"){
       gc() # Clean memory
       if(verbose) cat("Computing Ghosh distance within the shared areas\n")
-      idist <- loop_gdist(xdisc=xdisc, ydisc=ydisc, proj=proj, intersect=TRUE, parallel=parallel[2], cores=cores)
+      idist <- loop_gdist(xdisc=xdisc, ydisc=ydisc, proj=proj, intersect=TRUE, parallel=parallel[2], cores=cores[2])
       if(verbose) cat("Rescaling Ghosh distance\n")
       gdist <- gdist-idist
     }
diff --git a/man/hdist.Rd b/man/hdist.Rd
index 84e8d17..7b942ee 100644
--- a/man/hdist.Rd
+++ b/man/hdist.Rd
@@ -56,7 +56,9 @@ of length two to distinguish between parallelisation of sampling and Ghosh dista
 (because sampling over large areas can be memory intensive)}
 
 \item{cores}{the number of cores to use for parallel execution if \code{parallel} is TRUE.
-If not specified, the number of cores is set to the value of \code{parallel::detectCores()}}
+If not specified, the number of cores is set to the value of \code{parallel::detectCores()}.
+Similarly to \code{parallel}, it could be a vector of length two to distinguish between
+parallelisation of sampling and Ghosh distance}
 
 \item{verbose}{boolean indicating if information messages should be written to the console}
 
-- 
GitLab