Notas para contenedor de docker:

Comando de docker para ejecución de la nota de forma local:

nota: cambiar <ruta a mi directorio> por la ruta de directorio que se desea mapear a /datos dentro del contenedor de docker.

docker run --gpus all --rm -v <ruta a mi directorio>:/datos --name jupyterlab_nvidia_r_gputools_container -p 8888:8888 -d palmoreck/jupyterlab_nvidia_r_gputools:1.1.0_10.2

password para jupyterlab: qwerty

Detener el contenedor de docker:

docker stop jupyterlab_nvidia_r_gputools_container

Documentación de la imagen de docker palmoreck/jupyterlab_nvidia_r_gputools:1.1.0_10.2 en liga.


Nota: si se desean ejecutar los ejemplos que se presentan a continuación de forma local, es necesario tener una tarjeta gráfica NVIDIA.

Para ejecutar el notebook si están usando google colab:

  1. Elegir en el menú las opciones Entorno de ejecución > Cambiar tipo de entorno de ejecución > Acelerador por Hardware > GPU Runtime > Change > Hardware Accelerator > GPU .

  2. Instalar gputools con los siguientes comandos:

system('bash -c "wget https://cran.r-project.org/src/contrib/Archive/gputools/gputools_1.1.tar.gz"')
system('bash -c "R CMD INSTALL --configure-args="--with-r-include=/usr/share/R/include" gputools_1.1.tar.gz"')

creando celdas del notebook.

Ver googlecolab github para información sobre google colab.

gputools

Extraído de: gputools: A Few GPU Enabled Functions: Provides R interfaces to a handful of common functions implemented using the Nvidia CUDA toolkit. Some of the functions require at least GPU Compute Capability 1.3 ...

Github: nullsatz

Siguientes ejemplos extraídos de la documentación de gputools: man pages.


In [1]:
library(gputools)

In [2]:
chooseGpu(deviceId = 0)


  1. 0

In [3]:
getGpuId()


0

gpuCor

Calculate Various Correlation Coefficients With a GPU


In [4]:
example(gpuCor)


gpuCor> numAvars <- 5

gpuCor> numBvars <- 10

gpuCor> numSamples <- 30

gpuCor> A <- matrix(runif(numAvars*numSamples), numSamples, numAvars)

gpuCor> B <- matrix(runif(numBvars*numSamples), numSamples, numBvars)

gpuCor> gpuCor(A, B, method="pearson")
$coefficients
            [,1]        [,2]       [,3]        [,4]       [,5]        [,6]
[1,] -0.08729467 -0.06983783 -0.2829165  0.20262803 -0.3644699  0.18422258
[2,]  0.23199186 -0.27605408  0.1937008  0.22633040  0.1879168 -0.16176592
[3,]  0.01097281  0.18048467 -0.2475615 -0.09826196 -0.1434683  0.15520385
[4,]  0.04602896 -0.04294780  0.1734440  0.22959469 -0.1168651 -0.22965799
[5,] -0.32558337  0.15548959 -0.3702804 -0.12191509 -0.2158017  0.02700755
            [,7]        [,8]        [,9]        [,10]
[1,] -0.30262706  0.02763666 -0.08469377  0.012446512
[2,]  0.09078917 -0.10243496 -0.07214707  0.085500613
[3,] -0.07792642  0.24276388  0.22425395  0.001971742
[4,] -0.27895185  0.17332079 -0.31251362  0.014706546
[5,] -0.23352639  0.19226526 -0.17414017 -0.072604492

$ts
            [,1]       [,2]       [,3]       [,4]       [,5]       [,6]
[1,] -0.46369007 -0.3704516 -1.5608217  1.0949200 -2.0710504  0.9917892
[2,]  1.26201630 -1.5197970  1.0447551  1.2295336  1.0123980 -0.8674093
[3,]  0.05806616  0.9709808 -1.3520589 -0.5224819 -0.7670988  0.8313353
[4,]  0.24382077 -0.2274683  0.9319038  1.2482462 -0.6226583 -1.2486094
[5,] -1.82210577  0.8329037 -2.1092660 -0.6499624 -1.1694710  0.1429626
           [,7]       [,8]       [,9]       [,10]
