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

Optimize multiplication for Normed #213

Merged
merged 1 commit into from
Aug 24, 2020
Merged

Conversation

kimikage
Copy link
Collaborator

@kimikage kimikage commented Aug 14, 2020

This adds wrapping_mul, saturating_mul and checked_mul binary operations. However, this does not specialize them for Fixed and does not change * for Fixed.

This applies the wrapping arithmetic as the default arithmetic of * for the abstract FixedPoint, but as mentioned above, the *for Fixed is not affected by the default arithmetic. Furthermore, * for Normed overrides the default arithmetic with the "checked" arithmetic for backward compatibility. (Strictly speaking, the error type is changed from ArgumentError to OverflowError.)

This replaces most of Normed's implementation of multiplication with integer operations. This improves the speed in many cases and the accuracy in some cases.

Fixes #174

@codecov
Copy link

codecov bot commented Aug 14, 2020

Codecov Report

Merging #213 into master will increase coverage by 0.61%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #213      +/-   ##
==========================================
+ Coverage   90.39%   91.01%   +0.61%     
==========================================
  Files           6        6              
  Lines         510      534      +24     
==========================================
+ Hits          461      486      +25     
+ Misses         49       48       -1     
Impacted Files Coverage Δ
src/FixedPointNumbers.jl 87.75% <100.00%> (+0.32%) ⬆️
src/normed.jl 92.14% <100.00%> (+0.86%) ⬆️
src/utilities.jl 96.00% <0.00%> (+4.00%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5794adf...1dea1e9. Read the comment docs.

@kimikage
Copy link
Collaborator Author

Benchmark

x_n0f8  = collect(rand(N0f8,  1000, 1000)); y_n0f8  = collect(rand(N0f8,  1000, 1000));
x_n0f16 = collect(rand(N0f16, 1000, 1000)); y_n0f16 = collect(rand(N0f16, 1000, 1000));
x_n4f4  = collect(rand(N4f4,  1000, 1000)); y_n4f4  = collect(rand(N4f4,  1000, 1000));
x_n4f12 = collect(rand(N4f12, 1000, 1000)); y_n4f12 = collect(rand(N4f12, 1000, 1000));
z_n4f4  = N4f4.( rand(1000, 1000));
z_n4f12 = N4f12.(rand(1000, 1000));

@btime $x_n0f8 .* $y_n0f8;
@btime $x_n0f16 .* $y_n0f16;
@btime saturating_mul.($x_n4f4, $y_n4f4);
@btime saturating_mul.($x_n4f12, $y_n4f12);
@btime $x_n4f4 .* $z_n4f4;
@btime $x_n4f12 .* $z_n4f12;
Julia v1.5.0
before
Julia v1.5.0
after
Julia v1.4.2
before
Julia v1.4.2
after
x_n0f8 .* y_n0f8 1.965 ms 77.600 μs 1.978 ms 85.100 μs
x_n0f16 .* y_n0f16 2.172 ms 165.900 μs 2.168 ms 166.200 μs
saturating_mul.(x_n4f4, y_n4f4) - 105.900 μs - 104.100 μs
saturating_mul.(x_n4f12, y_n4f12) - 304.200 μs - 302.700 μs
$x_n4f4 .* $z_n4f4 (checked_mul) 2.018 ms 794.400 μs 1.991 ms 794.700 μs
$x_n4f12 .* $z_n4f12(checked_mul) 2.636 ms 1.086 ms 2.688 ms 1.090 ms

@kimikage kimikage force-pushed the mul_normed branch 2 times, most recently from e330f1d to 817f18f Compare August 15, 2020 16:05
@kimikage kimikage changed the title [WIP] Optimize multiplication for Normed Optimize multiplication for Normed Aug 15, 2020
@kimikage kimikage marked this pull request as ready for review August 15, 2020 16:24
Copy link
Member

@timholy timholy left a comment

Choose a reason for hiding this comment

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

Remarkable work as usual!

This adds `wrapping_mul`, `saturating_mul` and `checked_mul` binary operations.
However, this does not specialize them for `Fixed` and does not change `*` for `Fixed`.

This replaces most of Normed's implementation of multiplication with integer operations.
This improves the speed in many cases and the accuracy in some cases.
@kimikage
Copy link
Collaborator Author

Thank you for all your reviews.
As for the default arithmetic settings, I will ask for feedback again (in Discourse?) before the release of v0.9.

@kimikage kimikage merged commit 134646f into JuliaMath:master Aug 24, 2020
@kimikage kimikage deleted the mul_normed branch August 24, 2020 05:39
@kimikage kimikage mentioned this pull request Sep 10, 2020
@kimikage kimikage mentioned this pull request Apr 30, 2024
38 tasks
kimikage added a commit to kimikage/FixedPointNumbers.jl that referenced this pull request May 1, 2024
This adds `wrapping_mul` and `checked_mul` binary operations for `Normed`.

This replaces most of Normed's implementation of multiplication with integer operations.
This improves the speed in many cases and the accuracy in some cases.
kimikage added a commit to kimikage/FixedPointNumbers.jl that referenced this pull request May 1, 2024
This adds `wrapping_mul` and `checked_mul` binary operations for `Normed`.

This replaces most of Normed's implementation of multiplication with integer operations.
This improves the speed in many cases and the accuracy in some cases.
kimikage added a commit to kimikage/FixedPointNumbers.jl that referenced this pull request May 1, 2024
This adds `wrapping_mul` and `checked_mul` binary operations for `Normed`.

This replaces most of Normed's implementation of multiplication with integer operations.
This improves the speed in many cases and the accuracy in some cases.
kimikage added a commit to kimikage/FixedPointNumbers.jl that referenced this pull request May 1, 2024
This adds `wrapping_mul` and `checked_mul` binary operations for `Normed`.

This replaces most of Normed's implementation of multiplication with integer operations.
This improves the speed in many cases and the accuracy in some cases.
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.

Optimizing multiplication for Normed
2 participants