2

I am trying to use 'doSNOW' for parallel processing in R. During the foreach loop command, it is showing error: object j is not found. Please help me in this regard. The code works fine with the normal for loop. But is takes like an eternity to finish. Please help me in this regard. I will be ever grateful.

library(‘ncdf4′)
stryear = 2041
strmon = 1
strday = 1
library(‘foreach’)
library(‘doSNOW’)
cl <- makeCluster(6)
registerDoSNOW(cl)

filename = 'tasmax_WAS-44i_MPI-ESM-LR_rcp45_r1_CSIRO-CCAM-1391M_day_2041_2070.nc'
nc <- nc_open(filename)
tasmax <- ncvar_get(nc)
lon <- ncvar_get(nc, 'lon')
lat <- ncvar_get(nc, 'lat')
tasmax.len <- length(tasmax[1,1,])
x<-lon
lon.sel 87 & x<94)
y <- lat
lat.sel 20 & y<26)
lon.val <- lon[lon.sel]
lat.val <- lat[lat.sel]
#lon.length <- lon.val
#lat.length <- lat.val

strdate <- as.Date(paste(stryear,'-',strmon,'-', strday,sep=""))
finalVar <- data.frame( strday, strmon,stryear,matrix(NA,nrow=1, ncol=168))
colnames(finalVar)[1] <- "day"
colnames(finalVar)[2] <- "month"
colnames(finalVar)[3] <- "year"

locationData <- data.frame(matrix(NA,nrow=168, ncol=3))
colnames(locationData)[1]<-'y'
colnames(locationData)[2]<-'x'
colnames(locationData)[3]<-'Value'
n=1
rowno = 1

foreach(i = 1:12)
{ foreach(j = 1:14)
{ finalVar[rowno,n+3] <- tasmax[lon.sel[j],lat.sel[i],rowno]
colnames(finalVar)[n+3] <- paste(round(lat.val[i],2), ' & ', round(lon.val[j],2), sep="")
locationData[n,1] <- round(lat.val[i],2)
locationData[n,2] <- round(lon.val[j],2)
locationData[n,3] <- n
n= n+1
}
}

Error: object 'j' not found
  • 2
    That does not look like the right syntax, which I think must be more like `foreach(j=1:2) %do% (j*2)` – Frank May 20 '15 at 18:04
  • Thank you Mr. Frank for your reply. Yes i have understood my mistake. One problem i am facing that, to do faster works when i do %do% or %dopar% or %:%. Can you please explain this to me?? – Golam Rabbani Fahad May 24 '15 at 07:43
  • I think you're asking why parallel processing is faster? Instead of doing task A then task B, it does both at the same time, putting one on each of your computers processors. (It won't help unless your computer has multiple processors, though.) I think that's how it works anyways; haven't used it yet myself. This tutorial may be of help: http://biostat.mc.vanderbilt.edu/wiki/pub/Main/MinchunZhou/HPC_SNOW.rwn.pdf – Frank May 24 '15 at 13:39

0 Answers0