R - 对数组的每个元素并行应用函数
R - apply function on each element of array in parallel
我有组织为大小数组的最高和最低温度和降水量的测量值
(100x96x50769),其中 i 和 j 是具有相关坐标的网格单元,z 表示随时间变化的测量次数。
从概念上讲,它看起来像这样:
我正在使用 climdex.pcic
包来计算极端天气事件的指数。给定最高和最低温度和降水量的时间序列,climdexInput.raw
函数将 return 一个 climdexIput
对象,可用于确定几个指标:霜冻天数,夏季天数, 连续干燥天等
函数的调用非常简单:
ci <- climdexInput.raw(tmax=x, tmin=y, prec=z,
t, t, t, base.range=c(1961,1990))
其中 x 是最高温度的向量,y 是最低温度的向量,z 是降水量的向量,t 是包含测量 x、y 和 z 的日期的向量。
我想做的是提取数组的每个元素(即上图中的每个网格单元格)的时间序列,并将其用于 运行 climdexInput.raw
函数。
由于真实数据的元素较多,我想运行这个任务在我的4核Linux服务器上并行进行。但是,我没有 R 中并行化的经验。
这是我的程序的一个示例(有意减小尺寸以加快在您的计算机上的执行速度):
library(climdex.pcic)
# Create some dates
t <- seq(as.Date('2000-01-01'), as.Date('2010-12-31'), 'day')
# Parse the dates into PCICt
t <- as.PCICt(strftime(t), cal='gregorian')
# Create some dummy weather data, with dimensions `# of lat`, `# of lon` and `# of timesteps`
nc.min <- array(runif(10*9*4018, min=0, max=15), c(10, 9, 4018))
nc.max <- array(runif(10*9*4018, min=25, max=40), c(10, 9, 4018))
nc.prc <- array(runif(10*9*4018, min=0, max=25), c(10, 9, 4018))
# Create "ci" object
ci <- climdexInput.raw(tmax=nc.max[1,1,], tmin=nc.min[1,1,], prec=nc.prc[1,1,],
t, t, t, base.range=c(2000,2005))
# Once you have “ci”, you can compute any of the indices provided by the climdex.pcic package.
# The example below is for cumulative # of dry days per year:
cdd <- climdex.cdd(ci, spells.can.span.years = TRUE)
现在,请注意,在上面的示例中,我仅使用数组的第一个元素 ([1,1,]) 作为 climdexInput.raw
函数中的示例。
如何利用并行处理对所有元素执行相同的操作,可能是通过遍历数组的 i
和 j
维度?
您可以使用 foreach 来做到这一点:
library(doParallel)
registerDoParallel(cl <- makeCluster(3))
res <- foreach(j = seq_len(ncol(nc.min))) %:%
foreach(i = seq_len(nrow(nc.min))) %dopar% {
ci <- climdex.pcic::climdexInput.raw(
tmax=nc.max[i,j,],
tmin=nc.min[i,j,],
prec=nc.prc[i,j,],
t, t, t,
base.range=c(2000,2005)
)
}
stopCluster(cl)
请参阅我关于使用 foreach 的并行性指南:https://privefl.github.io/blog/a-guide-to-parallelism-in-r/。
然后,要计算索引,只需使用 climdex.cdd(res[[1]][[1]], spells.can.span.years = TRUE)
(j
首先,i
第二)。
我有组织为大小数组的最高和最低温度和降水量的测量值 (100x96x50769),其中 i 和 j 是具有相关坐标的网格单元,z 表示随时间变化的测量次数。
从概念上讲,它看起来像这样:
我正在使用 climdex.pcic
包来计算极端天气事件的指数。给定最高和最低温度和降水量的时间序列,climdexInput.raw
函数将 return 一个 climdexIput
对象,可用于确定几个指标:霜冻天数,夏季天数, 连续干燥天等
函数的调用非常简单:
ci <- climdexInput.raw(tmax=x, tmin=y, prec=z,
t, t, t, base.range=c(1961,1990))
其中 x 是最高温度的向量,y 是最低温度的向量,z 是降水量的向量,t 是包含测量 x、y 和 z 的日期的向量。
我想做的是提取数组的每个元素(即上图中的每个网格单元格)的时间序列,并将其用于 运行 climdexInput.raw
函数。
由于真实数据的元素较多,我想运行这个任务在我的4核Linux服务器上并行进行。但是,我没有 R 中并行化的经验。
这是我的程序的一个示例(有意减小尺寸以加快在您的计算机上的执行速度):
library(climdex.pcic)
# Create some dates
t <- seq(as.Date('2000-01-01'), as.Date('2010-12-31'), 'day')
# Parse the dates into PCICt
t <- as.PCICt(strftime(t), cal='gregorian')
# Create some dummy weather data, with dimensions `# of lat`, `# of lon` and `# of timesteps`
nc.min <- array(runif(10*9*4018, min=0, max=15), c(10, 9, 4018))
nc.max <- array(runif(10*9*4018, min=25, max=40), c(10, 9, 4018))
nc.prc <- array(runif(10*9*4018, min=0, max=25), c(10, 9, 4018))
# Create "ci" object
ci <- climdexInput.raw(tmax=nc.max[1,1,], tmin=nc.min[1,1,], prec=nc.prc[1,1,],
t, t, t, base.range=c(2000,2005))
# Once you have “ci”, you can compute any of the indices provided by the climdex.pcic package.
# The example below is for cumulative # of dry days per year:
cdd <- climdex.cdd(ci, spells.can.span.years = TRUE)
现在,请注意,在上面的示例中,我仅使用数组的第一个元素 ([1,1,]) 作为 climdexInput.raw
函数中的示例。
如何利用并行处理对所有元素执行相同的操作,可能是通过遍历数组的 i
和 j
维度?
您可以使用 foreach 来做到这一点:
library(doParallel)
registerDoParallel(cl <- makeCluster(3))
res <- foreach(j = seq_len(ncol(nc.min))) %:%
foreach(i = seq_len(nrow(nc.min))) %dopar% {
ci <- climdex.pcic::climdexInput.raw(
tmax=nc.max[i,j,],
tmin=nc.min[i,j,],
prec=nc.prc[i,j,],
t, t, t,
base.range=c(2000,2005)
)
}
stopCluster(cl)
请参阅我关于使用 foreach 的并行性指南:https://privefl.github.io/blog/a-guide-to-parallelism-in-r/。
然后,要计算索引,只需使用 climdex.cdd(res[[1]][[1]], spells.can.span.years = TRUE)
(j
首先,i
第二)。