Speeding up certain MetPy functions with Python multiprocessing #2259
DanielAdriaansen
started this conversation in
Ideas
Replies: 1 comment 1 reply
-
i got a BUG
How can i solve it? |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I asked a question over on #1533 which is still an open issue. I don't feel comfortable tackling the problem described there in terms of the MetPy code base and architecture, so I came up with a workaround that works for me outside of MetPy.
This workaround uses the
precipitable_water()
function, but it may be applicable to other functions within MetPy as well (likecape_cin
, or anything possibly in the documentation that says "only functions on 1D profiles"). The assumed output from this example is a 2D array. The data used here is ERA5 reanalysis data on pressure levels, subset to a 181 (latitude) x 281 (longitude) domain centered over the conterminous United States.The general concept of my workaround is to define a 2D ndarray to store the output, break it up into tiles, and then write a function to wrap
precipitable_water()
so that it can be called on multiple tiles of the output 2D grid simultaneously using themultiprocessing
Python package. Then the tiles are unpacked and put into the final 2D output array.Here's the code (it assumes you have the required inputs to
precipitable_water()
stored in an Xarray Dataset namedds
):Here are some time output using the Linux
time
command:ntilesx = 1
ntilesy = 1
ntilesx = 2
ntilesy = 2
ntilesx = 3
ntilesy = 3
ntilesx = 4
ntilesy = 4
After using 16 tiles (ntilesx=4, ntilesy=4, 16 total tiles) on this particular hardware, there's no more speedup and 30s is about as fast as I could get it to run. But this is miles ahead of the nearly 6 minutes when using the full domain (90%+ speedup!).
Some things to keep in mind:
Here's the output from the serial version using nested for loops on the full grid (or a single tile):
And here's the output using the multiprocessing method demonstrated above using 16 tiles:
I welcome any and all feedback or suggestions, and hope that this is useful to others.
Beta Was this translation helpful? Give feedback.
All reactions