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] Add HexagonThreadManager #11653

Merged
merged 46 commits into from
Jun 13, 2022
Merged

Conversation

adstraw
Copy link
Contributor

@adstraw adstraw commented Jun 9, 2022

This PR was made in collaboration with @JosephTheOctonaut.

Add HexagonThreadManager class and unit testing. HexagonThreadManager can be used to spawn a given number of QURT threads on which the user can Dispatch work including basic Signal and Wait functionality to create synchronization points between threads. This PR adds the low-level HexagonThreadManager class only. Related Hexagon Device API support and codegen to come in future PRs.

This PR is dependent on #11635 to pass CI.

cc @mehrdadh

JosephTheOctonaut and others added 30 commits June 9, 2022 10:28
- Updated HexagonThreadManager interface to use TVMStreams
- Added second `Dispatch` interfce in thread manager to use PackedFuncs
- Updated thread manager to use vector for dynamic semaphore allocation
- Added "#if defined(__hexagon__)" in several places to prevent compilation errors
 - Fixed GetStreams not returning the streams properly
 - Added missing semaphore cleanup to prevent qurt kernel resource leakage
 - new interface functions:
   - Start() : now all worker threads are blocked on initialization until ThreadManager->Start() is called
   - WaitOnThreads() : blocking call which waits until all worker thread queues are empty
 - added extra debug logging
 - Two new basic thread tests working
… capacity adjustment invaliding in-use addresses).
Copy link
Contributor

@kparzysz-quic kparzysz-quic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you move the buffer manager changes to a separate PR? They seem independent from the thread manager.

src/runtime/hexagon/hexagon_buffer_manager.h Outdated Show resolved Hide resolved
src/runtime/hexagon/hexagon_buffer_manager.h Outdated Show resolved Hide resolved
src/runtime/hexagon/hexagon_device_api.cc Outdated Show resolved Hide resolved
@kparzysz-quic
Copy link
Contributor

On another note---all the thread manager files spell "threadmanager" without the underscore, while "buffer manager" has one. Could you add the underscore for consistency?

@github-actions github-actions bot requested a review from mehrdadh June 9, 2022 23:38
@adstraw
Copy link
Contributor Author

adstraw commented Jun 9, 2022

Could you move the buffer manager changes to a separate PR? They seem independent from the thread manager.

I am basically moving this functionality from within the Hexagon Device API where it lived previously to a separate header file. Both the Hexagon Device API and the Hexagon Thread Manager need a Hexagon Buffer Manager. The alternative would be two duplicate code in two places.

Copy link
Contributor

@kparzysz-quic kparzysz-quic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My general feedback would be mostly about coding style. This patch doesn't conform to the style used in TVM (for example, private class member names should end with _, etc.). Besides that:

  • Almost any pointer can be implicitly converted to void*, so most (if not all) of the reinterpret_cast<void*>(...) are unnecessary.
  • Please use nullptr instead of reinterpret_cast<...>(0).
  • Please avoid C-style casts, i.e. (type)value. In particular, please replace (TVMStreamHandle)i with static_cast<TVMStreamHandle>(i).
  • In several cases, there is code that constructs a message for DBG/DLOG that will execute unconditionally, even if DLOG is disabled. Even though the constructed string is not used, the code creating it will most likely not be removed (because it can contain library calls, etc).

src/runtime/hexagon/hexagon_thread_manager.h Outdated Show resolved Hide resolved
src/runtime/hexagon/hexagon_thread_manager.h Outdated Show resolved Hide resolved
Copy link
Contributor

@kparzysz-quic kparzysz-quic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The (TVMStreamHandle)i is still there. It's not that much of a big deal, but it would be nice to replace it with static_cast eventually.

@kparzysz-quic
Copy link
Contributor

Thanks Adam!

@adstraw
Copy link
Contributor Author

adstraw commented Jun 10, 2022

The (TVMStreamHandle)i is still there. It's not that much of a big deal, but it would be nice to replace it with static_cast eventually.

