Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Hexagon][Runtime] Add QuRT thread pool backend #11018

Merged
merged 31 commits into from
May 3, 2022
Merged
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
76877fe
Initial take on adding QuRT thread support to TVM's thread pool. WIP;…
supersat Apr 8, 2022
2605634
Allocate QuRT thread stacks automatically
supersat Apr 8, 2022
82bad79
Remove duplicate stack in QuRTThread
supersat Apr 8, 2022
cb07bd3
Add more logging to QuRTThread
supersat Apr 8, 2022
f360a9f
Use QuRT mutexes and condition variables
supersat Apr 8, 2022
ffeaa90
Get QuRT thread pools working perhaps
supersat Apr 12, 2022
bc0ef77
Sleep for a little bit to let race condition bugs shine through
supersat Apr 12, 2022
e1fd5ee
ayeee it works!
supersat Apr 13, 2022
5948675
Remove custom hexagon implementations of std::mutex and std::conditio…
supersat Apr 13, 2022
deebe25
threading_backend.cc code cleanup
supersat Apr 13, 2022
b1e9265
Formatting changes
supersat Apr 13, 2022
d27746b
remove hexagon debugging
supersat Apr 13, 2022
c9ce982
Initial take on adding QuRT thread support to TVM's thread pool. WIP;…
supersat Apr 8, 2022
39097ab
Allocate QuRT thread stacks automatically
supersat Apr 8, 2022
832c125
Remove duplicate stack in QuRTThread
supersat Apr 8, 2022
a80713f
Add more logging to QuRTThread
supersat Apr 8, 2022
f99d32b
Use QuRT mutexes and condition variables
supersat Apr 8, 2022
85bc9b4
Get QuRT thread pools working perhaps
supersat Apr 12, 2022
91d2b23
Sleep for a little bit to let race condition bugs shine through
supersat Apr 12, 2022
cb2104a
ayeee it works!
supersat Apr 13, 2022
0d20028
Remove custom hexagon implementations of std::mutex and std::conditio…
supersat Apr 13, 2022
a0bf101
threading_backend.cc code cleanup
supersat Apr 13, 2022
95e99d9
Formatting changes
supersat Apr 13, 2022
912ecc6
remove hexagon debugging
supersat Apr 13, 2022
15fafc0
Merge branch 'main' into qurt-thread-pool
supersat Apr 14, 2022
e4d2b14
Add hexagon thread pool test
supersat Apr 18, 2022
590c1b3
style fixes for tests/python/contrib/test_hexagon/test_thread_pool.py
supersat Apr 19, 2022
5576fd3
Fix some style issues
supersat Apr 19, 2022
69d586e
Merge branch 'qurt-thread-pool' of github.com:supersat/tvm into qurt-…
supersat Apr 19, 2022
53ef297
Address some reviewer comments
supersat Apr 21, 2022
24f14f7
Add QuRT thread pool backend
supersat Apr 29, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Initial take on adding QuRT thread support to TVM's thread pool. WIP;…
… crashes
  • Loading branch information
supersat committed Apr 13, 2022
commit c9ce982f02faf0e65f25dc771c8c4aa261ee6f48
46 changes: 46 additions & 0 deletions src/runtime/threading_backend.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,59 @@
#endif
#if defined(__hexagon__)
#include <dlfcn.h>
#include <qurt.h>
#define HEXAGON_STACK_SIZE 65536
#endif
#include <algorithm>
#include <thread>
#define CURRENT_THREAD_HANDLE (static_cast<std::thread::native_handle_type>(0))
namespace tvm {
namespace runtime {
namespace threading {
#if defined(__hexagon__)
class QuRTThread {
public:
//template<class Function, class... Args>
//explicit QuRTThread(Function&& f) {
QuRTThread(std::function<void(int)> worker_callback, int worker_id) :
f(worker_callback), i(worker_id) {
qurt_thread_attr_t attr;
qurt_thread_attr_init(&attr);
stack = malloc(HEXAGON_STACK_SIZE);
qurt_thread_attr_set_stack_size(&attr, HEXAGON_STACK_SIZE);
qurt_thread_attr_set_stack_addr(&attr, stack);
qurt_thread_create(&thread, &attr, (void (*)(void *))run_func, this);
}
~QuRTThread() {
free(stack);
supersat marked this conversation as resolved.
Show resolved Hide resolved
}
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(t->i);
}
qurt_thread_t thread;
void * stack;
std::function<void(int)> f;
int i;
};
#endif
thread_local int max_concurrency = 0;
class ThreadGroup::Impl {
public:
Impl(int num_workers, std::function<void(int)> worker_callback, bool exclude_worker0)
: num_workers_(num_workers) {
ICHECK_GE(num_workers, 1) << "Requested a non-positive number of worker threads.";
for (int i = exclude_worker0; i < num_workers_; ++i) {
#ifdef __hexagon__
threads_.emplace_back(QuRTThread(worker_callback, i));
#else
threads_.emplace_back([worker_callback, i] { worker_callback(i); });
#endif // __hexagon__
}
InitSortedOrder();
}
Expand Down Expand Up @@ -116,6 +154,7 @@ class ThreadGroup::Impl {
// if worker 0 is offloaded to main, i.e. exclude_worker0 is true,
// the main thread is bound to core 0.
void SetAffinity(bool exclude_worker0, AffinityMode mode) {
#ifndef __hexagon__
const char* val = getenv("TVM_BIND_THREADS");
if (val != nullptr && atoi(val) != 1) {
return;
Expand Down Expand Up @@ -172,6 +211,7 @@ class ThreadGroup::Impl {
SetMasterThreadFullCpuAffinity(mode);
}
}
#endif // __hexagon__
}

void SetThreadFullCpuAffinity(std::thread::native_handle_type thread, AffinityMode mode) {
Expand All @@ -185,6 +225,7 @@ class ThreadGroup::Impl {
// Note: this works well on x86 too. Because x86 doesn't have BIG.LITTLE,
// our implementation will use kBig mode by default and will let main thread
// run on intended cores.
#ifndef __hexagon__
std::vector<unsigned> ids;
switch (mode) {
case kSpecifyOneCorePerThread:
Expand All @@ -206,6 +247,7 @@ class ThreadGroup::Impl {
break;
}
SetThreadAffinity(thread, ids);
#endif // __hexagon__
}

void SetMasterThreadFullCpuAffinity(AffinityMode mode) {
Expand Down Expand Up @@ -259,7 +301,11 @@ class ThreadGroup::Impl {
}

int num_workers_;
#if defined(__hexagon__)
std::vector<QuRTThread> threads_;
#else
std::vector<std::thread> threads_;
#endif
std::vector<unsigned int> sorted_order_;
int big_count_ = 0;
int little_count_ = 0;
Expand Down