Add version of tree with double internals
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.
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.).
I will close this issue now, but in case you submit a pull request, I would be glad to review it.
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.
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.
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.
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.
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/
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.
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
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.
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.
I believe Vector<T> 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.
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>.
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<T> 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
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?
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.