Skip to content

LCAO GPU Optimization V1 reduce device pointer look up (OpenMP target map) - #5342

Merged
prckent merged 22 commits into
QMCPACK:developfrom
anbenali:LCAO_Performance
Mar 6, 2025
Merged

prckent merged 22 commits into
QMCPACK:developfrom
anbenali:LCAO_Performance

Conversation

@anbenali

@anbenali anbenali commented Feb 26, 2025

Copy link
Copy Markdown
Contributor

Proposed changes

First set of optimizations allowing for a 2.2X speed up by reducing device pointer lookup via OpenMP.

The LCAO branch was about 3 to 4X slower than CPU. After investigation, It seemed like it was due to map(to:) not checking if data was already on device. So converted all calls to is_device_ptr which fixed the issue.

Tested on multiple systems, but more specifically "TOU ASN" molecule: O(3) S(1) C(8) N(5) H(17), with 106 electrons and 1185 basis functions.

What type(s) of changes does this code introduce?

Code Optimization

Does this introduce a breaking change?

  • No

What systems has this change been tested on?

Checklist

Update the following with a yes where the items apply. If you're unsure about any of them, don't hesitate to ask. This is
simply a reminder of what we are going to look for before merging your code.

  • Yes. This PR is up to date with current the current state of 'develop'
  • Yes. Code added or changed in the PR has been clang-formatted
  • No. This PR adds tests to cover any new code, or to catch a bug that is being fixed
  • No. Documentation has been added (if appropriate)
    throughput_comparison

…nnecessary transfers due to map(to) instead of using is_device_ptr

@ye-luo ye-luo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please undo changes to dev_ptr so this PR can focus on real optimization.

Comment thread src/Numerics/SoaCartesianTensor.h
Comment thread src/QMCWaveFunctions/LCAO/MultiQuinticSpline1D.h Outdated
@prckent

prckent commented Feb 26, 2025

Copy link
Copy Markdown
Contributor

Can you please add some brief specifics on what the CPU/GPU comparison was? e.g. Processor, GPU, which molecule (basis, electron count), batch size?

@anbenali

Copy link
Copy Markdown
Contributor Author

dev_ptr

I am not understanding this... What do you want me to do?

@ye-luo

ye-luo commented Feb 27, 2025

Copy link
Copy Markdown
Contributor

dev_ptr

I am not understanding this... What do you want me to do?

See my comment on the source code https://github.com/QMCPACK/qmcpack/pull/5342/files#r1972521897

Comment thread src/QMCWaveFunctions/LCAO/MultiQuinticSpline1D.h Outdated
@anbenali

Copy link
Copy Markdown
Contributor Author

Can you please add some brief specifics on what the CPU/GPU comparison was? e.g. Processor, GPU, which molecule (basis, electron count), batch size?

Preparing file with summary.

@anbenali anbenali changed the title [WIP] LCAO Optimization LCAO GPU Optimization V1 Feb 27, 2025
@ye-luo

ye-luo commented Feb 28, 2025

Copy link
Copy Markdown
Contributor

I profiled runs with and without this change. This optimization pattern works cross platform (nvidia/intel checked)
old

auto* ptr = a.data(); // a is a dual space container.
#pragma target map(ptr)
{}

proposed

auto* dev_ptr = a.data(); // a is a dual space container.
#pragma target is_device_ptr(dev_ptr)
{}

the difference is that the old code needs a runtime table lookup to find out the dev_ptr and it requires mutex locking the table during lookup and it is a bottleneck. When threads doesn't do the look up frequently, the cost is negligible.

The reason of slow LCAO was its small kernels, high call counts nature.
https://github.com/QMCPACK/qmcpack/blob/develop/src/QMCWaveFunctions/LCAO/SoaLocalizedBasisSet.cpp#L251
The basis computation goes through atoms one by one. It should be changed eventually to group the computation by atomic species.

The proposed way does have a drawback. Device pointers are exposed in the host code. This may cased segfault if they are not managed correctly, for example de-referenced on the host by accident.

For the moment, I think we can take this PR that uses device ptr. Eventually once we change the code computing basis species by species. We can restore the code using the old code pattern.

@ye-luo ye-luo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please only keep changes of switching to dev_ptr and run clang-format.

