From 4934c3458f43bb9e4a631ae0df19954fca7f43ba Mon Sep 17 00:00:00 2001
From: "raffaele.gaetano" <raffaele.gaetano@cirad.fr>
Date: Tue, 11 Dec 2018 15:37:15 +0100
Subject: [PATCH] Working on the resume option to resume segmentation after
 fail or abort.

---
 app/otbLSGRM.cxx                 | 17 ++++++++++++++++-
 include/lsgrmController.h        |  7 +++++++
 include/lsgrmController.txx      | 18 +++++++++++++++++-
 include/lsgrmGraphOperations.h   |  5 ++++-
 include/lsgrmGraphOperations.txx | 17 ++++++++++++++++-
 5 files changed, 60 insertions(+), 4 deletions(-)

diff --git a/app/otbLSGRM.cxx b/app/otbLSGRM.cxx
index ce21bc7..72d4540 100644
--- a/app/otbLSGRM.cxx
+++ b/app/otbLSGRM.cxx
@@ -112,8 +112,17 @@ private:
     AddChoice("tiling.none", "No tiling layout");
 
     AddParameter(ParameterType_Int, "memory", "Restrict memory use (mb)");
-
     MandatoryOff("memory");
+	
+	AddParameter(ParameterType_Bool, "resume", "Activate resume mode");
+	SetDefaultParameterBool("resume", false);
+	AddParameter(ParameterType_Int, "resume.tilerow", "Starting tile row in resume mode");
+	AddParameter(ParameterType_Int, "resume.tilecol", "Starting tile column in resume mode");
+	SetDefaultParameterInt("resume.tilerow", 0);
+	SetDefaultParameterInt("resume.tilecol", 0);
+	//MandatoryOff("resume");
+	//MandatoryOff("resume.tilex");
+	//MandatoryOff("resume.tiley");
   }
 
   void DoUpdateParameters()
@@ -224,6 +233,12 @@ private:
         otbAppLogINFO("Setting maximum memory to " << GetParameterInt("memory") << " MBytes");
         controller->SetInternalMemoryAvailable(GetParameterInt("memory"));
       }
+	  
+	 // Resume mode?
+	 if (GetParameterBool("resume"))
+	  {
+		controller.SetResumingMode(GetParameterInt("resume.tilerow"),GetParameterInt("resume.tilecol"));
+	  }
 
     // Run the segmentation
     controller->RunSegmentation();
diff --git a/include/lsgrmController.h b/include/lsgrmController.h
index e538cbe..85bdec7 100644
--- a/include/lsgrmController.h
+++ b/include/lsgrmController.h
@@ -44,6 +44,9 @@ public:
   void SetSpecificParameters(const SegmentationParameterType& params);
   void SetInputImage(ImageType * inputImage);
   void SetInternalMemoryAvailable(long long unsigned int v); // expecting a value in Mbytes.
+  
+  void SetResumingMode(unsigned int rRow, unsigned int rCol);
+  void StopResumingMode();
 
   /* Accessors */
   void SetTilingModeNone(){m_TilingMode = LSGRM_TILING_NONE; Modified();};
@@ -114,6 +117,10 @@ private:
   unsigned int              m_NumberOfFirstIterations; // number of iterations in the first step
   unsigned int              m_TileWidth;               // regular tile width (i.e. not left tiles)
   unsigned int              m_TileHeight;              // regular tile height (i.e. not bottom tiles)
+  bool						m_Resuming;				   // True if in resuming mode
+  unsigned int              m_ResumeTileRow;           // X index of starting tile in resume mode
+  unsigned int              m_ResumeTileCol;           // Y index of starting tile in resume mode
+  
 
   /* read-only variables */
   LSGRMTilingMode                    m_TilingMode;         // tiling mode (none/user/auto)
