Replies: 3 comments 10 replies
-
I think we have a third option but to be sure about that I need to carry out some experiments (will do it soon), and I will write down this option in more depth if they come out positively but summarizing I think we could be able to do both of your 1) and 2) all at once. If possible, this would be the best solution! (imho) |
Beta Was this translation helpful? Give feedback.
-
The experiment was a success, we can have the same perf as with numpy with array,array (without the conversion) and the conversion makes it the same perf as with cython + list. This is the code: cython array.array
But then I realized that it was unnecessary to perform this experiment xD since the same can be probably achieved using only numpy because the conversion will cost the same (we need to check but it seems probable). But in any case I think we can have both options 1) and 2) all at once. This is how:
This achieves both 1) and 2) at the same time. Also sometimes handling only the conversion can be quite fast, for example if the neighbors are not many, the list of ints which will be passed will be short. What do you think about this solution? This would be beneficial for all users |
Beta Was this translation helpful? Give feedback.
-
New findings. I ported
Both versions return a list of agents, and so can easily be consumed by other Python functions. On Ryzen:
It's still a significant speedup regardless. |
Beta Was this translation helpful? Give feedback.
-
Update 1: We have decided to go with Cython because:
@jitclass
requires all the methods to be strictly innopython
. This greatly limits the implementation detail. I can't even pass apos
(atuple[int, int]
) to a method, and will have to wrap a converter in an external function (defined outside the class definition) if I were to write the Numba code.TypeError: Failed in nopython mode pipeline (step: fix up args) Signature mismatch: 1 argument types given, but function takes 2 arguments
with no line numberNumba will likely still evolve a lot in the next few years. But for our use case, Cython is a better fit.
@Tortar and I have been experimenting with reimplementing Grid in Numba/Cython. So far we have tried to speed up
get_neighborhood
. I will summarize the finding.This is
_Grid.get_neighborhood((10, 10), True, include_center=True, radius=10)
withTorus=False
in various implementations"Cython array" is basically Cython with Pythons
array.array
. "Cython list" is Cython but with Python list. Numba/Cython withnp.ndarray
23-24x faster than "default" (the vanilla `get_neighborhood).Julia:
Julia implementation is ~2x faster than Numba/Cython
np.ndarray
.However, the story is different if we have to convert the array to Python list, at the end of computation:
This leaves us 2 options:
I think option 2 is more promising. But this means that Mesa users will have to code their model in Numba/Cython if they want this 23x speedup. This might be a viable use case for people who already have written their model in Mesa/Python, but find it too time-consuming to migrate their code to Julia/Agents.jl.
This summary is only for
get_neighborhood
. But we should expect an order of magnitude speedup foriter_cell_list_contents
as well.Code to reproduce this can be found at https://github.com/rht/mesa-perf.
Beta Was this translation helpful? Give feedback.
All reactions