[1,] -1.6801349  0.1462953 -0.4497734  0.06586585
[2,]  0.4824034 -0.5449012 -0.3827639  0.45408955
[3,] -0.4136056  1.3241986  1.2176530  0.01043350
[4,] -1.5370891  0.9312210 -1.7408607  0.07782815
[5,] -1.2708437  1.0367141 -0.9357608 -0.38520348

$pairs
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]   30   30   30   30   30   30   30   30   30    30
[2,]   30   30   30   30   30   30   30   30   30    30
[3,]   30   30   30   30   30   30   30   30   30    30
[4,]   30   30   30   30   30   30   30   30   30    30
[5,]   30   30   30   30   30   30   30   30   30    30


gpuCor> gpuCor(A, B, method="kendall")
$coefficients
            [,1]         [,2]       [,3]        [,4]        [,5]        [,6]
[1,] -0.03908046 -0.066666667 -0.2091954  0.17241379 -0.24597701  0.14942529
[2,]  0.12643678 -0.195402299  0.1310345  0.16321839  0.12183908 -0.08045977
[3,] -0.01609195  0.112643678 -0.1494253 -0.05287356 -0.06666667  0.08045977
[4,] -0.01149425 -0.002298851  0.1310345  0.14482759 -0.11724138 -0.13563218
[5,] -0.23678161  0.112643678 -0.2505747 -0.06206897 -0.15862069  0.05287356
            [,7]        [,8]        [,9]       [,10]
[1,] -0.22298851 -0.02528736 -0.02068966  0.03448276
[2,]  0.05287356 -0.04367816 -0.02988506  0.03448276
[3,] -0.04367816  0.16321839  0.17701149  0.02068966
[4,] -0.11264368  0.12183908 -0.25057471 -0.01149425
[5,] -0.12643678  0.16321839 -0.14482759 -0.03448276

$pairs
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]   30   30   30   30   30   30   30   30   30    30
[2,]   30   30   30   30   30   30   30   30   30    30
[3,]   30   30   30   30   30   30   30   30   30    30
[4,]   30   30   30   30   30   30   30   30   30    30
[5,]   30   30   30   30   30   30   30   30   30    30


gpuCor> A[3,2] <- NA

gpuCor> gpuCor(A, B, use="pairwise.complete.obs", method="pearson")
$coefficients
            [,1]        [,2]       [,3]        [,4]       [,5]        [,6]
[1,] -0.08729467 -0.06983783 -0.2829165  0.20262803 -0.3644699  0.18422258
[2,]  0.22442316 -0.23086287  0.2374171  0.18462920  0.2060235 -0.22708108
[3,]  0.01097281  0.18048467 -0.2475615 -0.09826196 -0.1434683  0.15520385
[4,]  0.04602896 -0.04294780  0.1734440  0.22959469 -0.1168651 -0.22965799
[5,] -0.32558337  0.15548959 -0.3702804 -0.12191509 -0.2158017  0.02700755
            [,7]        [,8]        [,9]        [,10]
[1,] -0.30262706  0.02763666 -0.08469377  0.012446512
[2,]  0.14077656 -0.12136377 -0.12475598  0.124425463
[3,] -0.07792642  0.24276388  0.22425395  0.001971742
[4,] -0.27895185  0.17332079 -0.31251362  0.014706546
[5,] -0.23352639  0.19226526 -0.17414017 -0.072604492

$ts
            [,1]       [,2]       [,3]       [,4]       [,5]       [,6]
[1,] -0.46369007 -0.3704516 -1.5608217  1.0949200 -2.0710504  0.9917892
[2,]  1.19666171 -1.2329040  1.2699664  0.9761431  1.0939989 -1.2115999
[3,]  0.05806616  0.9709808 -1.3520589 -0.5224819 -0.7670988  0.8313353
[4,]  0.24382077 -0.2274683  0.9319038  1.2482462 -0.6226583 -1.2486094
[5,] -1.82210577  0.8329037 -2.1092660 -0.6499624 -1.1694710  0.1429626
           [,7]       [,8]       [,9]       [,10]
