Skip to content

Add prefetch for HIP USM allocations #10430

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 1 commit into from
Aug 24, 2023
Merged

Conversation

ldrumm
Copy link
Contributor

@ldrumm ldrumm commented Jul 17, 2023

This change is necessary to workaround a delightful bug in either HIP runtime, or the HIP spec.

It's discussed at length in github.com//issues/7252 but for the purposes of this patch, it suffices to say that a call to hipMemPrefetchAsync is required for correctness in the face of global atomic operations on (at least) shared USM allocations.

The architecture of this change is slightly strange on first sight in that we reduntantly track allocation information in several places. The context now keeps track of all USM mappings. We require a mapping of pointers to the allocated size, but these allocations aren't pinned to any particular queue or HIP stream.
The hipMemPrefetchAsync, however, requires the associated HIP stream object, and the size of the allocation. The stream comes hot-off-the-queue only just before a kernel is launched, so we need to defer the prefetch until we have that information.

Finally, the kernel itself keeps track of pointer arguments in a more accessible way so we can determine which of the kernel's pointer arguments do, in-fact, point to USM allocations.

@ldrumm ldrumm requested a review from a team as a code owner July 17, 2023 17:27
@ldrumm ldrumm temporarily deployed to aws July 17, 2023 17:41 — with GitHub Actions Inactive
@ldrumm ldrumm temporarily deployed to aws July 17, 2023 19:19 — with GitHub Actions Inactive
@ldrumm ldrumm requested a review from omarahmed1111 July 18, 2023 16:38
@ldrumm ldrumm temporarily deployed to aws July 18, 2023 17:12 — with GitHub Actions Inactive
@ldrumm ldrumm temporarily deployed to aws July 18, 2023 17:58 — with GitHub Actions Inactive
Copy link
Contributor

@omarahmed1111 omarahmed1111 left a comment

Choose a reason for hiding this comment

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

Looks good, just minor stylish stuff

@ldrumm ldrumm temporarily deployed to aws July 19, 2023 13:06 — with GitHub Actions Inactive
@ldrumm ldrumm temporarily deployed to aws July 19, 2023 13:49 — with GitHub Actions Inactive
@ldrumm ldrumm temporarily deployed to aws July 19, 2023 14:53 — with GitHub Actions Inactive
@ldrumm ldrumm temporarily deployed to aws July 19, 2023 16:04 — with GitHub Actions Inactive
@ldrumm ldrumm requested a review from a team as a code owner August 3, 2023 09:33
@ldrumm ldrumm temporarily deployed to aws August 3, 2023 09:41 — with GitHub Actions Inactive
@ldrumm ldrumm temporarily deployed to aws August 3, 2023 10:35 — with GitHub Actions Inactive
@ldrumm ldrumm temporarily deployed to aws August 3, 2023 11:25 — with GitHub Actions Inactive
@ldrumm ldrumm force-pushed the gh-7252 branch 2 times, most recently from c0bd637 to 3187744 Compare August 4, 2023 15:37
@ldrumm ldrumm temporarily deployed to aws August 4, 2023 15:56 — with GitHub Actions Inactive
@ldrumm ldrumm temporarily deployed to aws August 4, 2023 16:42 — with GitHub Actions Inactive
@ldrumm ldrumm temporarily deployed to aws August 4, 2023 17:16 — with GitHub Actions Inactive
@ldrumm ldrumm temporarily deployed to aws August 4, 2023 18:04 — with GitHub Actions Inactive
This change is necessary to workaround a delightful bug in either HIP
runtime, or the HIP spec.

It's discussed at length in github.com/intel/issues/7252 but for
the purposes of this patch, it suffices to say that a call to
`hipMemPrefetchAsync` is *required* for correctness in the face of
global atomic operations on (*at least*) shared USM allocations.

