Skip to content
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

rewrite _momentX methods in more functional style #900

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

kalmarek
Copy link

depending on the width of vector instruction this is up to 4× faster (N=100), the old implementation matches for k = 6;

@devmotion

@andreasnoack
Copy link
Member

I think this is a good change. It requires the minimum Julia version to be 1.6 but I think that is okay.

s += z * z
end
varcorrection(n, corrected) * s
s = sum(x->abs2(x-m), v, init=zero(m))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should use an improved init value if possible - in many cases the result of sum(...) won't be of the same type as zero(m).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For instance, we could use

Suggested change
s = sum(x->abs2(x-m), v, init=zero(m))
init = (zero(eltype(v)) - zero(m))^2
s = sum(x->(x-m)^2, v; init=init)

end

varcorrection(wv, corrected) * s
s = sum(i -> (@inbounds abs2(v[i] - m) * wv[i]), eachindex(v), init=zero(m))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly, I think we need a different init here. Additionally, @inbounds is unsafe here as i might not be an actual index of wv.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
s = sum(i -> (@inbounds abs2(v[i] - m) * wv[i]), eachindex(v), init=zero(m))
init = (zero(eltype(v)) - zero(m))^2 * zero(eltype(wv))
s = sum(i -> (v[i] - m)^2 * wv[i], eachindex(v, wv); init=init)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@devmotion if v and wv do not have the same indices eachindex(v, wv) will throw and therefore it seems that @inbounds here should be safe. How about this:

Suggested change
s = sum(i -> (@inbounds abs2(v[i] - m) * wv[i]), eachindex(v), init=zero(m))
s = if isempty(v)
(zero(eltype(v)) - zero(m))^2*zero(eltype(wv))
elseif eachindex(v) == eachindex(wv)
sum(i -> (@inbounds (v[i] - m)^2 * wv[i]), eachindex(v, wv))
else
sum(i -> ((v_ - m)^2 * wv_) for (v_, wv_) in zip(v, wv))
end

s += z * z * z
end
s / n
s = sum(x->(x-m)^3, v, init=zero(m))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here as well.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
s = sum(x->(x-m)^3, v, init=zero(m))
init = (zero(eltype(v)) - zero(m))^3
s = sum(x->(x-m)^3, v; init=init)

Comment on lines +179 to +183
s = sum(
i -> (@inbounds (z = (v[i] - m); z * z * z * wv[i])),
eachindex(v),
init=zero(m),
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comments as above:

Suggested change
s = sum(
i -> (@inbounds (z = (v[i] - m); z * z * z * wv[i])),
eachindex(v),
init=zero(m),
)
init = (zero(eltype(v)) - zero(m))^3 * zero(eltype(wv))
s = sum(
i -> (v[i] - m)^3 * wv[i],
eachindex(v, wv);
init=init,
)

s += abs2(z * z)
end
s / n
s = sum(x-> (z = x-m; abs2(z*z)), v, init=zero(m))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
s = sum(x-> (z = x-m; abs2(z*z)), v, init=zero(m))
init = (zero(eltype(v)) - zero(m))^4
s = sum(x->(x-m)^4, v; init=init)

Comment on lines +193 to +197
s = sum(
i -> (@inbounds (z = (v[i] - m); abs2(z * z) * wv[i])),
eachindex(v),
init=zero(m),
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
s = sum(
i -> (@inbounds (z = (v[i] - m); abs2(z * z) * wv[i])),
eachindex(v),
init=zero(m),
)
init = (zero(eltype(v)) - zero(m))^4 * zero(eltype(wv))
s = sum(
i -> (v[i] - m)^4 * wv[i],
eachindex(v, wv);
init=init,
)

s += (z ^ k)
end
s / n
s = sum(x -> (x - m)^k, v, init=zero(m))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
s = sum(x -> (x - m)^k, v, init=zero(m))
init = (zero(eltype(v)) - zero(m))^k
s = sum(x -> (x - m)^k, v; init=init)

Comment on lines +207 to +211
s = sum(
i -> (@inbounds (z = (v[i] - m); z^k * wv[i])),
eachindex(v),
init=zero(m),
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
s = sum(
i -> (@inbounds (z = (v[i] - m); z^k * wv[i])),
eachindex(v),
init=zero(m),
)
init = (zero(eltype(v)) - zero(m))^k * zero(eltype(wv))
s = sum(
i -> (v[i] - m)^k * wv[i],
eachindex(v, wv);
init=init,
)

@devmotion
Copy link
Member

Since it's unspecified whether init is used at all for non-empty collections, an alternative might be to use the suggested init values only for empty collections and use sum without init keyword argument for non-empty collections (as a side effect this would not even require to bump the Julia version it seems).

@kalmarek
Copy link
Author

Since it's unspecified whether init is used at all for non-empty collections, an alternative might be to use the suggested init values only for empty collections and use sum without init keyword argument for non-empty collections (as a side effect this would not even require to bump the Julia version it seems).

I'd proceed with this version then. let me know what do you think about the suggestion I made above

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants