-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
better reducedim type inference #6994
Conversation
#6672 was filed for reductions other than those in reducedim, so probably wouldn't want to close #6672 just yet. I also don't think this will work for an example like On my todo-list is to completely eliminate the need to initialize the output. Basically generalize reductions like
to more than one dimension. The logic here is a little tricky, but I hope it's doable (though it might possibly hurt efficiency). If we can do that, then reducedim will be more flexible than it ever has been. But if we need a stopgap, one good option would be to at least allow the user to initialize the output manually and be able to run |
|
Unfortunately, your Doing this for |
I hadn't caught that back when it was filed & discussed. Rats.
Right, but isn't that what you give up with type-flexibility? For example, for
shouldn't we really have
? Anyway, the revised patch looks better than where we are now (I didn't even realize |
@timholy, obviously if you have an |
It sounds great to get type-stability in cases where the type of the container is less specific than it could have been, but I'm being a little slow in understanding how you achieve it. For example, arguably we shouldn't have In such cases, IIUC with this patch the element type of
and type-stability of But you seem to have thought about this, so I bet I'm just not getting it. |
@timholy, sorry I wasn't clear. For example, your |
Got it. Of course, that's fixed simply by changing the function to
and has the advantage of doing its job in a single pass. (I also think it's basically inevitable that someday, Stefan will agree that |
(But I'll add that this is all vaporware right now, and I have no objections to your version being merged, particularly if there is not a better way of getting the same effect.) |
@timholy. Right, but you also have to special-case 0 and 1 elements as I mentioned above. This is what |
Agreed, the logic for avoiding double-counting in reductions that involve more than one dimension simultaneously is not going to be entirely trivial. Adding the possibility of 0- and unit-length arrays on top of that makes it a bit worse, although I suspect that's going to be relatively minor ("do it on a separate code path") compared to the first. |
@eval function $f{T}(A::AbstractArray{T}, region) | ||
if method_exists($init, (Type{T},)) | ||
z = $op($init(T), $init(T)) | ||
Tr = typeof(z) == typeof($init(T)) ? T : typeof(z) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why so simply set Tr = typeof(z)
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If T
is Real
, then typeof(z)
will be Int
, and then you will get errors if there are non-integer values in the array.
On the other hand, if T
is Int8
, then z
will have type Int
, and I think we want Tr
to be Int
too. Hence the conditional.
I get the sense we should merge this? |
I think it is good to merge. |
better reducedim type inference
Was the perf test suite run before-after? |
@IainNZ, I didn't run the perf-test suite. This patch shouldn't really impact performance in the common case of |
Fixes #6672.