
R version 3.6.2 (2019-12-12) -- "Dark and Stormy Night"
Copyright (C) 2019 The R Foundation for Statistical Computing
Platform: x86_64-apple-darwin19.2.0 (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

Loading required package: survival
Loading required package: stats
[1] TRUE
> library(distcomp)
> 
> ##print(availableComputations())
> 
> svdDef <- data.frame(compType = names(availableComputations())[3],
+                      rank = 2L,
+                      he = FALSE,
+                      ncol = 5L,
+                      id = "SVD",
+                      stringsAsFactors = FALSE)
> 
> ##
> ## Start opencpu server in a separate process or terminal before proceeding
> ## This is because the opencpu server blocks the main thread when it runs
> ## library(opencpu)
> ## ocpu_server_start()
> ## Default port is localhost:5656
> ##
> 
> set.seed(12345)
> 
> ## Three sites
> nSites <- 3
> siteData <- lapply(seq.int(nSites), function(i) matrix(rnorm(100), nrow=20))
> sites <- lapply(seq.int(nSites),
+                 function(x) list(name = paste0("site", x),
+                                  url = "http://localhost:5656/ocpu"))
> 
> ## Upload definition
> ok <- Map(uploadNewComputation, sites,
+           lapply(seq.int(nSites), function(i) svdDef),
+           siteData)
> 
> stopifnot(all(as.logical(ok)))
> 
> ##
> master <- SVDMaster$new(defn = svdDef)
> 
> for (site in sites) {
+   master$addSite(name = site$name, url = site$url)
+ }
> 
> result <- master$run()
> 
> print(result)
$v
            [,1]        [,2]
[1,]  0.17947030  0.08275684
[2,]  0.78969198  0.34634459
[3,] -0.21294972  0.91875219
[4,] -0.54501407  0.16784298
[5,]  0.04229739 -0.03032954

$d
[1] 9.707451 8.200043

> 
> ## Compare with:
> 
> x <- do.call(rbind, siteData)
> 
> print(result$d)
[1] 9.707451 8.200043
> print(svd(x)$d)
[1] 9.707537 8.199827 7.982888 7.257286 6.235182
> 
> print(result$v)
            [,1]        [,2]
[1,]  0.17947030  0.08275684
[2,]  0.78969198  0.34634459
[3,] -0.21294972  0.91875219
[4,] -0.54501407  0.16784298
[5,]  0.04229739 -0.03032954
> print(svd(x)$v[, 1:2])
            [,1]        [,2]
[1,] -0.17946375  0.08268613
[2,] -0.78963831  0.34694371
[3,]  0.21305901  0.91839439
[4,]  0.54504905  0.16843629
[5,] -0.04232602 -0.03120945
> 
> ## All 5 singular values (takes much longer!)
> svdDef <- data.frame(compType = names(availableComputations())[3],
+                      rank = 5L,
+                      he = FALSE,
+                      ncol = 5L,
+                      id = "SVD",
+                      stringsAsFactors = FALSE)
> 
> ## Upload definition
> ok <- Map(uploadNewComputation, sites,
+           lapply(seq.int(nSites), function(i) svdDef),
+           siteData)
> 
> stopifnot(all(as.logical(ok)))
> 
> ##
> master <- SVDMaster$new(defn = svdDef)
> 
> for (site in sites) {
+   master$addSite(name = site$name, url = site$url)
+ }
> 
> result <- master$run()
> 
> print(result$d)
[1] 9.707451 8.200043 7.982650 7.257355 6.235351
> print(svd(x)$d)
[1] 9.707537 8.199827 7.982888 7.257286 6.235182
> 
> print(result$v)
            [,1]        [,2]       [,3]        [,4]         [,5]
[1,]  0.17947030  0.08275684  0.0165604  0.98008722 -0.008933396
[2,]  0.78969198  0.34634459 -0.3437723 -0.16504730  0.333181988
[3,] -0.21294972  0.91875219  0.2496210 -0.04479619 -0.214978886
[4,] -0.54501407  0.16784298 -0.5334277  0.10025749  0.616612820
[5,]  0.04229739 -0.03032954  0.7312254 -0.01140918  0.680060781
> print(svd(x)$v)
            [,1]        [,2]        [,3]        [,4]        [,5]
[1,] -0.17946375  0.08268613 -0.01644895 -0.98010572 -0.00883063
[2,] -0.78963831  0.34694371  0.34328503  0.16509457  0.33316749
[3,]  0.21305901  0.91839439 -0.25083926  0.04461477 -0.21505068
[4,]  0.54504905  0.16843629  0.53318714 -0.10009622  0.61663844
[5,] -0.04232602 -0.03120945 -0.73121540  0.01126215  0.68002329
> 
> 
> sessionInfo()
R version 3.6.2 (2019-12-12)
Platform: x86_64-apple-darwin19.2.0 (64-bit)
Running under: macOS Catalina 10.15.2

Matrix products: default
BLAS/LAPACK: /usr/local/Cellar/openblas/0.3.7/lib/libopenblasp-r0.3.7.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] graphics  grDevices datasets  stats     utils     methods   base     

other attached packages:
[1] distcomp_1.3   survival_3.1-8

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.3        magrittr_1.5      splines_3.6.2     tidyselect_0.2.5 
 [5] xtable_1.8-4      lattice_0.20-38   R6_2.4.1          rlang_0.4.2      
 [9] fastmap_1.0.1     stringr_1.4.0     httr_1.4.1        dplyr_0.8.3      
[13] tools_3.6.2       grid_3.6.2        htmltools_0.4.0   digest_0.6.23    
[17] assertthat_0.2.1  tibble_2.1.3      crayon_1.3.4      Matrix_1.2-18    
[21] gmp_0.5-13.5      shiny_1.4.0       purrr_0.3.3       later_1.0.0      
[25] promises_1.1.0    curl_4.3          glue_1.3.1        mime_0.8         
[29] stringi_1.4.3     compiler_3.6.2    pillar_1.4.3      homomorpheR_0.2-4
[33] jsonlite_1.6      sodium_1.1        httpuv_1.5.2      pkgconfig_2.0.3  
> 
> 
> proc.time()
    user   system  elapsed 
   8.845    0.394 3015.531 