The architecture of this change is slightly strange on first sight in
that we reduntantly track allocation information in several places.
The context now keeps track of all USM mappings. We require a mapping of
pointers to the allocated size, but these allocations aren't pinned to
any particular queue or HIP stream.
The `hipMemPrefetchAsync`, however, requires the associated HIP stream
object, and the size of the allocation. The stream comes
hot-off-the-queue *only* just before a kernel is launched, so we need to
defer the prefetch until we have that information.

Finally, the kernel itself keeps track of pointer arguments in a more
accessible way so we can determine which of the kernel's pointer
arguments do, in-fact, point to USM allocations.
@ldrumm ldrumm temporarily deployed to aws August 7, 2023 07:50 — with GitHub Actions Inactive
@ldrumm ldrumm temporarily deployed to aws August 7, 2023 08:47 — with GitHub Actions Inactive
@ldrumm ldrumm temporarily deployed to aws August 7, 2023 13:15 — with GitHub Actions Inactive
@ldrumm ldrumm temporarily deployed to aws August 7, 2023 13:23 — with GitHub Actions Inactive
@ldrumm
Copy link
Contributor Author

ldrumm commented Aug 14, 2023

@intel/llvm-reviewers-runtime ping

@aelovikov-intel
Copy link
Contributor

so we can determine which of the kernel's pointer arguments do, in-fact, point to USM allocations.

What if I pass that pointer via memory and not through kernel argument/capture?

@ldrumm
Copy link
Contributor Author

ldrumm commented Aug 14, 2023

so we can determine which of the kernel's pointer arguments do, in-fact, point to USM allocations.

What if I pass that pointer via memory and not through kernel argument/capture?

Then it won't work.

As I see it, there are basically two options for how to solve this problem:

  1. Do what this patch does, which is best effort. We could potentially track every argument type with compiler support, but I don't think the UR design supports this level of knowledge, and it's a significant amount of work
  2. prefetch all usm allocations before every kernel launch

For now, I think (1) is acceptable as most things already work - the atomics test cases linked in issue #7252 are a notable exception which motivated this patch

@npmiller
Copy link
Contributor

friendly ping @jandres742 @aelovikov-intel @intel/llvm-reviewers-runtime

@ldrumm
Copy link
Contributor Author

ldrumm commented Aug 24, 2023

so we can determine which of the kernel's pointer arguments do, in-fact, point to USM allocations.

What if I pass that pointer via memory and not through kernel argument/capture?

Then it won't work.

As I see it, there are basically two options for how to solve this problem:

1. Do what this patch does, which is best effort. We could potentially track every argument type with compiler support, but I don't think the UR design supports this level of knowledge, and it's a significant amount of work

2. prefetch _all_ usm allocations before _every_ kernel launch

For now, I think (1) is acceptable as most things already work - the atomics test cases linked in issue #7252 are a notable exception which motivated this patch

@aelovikov-intel is this good enough for merge, please?

@aelovikov-intel aelovikov-intel merged commit a6b8fa6 into intel:sycl Aug 24, 2023
veselypeta pushed a commit to veselypeta/llvm that referenced this pull request Sep 28, 2023
This change is necessary to workaround a delightful bug in either HIP
runtime, or the HIP spec.

It's discussed at length in github.com/intel/issues/7252 but for
the purposes of this patch, it suffices to say that a call to
`hipMemPrefetchAsync` is *required* for correctness in the face of
global atomic operations on (*at least*) shared USM allocations.

The architecture of this change is slightly strange on first sight in
that we reduntantly track allocation information in several places. The
context now keeps track of all USM mappings. We require a mapping of
pointers to the allocated size, but these allocations aren't pinned to
any particular queue or HIP stream.
The `hipMemPrefetchAsync`, however, requires the associated HIP stream
object, and the size of the allocation. The stream comes
hot-off-the-queue *only* just before a kernel is launched, so we need to
defer the prefetch until we have that information.

Finally, the kernel itself keeps track of pointer arguments in a more
accessible way so we can determine which of the kernel's pointer
arguments do, in-fact, point to USM allocations.
@ldrumm ldrumm deleted the gh-7252 branch November 15, 2023 11:27
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.

6 participants