我正在使用atakrig包。当我将核心数设置为 8 进行并行计算时,出现以下错误:0%Error in serialize(data, node$con) : error writing to connection
。如果我将核心数设置为 4,并行处理就正常了(没有错误)。我在使用该包的函数时出现此错误ataStartCluster()
。
cl <- parallel::makeCluster(8, type = "PSOCK")
我发现,如果设置而不是,就可以解决这个问题ataStartCluster()
。我还比较了使用 和不使用 时的执行时间parallel::makeCluster()
,看看并行计算是否按预期工作,结果确实如此。
使用并行代码时,cl <- parallel::makeCluster(8, type = "PSOCK")
执行时间为 1.5 分钟,而不使用并行代码时,执行时间为 3.2 分钟。问题是,我的另一台笔记本电脑上没有这个问题,它有 4GB 内存和 Windows 10 系统,但 R 版本和软件包与这台笔记本电脑上的相同(请参阅下面的会话信息)。
我正在使用的代码:
library(atakrig)
library(terra)
rpath <- system.file("extdata", package="atakrig")
aod3k <- rast(file.path(rpath, "MOD04_3K_A2017042.tif"))
aod10 <- rast(file.path(rpath, "MOD04_L2_A2017042.tif"))
aod3k.d <- discretizeRaster(aod3k, 1500)
aod10.d <- discretizeRaster(aod10, 1500)
grid.pred <- discretizeRaster(aod3k, 1500, type = "all")
aod3k.d$areaValues$value <- log(aod3k.d$areaValues$value)
aod10.d$areaValues$value <- log(aod10.d$areaValues$value)
## area-to-area Kriging ---# point-scale variogram from combined AOD-3k and AOD-10
aod.combine <- rbindDiscreteArea(x = aod3k.d, y = aod10.d)
vgm.ok_combine <- deconvPointVgm(aod.combine, model="Exp", ngroup=12, rd=0.75)
# point-scale cross-variogram
aod.list <- list(aod3k=aod3k.d, aod10=aod10.d)
aod.list <- list(aod3k=aod3k.d, aod10=aod10.d)
vgm.ck <- deconvPointVgmForCoKriging(aod.list, model="Exp", ngroup=12, rd=0.75,fixed.range = 9e4)
# prediction
cl <- parallel::makeCluster(8, type = "PSOCK")
pred.ataok <- ataKriging(aod10.d, grid.pred, vgm.ck$aod10, showProgress = TRUE, nopar = FALSE)
parallel::stopCluster(cl)
会话信息:
R version 4.4.3 (2025-02-28 ucrt)
Platform: x86_64-w64-mingw32/x64
Running under: Windows 11 x64 (build 26100)
Matrix products: default
locale:
[1] LC_COLLATE=English_United States.utf8 LC_CTYPE=English_United States.utf8 LC_MONETARY=English_United States.utf8
[4] LC_NUMERIC=C LC_TIME=English_United States.utf8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] terra_1.8-42 atakrig_0.9.8.1
loaded via a namespace (and not attached):
[1] DBI_1.2.3 KernSmooth_2.23-26 sf_1.0-20 doSNOW_1.0.20 zoo_1.8-13 spacetime_1.3-3 xts_0.14.1
[8] e1071_1.7-16 snow_0.4-4 sp_2.2-0 gstat_2.1-3 grid_4.4.3 classInt_0.4-11 foreach_1.5.2
[15] FNN_1.1.4.1 intervals_0.15.5 compiler_4.4.3 codetools_0.2-20 Rcpp_1.0.14 rstudioapi_0.17.1 lattice_0.22-7
[22] class_7.3-23 parallel_4.4.3 magrittr_2.0.3 tools_4.4.3 proxy_0.4-27 iterators_1.0.14 units_0.8-7
编辑
通过调用包foreach
(例如library(foreach)
),我成功运行了代码。我不知道这有什么用,也不知道它有什么作用。