This is an archive of the discontinued LLVM Phabricator instance.

[OpenMP] Add API for pinned memory
ClosedPublic

Authored by carlo.bertolli on Nov 29 2022, 11:08 AM.

Details

Summary

This patch adds API support for the atk_pinned trait for omp_alloc. It does not implement kmp_target_lock_mem and kmp_target_unlock_mem in libomptarget, but prepares libomp for it. Patches to libomptarget to implement lock/unlock coming after this one.

Diff Detail

Event Timeline

Herald added a project: Restricted Project. · View Herald TranscriptNov 29 2022, 11:08 AM
carlo.bertolli requested review of this revision.Nov 29 2022, 11:08 AM

Generally looks good. Great we are gaining support upstream. I have one question though and I hope some host runtime ppl can take a look too.

openmp/runtime/src/kmp_alloc.cpp
1658

Calling lock_mem on alloc_device memory is probably not going to work well, right?
I am unsure we need to lock this here at all, it's either "_host" (=pinned), "_shared" (=managed), or "_device" (=GPU) memory.
When does locking make sense in those three cases and can we guarantee it won't cause problems?

1694

Same as above.

carlo.bertolli marked 2 inline comments as done.
openmp/runtime/src/kmp_alloc.cpp
1658

Good catch. For now, I moved the pinning to the llvm_omp_target_host_memo_alloc. However, given that this is a predefined memory allocator, I do not see any way the OpenMP API would allow users to set a special trait for it. Setting traits is something you can do when initializing an allocator, which needs to be seeded with a predefined memory space - not with a predefined allocator.
So, unless there is something in OpenMP 6.0 TR1 that would enable users to customize predefined allocators, this is dead code.
If you agree, I can remove support from these two cases you indicated. If not, I'll leave it up.

jdoerfert added inline comments.Nov 29 2022, 9:52 PM
openmp/runtime/src/kmp_alloc.cpp
1658

I think we should remove it. "_host" allocator should already pin memory, plus your point.

carlo.bertolli marked an inline comment as done.

Removed llvm_omp_target_host_mem_alloc as a case for pinning.

jlpeyton added inline comments.Dec 12 2022, 8:35 AM
openmp/runtime/src/kmp_alloc.cpp
2043
  1. Should there be a check for is_pinned or similar?
  2. Should the default device always be 0?
openmp/runtime/test/api/omp_pinned.c
1 ↗(On Diff #478942)

Can we add another RUN line with env OMP_ALLOCATOR=omp_default_mem_space:pinned=true %libomp-run. This would have to be used in tandem with something like pinned_alloc2 = omp_get_default_allocator() below doing a second allocation and free.

[OpenMP] Add test for default allocator and move unlock call to appropriate site.

carlo.bertolli marked 2 inline comments as done.Dec 13 2022, 11:12 AM
jlpeyton added inline comments.Dec 13 2022, 11:51 AM
openmp/runtime/test/api/omp_pinned.c
2 ↗(On Diff #482565)

%libomp-compile-and-run -> %libomp-run

As written now with %libomp-compile-and-run, LIT will set the environment variable for the re-compilation only. It translates to:

$ env OMP_ALLOCATOR=... <compilation_command> && ./executable

We can avoid the recompilation and have the environment variable apply to the run command by using %libomp-run

carlo.bertolli marked an inline comment as done.

[OpenMP] Only rerun lit test for pinned memory, no need to rebuild.

This revision is now accepted and ready to land.Dec 13 2022, 1:10 PM
jdoerfert accepted this revision.Dec 13 2022, 7:26 PM

Thanks Carlo, this is great :)

This revision was automatically updated to reflect the committed changes.
Herald added a project: Restricted Project. · View Herald TranscriptDec 14 2022, 6:52 AM
xtian added a comment.Dec 14 2022, 7:47 AM

With the updated patch. LGTM