From 3e1f6ac063ec74901613a6144fe5bd5723987c69 Mon Sep 17 00:00:00 2001
From: Hashim Sharif <hsharif3@miranda.cs.illinois.edu>
Date: Mon, 5 Apr 2021 21:08:19 -0500
Subject: [PATCH] Separating Jetson-specific frequency utilities into a
 separate file

---
 .../include/jetson_freq_utils.h               | 125 ++++++++++++++++++
 .../tensor_runtime/src/hpvm-rt-controller.cpp | 124 ++---------------
 2 files changed, 133 insertions(+), 116 deletions(-)
 create mode 100644 hpvm/projects/hpvm-tensor-rt/tensor_runtime/include/jetson_freq_utils.h

diff --git a/hpvm/projects/hpvm-tensor-rt/tensor_runtime/include/jetson_freq_utils.h b/hpvm/projects/hpvm-tensor-rt/tensor_runtime/include/jetson_freq_utils.h
new file mode 100644
index 0000000000..7caee936a2
--- /dev/null
+++ b/hpvm/projects/hpvm-tensor-rt/tensor_runtime/include/jetson_freq_utils.h
@@ -0,0 +1,125 @@
+
+/****  
+
+   This file contains freqency setting routines specific to the Jetson Tx2 
+
+   NOTE: These routines are not used directly in the current code. 
+
+   Users testing frequency changes on the Jetson Tx2 (or similar devices) can use/repurpose these routines
+
+***/
+
+#include <fstream>
+
+
+const int available_freqs[] = {
+    140250000,  // 0
+    229500000,  // 1
+    318750000,  // 2
+    408000000,  // 3
+    497250000,  // 4
+    586500000,  // 5
+    675750000,  // 6
+    765000000,  // 7
+    854250000,  // 8
+    943500000,  // 9
+    1032750000, // 10
+    1122000000, // 11
+    1211250000, // 12
+    1300500000  // 13
+};
+
+
+// Sets frequency
+void setFreq(unsigned freq_index) {
+
+  unsigned target_freq = available_freqs[freq_index];
+
+  const char *const min_freq_file =
+      "/sys/devices/17000000.gp10b/devfreq/17000000.gp10b/min_freq";
+  const char *const max_freq_file =
+      "/sys/devices/17000000.gp10b/devfreq/17000000.gp10b/max_freq";
+
+  std::ofstream min_stream;
+  std::ofstream max_stream;
+
+  min_stream.open(min_freq_file, std::ofstream::out);
+  max_stream.open(max_freq_file, std::ofstream::out);
+
+  min_stream << target_freq << std::flush;
+  max_stream << target_freq << std::flush;
+
+  min_stream.close();
+  max_stream.close();
+}
+
+// Records frequency
+unsigned recordFreq() {
+
+  // Current frequency file
+  const char *const cur_freq_file =
+      "/sys/devices/17000000.gp10b/devfreq/17000000.gp10b/cur_freq";
+  std::ifstream cur_stream;
+  cur_stream.open(cur_freq_file, std::ifstream::in);
+
+  // Get starting frequency
+  unsigned cur_freq;
+  cur_stream >> cur_freq;
+  std::cout << "Starting frequency = " << cur_freq << "\n";
+  cur_stream.close();
+
+  return cur_freq;
+}
+
+// There will be no frequency request for the first batch
+// Therefore, we skip the first element by initializing to 1, not 0.
+FrequencyIndexList::FrequencyIndexList(std::vector<int> il, unsigned rf)
+    : idx_list(il), rep_factor(rf), count(1), idx(0) {}
+
+unsigned FrequencyIndexList::getNextIndex() {
+  if (count == rep_factor) {
+    count = 0;
+    idx = (idx + 1) % idx_list.size();
+  }
+  count++;
+  return idx_list[idx];
+}
+
+
+void RuntimeController::readIterationFrequency() {
+  if (PI)
+    PI->readIterationFrequency();
+}
+
+unsigned long RuntimeController::getIterationFrequency() {
+  return (PI ? PI->getIterationFrequency() : 0);
+}
+
+void RuntimeController::updateFrequency() {
+#ifdef JETSON_EXECUTION
+  unsigned freq_idx = FIL->getNextIndex();
+  //--- updateJetsonGPUFreq(freq_idx);
+
+  setFreq(freq_idx);
+
+#endif // JETSON_EXECUTION
+}
+
+unsigned long RuntimeController::getLastFrequency() { return g_freq; }
+
+void RuntimeController::setLastFrequency(unsigned long f) { g_freq = f; }
+
+
+
+void ProfileInfo::readIterationFrequency() {
+#ifdef JETSON_EXECUTION
+  //----- frequency_current_iteration = readJetsonGPUFreq();
+  frequency_current_iteration = recordFreq();
+#else
+  frequency_current_iteration = 0;
+#endif // JETSON_EXECUTION
+}
+
+unsigned long ProfileInfo::getIterationFrequency() {
+  return frequency_current_iteration;
+}
diff --git a/hpvm/projects/hpvm-tensor-rt/tensor_runtime/src/hpvm-rt-controller.cpp b/hpvm/projects/hpvm-tensor-rt/tensor_runtime/src/hpvm-rt-controller.cpp
index 038924a949..d25a76153f 100644
--- a/hpvm/projects/hpvm-tensor-rt/tensor_runtime/src/hpvm-rt-controller.cpp
+++ b/hpvm/projects/hpvm-tensor-rt/tensor_runtime/src/hpvm-rt-controller.cpp
@@ -23,92 +23,12 @@
 
 #include "hpvm-rt-controller.h"
 #include "global_data.h"
+#include "jetson_freq_utils.h"
 #include <fstream>
 
 
-const int available_freqs[] = {
-    140250000,  // 0
-    229500000,  // 1
-    318750000,  // 2
-    408000000,  // 3
-    497250000,  // 4
-    586500000,  // 5
-    675750000,  // 6
-    765000000,  // 7
-    854250000,  // 8
-    943500000,  // 9
-    1032750000, // 10
-    1122000000, // 11
-    1211250000, // 12
-    1300500000  // 13
-};
-
-
-// Sets frequency
-void setFreq(unsigned freq_index) {
-
-  unsigned target_freq = available_freqs[freq_index];
-
-  const char *const min_freq_file =
-      "/sys/devices/17000000.gp10b/devfreq/17000000.gp10b/min_freq";
-  const char *const max_freq_file =
-      "/sys/devices/17000000.gp10b/devfreq/17000000.gp10b/max_freq";
-
-  std::ofstream min_stream;
-  std::ofstream max_stream;
-
-  min_stream.open(min_freq_file, std::ofstream::out);
-  max_stream.open(max_freq_file, std::ofstream::out);
-
-  min_stream << target_freq << std::flush;
-  max_stream << target_freq << std::flush;
-
-  min_stream.close();
-  max_stream.close();
-}
-
-// Records frequency
-unsigned recordFreq() {
-
-  // Current frequency file
-  const char *const cur_freq_file =
-      "/sys/devices/17000000.gp10b/devfreq/17000000.gp10b/cur_freq";
-  std::ifstream cur_stream;
-  cur_stream.open(cur_freq_file, std::ifstream::in);
-
-  // Get starting frequency
-  unsigned cur_freq;
-  cur_stream >> cur_freq;
-  std::cout << "Starting frequency = " << cur_freq << "\n";
-  cur_stream.close();
-
-  return cur_freq;
-}
-
 //---------------------------------------------------------------------------//
 
-/*
- * Check if a file exists
- * Return true if the file exists, false else
- */
-bool fileExists(const std::string &file) {
-  struct stat buf;
-  return (stat(file.c_str(), &buf) == 0);
-}
-
-// There will be no frequency request for the first batch
-// Therefore, we skip the first element by initializing to 1, not 0.
-FrequencyIndexList::FrequencyIndexList(std::vector<int> il, unsigned rf)
-    : idx_list(il), rep_factor(rf), count(1), idx(0) {}
-
-unsigned FrequencyIndexList::getNextIndex() {
-  if (count == rep_factor) {
-    count = 0;
-    idx = (idx + 1) % idx_list.size();
-  }
-  count++;
-  return idx_list[idx];
-}
 
 // Functions
 void ProfileInfo::resetCurrentIterationTime() {
@@ -165,18 +85,6 @@ void ProfileInfo::end_iteration() {
   in_iteration = false;
 }
 
-void ProfileInfo::readIterationFrequency() {
-#ifdef JETSON_EXECUTION
-  //----- frequency_current_iteration = readJetsonGPUFreq();
-  frequency_current_iteration = recordFreq();
-#else
-  frequency_current_iteration = 0;
-#endif // JETSON_EXECUTION
-}
-
-unsigned long ProfileInfo::getIterationFrequency() {
-  return frequency_current_iteration;
-}
 
 void ProfileInfo::addToCurrentIterationComputeTime(const char *s, double t) {
   start_iteration();
@@ -477,24 +385,6 @@ double RuntimeController::getCurrentIterationComputeEnergy() {
   return (PI ? PI->getCurrentIterationComputeEnergy() : 0.0);
 }
 
-void RuntimeController::readIterationFrequency() {
-  if (PI)
-    PI->readIterationFrequency();
-}
-
-unsigned long RuntimeController::getIterationFrequency() {
-  return (PI ? PI->getIterationFrequency() : 0);
-}
-
-void RuntimeController::updateFrequency() {
-#ifdef JETSON_EXECUTION
-  unsigned freq_idx = FIL->getNextIndex();
-  //--- updateJetsonGPUFreq(freq_idx);
-
-  setFreq(freq_idx);
-
-#endif // JETSON_EXECUTION
-}
 
 void RuntimeController::writeProfileInfo() {
   if (PI)
@@ -546,6 +436,9 @@ std::pair<double, double> RuntimeController::conv_profile(
 // Constructor and descructor
 RuntimeController::RuntimeController() {
   configurationIdx = 0;
+
+  // NOTE: The 14 Frequency levels are specific to NVIDIA Jetson Tx2
+  // More Frequency utils (not used by default) present in include/jetson_freq_utils.h 
   FIL = new FrequencyIndexList({13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0},
                                10);
   
@@ -1094,8 +987,9 @@ void RuntimeController::printConfigurations(
     std::vector<struct Configuration> &Confs) {
 
   for (std::vector<struct Configuration>::iterator it = Confs.begin(),
-                                                   ie = Confs.end();
+       ie = Confs.end();
        it != ie; ++it) {
+    
     it->print();
   }
 }
@@ -1104,15 +998,13 @@ void RuntimeController::printConfigurations(
     std::vector<struct Configuration *> &Confs) {
 
   for (std::vector<struct Configuration *>::iterator it = Confs.begin(),
-                                                     ie = Confs.end();
+       ie = Confs.end();
        it != ie; ++it) {
+    
     (*it)->print();
   }
 }
 
-unsigned long RuntimeController::getLastFrequency() { return g_freq; }
-
-void RuntimeController::setLastFrequency(unsigned long f) { g_freq = f; }
 
 double RuntimeController::getLastSpeedup() { return g_speedup; }
 
-- 
GitLab