2014-04-21 45 views
1

我想在我們的集羣上運行此作業,並且我不斷收到此類型的對象'closure'不是子集表達式「錯誤。它基本上在一堆節點上運行這個函數「do_1()」。我正在進行子集化的閉包對象被稱爲「數據」,所以我認爲這是因爲RData文件沒有在每個節點上讀取(這可能不是調用每個這些單個數據集「數據」的最佳實踐,所以這是我的錯誤) 。集羣r腳本不正確讀取RData數據集

我將腳本剝離爲儘可能裸露的骨骼,並顯示在下面。提交作業時,它仍會產生相同的錯誤。我認爲有些東西我不知道在每個節點上的單獨數據集中讀取......我在調用load()時可能沒有指定一些參數。也許「數據」數據集不在正確的命名空間或什麼......我不確定。任何想法將受到讚賞。

library(parallel) 
library(Rmpi) 

np <- mpi.universe.size() 
cl <- makeCluster(np, type = "MPI") 

allFiles <- list.files("/bigtmp/trb5me/rdata_files/") 
allFiles <- sapply(allFiles, function(string) paste("/bigtmp/trb5me/rdata_files/", string, sep = "")) 

run_one_day <- function(daynum){ 

    # do we want to subset days to not the first hour? 
    train <- data[[daynum]] * 10000 
    train 
} 
clusterExport(cl = cl, "run_one_day") 

do_1 <- function(path_to_file){ 

    if(!require(xts)){ 
    install.packages("xts") 
    library(xts) 
    } 

    # load data 
    load(file=path_to_file) 

    # extract the symbol name so we cna save the results later 
    symbolName <- strsplit(path_to_file, "/")[[1]][5] 
    symbolName <- strsplit(symbolName, ".", fixed = T)[[1]][1] 

    # get the results 
    # there is also a function called data...so in this case it's length will be 1 
    mySequence <- 1:(length(data)-1) 
    myResults <- lapply(mySequence, run_one_day) #this is where the problem is! 

    # save the results 
    path_dest <- paste("/bigtmp/trb5me/mod1_results/", symbolName, ".RData", sep = "") 
    save(myResults, file = path_dest) 

    # remove everything from memory 
    rm(list=ls()) 

} 

parLapply(cl, allFiles, do_1) 

# turn off all the cluster stuff 
stopCluster(cl) 
mpi.exit() 
+0

錯誤來自哪個函數?嘗試包括選項(錯誤=回溯) –

+0

等等,我明白了;沒關係。 –

回答

0

data變量僅在主站上可用,而不在從站上可用。 由於碰巧存在一個叫做data的函數,這就是他們試圖使用的函數, 和[[給出了你得到的錯誤信息。

嘗試在計算之前將data變量導出到其他節點。

clusterExport(cl, "data") 
+0

即使在每個節點上調用「load()」,它僅在主服務器上可用?這意味着不同的RData文件不能在單獨的節點上加載。真的嗎? – Taylor

+0

@Taylor負載將數據放入'do_1'的環境中,'run_one_day'無法訪問。你最好將它作爲參數傳遞給'run_one_day',甚至更好的使用'readRDS' /'saveRDS'。 – hadley

+0

roger那。多謝你們。 – Taylor

0

可能是錯誤的,但它看起來像錯誤實際上是train <- data[[daynum]]。它試圖對函數或「閉包」數據進行子集化,當然還有漏洞。嘗試命名您的數據集而不是'數據',看看會發生什麼。

2

這是一個範圍問題:「數據」,在「do_1」,這是不是在「run_one_day」功能的範圍的局部環境被加載。 R使用詞法作用域,所以重要的是定義了「run_one_day」的位置,而不是它被調用的地方。

一種解決方案是使用負載「ENVIR」參數設置爲「數據」加載到全球環境:

load(file=path_to_file, envir=.GlobalEnv) 

另一種解決方案是定義「do_1」函數內部「run_one_day」。