You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tvm.apache.org by GitBox <gi...@apache.org> on 2022/04/19 03:42:28 UTC

[GitHub] [tvm] huajsj commented on a diff in pull request #11018: [Hexagon][Runtime] Add QuRT thread pool backend

huajsj commented on code in PR #11018:
URL: https://github.com/apache/tvm/pull/11018#discussion_r852556302


##########
src/runtime/threading_backend.cc:
##########
@@ -34,13 +34,61 @@
 #endif
 #if defined(__hexagon__)
 #include <dlfcn.h>
+#include <qurt.h>
+#include <stdlib.h>
+#define HEXAGON_STACK_SIZE 65536
+#define HEXAGON_STACK_ALIGNMENT 32
 #endif
 #include <algorithm>
 #include <thread>
 #define CURRENT_THREAD_HANDLE (static_cast<std::thread::native_handle_type>(0))
 namespace tvm {
 namespace runtime {
 namespace threading {
+#ifdef __hexagon__
+// pthreads are broken on older versions of qurt, so
+// we need to use native APIs instead of std::threads
+class QuRTThread {
+  typedef std::function<void()> Callback;
+
+ public:
+  explicit QuRTThread(Callback worker_callback) : f(worker_callback) {
+    static int id = 1;
+    qurt_thread_attr_t attr;
+    char name[32];
+    posix_memalign(&stack, HEXAGON_STACK_ALIGNMENT, HEXAGON_STACK_SIZE);
+    qurt_thread_attr_init(&attr);
+    qurt_thread_attr_set_stack_size(&attr, HEXAGON_STACK_SIZE);
+    qurt_thread_attr_set_stack_addr(&attr, stack);
+    snprintf(name, sizeof(name), "worker %d", id++);
+    qurt_thread_attr_set_name(&attr, name);
+    qurt_thread_create(&thread, &attr, (void (*)(void*))run_func, this);
+  }
+  QuRTThread(QuRTThread&& other) : thread(other.thread), f(other.f), stack(other.stack) {
+    other.thread = 0;
+  }
+  ~QuRTThread() {
+    if (thread) {
+      join();
+      free(stack);
+    }
+  }
+  bool joinable() const { return qurt_thread_get_id() != thread; }
+  void join() {
+    int status;
+    qurt_thread_join(thread, &status);
+  }
+
+ private:
+  static void run_func(QuRTThread* t) {
+    t->f();
+    qurt_thread_exit(QURT_EOK);
+  }
+  qurt_thread_t thread;
+  Callback f;
+  void* stack;

Review Comment:
   qurt_thread_t thread_;
   Callback f_;
   void* stack_;



##########
src/runtime/threading_backend.cc:
##########
@@ -34,13 +34,61 @@
 #endif
 #if defined(__hexagon__)
 #include <dlfcn.h>
+#include <qurt.h>
+#include <stdlib.h>
+#define HEXAGON_STACK_SIZE 65536
+#define HEXAGON_STACK_ALIGNMENT 32
 #endif
 #include <algorithm>
 #include <thread>
 #define CURRENT_THREAD_HANDLE (static_cast<std::thread::native_handle_type>(0))
 namespace tvm {
 namespace runtime {
 namespace threading {
+#ifdef __hexagon__
+// pthreads are broken on older versions of qurt, so
+// we need to use native APIs instead of std::threads
+class QuRTThread {
+  typedef std::function<void()> Callback;
+
+ public:
+  explicit QuRTThread(Callback worker_callback) : f(worker_callback) {
+    static int id = 1;
+    qurt_thread_attr_t attr;
+    char name[32];
+    posix_memalign(&stack, HEXAGON_STACK_ALIGNMENT, HEXAGON_STACK_SIZE);
+    qurt_thread_attr_init(&attr);
+    qurt_thread_attr_set_stack_size(&attr, HEXAGON_STACK_SIZE);
+    qurt_thread_attr_set_stack_addr(&attr, stack);
+    snprintf(name, sizeof(name), "worker %d", id++);
+    qurt_thread_attr_set_name(&attr, name);
+    qurt_thread_create(&thread, &attr, (void (*)(void*))run_func, this);
+  }
+  QuRTThread(QuRTThread&& other) : thread(other.thread), f(other.f), stack(other.stack) {
+    other.thread = 0;
+  }
+  ~QuRTThread() {
+    if (thread) {
+      join();
+      free(stack);
+    }
+  }
+  bool joinable() const { return qurt_thread_get_id() != thread; }

Review Comment:
   function name convention should use google c/c++ style.



##########
src/runtime/threading_backend.cc:
##########
@@ -34,13 +34,61 @@
 #endif
 #if defined(__hexagon__)
 #include <dlfcn.h>
+#include <qurt.h>
+#include <stdlib.h>
+#define HEXAGON_STACK_SIZE 65536
+#define HEXAGON_STACK_ALIGNMENT 32
 #endif
 #include <algorithm>
 #include <thread>
 #define CURRENT_THREAD_HANDLE (static_cast<std::thread::native_handle_type>(0))
 namespace tvm {
 namespace runtime {
 namespace threading {
+#ifdef __hexagon__
+// pthreads are broken on older versions of qurt, so
+// we need to use native APIs instead of std::threads
+class QuRTThread {
+  typedef std::function<void()> Callback;
+
+ public:
+  explicit QuRTThread(Callback worker_callback) : f(worker_callback) {
+    static int id = 1;
+    qurt_thread_attr_t attr;
+    char name[32];
+    posix_memalign(&stack, HEXAGON_STACK_ALIGNMENT, HEXAGON_STACK_SIZE);
+    qurt_thread_attr_init(&attr);
+    qurt_thread_attr_set_stack_size(&attr, HEXAGON_STACK_SIZE);
+    qurt_thread_attr_set_stack_addr(&attr, stack);
+    snprintf(name, sizeof(name), "worker %d", id++);
+    qurt_thread_attr_set_name(&attr, name);
+    qurt_thread_create(&thread, &attr, (void (*)(void*))run_func, this);
+  }
+  QuRTThread(QuRTThread&& other) : thread(other.thread), f(other.f), stack(other.stack) {
+    other.thread = 0;
+  }
+  ~QuRTThread() {
+    if (thread) {
+      join();
+      free(stack);
+    }
+  }
+  bool joinable() const { return qurt_thread_get_id() != thread; }
+  void join() {
+    int status;
+    qurt_thread_join(thread, &status);
+  }
+
+ private:
+  static void run_func(QuRTThread* t) {

Review Comment:
   RunFunction()



##########
src/runtime/threading_backend.cc:
##########
@@ -34,13 +34,61 @@
 #endif
 #if defined(__hexagon__)
 #include <dlfcn.h>
+#include <qurt.h>
+#include <stdlib.h>
+#define HEXAGON_STACK_SIZE 65536
+#define HEXAGON_STACK_ALIGNMENT 32
 #endif
 #include <algorithm>
 #include <thread>
 #define CURRENT_THREAD_HANDLE (static_cast<std::thread::native_handle_type>(0))
 namespace tvm {
 namespace runtime {
 namespace threading {
+#ifdef __hexagon__
+// pthreads are broken on older versions of qurt, so
+// we need to use native APIs instead of std::threads
+class QuRTThread {
+  typedef std::function<void()> Callback;
+
+ public:
+  explicit QuRTThread(Callback worker_callback) : f(worker_callback) {
+    static int id = 1;
+    qurt_thread_attr_t attr;
+    char name[32];
+    posix_memalign(&stack, HEXAGON_STACK_ALIGNMENT, HEXAGON_STACK_SIZE);
+    qurt_thread_attr_init(&attr);
+    qurt_thread_attr_set_stack_size(&attr, HEXAGON_STACK_SIZE);
+    qurt_thread_attr_set_stack_addr(&attr, stack);
+    snprintf(name, sizeof(name), "worker %d", id++);
+    qurt_thread_attr_set_name(&attr, name);
+    qurt_thread_create(&thread, &attr, (void (*)(void*))run_func, this);
+  }
+  QuRTThread(QuRTThread&& other) : thread(other.thread), f(other.f), stack(other.stack) {
+    other.thread = 0;
+  }
+  ~QuRTThread() {
+    if (thread) {
+      join();
+      free(stack);

Review Comment:
   check value of 'stack' before doing free



##########
src/runtime/threading_backend.cc:
##########
@@ -34,13 +34,61 @@
 #endif
 #if defined(__hexagon__)
 #include <dlfcn.h>
+#include <qurt.h>
+#include <stdlib.h>
+#define HEXAGON_STACK_SIZE 65536
+#define HEXAGON_STACK_ALIGNMENT 32
 #endif
 #include <algorithm>
 #include <thread>
 #define CURRENT_THREAD_HANDLE (static_cast<std::thread::native_handle_type>(0))
 namespace tvm {
 namespace runtime {
 namespace threading {
+#ifdef __hexagon__
+// pthreads are broken on older versions of qurt, so
+// we need to use native APIs instead of std::threads
+class QuRTThread {
+  typedef std::function<void()> Callback;
+
+ public:
+  explicit QuRTThread(Callback worker_callback) : f(worker_callback) {
+    static int id = 1;
+    qurt_thread_attr_t attr;
+    char name[32];
+    posix_memalign(&stack, HEXAGON_STACK_ALIGNMENT, HEXAGON_STACK_SIZE);
+    qurt_thread_attr_init(&attr);
+    qurt_thread_attr_set_stack_size(&attr, HEXAGON_STACK_SIZE);
+    qurt_thread_attr_set_stack_addr(&attr, stack);
+    snprintf(name, sizeof(name), "worker %d", id++);
+    qurt_thread_attr_set_name(&attr, name);
+    qurt_thread_create(&thread, &attr, (void (*)(void*))run_func, this);
+  }
+  QuRTThread(QuRTThread&& other) : thread(other.thread), f(other.f), stack(other.stack) {
+    other.thread = 0;
+  }
+  ~QuRTThread() {
+    if (thread) {
+      join();
+      free(stack);
+    }
+  }
+  bool joinable() const { return qurt_thread_get_id() != thread; }
+  void join() {
+    int status;
+    qurt_thread_join(thread, &status);

Review Comment:
   check 'qurt_thread_join' return value?



##########
src/runtime/threading_backend.cc:
##########
@@ -276,7 +332,14 @@ int ThreadGroup::Configure(AffinityMode mode, int nthreads, bool exclude_worker0
   return impl_->Configure(mode, nthreads, exclude_worker0, cpus);
 }
 
-void Yield() { std::this_thread::yield(); }
+void Yield() {
+#ifdef __hexagon__
+  qurt_sleep(1);

Review Comment:
   How to determine the 1 as the sleep number? any todo or comments for explain?



##########
src/runtime/threading_backend.cc:
##########
@@ -34,13 +34,61 @@
 #endif
 #if defined(__hexagon__)
 #include <dlfcn.h>
+#include <qurt.h>
+#include <stdlib.h>
+#define HEXAGON_STACK_SIZE 65536
+#define HEXAGON_STACK_ALIGNMENT 32
 #endif
 #include <algorithm>
 #include <thread>
 #define CURRENT_THREAD_HANDLE (static_cast<std::thread::native_handle_type>(0))
 namespace tvm {
 namespace runtime {
 namespace threading {
+#ifdef __hexagon__
+// pthreads are broken on older versions of qurt, so
+// we need to use native APIs instead of std::threads
+class QuRTThread {
+  typedef std::function<void()> Callback;
+
+ public:
+  explicit QuRTThread(Callback worker_callback) : f(worker_callback) {
+    static int id = 1;
+    qurt_thread_attr_t attr;
+    char name[32];
+    posix_memalign(&stack, HEXAGON_STACK_ALIGNMENT, HEXAGON_STACK_SIZE);
+    qurt_thread_attr_init(&attr);
+    qurt_thread_attr_set_stack_size(&attr, HEXAGON_STACK_SIZE);
+    qurt_thread_attr_set_stack_addr(&attr, stack);
+    snprintf(name, sizeof(name), "worker %d", id++);
+    qurt_thread_attr_set_name(&attr, name);
+    qurt_thread_create(&thread, &attr, (void (*)(void*))run_func, this);
+  }
+  QuRTThread(QuRTThread&& other) : thread(other.thread), f(other.f), stack(other.stack) {
+    other.thread = 0;
+  }
+  ~QuRTThread() {
+    if (thread) {
+      join();
+      free(stack);
+    }
+  }
+  bool joinable() const { return qurt_thread_get_id() != thread; }
+  void join() {
+    int status;
+    qurt_thread_join(thread, &status);
+  }
+
+ private:
+  static void run_func(QuRTThread* t) {
+    t->f();
+    qurt_thread_exit(QURT_EOK);
+  }
+  qurt_thread_t thread;
+  Callback f;
+  void* stack;

Review Comment:
   stack_ = nullptr



##########
src/runtime/threading_backend.cc:
##########
@@ -34,13 +34,61 @@
 #endif
 #if defined(__hexagon__)
 #include <dlfcn.h>
+#include <qurt.h>
+#include <stdlib.h>
+#define HEXAGON_STACK_SIZE 65536
+#define HEXAGON_STACK_ALIGNMENT 32
 #endif
 #include <algorithm>
 #include <thread>
 #define CURRENT_THREAD_HANDLE (static_cast<std::thread::native_handle_type>(0))
 namespace tvm {
 namespace runtime {
 namespace threading {
+#ifdef __hexagon__
+// pthreads are broken on older versions of qurt, so
+// we need to use native APIs instead of std::threads
+class QuRTThread {
+  typedef std::function<void()> Callback;
+
+ public:
+  explicit QuRTThread(Callback worker_callback) : f(worker_callback) {
+    static int id = 1;
+    qurt_thread_attr_t attr;
+    char name[32];
+    posix_memalign(&stack, HEXAGON_STACK_ALIGNMENT, HEXAGON_STACK_SIZE);

Review Comment:
   check return value.



##########
src/runtime/threading_backend.cc:
##########
@@ -34,13 +34,61 @@
 #endif
 #if defined(__hexagon__)
 #include <dlfcn.h>
+#include <qurt.h>
+#include <stdlib.h>
+#define HEXAGON_STACK_SIZE 65536
+#define HEXAGON_STACK_ALIGNMENT 32
 #endif
 #include <algorithm>
 #include <thread>
 #define CURRENT_THREAD_HANDLE (static_cast<std::thread::native_handle_type>(0))
 namespace tvm {
 namespace runtime {
 namespace threading {
+#ifdef __hexagon__
+// pthreads are broken on older versions of qurt, so
+// we need to use native APIs instead of std::threads
+class QuRTThread {
+  typedef std::function<void()> Callback;
+
+ public:
+  explicit QuRTThread(Callback worker_callback) : f(worker_callback) {
+    static int id = 1;
+    qurt_thread_attr_t attr;
+    char name[32];
+    posix_memalign(&stack, HEXAGON_STACK_ALIGNMENT, HEXAGON_STACK_SIZE);
+    qurt_thread_attr_init(&attr);
+    qurt_thread_attr_set_stack_size(&attr, HEXAGON_STACK_SIZE);
+    qurt_thread_attr_set_stack_addr(&attr, stack);
+    snprintf(name, sizeof(name), "worker %d", id++);
+    qurt_thread_attr_set_name(&attr, name);
+    qurt_thread_create(&thread, &attr, (void (*)(void*))run_func, this);
+  }
+  QuRTThread(QuRTThread&& other) : thread(other.thread), f(other.f), stack(other.stack) {
+    other.thread = 0;
+  }
+  ~QuRTThread() {
+    if (thread) {
+      join();
+      free(stack);
+    }
+  }
+  bool joinable() const { return qurt_thread_get_id() != thread; }
+  void join() {

Review Comment:
   Join()



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org