Operators work directly on raw arrays if most agents are active #843
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Operators on
Arr
andBoolArr
useasnew()
to return newArr
instances. These work along the lines ofself.values
to create a numpy array with length equal toauids
other.values
ifother
is anArr
auids
uids
auids
The intention is that the alternative of operating on every UID (i.e., on the
raw
arrays directly) would be unnecessarily slow if there are many inactive agents. However, in practice the overhead associated with creating the intermediate arrays and then sparsely inserting it is fairly significant. For simulations that do not have a significant proportion of inactive agents, it can be faster to just operate on every entry. In that case, no intermediate arrays need to be created - the output of the logical operation of the two raw arrays can be directly used as the raw array for the newArr
instances. In some simulations this can give a speedup of around 40%.This PR thus adds an attribute for
Arr
that corresponds to whether this optimization should be used, with a threshold of 50% (i.e., it will use the raw entries if over half the agents are active). This threshold could be tuned as we get more use cases. There is a corresponding update toasnew
to optionally take in araw
array, which will be used if provided.There should be no changes necessary in any user code and simulation output should be the same
Checklist