-
Notifications
You must be signed in to change notification settings - Fork 116
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
[umf] optimize bucket selection for allocation sizes #994
Conversation
This affects SYCL, so please follow the process outlined in the contributing guide. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hm, can you please do a micro-benchmark to see which implementation is better? My initial thinking was something like 0f296e7 - that's the common approach to this problem.
@@ -902,6 +904,10 @@ umf_result_t DisjointPool::initialize(umf_memory_provider_handle_t *providers, | |||
!((parameters.MinBucketSize & (parameters.MinBucketSize - 1)) == 0)) { | |||
return UMF_RESULT_ERROR_INVALID_ARGUMENT; | |||
} | |||
// MinBucketSize parameter can't be larger than CutOff. | |||
if (parameters.MinBucketSize > CutOff) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is CutOff exposed somehow to the user? If not, how will user know what is the max size he can set?
If this is not exposed, I think you should just set MinBucketSize to CutOff if it's bigger.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
You could also try moving the |
a9b326f
to
5ab7dc0
Compare
Codecov ReportAll modified and coverable lines are covered by tests ✅
❗ Your organization needs to install the Codecov GitHub app to enable full functionality. Additional details and impacted files@@ Coverage Diff @@
## main #994 +/- ##
==========================================
+ Coverage 16.19% 16.26% +0.07%
==========================================
Files 220 221 +1
Lines 30792 30818 +26
Branches 3481 3487 +6
==========================================
+ Hits 4986 5012 +26
Misses 25755 25755
Partials 51 51 ☔ View full report in Codecov by Sentry. |
5ab7dc0
to
8e3b168
Compare
c8b6be2
to
e0c887a
Compare
After microbenchmarking I changed the implementation from binary search (which was much slower) to the approach with calculating index using bitwise operations. |
|
||
assert((It != Buckets.end()) && "Bucket should always exist"); | ||
if (Size == lower_bound) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The compiler might already do this but you could avoid the branches entirely by implementing it like this:
auto isPowerOf2 = 0 == (Size & (Size - 1));
auto largerThanHalfwayBetweenPowersOf2 = !isPowerOf2 && bool((Size - 1) && (1 << (position - 1)));
auto index = (position - MinBucketSizeExp) * 2 + !isPowerOf2 + largerThanHalfwayBetweenPowersOf2;
return index;
(if you decide to use this code, you should check if it's actually correct ;d)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately, it doesn't work correctly with halfway values and personally I find if
branches more readable. Also wouldn't this code branch anyway because of a usage of the &&
operator?
e0c887a
to
731cd99
Compare
731cd99
to
7602d17
Compare
I am closing this PR because there is a corresponding one in the unified-memory-framework repositorium: |
This PR changes linear search for buckets to calculating the bucket index based on calculating the power of two (or halfway power of two) larger than Size parameter by using bitwise operations.
Solves: #871