Comment thread src/QMCWaveFunctions/LCAO/SoaAtomicBasisSet.h Outdated
Comment thread src/QMCWaveFunctions/LCAO/SoaAtomicBasisSet.h Outdated
Comment thread src/QMCWaveFunctions/LCAO/SoaAtomicBasisSet.h Outdated
Comment thread src/QMCWaveFunctions/LCAO/SoaLocalizedBasisSet.h Outdated
@anbenali

Copy link
Copy Markdown
Contributor Author

Fantastic!!! Thanks Ye.
Will clean asap.

However, I am almost half through isolating per basiset. Will start in a new PR.

@ye-luo

ye-luo commented Feb 28, 2025

Copy link
Copy Markdown
Contributor

Fantastic!!! Thanks Ye. Will clean asap.

However, I am almost half through isolating per basiset. Will start in a new PR.

Please make small PRs. large ones are too painful to review.

Comment thread src/QMCWaveFunctions/LCAO/SoaAtomicBasisSet.h Outdated
Comment thread src/QMCWaveFunctions/LCAO/SoaAtomicBasisSet.h Outdated
Comment thread src/QMCWaveFunctions/LCAO/SoaAtomicBasisSet.h Outdated
Comment thread src/QMCWaveFunctions/LCAO/SoaAtomicBasisSet.h Outdated
Comment thread src/QMCWaveFunctions/LCAO/SoaLocalizedBasisSet.cpp Outdated
@ye-luo ye-luo changed the title LCAO GPU Optimization V1 LCAO GPU Optimization V1 reduce device pointer look up (OpenMP target map) Feb 28, 2025
@prckent

prckent commented Mar 5, 2025

Copy link
Copy Markdown
Contributor

Checking on the status of this, which I think should be merged ahead of #5357 . Looks like some quick tidying is all that remains? @anbenali What are your plans/preferences?

@anbenali

anbenali commented Mar 5, 2025

Copy link
Copy Markdown
Contributor Author

Checking on the status of this, which I think should be merged ahead of #5357 . Looks like some quick tidying is all that remains? @anbenali What are your plans/preferences?

There is only one "serious" question to @ye-luo to see if we use

auto* SuperTwist_ptr = SuperTwist.data();

and transfer that to the GPU or not.

@ye-luo

ye-luo commented Mar 5, 2025

Copy link
Copy Markdown
Contributor

Checking on the status of this, which I think should be merged ahead of #5357 . Looks like some quick tidying is all that remains? @anbenali What are your plans/preferences?

There is only one "serious" question to @ye-luo to see if we use

auto* SuperTwist_ptr = SuperTwist.data();

and transfer that to the GPU or not.

OK. Here is what I found. In the current way using SuperTwist in the kernel, I believe the compiler does map(tofrom: this[:1]) for you. The are transfers both H2D and D2H. It works but not efficient. This can be made more efficient by keeping a resident copy on the GPU. We can deal with it later if it becomes a bottleneck.

@anbenali

anbenali commented Mar 5, 2025

Copy link
Copy Markdown
Contributor Author

Here is the problem, this affects only pbc.. some other users might need it but obviously I won't be able to touch it then. I don't mind doing it on device right away.. was just not sure of the behavior

@ye-luo

ye-luo commented Mar 5, 2025

Copy link
Copy Markdown
Contributor

My consideration is to get this PR merged timely. So we can look at your other PR that depends on this one.

Here is the problem, this affects only pbc.. some other users might need it but obviously I won't be able to touch it then. I don't mind doing it on device right away.. was just not sure of the behavior

Right now, the price is being paid regardless of using PBC. If you are willing to address it right away, simply make a new PR.

@ye-luo ye-luo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please do a review of your own code using the github web interface to prevent missing requests. There are two conversations being marked resolved without actually being addressed. All the rest LGTM.

@anbenali anbenali left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

LGTM

Comment thread src/QMCWaveFunctions/LCAO/SoaAtomicBasisSet.h
Comment thread src/QMCWaveFunctions/LCAO/SoaAtomicBasisSet.h Outdated
@ye-luo

ye-luo commented Mar 5, 2025

Copy link
Copy Markdown
Contributor

Test this please

@prckent
prckent merged commit 12adf3f into QMCPACK:develop Mar 6, 2025
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