我有这个三重循环,输出全部写入同一个数组asc.a.out。我想知道是否有一种简单的方法来并行化这个,我尝试了foreach(现已注释掉),但输出数组是空的。有没有一种简单的方法来加速像这样的结构,我记得在MATLAB循环中可以简单地转换为并行循环而不需要任何重大的代码更改,就像openmp一样。我的数据在这个结构中的原因是因为它是在每个位置都有时间序列的空间数据。
编辑: 我最后添加了一个简单的例子。
asc.a = array(55,dim=c(120,180,2190)) # filled with nonsense data as an example.
asc.a.out = array(NaN,dim=c(120,180,426))
for (i in 1:120) {
#foreach (i = 1:120) %dopar% {
for (j in 1:180) {
yy = seq(1:2190)
asc.a.b = rbind(asc.a[i,j,1:365],asc.a[i,j,366:730],asc.a[i,j,731:1095],asc.a[i,j,1096:1460],asc.a[i,j,1461:1825],asc.a[i,j,1826:2190])
asc.a.b = cbind(asc.a.b[,335:365],asc.a.b,asc.a.b[,1:30])
asc.a.c = c(asc.a[i,j,1:365],asc.a[i,j,366:730],asc.a[i,j,731:1095],asc.a[i,j,1096:1460],asc.a[i,j,1461:1825],asc.a[i,j,1826:2190])
check = asc.a.c
check = sort(asc.a.c,decreasing = T)
if (sum(!is.na(check[1:365])) < 100) next
model <- rlm(asc.a.c ~ poly(yy,25))
predicted.intervals <- predict(model,data.frame(x=yy),interval='confidence',level=0.99)
asc.a.c.tmp = asc.a.c - predicted.intervals[,1]
sd = sd(asc.a.c.tmp,na.rm=T)
med = median(asc.a.c.tmp,na.rm=T)
q25 = quantile(asc.a.c.tmp,na.rm=T)
asc.a.c.tmp[asc.a.c.tmp < (0-sd)] = NaN
asc.a.c.tmp[asc.a.c.tmp > (0+sd)] = NaN
asc.clim = asc.a.c.tmp + predicted.intervals[,1]
asc.a.d = rbind(asc.clim[1:365],asc.clim[366:730],asc.clim[731:1095],asc.clim[1096:1460],asc.clim[1461:1825],asc.clim[1826:2190])
asc.a.d = cbind(asc.a.d[,335:365],asc.a.d,asc.a.d[,1:30])
for (k in 16:411) {
tmp1 = asc.a.b[,((k-15):(k+15))]
tmp = quantile(asc.a.d[,((k-15):(k+15))],na.rm=T)
tmp1[tmp1 < tmp[2]] = NaN
tmp1[tmp1 > tmp[4]] = NaN
asc.a.out[i,j,k] = mean(tmp1,na.rm=T)
}
}
print(i)
}
编辑: 具有与上述相同代码结构的较小示例。
asc.a = array(55,dim=c(120,180,426))
asc.a.out = array(NaN,dim=c(120,180,426))
for (i in 1:120) {
#foreach (i = 1:120) %dopar% {
for (j in 1:180) {
tmp = asc.a[i,j,]
for (k in 16:411) {
tmp1 = tmp[((k-15):(k+15))]
asc.a.out[i,j,k] = mean(tmp1,na.rm=T)
}
}
print(i)
}