diff --git a/include/lsgrmController.txx b/include/lsgrmController.txx
index aa645f7..bfd630e 100644
--- a/include/lsgrmController.txx
+++ b/include/lsgrmController.txx
@@ -18,6 +18,9 @@ Controller<TSegmenter>::Controller()
   m_NbTilesY = 0;
   m_Threshold = 75;
   m_Memory = 0;
+  m_Resuming = false;
+  m_ResumeTileRow = 0;
+  m_ResumeTileCol = 0;
 
 }
 
@@ -122,7 +125,12 @@ void Controller<TSegmenter>::RunSegmentation()
           m_InputImage->GetLargestPossibleRegion().GetSize()[0],
           m_InputImage->GetLargestPossibleRegion().GetSize()[1],
           m_InputImage->GetNumberOfComponentsPerPixel(),
-          isFusion);
+          isFusion,
+		  m_Resuming,
+		  m_ResumeTileRow,
+		  m_ResumeTileCol);
+      
+	  if (m_Resuming) StopResumingMode();
 
 #ifdef OTB_USE_MPI
       GatherUsefulVariables(accumulatedMemory, isFusion);
@@ -461,5 +469,13 @@ std::vector<std::string> Controller<TSegmenter>::GetTemporaryFilesList()
   return list;
 }
 
+template<class TSegmenter>
+void Controller<TSegmenter>::SetResumingMode(unsigned int rX, unsigned int rY)
+{
+  m_Resuming = true;
+  m_ResumeTileX = rx;
+  m_ResumeTileY = ry;
+}
+
 } // end of namespace lsgrm
 #endif
diff --git a/include/lsgrmGraphOperations.h b/include/lsgrmGraphOperations.h
index 3ce66f2..c8faa4d 100644
--- a/include/lsgrmGraphOperations.h
+++ b/include/lsgrmGraphOperations.h
@@ -68,7 +68,10 @@ long long unsigned int RunPartialSegmentation(
     const unsigned int imageWidth,
     const unsigned int imageHeight,
     const unsigned int imageBands,
-    bool& isFusion);
+    bool& isFusion,
+	bool resume = false,
+	unsigned int rRow = 0,
+	unsigned int rCol = 0);
 
 template<class TSegmenter>
 void RemoveUselessNodes(ProcessingTile& tile,
diff --git a/include/lsgrmGraphOperations.txx b/include/lsgrmGraphOperations.txx
index d209477..71fd3e8 100644
--- a/include/lsgrmGraphOperations.txx
+++ b/include/lsgrmGraphOperations.txx
@@ -138,7 +138,10 @@ long long unsigned int RunPartialSegmentation(const typename TSegmenter::ParamTy
     const unsigned int imageWidth,
     const unsigned int imageHeight,
     const unsigned int imageBands,
-    bool& isFusion)
+    bool& isFusion,
+	bool resume,
+	unsigned int rRow,
+	unsigned int rCol)
 {
   long long unsigned int accumulatedMemory = 0;
   isFusion = false;
@@ -155,6 +158,18 @@ long long unsigned int RunPartialSegmentation(const typename TSegmenter::ParamTy
       if (MyTurn(row*nbTilesX + col))
 #endif
         {
+		// If resume mode, get accumulated memory on previous tiles
+		if (resume && (row*nbTilesX+col) < (rRow*nbTilesX+rCol) ) {
+		// Load the graph
+        std::cout << "\tResuming graph..." << std::endl;
+        TSegmenter segmenter;
+        ReadGraph<TSegmenter>(segmenter.m_Graph, currentTile.nodeFileName, currentTile.edgeFileName);
+		// Retrieve the amount of memory to store this graph
+        std::cout << "\tGet graph memory..." << std::endl;
+        accumulatedMemory += segmenter.GetGraphMemory();
+		continue;
+		}
+		
         // Get the current tile
         std::cout << "Processing tile " << row << ", " << col << std::endl;
         ProcessingTile currentTile = tiles[row*nbTilesX + col];
-- 
GitLab