Question

I want to accelerate my algorithm using long long instead of double data type. My algorithm is to find the shortest path in a directed acyclic graph (DAG). Simply, it adds the weight of an edge "E: a->b" to b, and if the new weight of b is lower than the previous one, it is updated along with its parent which is set to a.

I mean, my algorithm is simply some addition and comparison operations. The weight of edges are originally of "double", is it possible for me to multiply them to a large number and cast them to "long long". If this tweak makes my program faster and worth considering. How can I handle instability problems due to rounding big double to long long.

Thanks

Était-ce utile?

La solution

On i5 x64 even imul seems to about 40% faster [than double multiplication]. Integer addition should also happen in fewer cycles / better throughput. About the "inexactness" problem you should be aware that doubles can be more inexact than integers.

Calculate which numbers cause problems when converting decimal to floating point?

If you have access to the original data (e.g. decimal representation of the weights, multiplying them with a large power of ten should produce exact integers without any rounding artifacts. With long longs the only concern will be that of an overflow.

How to address possible rounding instability depends on the dynamic range of your weights, and the maximum number of iterations. E.g. if your weights are all less than 1.0 and larger than 2^-52, then multiplying with 2^52 gives exact integers with no rounding errors. Then the "instability" is determined by the possibility of an overflow. (2^12 * 2^52) >= 2^64.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top