From b1a7cb9c6bd20ef52faae8b10853b7d9d411eff3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andr=C3=A9=20Offringa?= <offringa@astron.nl>
Date: Mon, 27 Sep 2010 14:31:34 +0000
Subject: [PATCH] Bug 1491: adding a ChangeOriginalFlags action and reserving
 one thread for IO, such that -j 8 would be best for 8 cores

---
 .../gui/strategyframes/setflaggingframe.h     | 24 +++++++++++++------
 .../rfi/strategy/foreachbaselineaction.h      | 10 ++++++++
 .../rfi/strategy/setflaggingaction.h          |  9 +++++--
 .../rfi/strategy/foreachbaselineaction.cpp    |  5 ++--
 4 files changed, 37 insertions(+), 11 deletions(-)

diff --git a/CEP/DP3/AOFlagger/include/AOFlagger/gui/strategyframes/setflaggingframe.h b/CEP/DP3/AOFlagger/include/AOFlagger/gui/strategyframes/setflaggingframe.h
index d50821954b6..dc89902024b 100644
--- a/CEP/DP3/AOFlagger/include/AOFlagger/gui/strategyframes/setflaggingframe.h
+++ b/CEP/DP3/AOFlagger/include/AOFlagger/gui/strategyframes/setflaggingframe.h
@@ -38,7 +38,8 @@ class SetFlaggingFrame : public Gtk::Frame {
 		_editStrategyWindow(editStrategyWindow), _action(action),
 		_setNoneButton("Set none"),
 		_setAllButton("Set all"),
-		_setOriginalButton("Set as original"),
+		_setFromOriginalButton("Restore original"),
+		_setToOriginalButton("Change original"),
 		_setInvertButton("Invert"),
 		_setPolarisationsEqualButton("Equal polarisations"),
 		_flagZerosButton("Flag zeros"),
@@ -53,8 +54,11 @@ class SetFlaggingFrame : public Gtk::Frame {
 			_box.pack_start(_setAllButton);
 			_setAllButton.set_group(group);
 
-			_box.pack_start(_setOriginalButton);
-			_setOriginalButton.set_group(group);
+			_box.pack_start(_setFromOriginalButton);
+			_setFromOriginalButton.set_group(group);
+
+			_box.pack_start(_setToOriginalButton);
+			_setToOriginalButton.set_group(group);
 
 			_box.pack_start(_setInvertButton);
 			_setInvertButton.set_group(group);
@@ -77,7 +81,10 @@ class SetFlaggingFrame : public Gtk::Frame {
 					_setAllButton.set_active(true);
 					break;
 				case rfiStrategy::SetFlaggingAction::FromOriginal:
-					_setOriginalButton.set_active(true);
+					_setFromOriginalButton.set_active(true);
+					break;
+				case rfiStrategy::SetFlaggingAction::ToOriginal:
+					_setToOriginalButton.set_active(true);
 					break;
 				case rfiStrategy::SetFlaggingAction::Invert:
 					_setInvertButton.set_active(true);
@@ -95,7 +102,8 @@ class SetFlaggingFrame : public Gtk::Frame {
 
 			_setNoneButton.show();
 			_setAllButton.show();
-			_setOriginalButton.show();
+			_setFromOriginalButton.show();
+			_setToOriginalButton.show();
 			_setInvertButton.show();
 			_setPolarisationsEqualButton.show();
 			_flagZerosButton.show();
@@ -119,7 +127,7 @@ class SetFlaggingFrame : public Gtk::Frame {
 		Gtk::HButtonBox _buttonBox;
 		Gtk::Label _baselinesLabel;
 		Gtk::RadioButton
-			_setNoneButton, _setAllButton, _setOriginalButton, _setInvertButton, _setPolarisationsEqualButton, _flagZerosButton, _orOriginalButton;
+			_setNoneButton, _setAllButton, _setFromOriginalButton, _setToOriginalButton, _setInvertButton, _setPolarisationsEqualButton, _flagZerosButton, _orOriginalButton;
 		Gtk::Button _applyButton;
 
 		void onApplyClicked()
@@ -128,8 +136,10 @@ class SetFlaggingFrame : public Gtk::Frame {
 				_action.SetNewFlagging(rfiStrategy::SetFlaggingAction::None);
 			else if(_setAllButton.get_active())
 				_action.SetNewFlagging(rfiStrategy::SetFlaggingAction::Everything);
-			else if(_setOriginalButton.get_active())
+			else if(_setFromOriginalButton.get_active())
 				_action.SetNewFlagging(rfiStrategy::SetFlaggingAction::FromOriginal);
+			else if(_setToOriginalButton.get_active())
+				_action.SetNewFlagging(rfiStrategy::SetFlaggingAction::ToOriginal);
 			else if(_setInvertButton.get_active())
 				_action.SetNewFlagging(rfiStrategy::SetFlaggingAction::Invert);
 			else if(_setPolarisationsEqualButton.get_active())
diff --git a/CEP/DP3/AOFlagger/include/AOFlagger/rfi/strategy/foreachbaselineaction.h b/CEP/DP3/AOFlagger/include/AOFlagger/rfi/strategy/foreachbaselineaction.h
index dd4ac05a8c8..5b1ba410e7b 100644
--- a/CEP/DP3/AOFlagger/include/AOFlagger/rfi/strategy/foreachbaselineaction.h
+++ b/CEP/DP3/AOFlagger/include/AOFlagger/rfi/strategy/foreachbaselineaction.h
@@ -72,6 +72,16 @@ namespace rfiStrategy {
 			void SetExceptionOccured();
 			void SetFinishedBaselines();
 			void SetProgress(ProgressListener &progress, int no, int count, std::string taskName, int threadId);
+			size_t mathThreadCount() const
+			{
+				// Since IO also takes some CPU, and IO should not be
+				// starved by math (because it is mostly IO limited),
+				// we reserve one of the threads for that.
+				if(_threadCount > 1)
+					return _threadCount - 1;
+				else
+					return _threadCount;
+			}
 
 			size_t BaselineProgress()
 			{
diff --git a/CEP/DP3/AOFlagger/include/AOFlagger/rfi/strategy/setflaggingaction.h b/CEP/DP3/AOFlagger/include/AOFlagger/rfi/strategy/setflaggingaction.h
index 41a9125dd30..9ad47196a60 100644
--- a/CEP/DP3/AOFlagger/include/AOFlagger/rfi/strategy/setflaggingaction.h
+++ b/CEP/DP3/AOFlagger/include/AOFlagger/rfi/strategy/setflaggingaction.h
@@ -31,7 +31,7 @@ namespace rfiStrategy {
 	class SetFlaggingAction : public Action
 	{
 		public:
-			enum NewFlagging { None, Everything, FromOriginal, Invert, PolarisationsEqual, FlagZeros, OrOriginal };
+			enum NewFlagging { None, Everything, FromOriginal, Invert, PolarisationsEqual, FlagZeros, OrOriginal, ToOriginal };
 
 			SetFlaggingAction() : _newFlagging(None) { }
 
@@ -45,7 +45,9 @@ namespace rfiStrategy {
 					case Everything:
 						return "Set everything flagged";
 					case FromOriginal:
-						return "Set original flags";
+						return "Restore original flags";
+					case ToOriginal:
+						return "Change original flags";
 					case Invert:
 						return "Set inverted flags";
 					case PolarisationsEqual:
@@ -70,6 +72,9 @@ namespace rfiStrategy {
 					case FromOriginal:
 						artifacts.ContaminatedData().SetGlobalMask(artifacts.OriginalData().GetSingleMask());
 						break;
+					case ToOriginal:
+						artifacts.OriginalData().SetGlobalMask(artifacts.ContaminatedData().GetSingleMask());
+						break;
 					case Invert: {
 						Mask2DPtr mask = Mask2D::CreateCopy(artifacts.ContaminatedData().GetSingleMask());
 						mask->Invert();
diff --git a/CEP/DP3/AOFlagger/src/rfi/strategy/foreachbaselineaction.cpp b/CEP/DP3/AOFlagger/src/rfi/strategy/foreachbaselineaction.cpp
index ba97ef22e31..0d0d6d162c9 100644
--- a/CEP/DP3/AOFlagger/src/rfi/strategy/foreachbaselineaction.cpp
+++ b/CEP/DP3/AOFlagger/src/rfi/strategy/foreachbaselineaction.cpp
@@ -84,7 +84,8 @@ namespace rfiStrategy {
 			ReaderFunction reader(*this);
 			threadGroup.create_thread(reader);
 			
-			for(unsigned i=0;i<_threadCount;++i)
+			size_t mathThreads = mathThreadCount();
+			for(unsigned i=0;i<mathThreads;++i)
 			{
 				PerformFunction function(*this, progress, i);
 				threadGroup.create_thread(function);
@@ -244,7 +245,7 @@ namespace rfiStrategy {
 	{
 		Stopwatch watch(true);
 		bool finished = false;
-		size_t threadCount = _action._threadCount;
+		size_t threadCount = _action.mathThreadCount();
 		do {
 			watch.Pause();
 			_action.WaitForBufferAvailable(threadCount);
-- 
GitLab