Skip to contents

Computes crossprod(X, Y) (i.e. t(X) %*% Y) using:

  • shared/mmap-backed inputs (one copy),

  • block views (no slice materialization),

  • BLAS-3 dgemm in each tile,

  • an explicit shared output buffer (no gather/bind spikes).

Usage

shard_crossprod(
  X,
  Y,
  workers = NULL,
  block_x = "auto",
  block_y = "auto",
  backing = c("mmap", "shm"),
  materialize = c("auto", "never", "always"),
  materialize_max_bytes = 512 * 1024^2,
  diagnostics = TRUE
)

Arguments

X, Y

Double matrices with the same number of rows.

workers

Number of worker processes.

block_x, block_y

Tile sizes over ncol(X) and ncol(Y). Use "auto" (default) to autotune on the current machine.

backing

Backing for shared inputs and output buffer ("mmap" or "shm").

materialize

Whether to return the result as a standard R matrix: "never" (return buffer handle), "always", or "auto" (materialize if estimated output size is below materialize_max_bytes).

materialize_max_bytes

Threshold for "auto" materialization.

diagnostics

Whether to collect shard_map diagnostics.

Value

A list with:

  • buffer: shard_buffer for the result (p x v)

  • value: materialized matrix if requested, otherwise NULL

  • run: the underlying shard_result from shard_map

  • tile: chosen tile sizes

Details

This is intended as an ergonomic entry point for the "wow" path: users shouldn't have to manually call share(), view_block(), buffer(), tiles2d(), and shard_map() for common patterns.

Examples

