Skip to content

Commit

Permalink
check system memory and set cores to use based on return
Browse files Browse the repository at this point in the history
  • Loading branch information
jdbcode committed Apr 24, 2018
1 parent 7f43bec commit 721b5a5
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion R/prepare_images.r
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,23 @@ prepare_images = function(scenedir, demfile=NULL, proj="default", process=seq(1:

cfun = function(a, b) NULL

# get available memory and set cores accordingly
if(cores == 2){
cores = 1 # assume that we only have enough memory for one core
mem = tryCatch(system('wmic OS get FreePhysicalMemory /Value', intern = TRUE, show.output.on.console=F), error=function(e)c(1,2))
mem = mem[mem != "\r"] # start parsing memory out
if(length(mem) == 1){
mem = sub('\r','',mem)
if(grep('=', mem) == 1){
mem = unlist(strsplit(mem, '='))
if(length(mem) == 2){
mem = suppressWarnings(as.numeric(mem[2]))
if(!is.na(mem)){
if((mem/1000000) > 10){
cores=2
}}}}}}


#mssunpackr
if(all(is.na(match(process,1))) == F){
print("Running mssunpackr")
Expand Down Expand Up @@ -45,7 +62,6 @@ prepare_images = function(scenedir, demfile=NULL, proj="default", process=seq(1:
print("...in parallel")
cl=makeCluster(cores)
registerDoParallel(cl)
#o = foreach(i=1:length(files), .combine="cfun",.packages="LandsatLinkr") %dopar% msswarp(reffile=reffile, fixfile=files[i], sample=1000)
o = foreach(i=1:length(files), .combine="cfun",.packages="LandsatLinkr") %dopar% msswarp(reffile=reffile, fixfile=files[i])
stopCluster(cl)
} else {for(i in 1:length(files)){msswarp(reffile=reffile, fixfile=files[i])}}
Expand Down

0 comments on commit 721b5a5

Please sign in to comment.