using parallel's parLapply: unable to access variables within parallel code

后端 未结 3 1111
无人及你
无人及你 2020-12-02 13:14

I recently got a computer with several cores and am learning to use parallel computing. I\'m fairly proficient with lapply and was told parLapply

3条回答
  •  伪装坚强ぢ
    2020-12-02 13:55

    out1<-lapply(seq_len(ntv), function(i) {x <- pos(text.var[i]);if (i%%gc.rate==0) gc();return(x)})
    out2<-parLapply(cl, seq_len(ntv), function(i) {x <- pos(text.var[i]);if (i%%gc.rate==0) gc();return(x)})
    
    >     identical(out1,out2)
    # [1] TRUE
    require(rbenchmark)
    benchmark(lapply(seq_len(ntv), function(i) {x <- pos(text.var[i]);if (i%%gc.rate==0) gc();return(x)}),parLapply(cl, seq_len(ntv), function(i) {x <- pos(text.var[i]);if (i%%gc.rate==0) gc();return(x)}))
    
    
                                                                                           test
    #1        lapply(seq_len(ntv), function(i) {\n    x <- pos(text.var[i])\n    if (i%%gc.rate == 0) \n        gc()\n    return(x)\n})
    #2 parLapply(cl, seq_len(ntv), function(i) {\n    x <- pos(text.var[i])\n    if (i%%gc.rate == 0) \n        gc()\n    return(x)\n})
    #  replications elapsed relative user.self sys.self user.child sys.child
    #1          100   20.03 3.453448     20.31     0.05         NA        NA
    #2          100    5.80 1.000000      0.22     0.03         NA        NA
    
    > cl
    socket cluster with 2 nodes on host ‘localhost’
    

提交回复
热议问题