Skip to content

[SYCL] Usm allocator refactor #7785

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

Merged
merged 3 commits into from
Feb 7, 2023
Merged

Conversation

igchor
Copy link
Member

@igchor igchor commented Dec 14, 2022

This commit makes USMAllocator unaware of the memory type on which it is operating. The user of USMAllocator is responsible
for setting appropriate options for each instance of USMAllocator.

This is the first step to making USMAllocator generic. In the next step, I'd like to start integration with Unified Memory Allocation (oneMemory). USMAllocator would be used as a heap manager by UMA.

@igchor igchor requested a review from a team as a code owner December 14, 2022 20:28
@smaslov-intel
Copy link
Contributor

@igchor : rather than adding new stuff into L0 Plugin, could you first move the USM allocator into Unified Runtime please?

@igchor
Copy link
Member Author

igchor commented Dec 16, 2022

@igchor : rather than adding new stuff into L0 Plugin, could you first move the USM allocator into Unified Runtime please?

Sure, done: #7820
I've also moved some move utils from level_zero to UR so that we can implement ur_context, etc: #7821

Copy link
Contributor

@smaslov-intel smaslov-intel left a comment

Choose a reason for hiding this comment

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

Update after #7820 is merged

@igchor igchor force-pushed the usm_allocator_refactor branch from d84b2d9 to a78068d Compare December 19, 2022 20:54
@igchor igchor requested a review from a team as a code owner December 19, 2022 20:54
@igchor
Copy link
Member Author

igchor commented Dec 19, 2022

Update after #7820 is merged

Done.

@igchor igchor requested review from smaslov-intel and removed request for rdeodhar and KseniyaTikhomirova January 18, 2023 21:33
@smaslov-intel
Copy link
Contributor

@rdeodhar : please help review the change to USM allocator


// Generate buckets sized such as: 64, 96, 128, 192, ..., CutOff.
// Powers of 2 and the value halfway between the powers of 2.
auto Size1 = USMSettings.MinBucketSize[MemHandle->getMemType()];
auto Size1 = params.MinBucketSize;
Copy link
Contributor

Choose a reason for hiding this comment

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

would this affect performance during this period of transition, if MinBucketSize may be different depending on the memory type? See here

MinBucketSize[MemType::Host] = 64;

Copy link
Member Author

Choose a reason for hiding this comment

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

No, in fact, my goal was to keep the original behavior intact. This assignment is just in a different place: https://github.com/intel/llvm/blob/51609155370086670b74ba99f3724d3d8d340852/sycl/plugins/unified_runtime/ur/usm_allocator_config.cpp

In the original implementation, those variables (MinBucketSize, Capacity, itp.) were stored in a single global structure and indexed using [getMemType()]. I have moved those variables to a separate class: USMAllocatorParameters and I'm now passing instance of this class to the USMAllocContext as you can see here: https://github.com/intel/llvm/blob/51609155370086670b74ba99f3724d3d8d340852/sycl/plugins/level_zero/pi_level_zero.cpp#L844

Basically, instead of having Structure of Arrays:

class SetLimits() {
   ...
   size_t MinBucketSize[MemType::All];
};

we now have Array of Structs:

class USMAllocatorParameters {
   ...
  size_t MinBucketSize;
} configs[MemType::All];

Copy link
Member Author

Choose a reason for hiding this comment

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

The goal of this is to split the pool management logic from reading/setting the configuration since this might differ for other adapters.

Copy link
Contributor

Choose a reason for hiding this comment

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

thanks @igchor.

size_t SlabMinSize() {
return USMSettings.SlabMinSize[(*MemHandle).getMemType()];
};
size_t SlabMinSize() { return params.SlabMinSize; };
Copy link
Contributor

Choose a reason for hiding this comment

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

same comment about performance.

Copy link
Member Author

Choose a reason for hiding this comment

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

Please see my previous answer

if (OwnAllocCtx.getParams().limits->TotalSize.compare_exchange_strong(
PoolSize, NewPoolSize)) {
if (chunkedBucket)
++chunkedSlabsInPool;
Copy link
Contributor

Choose a reason for hiding this comment

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

how is this protected now that the lock at the beginning of this function was removed?

Copy link
Member Author

Choose a reason for hiding this comment

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

My assumption was that the PoolLock was here only to protect CurPoolSize update (since this is a global variable, shared between all instances of USMAllocContext. This would match the comment: https://github.com/intel/llvm/blob/sycl/sycl/plugins/unified_runtime/ur/usm_allocator.cpp#L62 Now, instead of this lock I just rely on CAS to update the value.

As for the remaining variables (e.g. chunkedSlabsInPool) they should be protected by BucketLock which must be taken before calling CanPool

Copy link
Contributor

Choose a reason for hiding this comment

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

thanks @igchor. if that's the case, then yes, we would be ok.

@@ -885,11 +622,11 @@ size_t Bucket::Capacity() {
if (getSize() <= ChunkCutOff())
return 1;
else
return USMSettings.Capacity[getMemType()];
return OwnAllocCtx.getParams().Capacity;
Copy link
Contributor

Choose a reason for hiding this comment

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

how we will handle different values for Capacity?

Copy link
Member Author

Choose a reason for hiding this comment

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

Please see my previous answer

This is the first step to make USMAllocator generic
and usable for different plugins/adapters.

This commit makes USMAllocator unaware of memory type
on which it is operating. User of USMAllocator is responsible
for setting appropriate options for each instance of USMAllocator.
all memory types when MemType::All is specified.

Previously, SharedReadyOnly was not updated in MemParser.
since TotalSize is now updated atomically,
there is no need for additional lock. All other
(per-bucket) stats are protected by a per-bucket lock.
@igchor igchor force-pushed the usm_allocator_refactor branch from 5160915 to e684a78 Compare January 30, 2023 22:24
@igchor igchor temporarily deployed to aws January 30, 2023 22:29 — with GitHub Actions Inactive
@igchor
Copy link
Member Author

igchor commented Jan 30, 2023

@rdeodhar : please help review the change to USM allocator

@rdeodhar would you please take a look?

@igchor igchor force-pushed the usm_allocator_refactor branch from e684a78 to a684627 Compare January 30, 2023 22:36
@igchor igchor temporarily deployed to aws January 30, 2023 23:01 — with GitHub Actions Inactive
@igchor igchor temporarily deployed to aws January 30, 2023 23:32 — with GitHub Actions Inactive
@igchor
Copy link
Member Author

igchor commented Feb 3, 2023

@intel/llvm-gatekeepers @againull @smaslov-intel is there something pending to merge this?

@bader
Copy link
Contributor

bader commented Feb 3, 2023

@intel/llvm-gatekeepers @againull @smaslov-intel is there something pending to merge this?

@smaslov-intel requested changes. He must approve before it can be merged.

Copy link
Contributor

@smaslov-intel smaslov-intel left a comment

Choose a reason for hiding this comment

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

LGTM

@smaslov-intel
Copy link
Contributor

@intel/llvm-gatekeepers @againull @smaslov-intel is there something pending to merge this?

@smaslov-intel requested changes. He must approve before it can be merged.

I think this is good to go in.

@bader
Copy link
Contributor

bader commented Feb 6, 2023

Approval from @intel/llvm-reviewers-runtime team is required.

@bader bader merged commit edd9002 into intel:sycl Feb 7, 2023
@igchor igchor deleted the usm_allocator_refactor branch February 7, 2023 18:15
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.

5 participants