From 93ec933222a6a1329f253ec6f5733bbb2fff86f2 Mon Sep 17 00:00:00 2001
From: Yifan Zhao <yifanz16@illinois.edu>
Date: Thu, 13 Feb 2020 21:15:39 -0600
Subject: [PATCH] Fixed linker errors

---
 llvm/projects/hpvm-tensor-rt/CMakeLists.txt   |  1 +
 .../include/hpvm-rt-controller.h              | 22 +------
 .../include/rt-controller-api.h               |  5 +-
 .../tensor_runtime/src/hpvm-rt-controller.cpp | 58 ++++++++++++++++---
 .../tensor_runtime/src/img_tensor_runtime.cu  |  5 --
 .../tensor_runtime/src/tensor_runtime.cu      |  5 --
 6 files changed, 54 insertions(+), 42 deletions(-)

diff --git a/llvm/projects/hpvm-tensor-rt/CMakeLists.txt b/llvm/projects/hpvm-tensor-rt/CMakeLists.txt
index 74ec43d032..45fa4d61ef 100644
--- a/llvm/projects/hpvm-tensor-rt/CMakeLists.txt
+++ b/llvm/projects/hpvm-tensor-rt/CMakeLists.txt
@@ -54,6 +54,7 @@ set(
   common.cpp
   configuration.cpp
   debug.cc
+  debug.cpp
   device_math.cu
   error.cu
   fp16_gemm.cu
diff --git a/llvm/projects/hpvm-tensor-rt/tensor_runtime/include/hpvm-rt-controller.h b/llvm/projects/hpvm-tensor-rt/tensor_runtime/include/hpvm-rt-controller.h
index 4e3542fdfa..2bb43fdde2 100644
--- a/llvm/projects/hpvm-tensor-rt/tensor_runtime/include/hpvm-rt-controller.h
+++ b/llvm/projects/hpvm-tensor-rt/tensor_runtime/include/hpvm-rt-controller.h
@@ -126,7 +126,7 @@ class Slowdowns {
 
 class RuntimeController;
 
-RuntimeController *RC;
+extern RuntimeController *RC;
 
 class RuntimeController {
   private:
@@ -264,31 +264,11 @@ class RuntimeController {
 #define AL_THRESHOLD 0.01
 #define MAX_GOAL_SPEEDUP 9
 
-// Functions to be inserted with initializeTensorRT and clearTensorRT
-void llvm_hpvm_initializeRuntimeController(const char *ConfigFile, const char *QRangeFile);
-
-void llvm_hpvm_clearRuntimeController();
-
 //*** Methods to compute accuracy of a tensor by the runtime controller   ***//
-extern uint32_t* labels_from_file;
 
 uint32_t* hpvm_rt_readLabelsBatch_cached(const char* labels_file, int start, int end);
 
 //*** Copied from dnn_sources/include/utils.h                             ***//
 float hpvm_rt_computeAccuracy3(uint32_t* labels, void* result_ptr);
 
-#define llvm_hpvm_invokeRtControl_SLOWDOWN_PR llvm_hpvm_invokeRtControl
-
-void llvm_hpvm_invokeRtControl_BASE(void* result, const char* str, int start, int end);
-
-void llvm_hpvm_invokeRtControl_ITERATE(void* result, const char* str, int start, int end);
-
-void llvm_hpvm_invokeRtControl_ADJUST(void* result, const char* str, int start, int end);
-
-void llvm_hpvm_invokeRtControl_SLOWDOWN(void* result, const char* str, int start, int end);
-
-void llvm_hpvm_invokeRtControl_SLOWDOWN_PR(void* result, const char* str, int start, int end);
-
-void llvm_hpvm_invokeRtControl_RAND(void* result, const char* str, int start, int end);
-
 #endif
diff --git a/llvm/projects/hpvm-tensor-rt/tensor_runtime/include/rt-controller-api.h b/llvm/projects/hpvm-tensor-rt/tensor_runtime/include/rt-controller-api.h
index 071ea27746..6dd06cb10a 100644
--- a/llvm/projects/hpvm-tensor-rt/tensor_runtime/include/rt-controller-api.h
+++ b/llvm/projects/hpvm-tensor-rt/tensor_runtime/include/rt-controller-api.h
@@ -1,9 +1,8 @@
 extern "C" {
-
+// Functions to be inserted with initializeTensorRT and clearTensorRT
 void llvm_hpvm_initializeRuntimeController(const char *, const char *);
 void llvm_hpvm_clearRuntimeController();
 void llvm_hpvm_invokeRtControl(
     void *result, const char *str, int start, int end);
-void llvm_hpvm_imgInvokeRtControl(
-    void* result, void *gold, int start, int end);
+void llvm_hpvm_imgInvokeRtControl(void *result, void *gold, int start, int end);
 }
diff --git a/llvm/projects/hpvm-tensor-rt/tensor_runtime/src/hpvm-rt-controller.cpp b/llvm/projects/hpvm-tensor-rt/tensor_runtime/src/hpvm-rt-controller.cpp
index 2730e2f278..fb116d5164 100644
--- a/llvm/projects/hpvm-tensor-rt/tensor_runtime/src/hpvm-rt-controller.cpp
+++ b/llvm/projects/hpvm-tensor-rt/tensor_runtime/src/hpvm-rt-controller.cpp
@@ -1,4 +1,5 @@
 #include "hpvm-rt-controller.h"
+#include "img_tensor_utils.h"
 
 /*
  * Check if a file exists
@@ -204,6 +205,8 @@ float Slowdowns::getNextSlowdown() {
   return tmp;
 }
 
+RuntimeController *RC;
+
 // Functions
 
 // Private functions of profiler
@@ -1057,14 +1060,14 @@ double RuntimeController::getBaselineTime() { return baseline_time; }
 Slowdowns *RuntimeController::getSlowdowns() { return slowdowns; }
 
 // Functions to be inserted with initializeTensorRT and clearTensorRT
-void llvm_hpvm_initializeRuntimeController(
+extern "C" void llvm_hpvm_initializeRuntimeController(
     const char *ConfigFile, const char *QRangeFile) {
   RC = new RuntimeController();
   RC->init(ConfigFile, QRangeFile);
   return;
 }
 
-void llvm_hpvm_clearRuntimeController() {
+extern "C" void llvm_hpvm_clearRuntimeController() {
   delete RC;
   return;
 }
@@ -1157,7 +1160,11 @@ float hpvm_rt_computeAccuracy3(uint32_t *labels, void *result_ptr) {
 
   return accuracy;
 }
-void llvm_hpvm_invokeRtControl_BASE(
+
+
+#define llvm_hpvm_invokeRtControl_SLOWDOWN_PR llvm_hpvm_invokeRtControl
+
+extern "C" void llvm_hpvm_invokeRtControl_BASE(
     void *result, const char *str, int start, int end) {
 
   RC->resume_profiler();
@@ -1183,7 +1190,7 @@ void llvm_hpvm_invokeRtControl_BASE(
   RC->end_iteration();
 }
 
-void llvm_hpvm_invokeRtControl_ITERATE(
+extern "C" void llvm_hpvm_invokeRtControl_ITERATE(
     void *result, const char *str, int start, int end) {
 
   uint32_t *labels_cached = hpvm_rt_readLabelsBatch_cached(str, start, end);
@@ -1216,7 +1223,7 @@ void llvm_hpvm_invokeRtControl_ITERATE(
   RC->end_iteration();
 }
 
-void llvm_hpvm_invokeRtControl_ADJUST(
+extern "C" void llvm_hpvm_invokeRtControl_ADJUST(
     void *result, const char *str, int start, int end) {
 
   uint32_t *labels_cached = hpvm_rt_readLabelsBatch_cached(str, start, end);
@@ -1246,7 +1253,7 @@ void llvm_hpvm_invokeRtControl_ADJUST(
   RC->end_iteration();
 }
 
-void llvm_hpvm_invokeRtControl_SLOWDOWN(
+extern "C" void llvm_hpvm_invokeRtControl_SLOWDOWN(
     void *result, const char *str, int start, int end) {
 
   uint32_t *labels_cached = hpvm_rt_readLabelsBatch_cached(str, start, end);
@@ -1287,7 +1294,7 @@ void llvm_hpvm_invokeRtControl_SLOWDOWN(
   RC->end_iteration();
 }
 
-void llvm_hpvm_invokeRtControl_SLOWDOWN_PR(
+extern "C" void llvm_hpvm_invokeRtControl_SLOWDOWN_PR(
     void *result, const char *str, int start, int end) {
 
   uint32_t *labels_cached = hpvm_rt_readLabelsBatch_cached(str, start, end);
@@ -1329,7 +1336,7 @@ void llvm_hpvm_invokeRtControl_SLOWDOWN_PR(
   RC->end_iteration();
 }
 
-void llvm_hpvm_invokeRtControl_RAND(
+extern "C" void llvm_hpvm_invokeRtControl_RAND(
     void *result, const char *str, int start, int end) {
 
   uint32_t *labels_cached = hpvm_rt_readLabelsBatch_cached(str, start, end);
@@ -1355,3 +1362,38 @@ void llvm_hpvm_invokeRtControl_RAND(
   // Note the end of iteration
   RC->end_iteration();
 }
+
+template <typename T>
+static void writeVectorToFile(const char *path, const std::vector<T> &vec) {
+  std::ofstream of(path, std::ofstream::out | std::ofstream::app);
+  if (!of.good())
+    ERROR("Cannot write to %s file", path);
+  for (float f: vec)
+    of << f << ' ';
+  of << '\n';
+}
+
+extern "C" void llvm_hpvm_imgInvokeRtControl(void* result, void *gold, int start, int end) {
+  RC->resume_profiler();
+
+  if (gold != nullptr) {
+    writeVectorToFile("psnr.txt", PSNR(gold, result));
+    writeVectorToFile("ssim.txt", SSIM(gold, result));
+  }
+
+  // Read stats for iteration that was just completed
+  double current_iteration_time = RC->getCurrentIterationComputeTime();
+  double current_iteration_energy = RC->getCurrentIterationComputeEnergy();
+
+  RC->pause_profiler();
+  std::pair<double, double> pinfo = RC->get_time_energy();
+  RC->reset_profiler();
+  RC->addToCurrentIterationControlTime(pinfo.first);
+  RC->addToCurrentIterationControlEnergy(pinfo.second);
+
+  INFO("current iteration time = %f, current iteration energy = %f\n\n",
+       current_iteration_time, current_iteration_energy);
+
+  // Note the end of iteration
+  RC->end_iteration();
+}
diff --git a/llvm/projects/hpvm-tensor-rt/tensor_runtime/src/img_tensor_runtime.cu b/llvm/projects/hpvm-tensor-rt/tensor_runtime/src/img_tensor_runtime.cu
index 7f10ee0acd..2db6a9125a 100644
--- a/llvm/projects/hpvm-tensor-rt/tensor_runtime/src/img_tensor_runtime.cu
+++ b/llvm/projects/hpvm-tensor-rt/tensor_runtime/src/img_tensor_runtime.cu
@@ -9,11 +9,6 @@
 #include <cufft.h>
 #include <cufftXt.h>
 #include <thrust/device_vector.h>
-// FIXME: really just a hack to compile into a single .o
-#include "common.cpp"
-#include "debug.cpp"
-#include "device_math.cu"
-#include "img_tensor_utils.cpp"
 
 template <typename T> struct DivFunctor {
   const T dividend;
diff --git a/llvm/projects/hpvm-tensor-rt/tensor_runtime/src/tensor_runtime.cu b/llvm/projects/hpvm-tensor-rt/tensor_runtime/src/tensor_runtime.cu
index 6e97043591..442e0b802a 100644
--- a/llvm/projects/hpvm-tensor-rt/tensor_runtime/src/tensor_runtime.cu
+++ b/llvm/projects/hpvm-tensor-rt/tensor_runtime/src/tensor_runtime.cu
@@ -44,11 +44,6 @@
 #include "op_overheads.h"
 #include "half_precision_api.h"
 
-
-
-// Image tensor runtime implementation
-#include "img_tensor_runtime.cu"
-
 //** Potential Improvements:
 //   1) Add support for dataypes beyond floats and half 
 //   2) Support for more CUDNN operations
-- 
GitLab