From 74146c6f730843fed5e3ae5058ee52f7d41fb759 Mon Sep 17 00:00:00 2001
From: David Klein <dklein@pik-potsdam.de>
Date: Tue, 14 Jan 2020 16:29:48 +0100
Subject: [PATCH] Refactoring start scripts: adapt coupling scripts (get rid of
 *.cmd files by creating the SLURM command on the fly)

---
 start_bundle_coupled.R | 23 ++++++++++-------------
 start_coupled.R        | 14 +++++++++++++-
 2 files changed, 23 insertions(+), 14 deletions(-)

diff --git a/start_bundle_coupled.R b/start_bundle_coupled.R
index f0bc632..891635a 100644
--- a/start_bundle_coupled.R
+++ b/start_bundle_coupled.R
@@ -239,11 +239,6 @@ for(scen in common){
 	  settings_remind[scen, "path_gdx_ref"] <- scenarios_coupled[scen, "path_gdx_ref"]
 	  cat("Replacing gdx_ref information with those specified in\n  ",path_settings_coupled,"\n  ",settings_remind[scen, "path_gdx_ref"],"\n")
   }
-
-  if (!is.na(scenarios_coupled[scen, "path_gdx_opt"])) {
-	  settings_remind[scen, "path_gdx_opt"] <- scenarios_coupled[scen, "path_gdx_opt"]
-	  cat("Replacing gdx_opt information with those specified in\n  ",path_settings_coupled,"\n  ",settings_remind[scen, "path_gdx_opt"],"\n")
-  }
  
   # Create list of previously defined paths to gdxs
   gdxlist <- c(input.gdx     = settings_remind[scen, "path_gdx"], # eventually this was updated if older runs exists in this folder (see above)
@@ -255,7 +250,7 @@ for(scen in common){
   
   # add information on subsequent runs to start after the current run is finished
   # take rownames (which is the runname) of that row, that has the current scenario in its gdx_ref
-  cfg_rem$subsequentruns        <- intersect(rownames(settings_remind[settings_remind$path_gdx_ref == scen & !is.na(settings_remind$path_gdx_ref),]),common)
+  cfg_rem$subsequentruns <- intersect(rownames(settings_remind[settings_remind$path_gdx_ref == scen & !is.na(settings_remind$path_gdx_ref),]),common)
      
   # immediately start run if it has a real gdx file (not a runname) given (last four letters are ".gdx") in path_gdx_ref or where this field is empty (NA)
   start_now <- (substr(settings_remind[scen,"path_gdx_ref"], nchar(settings_remind[scen,"path_gdx_ref"])-3, nchar(settings_remind[scen,"path_gdx_ref"])) == ".gdx" 
@@ -300,22 +295,24 @@ for(scen in common){
  
   # create cluster_start_coupled_scen.cmd file
   # 1. copy general cluster_start_coupled file
-  system(paste0("cp cluster_start_coupled.cmd cluster_start_coupled_",scen,".cmd"))
+  #system(paste0("cp cluster_start_coupled.cmd cluster_start_coupled_",scen,".cmd"))
   # 2. modify accordingly
-  manipulateConfig(paste0("cluster_start_coupled_",scen,".cmd"),coupled_config=paste0(runname,".RData"),line_endings = "NOTwin")
-  manipulateConfig(paste0("cluster_start_coupled_",scen,".cmd"),"--job-name"=runname,line_endings = "NOTwin")
-  manipulateConfig(paste0("cluster_start_coupled_",scen,".cmd"),"--output"=paste0(runname,".log"),line_endings = "NOTwin")
+  #manipulateConfig(paste0("cluster_start_coupled_",scen,".cmd"),coupled_config=paste0(runname,".RData"),line_endings = "NOTwin")
+  #manipulateConfig(paste0("cluster_start_coupled_",scen,".cmd"),"--job-name"=runname,line_endings = "NOTwin")
+  #manipulateConfig(paste0("cluster_start_coupled_",scen,".cmd"),"--output"=paste0(runname,".log"),line_endings = "NOTwin")
   if (cfg_rem$gms$optimization == "nash" && cfg_rem$gms$cm_nash_mode == "parallel") {
     # for nash: set the number of CPUs per node to number of regions + 1
     nr_of_regions <- length(levels(read.csv2(cfg_rem$regionmapping)$RegionCode)) + 1 
-    manipulateConfig(paste0("cluster_start_coupled_",scen,".cmd"),"--tasks-per-node"=nr_of_regions,line_endings = "NOTwin")
+    #manipulateConfig(paste0("cluster_start_coupled_",scen,".cmd"),"--tasks-per-node"=nr_of_regions,line_endings = "NOTwin")
   } else {
     # for negishi: use only one CPU
-    manipulateConfig(paste0("cluster_start_coupled_",scen,".cmd"),"--tasks-per-node"=1,line_endings = "NOTwin")
+    nr_of_regions <- 1
+    #manipulateConfig(paste0("cluster_start_coupled_",scen,".cmd"),"--tasks-per-node"=1,line_endings = "NOTwin")
   }
 
   if (start_now){
-      if (!exists("test")) system(paste0("sbatch cluster_start_coupled_",scen,".cmd"))
+      #if (!exists("test")) system(paste0("sbatch cluster_start_coupled_",scen,".cmd"))
+      if (!exists("test")) system(paste0("sbatch --qos=standby --job-name=",runname," --output=",runname,".log --mail-type=END --comment=REMIND-MAgPIE --tasks-per-node=",nr_of_regions," --wrap=\"Rscript start_coupled.R \" coupled_config=",runname,".RData"))
       else cat("Test mode: run NOT submitted to the cluster\n")
   } else {
      cat(paste0("Run ",runname," will start after preceding run ",prefix_runname,settings_remind[scen,"path_gdx_ref"]," has finished\n"))
diff --git a/start_coupled.R b/start_coupled.R
index 63ca85f..2600d7d 100644
--- a/start_coupled.R
+++ b/start_coupled.R
@@ -201,10 +201,22 @@ start_coupled <- function(path_remind,path_magpie,cfg_rem,cfg_mag,runname,max_it
   setwd(mainwd)
   cat(" to",getwd(),"\n")
   
+  # for the sbatch command of the subsequent runs below set the number of tasks per node
+  # this not clean, because we use the number of regions of the *current* run to set the number of tasks for the *subsequent* runs
+  # but it is sufficiently clean, since the number of regions should not differ between current and subsequent
+  if (cfg_rem$gms$optimization == "nash" && cfg_rem$gms$cm_nash_mode == "parallel") {
+    # for nash: set the number of CPUs per node to number of regions + 1
+    nr_of_regions <- length(levels(read.csv2(cfg_rem$regionmapping)$RegionCode)) + 1 
+  } else {
+    # for negishi: use only one CPU
+    nr_of_regions <- 1
+  }
+
   #start subsequent runs via cmd scripts created at the end of start_bundle_coupled.R
   for(run in cfg_rem$subsequentruns){
     cat("Submitting subsequent run",run,"\n")
-    system(paste0("sbatch cluster_start_coupled_",run,".cmd"))
+    #system(paste0("sbatch cluster_start_coupled_",run,".cmd"))
+    system(paste0("sbatch --qos=standby --job-name=",run," --output=",run,".log --mail-type=END --comment=REMIND-MAgPIE --tasks-per-node=13",nr_of_regions," --wrap=\"Rscript start_coupled.R \" coupled_config=",run,".RData"))
   }
   
   # Read runtime of ALL coupled runs (not just the current scenario) and produce comparison pdf
-- 
GitLab