代码之家  ›  专栏  ›  技术社区  ›  D1X

r中的colmeans()、colsums()..是否也被并行化?

  •  1
  • D1X  · 技术社区  · 6 年前

    我们知道使用 colMeans() , colSums() 比他们同等的使用速度快得多 apply() ,因为这是预编译的(in C , C++ ……?)

    这些函数在这些语言中是否也自动并行化?

    也许它并没有真正意义上的并行化开销,因为它只是一个非常简单的函数,对吗?但是,对于非常大的矩阵来说,这是有意义的吗?

    2 回复  |  直到 6 年前
        1
  •  6
  •   user2974951    6 年前

    R中没有自动并行化的函数,但是这些精确的函数存在于 Rfast 带有并行参数的包。

        2
  •  3
  •   Stefanos    6 年前

    即使没有并行化,rfast实现也更好。

    x = matrix(rnorm(1000*100),ncol=100)
    
    microbenchmark::microbenchmark(.colSums(x,1000,100), colSums(x),Rfast::colsums(x,parallel=0),times=1000)
    Unit: microseconds
                              expr     min      lq     mean  median      uq     max    neval
             .colSums(x, 1000, 100) 124.870 125.725 127.28580 125.726 126.153 301.911  1000
                         colSums(x) 132.567 133.423 136.30507 134.705 135.134 282.668  1000
    Rfast::colsums(x, parallel = 0)  79.541  80.824  84.00742  81.252  82.107 307.470  1000
    

    更大的矩阵使得并行化相关(2核笔记本电脑的基准)

    x = matrix(rnorm(1000*1000),ncol=1000)
    
    microbenchmark::microbenchmark(.colSums(x,1000,1000), colSums(x),Rfast::colsums(x,parallel=0),
        Rfast::colsums(x,parallel=1),times=1000)
    Unit: microseconds
                               expr      min       lq      mean    median        uq      max  neval
            .colSums(x, 1000, 1000) 1313.268 1336.3600 1366.5152 1344.486 1355.1760 2468.310  1000
                         colSums(x) 1350.473 1366.2950 1401.0574 1374.420 1386.8210 2826.241  1000
    Rfast::colsums(x, parallel = 0)  755.205  769.7440  801.5617  779.152  796.6850 1796.068  1000
    Rfast::colsums(x, parallel = 1)  604.249  637.8185  680.3327  651.289  674.1675 1808.042  1000
    
    x = matrix(rnorm(100000*100),ncol=100)
    
    microbenchmark::microbenchmark(.colSums(x,100000,100), colSums(x),Rfast::colsums(x,parallel=0),
        Rfast::colsums(x,parallel=1),times=1000)
    
     Unit: milliseconds
                                expr       min        lq      mean    median        uq     max    neval
             .colSums(x, 1e+05, 100) 12.910733 12.971671 13.124897 13.011441 13.087346 26.914502  1000
                          colSums(x) 12.944944 13.012511 13.142074 13.058268 13.128400 14.879138  1000
     Rfast::colsums(x, parallel = 0)  6.988414  7.009796  7.165310  7.068810  7.154765 16.826589  1000
     Rfast::colsums(x, parallel = 1)  4.061679  4.251977  4.373995  4.296451  4.385827  7.610196  1000