Skip to content
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

Feature: Add diago_dav_subspace module to pyabacus #4883

Merged
merged 22 commits into from
Aug 16, 2024

Conversation

a1henu
Copy link
Collaborator

@a1henu a1henu commented Aug 6, 2024

This commit adds the diago_dav_subspace module to the pyabacus package, which is responsible for diagonalizing the Hamiltonian matrix using Davison subspace method.

Reminder

  • Have you linked an issue with this pull request?

Linked Issue

#4882

This commit adds the `diago_dav_subspace` module to the `pyabacus` package, which is responsible for diagonalizing the Hamiltonian matrix using Davison subspace method.

The current Pythonization version targets matrix diagonalization. The original algorithm does not require passing a matrix; instead, it uses a function pointer to pass a linear operator for diagonalization.

So the current implementation still needs to wrap std::function<void(T*, T*, const int, const int, const int, const int)> to a Python callable type, which requires further iteration.
@a1henu a1henu changed the title <Feature>[pyabacus]: Add diago_dav_subspace module to pyabacus Feature: Add diago_dav_subspace module to pyabacus Aug 6, 2024
@mohanchen mohanchen requested a review from jieli-matrix August 6, 2024 11:38
@jieli-matrix
Copy link
Collaborator

A good practice -- the first contribution to pyabacus from the external users!🎉
However, I think the try will start from the one-one mapping (ABACUS <-> pyabacus) but not stop at such level: math operations will separate from the physics features, and it would be the cornerstone of numerical simulations.

I list the TODOs as follows:

  • Add the tests with comparison to scipy.linalg.eigh, Refer to tests here. Both precision and performance.
  • Design the python APIs so that it can be treated as a pure math feature.
  • Plz add docs in pyabacus like this, to notify users which case your Davidson methods can work(Hermitian matrix or non-sym matrix)?
  • (optional): with the tests results from the first item, you may need to add MPI support to pyabacus. I have set up the environment for you. Try it!

python/pyabacus/src/py_diago_dav_subspace.cpp Outdated Show resolved Hide resolved
python/pyabacus/tests/test_diago_dav_subspace.py Outdated Show resolved Hide resolved
a1henu added 11 commits August 13, 2024 16:14
The destructor of Diago_DavSubspace was causing a memory issue, preventing the program from running correctly. Lines 72-74 were previously commented out to avoid the issue, but this led to a memory leak. This commit addresses the root cause of the memory issue and ensures proper memory deallocation without causing a crash.

- Uncommented lines 72-74 in the destructor.

This ensures that all allocated memory is properly deallocated, preventing memory leaks but still unable to run the `pyabacus::hsolver::dav_subspace()`
…ices

Previously, our module could only accept explicit matrices for diagonalization. I have now modified the code to accept operators as parameters for diagonalization, eliminating the need to store the entire matrix explicitly. This allows us to compute eigenvalues for larger Hamiltonians using the sparse matrix storage provided by `SciPy`.

The operator is a function pointer that accepts a vector and returns a vector. To diagonalize an operator A, we define a function or lambda function `mv_op` such that `mv_op(x) = Ax`, which can then be used in the diagonalization process.

Additionally, I have added a new test case for the Hamiltonian matrix corresponding to H2O. The matrix size is `67024x67024`, which previously could not be stored explicitly due to memory constraints. The updated operator method now supports the computation of eigenvalues for such large matrices.

Test cases have been updated to reflect these changes.
a1henu added 3 commits August 15, 2024 00:11
…nd potential performance improvement

- Replaced manual loop with `std::copy` to copy elements from `psi_in` to `psi` and from `hpsi_ptr` to `hpsi_out`.
- This change simplifies the code, enhances readability and may improve performance due to optimized standard library `std::copy` implementations.
…nd rename two variables' name

- Modified `hpsi_func` to handle matrix-matrix multiplication instead of vector operations.
- This vectorization significantly improved computation speed.
- Renamed variables in the Python frontend interface:
  - `nbasis` to `dim`
  - `nband` to `num_eigs`
- These changes align with the module's design as a pure mathematical interface.
a1henu added 3 commits August 15, 2024 12:36
- Updated test cases to ensure absolute error is less than 1e-8.
- Added a new test case for random sparse matrices to validate the implementation.
@mohanchen
Copy link
Collaborator

It seems that the PR has sufficient content to be merged. Further updates are welcome.

@mohanchen mohanchen merged commit d0b25ea into deepmodeling:develop Aug 16, 2024
14 checks passed
@a1henu a1henu deleted the devdev branch August 26, 2024 12:32
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