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

Add version of tree with double internals #4

Open
sqeezy opened this issue Mar 1, 2021 · 16 comments
Open

Add version of tree with double internals #4

sqeezy opened this issue Mar 1, 2021 · 16 comments
Labels
enhancement New feature or request

Comments

@sqeezy
Copy link

sqeezy commented Mar 1, 2021

It would be super helpful to have a version of this library that works with double instead of float internals. Would this be a possible addition? I would be happy to provide a pull request.

@mcserep
Copy link
Owner

mcserep commented Mar 6, 2021

Hello,

Do you have a suggestion which would solve this without duplicating the complete codebase?
For example it would be really elegant, but unfortunately I don't see how this could work e.g. by using a generic parameter instead of float, as there are a lot of mathematical operations going on, which depend on the mathematical operators (+, -, *, /, etc.).

@mcserep
Copy link
Owner

mcserep commented Mar 14, 2021

I will close this issue now, but in case you submit a pull request, I would be glad to review it.

@mcserep mcserep closed this as completed Mar 14, 2021
@sqeezy
Copy link
Author

sqeezy commented Mar 22, 2021

It seems there is no easy way to have generic implementations on number types because there are no contracts/constrains for numerical operators. One would need to implement a kind of calculator class for every generic parameter (double, float). This kind of implementation makes the code harder to read and might bring performance penalties.
Another option would be templating. It could be done via something like T4 templates or Source Generators. This is performant but might have a problem regarding readability and maintainability.
It seems to me that some way forward is needed though, if the goal is to make this library usable in a wider field.
On that note. I would be happy to help and plan, but I don't think a pull request is the first step here. Some planning needs to be done first.

@mcserep
Copy link
Owner

mcserep commented Mar 22, 2021

I think T4 templates can make it work while also keeping the codebase maintainable and readable. Since mostly only the numerical operators and static methods from System.Math are used (which have overloads for double), this shall basically work by simply replacing float with a placeholder. (A little extra work is required, e.g. there is a MathExtensions.Clamp function, which shall be defined for doubles as well.)

That way a PointOctree_Single and PointOctree_Double type could be generated compile time. The PointOctree class can inherit from PointOctree_Single (without adding anything to it), so the codebase will also remain backward compatible.
Of course the same shall be applied on the other types.

Note: I wouldn't go for Source Generators, as it is still a new feature under preview as far as I have followed it.

@mcserep mcserep reopened this Mar 22, 2021
@sqeezy
Copy link
Author

sqeezy commented Mar 23, 2021

I'm not firm on how to use T4 templates. It seems to be the proper tool here though. As it seems to me that the dependencies of this project should be minimal, I'd use something like dotnet-t4 to transform the templates.
I am happy to help at some point and provide a pull request, most likely early to give you a chance on early feedback. I am not yet sure when I will get to it.

@myblindy
Copy link

Hello,

Do you have a suggestion which would solve this without duplicating the complete codebase?
For example it would be really elegant, but unfortunately I don't see how this could work e.g. by using a generic parameter instead of float, as there are a lot of mathematical operations going on, which depend on the mathematical operators (+, -, *, /, etc.).

FYI .Net 6 added support for generic mathematical operators.

@mcserep
Copy link
Owner

mcserep commented Aug 20, 2021

Thanks @myblindy for sharing this information here. Generic mathematical operators would significantly simplify this issue.
Unfortunately it seems that it is only a preview feature in .NET 6, not intended for production usage yet.
https://devblogs.microsoft.com/dotnet/preview-features-in-net-6-generic-math/

@mcserep mcserep added the enhancement New feature or request label Feb 14, 2022
@mcserep
Copy link
Owner

mcserep commented Aug 31, 2022

Since then we have moved on to use the Vector3 type from System.Numerics (see #5).

Further development can be made to use Vector<T>.
I will close this now, but in case you submit a PR, I would be glad to review it.

@mcserep mcserep closed this as completed Aug 31, 2022
@mcserep mcserep added the wontfix This will not be worked on label Aug 31, 2022
@sqeezy
Copy link
Author

sqeezy commented Sep 11, 2022

I will test an implementation using Vector<T> soon, but it seems that the optimizations are not nearly as good with this BCL type. Blog Article for Reference

@mcserep
Copy link
Owner

mcserep commented Sep 18, 2022

Thanks for the update @sqeezy on this topic. The related blog post is useful and interesting, although the results seems te be odd for me at some points. (The custom vector implementation was better than the built-in Vector type on Xamarin.iOS.)
The blog post is also 3 years old, so if you could make a test with Vector<T>, that would be really nice.

@mcserep
Copy link
Owner

mcserep commented Sep 29, 2022

I reopen this issue, as in another issue #8 , it was suggested by @ricaun that we could use the System.DoubleNumerics library for double internal support.

With the original idea of using T4 templates or Source Generators, this could provide a solution which could be implemented without much effort.

@mcserep mcserep reopened this Sep 29, 2022
@mcserep mcserep removed the wontfix This will not be worked on label Sep 29, 2022
@sqeezy
Copy link
Author

sqeezy commented Sep 29, 2022

I believe Vector is not really useful here. When having exactly three long vectors all the time, the fixed length types should be better.

How did you come across System.DoubleNumerics? This one is new to me.

@mcserep
Copy link
Owner

mcserep commented Sep 29, 2022

System.DoubleNumerics was suggested by @ricaun . I was also not familiar with it before, mostly it seems like a simple copy-paste of System.Numerics' source code, modified to use doubles instead of float.

This way we could use the fixed length (3) version, no need for Vector<T>.

@sqeezy
Copy link
Author

sqeezy commented Sep 29, 2022

In the end this wouldn't give us the SIMD optimization of the framework types. At least that's my understanding.

After short research that actually should work with Vector so my test must have been flawed. I will look into that again.

Reference: https://stackoverflow.com/questions/51225026/vectordouble-weak-simd-performance/51289848#51289848

@mcserep
Copy link
Owner

mcserep commented Sep 29, 2022

These SIMD optimizations should automatically kick in to my knowledge, but I am not an expert on this topic.

At least some people managed to implement custom vector types with the same performance as the built-in ones.
Reference: https://stackoverflow.com/questions/62049826/is-it-possible-to-create-a-custom-vector-type-with-performance-similar-to-system

@ricaun , since you mentioned you have used System.DoubleNumerics for a longer time, what was your experience with it? Have you ever compared its performance to the classic System.Numerics library?

@ricaun
Copy link
Contributor

ricaun commented Sep 30, 2022

These SIMD optimizations should automatically kick in to my knowledge, but I am not an expert on this topic.

At least some people managed to implement custom vector types with the same performance as the built-in ones. Reference: https://stackoverflow.com/questions/62049826/is-it-possible-to-create-a-custom-vector-type-with-performance-similar-to-system

@ricaun , since you mentioned you have used System.DoubleNumerics for a longer time, what was your experience with it? Have you ever compared its performance to the classic System.Numerics library?

I believe this is the source code of the package: https://github.com/Weingartner/System.Numerics.DoubleVectors

In my case, I need to compute vector and quaternions but my input is in double format. First I was transforming double to float to make work and I found this package that is a copy of System.Numerics but with double and makes the code much cleaner.

I didn't make any performance benchmark.

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

No branches or pull requests

4 participants