Comments (12)
Hi. I can only guess without seeing the code but you're using most of the facilities of the library and some of them incur run-time costs of one kind or another.
- Are you sure saturation is what you want? It often produces completely wrong results: it's not 'sticky' like floating-point saturation. Can you not simply avoid overflow instead? Try using the trapping tag instead and see if there is actually any overflow and then see why that is.
- How many digits are actually being used? If it's greater than 63, you may be invoking multi-word wide_integer types. If it's over 127, you almost certainly are.
- I have to ask: do you have optimisations enabled, in particular inlining?
from cnl.
Hi.
-
Yes, I would need saturation. Maybe i will try to use the overflow integer, and just let the program run without it to see if there are any improvements.
-
I am actually using this: using float_t = saturated_elastic_scaled_integer<5, -4>;
-
Yes, i have optimizations enabled!
Do you have any other suggestions? Maybe i can try to avoid using wide_integer too? Thank you very much!!
from cnl.
With numbers that small, wide_integer
shouldn't affect things although if your compiler is unable to inline code aggressively enough, it might get in the way of optimisation. You certainly shouldn't need it. You can test this by removing it and seeing if there are compiler errors.
But really, saturated_overflow_tag is probably your biggest problem. Try replacing with trapping_overflow_tag. If that runs without error, try undefined_overflow_tag to see if that improves performance. Currently, you're testing many operations for overflow -- many of which cannot overflow and all of which arguably shouldn't overflow. When overflow occurs, errors of arbitrary scale creep in, often accumulating over time.
from cnl.
Can I ask if you're emulating saturating hardware? If so, does every last operation saturate?
from cnl.
Hi, I did some tests and removing overflow_integer made the simulations run faster. Now it requires more or less 30 minutes! So it is a good time.
Regarding the hardware, yes. I am trying to emulate a specific hardware and I would need every operation that i perform to stay on 6 bits let's say. That's why i was using overflow_integer with the saturated_overflow_tag. Are there any alternatives?
from cnl.
When you use float, how is are the values being saturated?
from cnl.
Maybe it is better if i explain the scenario on which i am working. I am currently using the types provided by CNL inside Tiny-DNN, that is a library for deep learning in C++. I am training my models using float and then i have to perform the inference using fixed-point (to simulate the hardware, since for now it is only possible to do inference and not training). So i am not saturating when i use float, instead when i use fixed-point i would need the values to saturate.
from cnl.
Try to use types with just enough range to always avoid overflow and reduce incidence of conversion between types with different scales. For example, if b
has non-zero exponent, the following incurs a scaling operation:
a = a * b;
To that end, use auto
to try and avoid unnecessary conversion:
auto c = a * b; // guaranteed not to scale
Division can be more expensive for integers than for floats. So try and produce an inverse against which you can multiply many values. If that inverse has the right exponent, you will also avoid a scaling operation and get much more efficient code.
Think about other ways to avoid expensive calculations like replacing sqrt(distance) < radius
with distance*distance < radius*radius
. Remember that elastic_integer
helps you avoid the overflow by widening which is generally cheaper than testing for overflow and far more accurate than saturating.
Perhaps if you can extract the inner loop and get it building on Compiler Explorer (example). If it's a simple piece of code, I'd be happy to take a quick look. Otherwise, it's still a lot of guesswork.
Remember you're using a fraction of the silicon. The downside is that much of the burden for handling range rests with you.
from cnl.
I think i will go with elastic_integer! Thank you for all the suggestions!!
from cnl.
from cnl.
from cnl.
Yes, using the simplest conversion, it will scale up or down as necessary. The lower digits will be lost, i.e. precision loss. (That's kind-of inevitable.) If the number was too big to fit, you will get erroneous results.
One other thing that might help performance: use overflow_integer
with its default tag (undefined) and in an optimised build, define NDEBUG
. This tells the optimiser to assume there's no overflow.
Example: https://godbolt.org/z/sxYqvjanv
from cnl.
Related Issues (20)
- Don't reopen std::
- remove cnl/bit.h
- Clang-Tidy won't re-run when .clang-tidy changes
- ensure FetchContent-able
- Put slowest tests first
- the godbolt link in the readme does not compile
- Build error on v1.1.2: redefinition of ‘struct std::hash<std::filesystem::__cxx11::path>’ HOT 5
- v1.1.7 crash after multiplication HOT 1
- Publishing CNL 1.x libraries to platformio HOT 3
- Serialization. HOT 3
- Missing rounding conversion operators HOT 1
- MSVC sanitizers HOT 2
- Simplify `__int128` support. HOT 1
- CMake via Conan? HOT 1
- Add base parameter to to_chars HOT 2
- Only build CNL as Debug HOT 1
- to_chars_static allocates buffer too small for -ve numbers HOT 1
- Replace CMAKE_TOOLCHAIN_FILE with CMAKE_PROJECT_INCLUDE_BEFORE HOT 4
- How to use as scaled decimal number (1.x) HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cnl.