Created
June 16, 2014 09:36
-
-
Save macieksk/e640d91214801fae98ee to your computer and use it in GitHub Desktop.
R CRAN code. Parallel execution - helper functions for the "multicore" library.
Run jobs (expressions) in the background without blocking. Consider using "source.gist" http://cran.r-project.org/web/packages/source.gist/index.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
################ | |
################ Parallel execution - helper functions for multicore library | |
################ Run jobs (expressions) in the background without blocking | |
################ | |
library(multicore) #better to be loaded last of all libraries | |
inpar<-function(name,expr){ | |
#Runs named expression in parallel with main thread - non-blocking | |
name<-deparse(substitute(name)) | |
vname<-paste(name,"tocollect",sep=".") | |
print(paste(vname,Sys.time())) | |
assign(vname, list(mcparallel(eval(expr),name)), envir=.GlobalEnv) | |
} | |
cpar<-function(name){ | |
#Collects previously run expression if finished - non-blocking | |
name<-deparse(substitute(name)) | |
vname<-paste(name,"tocollect",sep=".") | |
vjob<-get(vname,envir=.GlobalEnv)[[1]] | |
cres<-collect(vjob,wait=FALSE,timeout=1) | |
print(paste("Obtained result:",!is.null(cres))) | |
if (!is.null(cres)){ | |
while (!is.null(readChild(vjob$pid))){} | |
assign(name,cres[[1]], envir=.GlobalEnv) | |
} | |
print("Children left:") | |
print(children()) | |
} | |
kill.all.jobs<-function(){ | |
print(children()) | |
while(!is.null(readChildren())) TRUE; | |
print("Children left:") | |
print(children()) | |
} | |
ex<-expression | |
##Example use | |
#inpar(testp, ex( mean(rnorm(1000)) ) ) | |
#cpar(testp) | |
#testp | |
#inpar(testp2,ex({Sys.sleep(10); 1})) | |
#cpar(testp2) | |
#testp2 | |
##Wait for background task completion... | |
#Sys.sleep(10) | |
#cpar(testp2) | |
#testp2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment