Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@santisoler
Copy link
Member

Summary

Add the option to use Choclo as the engine for running the gravity simulations, considerably reducing the computation time of the forward simulations and the creation of the sensitivity matrix. Add Choclo as one of the optional dependencies. Update and extend tests for the gravity simulation using pytest. By default, the simulation will use geoana as the engine. Users can alternatively choose choclo instead if it's installed.

PR Checklist

  • If this is a work in progress PR, set as a Draft PR
  • Linted my code according to the style guides.
  • Added tests to verify changes to the code.
  • Added necessary documentation to any new functions/classes following the
    expect style.
  • Marked as ready for review (if this is was a draft PR), and converted
    to a Pull Request
  • Tagged @simpeg/simpeg-developers when ready for review.

Reference issue

What does this implement/fix?

Additional information

Don't force values to be np.float32, Numba will cast them automatically
when trying to write to the preallocated matrix.
When defining the decorated functions as attributes of the class, then
every new simulation is created, they need to be recompiled.
Move the test for gravity inversions to pure pytest. Use parametrization
to run the same test using geoana and choclo. Add a `random_seed` to
`make_synthetic_data` to avoid tests failing randomly.
Add tests for sanity checks of the engine and some other attributes. Add
tests for running the forward in parallel and serial. Add test for
conversion factor.
@codecov
Copy link

codecov bot commented Sep 19, 2023

Codecov Report

Attention: 35 lines in your changes are missing coverage. Please review.

Comparison is base (211f627) 82.36% compared to head (edab185) 82.35%.

Files Patch % Lines
...imPEG/potential_fields/gravity/_numba_functions.py 32.55% 29 Missing ⚠️
SimPEG/potential_fields/gravity/simulation.py 94.05% 6 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1285      +/-   ##
==========================================
- Coverage   82.36%   82.35%   -0.02%     
==========================================
  Files         164      165       +1     
  Lines       25099    25239     +140     
==========================================
+ Hits        20674    20785     +111     
- Misses       4425     4454      +29     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Remove the `random_seed` argument added to the `make_synthetic_data` method.
The change was moved to a separate PR to check the reason why some tests
were failing in CI.
@santisoler santisoler marked this pull request as ready for review September 27, 2023 17:25
@thibaut-kobold
Copy link
Contributor

@santisoler : does this mean SimPEG PF will support GPU with this change?

@santisoler
Copy link
Member Author

@santisoler : does this mean SimPEG PF will support GPU with this change?

Not with the changes in this PR. I'm using Numba, but running the simulation in CPU.

Copy link
Contributor

@jedman jedman left a comment

Choose a reason for hiding this comment

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

fantastic work @santisoler , this addition dramatically improves the performance of the gravity simulation. looking forward to propagating it to the magnetic simulation too!

@santisoler
Copy link
Member Author

Hi @domfournier! Thanks for those suggestions. I've applied some of them, specially the ones related to reducing code duplication. I've also replied the other ones explaining better my design decisions.

I think we can move forward with this PR if you are happy with it, and continue the discussion about design on a future PR. Specially the one I'm going to open implementing the mag simulation.

@domfournier domfournier self-requested a review October 25, 2023 12:07
Copy link
Contributor

@domfournier domfournier left a comment

Choose a reason for hiding this comment

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

Thanks for the changes. Looking good on this side.

@domfournier
Copy link
Contributor

Hi @domfournier! Thanks for those suggestions. I've applied some of them, specially the ones related to reducing code duplication. I've also replied the other ones explaining better my design decisions.

I think we can move forward with this PR if you are happy with it, and continue the discussion about design on a future PR. Specially the one I'm going to open implementing the mag simulation.

Sounds good yea, we can generalize on the second round for mag. Looking forward to testing the speedup!

Copy link
Member

@jcapriot jcapriot left a comment

Choose a reason for hiding this comment

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

I've been playing around a bit with numba myself the last couple days. I like that it's able to jit a lot of the functions in here so we start avoiding the costly operations of initializing new (rather large) numpy arrays and also avoid the stacking operation. Avoiding the costly multiprocessing step here is great too. I've been looking into updating geoana as well so those functions could be used inside numba's functions. At that point the speed is roughly the same between the two (geoana vs choclo).

Eventually we should put in the simplifications when you have gxx, gyy, and gzz, but as those are not currently in the code (for accuracy reasons) no reason to add them in now.

raise TypeError(f"Invalid mesh of type {self.mesh.__class__.__name__}.")
return cell_nodes

