Windows 不支持 mc.cores > 1

Mar*_*aei 6 windows parallel-processing multicore r

在此处输入图片说明我是 R 编程的新手,我有如下代码,我知道 Windows 不支持多核,但我不知道如何更改这部分代码。有人可以在不使用 mc.cores 功能的情况下向我建议等效代码吗?

rpl <- unlist( lapply( waydf$geometry$coordinates , nrow ) ) # row per line
 waydf <- waydf[ rpl > 1 , ]
ll <- parallel::mclapply( waydf$geometry$coordinates , st_linestring,
                         mc.cores =parallel::detectCores() - 1  )
outdf <- sf::st_sf(
line_geometry = sf::st_sfc( ll , crs = epsg ) ,
osm_id = waydf$id
)
Run Code Online (Sandbox Code Playgroud)

CPa*_*Pak 6

您必须澄清st_linestring是什么或做什么,因为您试图将 的内容传递waydf$geometry$coordinates给它,但没有指定任何参数,例如st_linestring(waydf$geometry$coordinates[i])

在 Windows 中,您将使用parLapply而不是mclapply.

# original
ll <- parallel::mclapply( waydf$geometry$coordinates , st_linestring,
                         mc.cores =parallel::detectCores() - 1  )

# replace the above with all of the below
library(parallel)
cl <- makeCluster(detectCores())
cl <- clusterEvalQ(cl, { library(sf) })  # you need to export packages as well
# cl <- clusterExport(cl, "st_linestring")  # each worker is a new environment, you will need to export variables/functions to
ll <- parallel::parLapply(cl, waydf$geometry$coordinates, function(i) st_linestring)    # if st_linestring takes arguments then st_linestring(x)
stopCluster(cl)
Run Code Online (Sandbox Code Playgroud)

编辑因为 st_linestring 是 sf 包中的函数,所以导出 sf 就足够了

第二次编辑

rpl <- unlist( lapply( waydf$geometry$coordinates , nrow ) ) # row per line
 waydf <- waydf[ rpl > 1 , ]

library(parallel)
cl <- makeCluster(detectCores())
cl <- clusterEvalQ(cl, { library(sf) })  # you need to export packages as well
# cl <- clusterExport(cl, "st_linestring")  # each worker is a new environment, you will need to export variables/functions to
ll <- parallel::parLapply(cl, waydf$geometry$coordinates, function(i) st_linestring)    # if st_linestring takes arguments then st_linestring(x)
stopCluster(cl)

outdf <- sf::st_sf(
line_geometry = sf::st_sfc( ll , crs = epsg ) ,
osm_id = waydf$id
)
Run Code Online (Sandbox Code Playgroud)


Aar*_*ica 4

如果您想要做的就是使该代码不并行运行,您只需告诉它使用 1 个核心,然后它就会lapply在后台使用。

ll <- parallel::mclapply(waydf$geometry$coordinates, st_linestring, mc.cores = 1)
Run Code Online (Sandbox Code Playgroud)

或者直接mclapply换成lapply.

ll <- lapply(waydf$geometry$coordinates, st_linestring)
Run Code Online (Sandbox Code Playgroud)