From 08bab65a7193bd5d87904c45941abeb3c81b9121 Mon Sep 17 00:00:00 2001
From: Maria Kotsifakou <kotsifa2@illinois.edu>
Date: Sat, 16 Sep 2017 12:05:48 -0500
Subject: [PATCH] Interactive policy for CFAR demo

---
 llvm/projects/visc-rt/policy.h    | 52 ++++++++++++++++++++++++++++---
 llvm/projects/visc-rt/visc-rt.cpp |  3 +-
 2 files changed, 49 insertions(+), 6 deletions(-)

diff --git a/llvm/projects/visc-rt/policy.h b/llvm/projects/visc-rt/policy.h
index 7fa3c27e3b..f30c310c1a 100644
--- a/llvm/projects/visc-rt/policy.h
+++ b/llvm/projects/visc-rt/policy.h
@@ -8,6 +8,7 @@
 class Policy {
   public:
     virtual int getVersion(const char *, int64_t) = 0;
+    virtual ~Policy() {};
 };
 
 class NodePolicy : public Policy {
@@ -26,17 +27,16 @@ class NodePolicy : public Policy {
     //  std::cout << s << ": CPU" << "\n";
     //  return 0;
     //}
-    std::cout << s << ": GPU" << "\n";
-    return 1;
+    return 2;
   }
 };
 
 class IterationPolicy : public Policy {
   virtual int getVersion(const char *name, int64_t it) override {
-    if (it % 10 == 0)
+    if ((it % 10 == 0) || (it % 10 == 1))
       return 0;
     else
-      return 1;
+      return 2;
   }
 };
 
@@ -44,7 +44,7 @@ class DeviceStatusPolicy : public Policy {
   virtual int getVersion(const char *name, int64_t it) override {
     if (deviceStatus) {
       //std::cout << "Returning GPU\n";
-      return 1;
+      return 2;
     }
     else {
       //std::cout << "Returning CPU\n";
@@ -53,4 +53,46 @@ class DeviceStatusPolicy : public Policy {
   }
 };
 
+/* ------------------------------------------------------------------------- */
+// Added for the CFAR interactive policy demo.
+
+class InteractivePolicy : public Policy {
+private:
+  // 0 :for CPU, 1 for GPU, 2 for Vector
+  unsigned int userTargetDeviceChoice;
+  // Used to end thread execution
+  bool end;
+  // Thread that will update userTargetDeviceChoice
+  std::thread userTargetDeviceChoiceThread;
+  // Thread function
+  void updateUserTargetChoice() {
+    while (!end) {
+      std::cout << "Select target device (0 for CPU, 1 fpr GPU): ";
+      std::cin >> userTargetDeviceChoice;
+      if (userTargetDeviceChoice > 1) {
+        std::cout << "Invalid target device. Selecting GPU instead.\n";
+        userTargetDeviceChoice = 1;
+      }
+    }
+  }
+
+public:
+  // Inherited method, erquired for every policy object
+  virtual int getVersion(const char *name, int64_t it) {
+    return userTargetDeviceChoice;
+  }
+
+  InteractivePolicy() {
+    userTargetDeviceChoice = 1;
+    end = false;
+    userTargetDeviceChoiceThread =
+      std::thread(&InteractivePolicy::updateUserTargetChoice, this);
+  }
+
+  ~InteractivePolicy() {
+    end = true;
+    userTargetDeviceChoiceThread.join(); 
+  }
+};
+
 #endif // __POLICY__
diff --git a/llvm/projects/visc-rt/visc-rt.cpp b/llvm/projects/visc-rt/visc-rt.cpp
index 0b563191fd..9f26411e3f 100644
--- a/llvm/projects/visc-rt/visc-rt.cpp
+++ b/llvm/projects/visc-rt/visc-rt.cpp
@@ -71,9 +71,10 @@ static inline void checkErr(cl_int err, cl_int success, const char * name) {
 /************************* Policies *************************************/
 void llvm_visc_policy_init() {
   cout << "Initializing policy object ...\n";
-  policy = new NodePolicy();
+//  policy = new NodePolicy();
 //  policy = new IterationPolicy();
 //  policy = new DeviceStatusPolicy();
+  policy = new InteractivePolicy();
   cout << "DONE: Initializing policy object.\n";
 }
 
-- 
GitLab