[1,] -1.6801349  0.1462953 -0.4497734  0.06586585
[2,]  0.7388544 -0.6353208 -0.6533554  0.65159726
[3,] -0.4136056  1.3241986  1.2176530  0.01043350
[4,] -1.5370891  0.9312210 -1.7408607  0.07782815
[5,] -1.2708437  1.0367141 -0.9357608 -0.38520348

$pairs
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]   30   30   30   30   30   30   30   30   30    30
[2,]   29   29   29   29   29   29   29   29   29    29
[3,]   30   30   30   30   30   30   30   30   30    30
[4,]   30   30   30   30   30   30   30   30   30    30
[5,]   30   30   30   30   30   30   30   30   30    30

gpuLm

Fitting Linear Models using a GPU-enabled QR


In [5]:
example(gpuLm)


gpuLm> # require(graphics)
gpuLm> 
gpuLm> ## Annette Dobson (1990) "An Introduction to Generalized Linear Models".
gpuLm> ## Page 9: Plant Weight Data.
gpuLm> ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14)

gpuLm> trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69)

gpuLm> group <- gl(2,10,20, labels=c("Ctl","Trt"))

gpuLm> weight <- c(ctl, trt)

gpuLm> anova(lm.D9 <- gpuLm(weight ~ group))
Analysis of Variance Table

Response: weight
          Df Sum Sq Mean Sq F value Pr(>F)
group      1 0.6882 0.68820  1.4191  0.249
Residuals 18 8.7293 0.48496               

gpuLm> summary(lm.D90 <- gpuLm(weight ~ group - 1))# omitting intercept

Call:
gpuLm(formula = weight ~ group - 1)

Residuals:
    Min      1Q  Median      3Q     Max 
-1.0710 -0.4938  0.0685  0.2462  1.3690 

Coefficients:
         Estimate Std. Error t value Pr(>|t|)
groupCtl       NA         NA      NA       NA
groupTrt       NA         NA      NA       NA

Residual standard error: 0.6964 on 18 degrees of freedom
Multiple R-squared:  0.9818,	Adjusted R-squared:  0.9798 
F-statistic: 485.1 on 2 and 18 DF,  p-value: < 2.2e-16


gpuLm> summary(resid(lm.D9) - resid(lm.D90)) #- residuals almost identical
      Min.    1st Qu.     Median       Mean    3rd Qu.       Max. 
-7.749e-07  0.000e+00  3.725e-08  2.794e-09  5.960e-08  1.788e-07 

gpuLm> opar <- par(mfrow = c(2,2), oma = c(0, 0, 1.1, 0))

gpuLm> plot(lm.D9, las = 1) # Residuals, Fitted, ...

gpuLm> par(opar)

gpuLm> ## model frame :
gpuLm> stopifnot(identical(gpuLm(weight ~ group, method = "model.frame"),
gpuLm+ 	model.frame(lm.D9)))

gpuLm> ### less simple examples in "See Also" above
gpuLm> 
gpuLm> 
gpuLm> 

Multiplicación de matrices:


In [6]:
example(gpuMatMult)


gpMtMl> matA <- matrix(runif(2*3), 2, 3)

gpMtMl> matB <- matrix(runif(3*4), 3, 4)

gpMtMl> gpuMatMult(matA, matB)
         [,1]      [,2]      [,3]      [,4]
[1,] 1.333496 0.3165404 1.0699142 0.2132063
[2,] 1.177227 0.2777268 0.9280637 0.1858729

Descomposición QR

gpuQr: Estimate the QR decomposition for a matrix


In [7]:
example(gpuQr)


gpuQr> # get some random data of any shape at all
gpuQr> x <- matrix(runif(25), 5, 5)

gpuQr> qr <- gpuQr(x)

gpuQr> print(qr)
$qr
            [,1]       [,2]       [,3]       [,4]       [,5]
[1,] -1.59715557 -0.8259729 -1.4240980 -1.0919213 -0.8331137
[2,]  0.46500883 -1.2888062 -0.3464067 -0.3853562 -0.5215801
[3,]  0.57993132  0.1866294  0.4750699  0.6428863  0.1104327
[4,]  0.05605243  0.7421889 -0.2167272  0.6686384 -0.2027192
[5,]  0.23396426  0.6411179  0.9413742 -0.9878945 -0.3403886
attr(,"Csingle")
[1] TRUE

$pivot
[1] 3 4 1 2 5

$qraux
[1] 1.6241517 1.0573833 1.2585416 1.1551274 0.3403886

$rank
[1] 5

attr(,"class")
[1] "qr"

Comparación con CPU


In [8]:
?gpuDist


gpuDist {gputools}R Documentation

Compute Distances Between Vectors on a GPU

Description

This function computes the distance between each vector of the 'points' argument using the metric specified by 'method'.

Usage

	gpuDist(points, method = "euclidean", p = 2.0)

Arguments

points

a matrix of floating point numbers in which each row is a vector in $R^n$ space where $n$ is ncol(points).

method

a string representing the name of the metric to use to calculate the distance between the vectors of 'points'. Currently supported values are: "binary", "canberra", "euclidean", "manhattan", "maximum", and "minkowski".

p

a floating point parameter for the Minkowski metric.

Value

a class of type "dist" containing floating point numbers representing the distances between vectors from the 'points' argument.

See Also

dist

Examples

numVectors <- 5
dimension <- 10
Vectors <- matrix(runif(numVectors*dimension), numVectors, dimension)
gpuDist(Vectors, "euclidean")
gpuDist(Vectors, "maximum")
gpuDist(Vectors, "manhattan")
gpuDist(Vectors, "minkowski", 4)

[Package gputools version 1.1 ]

In [9]:
N <- 9
set.seed(2020)
m <- matrix(sample(3, size = N*N, replace = T), nrow = N)

In [10]:
m


A matrix: 9 × 9 of type int
322231122
223311233
112213322
123322122
232332112
221112231
113111121
122113231
222232313

In [11]:
sqrt(sum((m[1,]-m[2,])^2))


3.16227766016838

In [12]:
sqrt(sum((m[1,]-m[3,])^2))


4.12310562561766

In [13]:
print(dist(m))


         1        2        3        4        5        6        7        8
2 3.162278                                                               
3 4.123106 3.316625                                                      
4 2.828427 2.449490 3.000000                                             
5 2.236068 3.605551 4.000000 2.236068                                    
6 3.316625 3.605551 2.828427 3.605551 4.000000                           
7 3.464102 3.464102 3.316625 2.828427 4.123106 3.000000                  
8 4.000000 3.741657 2.236068 3.162278 4.123106 1.732051 2.828427         
9 2.828427 3.464102 3.000000 3.162278 2.645751 3.872983 4.242641 4.000000

In [14]:
system.time(dist(m))


   user  system elapsed 
      0       0       0 

In [15]:
system.time(gpuDist(m))


   user  system elapsed 
  0.000   0.000   0.001 

In [16]:
N <- 1e3
set.seed(2020)
m <- matrix(sample(100, size = N*N, replace = T), nrow = N)

In [17]:
system.time(dist(m))


   user  system elapsed 
  4.181   0.000   4.181 

In [18]:
system.time(gpuDist(m))


   user  system elapsed 
  0.070   0.009   0.078 

In [19]:
install.packages("microbenchmark",lib="/usr/local/lib/R/site-library/",
                repos="https://cran.itam.mx/",verbose=TRUE)


system (cmd0): /usr/lib/R/bin/R CMD INSTALL

foundpkgs: microbenchmark, /tmp/RtmpYhHjzK/downloaded_packages/microbenchmark_1.4-7.tar.gz

files: /tmp/RtmpYhHjzK/downloaded_packages/microbenchmark_1.4-7.tar.gz

1): succeeded '/usr/lib/R/bin/R CMD INSTALL -l '/usr/local/lib/R/site-library' /tmp/RtmpYhHjzK/downloaded_packages/microbenchmark_1.4-7.tar.gz'


In [20]:
library(microbenchmark)

In [21]:
mbk<-microbenchmark(
    dist(m),
    gpuDist(m),
    times=5
    )

In [22]:
print(mbk)


Unit: milliseconds
       expr        min         lq       mean     median        uq       max
    dist(m) 4167.70018 4175.95147 4183.22507 4187.20899 4191.0032 4194.2615
 gpuDist(m)   66.30062   74.83907   89.57377   76.32318  111.9686  118.4374
 neval
     5
     5