# \donttest{
X <- matrix(rnorm(2000), 100, 20)
Y <- matrix(rnorm(2000), 100, 20)
res <- shard_crossprod(X, Y, block_x = 50, block_y = 10, workers = 2)
pool_stop()
res$value
#>              [,1]        [,2]        [,3]       [,4]        [,5]        [,6]
#>  [1,]  -0.9034062   9.0595521   3.2061180   7.892693  -2.0399819  -1.8565279
#>  [2,]  -0.1692529   0.7483117   3.3641226  -1.404396  -5.5927017   3.2522594
#>  [3,]  -1.9542926  13.1962955   0.2495751 -12.797336 -10.6638176   5.5078955
#>  [4,] -10.6343021  -6.1564949  -6.3659045   7.687602   0.8873051   3.2095718
#>  [5,]   2.4665295 -17.9035985   1.2505585  20.022471   6.6067826   5.8962716
#>  [6,]   3.4227007  14.2290701 -16.9890052  -4.359612  13.5851614  -7.5604179
#>  [7,]  15.3301585   7.4472282 -29.0296463   5.609980  -9.0889372  10.1997395
#>  [8,] -17.7426207   1.2876177   8.2276106  18.816146  -3.8337844  11.3761128
#>  [9,]   3.0034072  10.3670620   4.9967125   1.792312  -8.7561458 -11.4804140
#> [10,]   2.8165637  11.1055522 -18.3213165 -12.912663   0.8406340  18.0932823
#> [11,]   6.7794041   4.7894811   6.7243904  -9.989881  -7.5103336  17.3275721
#> [12,]   2.3720148   1.0492721  -6.9874287  -8.940615   3.6254617   0.3930853
#> [13,]  -3.7966004  -4.3323992  -4.2984672 -15.860710 -16.3820522   6.5269879
#> [14,]   5.4242821  10.9610604 -11.8816619  13.599877   2.1892763  -4.1794989
#> [15,]  -1.9655459   8.0138656  15.0839700  10.085077  15.9048719  -9.9785830
#> [16,]   5.0374369   8.7144662  -7.3345488  -5.537239  -6.8960640  -9.1237778
#> [17,]  -7.0093582   8.0138549  12.1764220  10.452902  16.9599096  -5.9443652
#> [18,]  -2.0942075  -9.3858535  -8.2522701   1.994911  -4.0433446   3.9631785
#> [19,]   3.8242720  -1.3068255  -1.7688456   9.827692  -5.5825583  -9.4703844
#> [20,]  -8.4502576  10.6567464   2.5396343   2.968504  -8.6392519   5.2041934
#>              [,7]        [,8]       [,9]       [,10]       [,11]       [,12]
#>  [1,] -10.0845519 -11.1574021   1.279059  -5.4719553 -16.5334562  -2.3384299
#>  [2,] -12.5007307   5.9356820   1.720037   0.8391791   7.6015379  -3.0238181
#>  [3,]   4.5787490 -14.2147115   7.507892   8.3111769  -8.5521130   6.4239798
#>  [4,]   3.5804272  -2.9715419  -5.646767   1.5085585   8.8432253   1.2654690
#>  [5,]  -7.9743709 -13.2947042 -10.524207   8.9296995   0.4503373  -0.6317890
#>  [6,]   6.4525967 -14.9213493   4.581148  -3.0392082  -4.9712511  -7.6454512
#>  [7,]  -7.8960177  -1.3160289  11.290575  -7.9838696  -5.2641289   8.7044087
#>  [8,]   1.6916693  -8.6408430  17.673610 -14.3440668   9.2485967   1.9925431
#>  [9,]   6.3859171 -16.7718081  19.248318  -8.1226514  -9.3586378 -11.0591156
#> [10,]   2.6935568 -12.9107567   3.871027  -8.7699655  -7.0643866  12.7422312
#> [11,]   7.8641409  -4.9610543  -3.742419  -8.8593043  12.5606919   4.9537540
#> [12,]   0.7959160   7.5934378  13.354802  10.0206877 -10.1068866 -11.6382643
#> [13,]   5.3785211  -1.3596003 -11.212598   2.9313307   1.6465805  -1.0505258
#> [14,]  -8.2962272  -6.5358503  -8.619281  15.6362716  -6.0030731   0.3691946
#> [15,]  -5.5686878 -20.9903521  -2.030222  12.0639312  -7.8348549 -18.3242335
#> [16,]   0.8443718  -5.6648159  -7.910572  -5.4371544  -4.3809901   7.3927191
#> [17,] -12.3157075   5.6966169   1.528931   9.9370864  15.0457622  16.3949983
#> [18,]   4.4969813   1.9913537  -3.825316  -6.8509096   7.5122776  -2.8612668
#> [19,]   5.8223568  -0.5615947  -5.883772 -13.3410615   7.0404206 -11.2034355
#> [20,]  -2.0307884 -14.4104853   4.302333  10.5153497  19.5622276 -10.1985224
#>            [,13]      [,14]       [,15]       [,16]       [,17]       [,18]
#>  [1,]   7.194845  14.853194  -8.7615650   0.2444127  18.0698978  -7.3408907
#>  [2,]   1.960246 -16.606164  20.7652690  16.6023495  -8.9742150   1.2710380
#>  [3,]  -9.359907 -10.827994  -4.4009123 -13.0188581   5.2169656 -11.0685776
#>  [4,]   9.242487  -5.499173  -8.8403342   7.9756471  -5.1731125  -7.0154198
#>  [5,]  19.383234  11.094341   1.2439926 -13.2486352  -6.5032010   6.5864700
#>  [6,]  -6.367541  21.588808 -14.4441796  -2.7107362   9.6027957 -11.6552581
#>  [7,] -11.208083  10.061343 -13.6690526  16.8992162   1.5041088  -0.4798614
#>  [8,]  27.795625  -7.713775   0.8295278  -2.7342437   5.7867487  -4.7434642
#>  [9,]  13.240139  15.593366   8.1547766  -3.8573258  18.0307406   6.9276809
#> [10,] -15.722377  10.330952 -11.5174323  17.6105988   0.4488351   0.7041939
#> [11,]   1.499660  -7.526971   8.9621094   5.4995445  -7.2539238  20.5718979
#> [12,]  12.708233  16.123238   3.0495472   8.6009327   4.2964068  -5.5340358
#> [13,]   1.162690 -10.733333   5.8105109   1.9688542  13.6221148  15.0919436
#> [14,]  -7.154604  17.759654  -8.3033459  -5.4327965 -14.1488976 -19.5579268
#> [15,]   1.518130 -18.917066  -2.7625284  10.3104989  -0.4363382   2.2617260
#> [16,] -10.397910   9.214768 -14.8157351  -7.4985217   0.6220416  -8.8842830
#> [17,]  -3.860773 -18.046212   4.3009587  -9.2126820 -17.0648798  -7.3512072
#> [18,]  11.422078 -13.110124  -6.6210499  -2.4422708   1.1723564  10.1630779
#> [19,]   2.217738  10.434194  -2.5185372  -9.5664711   4.5749122  10.7361249
#> [20,]   2.399744   7.663376   8.6741408  -6.8203593  -6.1924662   9.1009746
#>             [,19]      [,20]
#>  [1,]  -8.0050856  15.112744
#>  [2,]  12.0589270   1.355568
#>  [3,]   2.0652418  -5.873131
#>  [4,]  -2.7994516  -5.608761
#>  [5,]   5.1581855  -9.573963
#>  [6,]  -2.1526052   8.722917
#>  [7,]   3.0832788   4.732188
#>  [8,] -22.8845498  -6.587707
#>  [9,]  -7.7844339  16.281203
#> [10,]  15.9561542  -2.332857
#> [11,]  -3.2546073 -12.868591
#> [12,]   4.4656372  -7.750099
#> [13,]   5.6018456  13.611967
#> [14,]   8.8789787 -10.560227
#> [15,] -24.2253255  -3.755416
#> [16,]   6.2129431   2.242726
#> [17,]   2.6782554  -4.876901
#> [18,]   6.5880417 -14.142763
#> [19,]   0.6190989  -8.671129
#> [20,]   1.7835981  -6.522080
# }