def _get_tensormesh_cell_nodes(self):
Copy link
Member

Choose a reason for hiding this comment

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

Let's get a new version of discretize out and tagged so we don't need to include this bit.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure!

@santisoler
Copy link
Member Author

santisoler commented Oct 25, 2023

Thanks @jcapriot for the review! Yes, numba has neat things indeed!

Eventually we should put in the simplifications when you have gxx, gyy, and gzz, but as those are not currently in the code (for accuracy reasons) no reason to add them in now.

Yes. We might want to add some optimizations to this simulation when having multiple components of acceleration or tensor. These are also relevant for the mag simulation that's coming shortly.

Use the new `discretize.TensorMesh.cell_nodes` instead of the private
method that was in the simulation. Increase the minimum version of
discretize in `setup.py` and `environment_test.yml`.
@santisoler
Copy link
Member Author

@jcapriot I've updated the simulation to use the new TensorMesh.cell_nodes method in discretize v0.10.0. I've also increased the minimum version of discretize in setup.py and environment_test.yml.

Copy link
Member

@jcapriot jcapriot left a comment

Choose a reason for hiding this comment

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

Throughout the documentation, instead of writing array write either numpy.ndarray (or array_like where appropriate). Also, it could be worth it to run a comparison with the most recent geoana with the jittable prism kernels now available there.

@santisoler
Copy link
Member Author

@jcapriot do you think we should change anything else on this PR, or is it ready to be merged?

@jcapriot
Copy link
Member

@jcapriot do you think we should change anything else on this PR, or is it ready to be merged?

The only thing I would change is the actual argument named after choclo, (choclo_parallel) I would rename it something related to numba (numba_parallel?) as geoana prism kernels should now support being jitted. We should add another PR to update the internals to also support the geoana ones as well if it's not too different.

I feel like the default behavior (in the future) should be: If numba is installed, use numba parallelized geoana (since geoana is already a requirement), else use the vectorized calls, We can add choclo as a further optional thing if that behavior is actually needed.

I also think we should probably advertise it as being numba accelerated as well in that case.

@lheagy
Copy link
Member

lheagy commented Nov 20, 2023

I am really excited to get this in, congrats @santisoler! This accomplishes a major goal of having a concrete connection between the Fatiando and SimPEG communities and the benchmarks you have shown are impressive!

We now have a couple of implementations, and that can be useful for a number of reasons, including testing. I think the discussion about the default behaviour is an important one. choclo is a lightweight dependency and from my perspective, the explicit connection between the SimPEG and Fatiando communities is something that should be valued as we consider this. We can chat more at some of the upcoming meetings.

Again, nice work Santi!

@jcapriot
Copy link
Member

Oh yeah, I fully support keeping choclo as an option, I just don’t want people to get a bad impression of geoana either… (I.e geoana=slow, choclo=fast). It’s more like they are both good and fast!

@santisoler
Copy link
Member Author

Thanks @jcapriot and @lheagy!

Considering the work you've been doing in allowing geoana kernels to be called within Numba jitted functions, it makes sense to rename the argument to numba_parallel. I've just pushed that change.

Oh yeah, I fully support keeping choclo as an option, I just don’t want people to get a bad impression of geoana either… (I.e geoana=slow, choclo=fast). It’s more like they are both good and fast!

I'm sorry for making that impression. The main difference in performance doesn't come from geoana, but from the Python implementation of the simulation in SimPEG. The choclo and geoana kernels have equivalent runtimes.

@lheagy lheagy added this to the 0.21.0 milestone Nov 29, 2023
@santisoler santisoler merged commit eb07ef1 into main Dec 7, 2023
@santisoler
Copy link
Member Author

I just merged this! 🚀

Thanks a lot for the reviews, help and comments. I'm looking forward to open the PR for the mag simulation now!

I'll keep this branch up for a while, so I don't break any environment in case someone was using it. But I'll eventually delete it, so please install the latest version on main or wait for the next release (v0.21.0).

@lheagy
Copy link
Member

lheagy commented Dec 7, 2023

💥 So excited to see this come in!

thibaut-kobold added a commit to KoBoldMetals/simpeg that referenced this pull request Dec 19, 2023
* main:
  Fix minor flake8 warning (simpeg#1307)
  Gravity simulation with Choclo as engine (simpeg#1285)
@santisoler santisoler deleted the gravity-choclo branch September 17, 2025 23:40
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.

7 participants