-
-
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
fix for loop for array indexing #15497
Conversation
@@ -833,7 +833,7 @@ end | |||
function indcopy(sz::Dims, I::Vector) | |||
n = length(I) | |||
s = sz[n] | |||
for i = n+1:length(sz) | |||
for i = n+1:length(sz) #Fixme iter, might be unnecessary, because it is not exported |
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.
This is OK here since I
is restricted to Vector
(as opposed to AbstractVector
).
Thanks! I think most of the FIXMEs can be fixed easily, though some of them require more though/work/features. |
Thanks for the review. I have removed some FIXME comments and updated some type signatures according to your remarks. |
Does all this |
It doesn't (well, currently almost not, and with a special iterator type not at all): #15356 (comment) |
@@ -179,8 +180,8 @@ end | |||
|
|||
function ipermutedims(A::AbstractArray,perm) | |||
iperm = Array(Int,length(perm)) | |||
for i = 1:length(perm) | |||
iperm[perm[i]] = i | |||
for (i,j) = zip(1:length(perm),eachindex(perm)) |
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.
is this the same as enumerate(eachindex(perm))
?
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.
Yes, would the enumerate version be preferred?
PR is updated according to the comments. |
Sorry for the confusion, changes were still local... |
for i = 1:length(A) | ||
if testf(A[i]) | ||
for (i,j) = enumerate(eachindex(A)) | ||
if testf(A[j]) |
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 is the enumerate(eachindex(A))
preferable over the straightforward i=1:length(A)
?
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.
Because some package might implement a non-standard Fortran-style indexing, where indices have an offset and don't start at 1.
Also Linear indexing might be slow if A is a SubArray.
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.
Makes sense, although makes the code kinda ugly.
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.
You should be able to use enumerate(A)
directly.
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.
In fact the code seems to have a bug as written. It should be something like
for (i, a) in enumerate(A)
if testf(a)
(In other words, a = A[i]
.)
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.
I see, one doesn't need the index of A at all, I will update. But just out of curiosity: is this a bug? The code worked for me, are there cases where it doesn't?
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.
julia> A = rand(5)
5-element Array{Float64,1}:
0.819467
0.980514
0.190881
0.936337
0.882508
julia> for (i, j) in enumerate(A)
@show A[j]
end
WARNING: Indexing with non-Integer Reals is deprecated. It may be that your index arose from an integer division of the form i/j, in which case you should consider using i÷j or div(i,j) instead.
in depwarn at deprecated.jl:73
in to_index at deprecated.jl:447
in getindex at array.jl:282
[inlined code] from show.jl:127
in anonymous at no file:0
while loading no file, in expression starting on line 0
ERROR: InexactError()
in to_index at deprecated.jl:448
in getindex at array.jl:282
[inlined code] from show.jl:127
in anonymous at no file:0
julia> for (i, a) in enumerate(A)
@show a
end
a = 0.8194665233152139
a = 0.9805141257931855
a = 0.19088128894552314
a = 0.9363374114892509
a = 0.8825084643072016
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.
for (i, j) in enumerate(A)
is not @meggart wrote. He wrote for (i, j) in enumerate(eachindex(A))
which seems like it would work.
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.
Oh, I was reading too fast. OK, then this wasn't a bug, just a style point.
@@ -179,8 +180,8 @@ end | |||
|
|||
function ipermutedims(A::AbstractArray,perm) | |||
iperm = Array(Int,length(perm)) | |||
for i = 1:length(perm) | |||
iperm[perm[i]] = i | |||
for (i,j) = enumerate(eachindex(perm)) |
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.
(i,p) = enumerate(perm)
?
Is this ok now? |
LGTM. Maybe I should wait for someone else to take a second look 😉. |
Another option would be to wait until #15459 gets merged and then revisit some of the FIXMEs. |
Sorry for the delay here, @meggart. This is some nice work, exhibiting a clear understanding of the goals and strategies in #15434. I'm guessing #15459 is the beginning, but not end, of a thread of ideas, so I'd rather not hold this hostage to future improvements. I see this got another thumbs up, so at least one set of eyeballs besides mine has checked this. To satisfy @KristofferC's excellent point in #15564 (comment), let's |
@@ -1261,14 +1261,14 @@ end | |||
|
|||
## 2 argument | |||
function map!{F}(f::F, dest::AbstractArray, A::AbstractArray, B::AbstractArray) | |||
for i = 1:length(A) | |||
dest[i] = f(A[i], B[i]) | |||
for (i,j,k) = zip(eachindex(dest),eachindex(A),eachindex(B)) |
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.
change to in
to be consistent with the rest?
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.
Also, spaces after commas are the most frequent convention.
Something weird going on with nanosoldier? cc @jrevels |
It seems that the build failed due to some issue with threading. Do the other CI services not enable the Here's the last couple lines of the build log, if anybody's curious:
|
|
I also have the impression that threading performance may have been broken for a while, so I'm not certain that the threading benchmarks are worth much now, unless one compares against a much older julia-0.5. |
I'll go ahead and disable the flag then. I'll retrigger the benchmarks here once I've made the change. |
|
I updated lines 1039 and 1264 according to comments. Looks like this interrupted the perfomance tests? Sorry for this... |
@@ -362,8 +362,8 @@ function ctransposeblock!(B::AbstractMatrix,A::AbstractMatrix,m::Int,n::Int,offs | |||
return B | |||
end | |||
function ccopy!(B, A) | |||
for i = 1:length(A) | |||
B[i] = ctranspose(A[i]) | |||
for (i,j) = zip(eachindex(B),eachindex(A)) |
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.
Also here use in
Don't be! The performance tests are still running on the version of the PR that they were triggered on, it's just that GitHub's UI only shows the status of the latest version. |
Your benchmark job has completed - possible performance regressions were detected. A full report can be found here. cc @jrevels |
Nice! @meggart, there often seem to be a few tests that get reported as regressions due to noise (there are hundreds of tests run, and so even a If you need any help, give a shout! |
The SIMD benchmarks are currently quite noisy. |
I've definitely seen false reports on the |
Thanks very much, @meggart! |
This is the iteration fixes for the files abstractarray.jl ... arraymath.jl, see issue #15434
There were quite a few cases where the fix was not obviuos because of one of these reasons:
for i=2:length(A)
)map_n!
andmap_to_n!
However,
#Fixme iter
and#Fixme comp
comments were added in these cases.