Missed this. Will fix. [Edit] We need reinterpret_cast to cast between unsigned and pointer type.
[Edit] Also missed the comment about member_variables_. Will fix that too.

@adstraw
Copy link
Contributor Author

adstraw commented Jun 10, 2022

Just realized that I named my commit static cast for TVMStreamHandle before I realized that reinterpret_cast was required. I can rename that commit or ...

Just thinking out loud here ... the other thing we can do RE TVMStreamHandle is just remove it from this PR. It's there because we are looking ahead to working with actual stream handles at the Device API level. But, what we really need is just an unsigned thread ID. What's there is a bit of a misuse of a pointer type to stash an integer and probably over-engineered at least for the time being.

@kparzysz-quic
Copy link
Contributor

I wouldn't worry about renaming a commit in the PR. The code looks fine. For some reason I thought that the stream handle was actually an integer...
When the CI passes, I can change the entry in the commit message to read "reinterpret" instead of "static" right before committing.

@adstraw
Copy link
Contributor Author

adstraw commented Jun 10, 2022

Just a reminder that CI is failing because it requires this PR #11635

@kparzysz-quic kparzysz-quic merged commit 76b9ce9 into apache:main Jun 13, 2022
@adstraw adstraw deleted the hex-thread-mgr branch June 13, 2022 17:51
juda pushed a commit to juda/tvm that referenced this pull request Jun 21, 2022
* Adding initial threadmanager class

* Fixed compile errors

* Moving constant defines inside class

* Updating qurt includes

* use default scope for hexagon buffers

* Updating buffer allocations

* Fixed bug where array of pointers treated as array of structs

* - Updated HexgonDeviceAPI to use HexagonThreadManager
- Updated HexagonThreadManager interface to use TVMStreams
- Added second `Dispatch` interfce in thread manager to use PackedFuncs
- Updated thread manager to use vector for dynamic semaphore allocation
- Added "#if defined(__hexagon__)" in several places to prevent compilation errors

* Bug fixes + interface addition + basic thread tests
 - Fixed GetStreams not returning the streams properly
 - Added missing semaphore cleanup to prevent qurt kernel resource leakage
 - new interface functions:
   - Start() : now all worker threads are blocked on initialization until ThreadManager->Start() is called
   - WaitOnThreads() : blocking call which waits until all worker thread queues are empty
 - added extra debug logging
 - Two new basic thread tests working

* Adding initial ThreadManager tests

* HexagonThreadManager tests and refactor

* remove stack / pipe size member vars

* init pointers in the header file

* move all mem allocs to SpawnThreads

* start_semaphore as object instead of pointer

* fix bug with WaitOnThreads deadlock + Wait/Signal off by one error

* add / refactor Signal / Wait tests

* add SyncFromTo test cases

* add Dispatch test cases

* add pipe fill and overflow cases

* Updating dispatch to return bool and fix pipe overflow problem

* change around min / max values for stack / pipe

* integrate pipe fill / overflow tests back into HTM test suite

* use HexagonBuffer

* assert if stack / pipe sizes fall below min

* Changed semaphore vector to store pointers, not structs (fixes vector capacity adjustment invaliding in-use addresses).

* add producer consumer, thread order test cases

* change to unordered_map for semaphores and remove PreallocateSyncs

* tests running on device

* code cleanup for compile warnings

* remove #if defined(__hexagon__) guards

* copyright, format, lint

* add hexagon buffer map class

* remove redundant thread manager tests

* revert Hex Dev API changes for threading

* add comments; remove untested code to dispatch / wrap a packed func

* pass pipe address and not HTM pointer to thread context

* rename to HexagonBufferManager

* cleanup ahead of PR

* use DLOG(INFO)

* refactor GetStreamHandles to return a vector by value

* adjust HexagonBufferManager methods; use thread_manager file names

* style guidelines and debug prints

* reinterpret cast for TVMStreamHandle

* end member variables with underscore

Co-authored-by: Joseph McMahan <jmcmahan@octoml.ai>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants