Re: [eigen] Signed or unsigned indexing |

[ Thread Index | Date Index | More lists.tuxfamily.org/eigen Archives ]

*To*: eigen <eigen@xxxxxxxxxxxxxxxxxxx>*Subject*: Re: [eigen] Signed or unsigned indexing*From*: Gael Guennebaud <gael.guennebaud@xxxxxxxxx>*Date*: Sat, 21 Jan 2017 10:42:29 +0100*Dkim-signature*: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to; bh=F8PldgbGyXLTfY4qDMtNeyR41CuveZBY42vXk3al7tI=; b=E1WF+ow//kp7XB5AX23dkR1hvZrjiGJxWlCN4zbjebFjeUVYcBEAJzbeGCprjEz2C9 YPNZm/3HZkNBOs7kK/Efo1iW208pUNvICnu/Brnqgb+RZbGKSI+gNkXGkD0i4eqqi0/a QDT3yo94s/BadxAo3mFGrzwW6xz2d/y5Cfbmwv6YtkDXnLTPU6rqpuVVyzCCwfTVrnxw qz5xGb+r7ORcqnv0T6II+YpUvK+EvVR9eriiLYDxkpAzDgy0/rEElw9RLZFVhKgw5axl FUD9rKQmqfwt1bLKWxf96jGUuq8QV4X6sMnEBsTu+FwrGC6JEB1VO52ENJ2L18kxstzK Ax3Q==

On Fri, Jan 20, 2017 at 8:45 PM, François Fayard <fayard@xxxxxxxxxxxxx> wrote:

- Once the war of signed/unsigned comes to an end, there is also the problem of 32/64 bits integers on a 64-bit machine. If you have bandwidth problems, or vectorized code, the choice is obvious: use 32-bits indices. But what should you do in other cases? One has to know that when if p is a pointer and k is a 32-bit integer, to get the address of p[k] one need to convert first k to a 64-bit integer. So, you might think that using 64-bit indices is better on x86-64. I even found a very contrived benchmark where it is indeed more efficient. But for almost every code, it just does not make any difference.

I remember we observed a clear speedup when we moved from int to ptrdiff_t in Eigen. That was before we release 2..0, so with much older compilers (gcc 4.2) and older hardware (core2).

Actually, both questions are highly related because when you start mixing 32 and 64 bits integers, I have to admit that unsigned types win here because the conversion is a no op in this case, whereas signed types require a special treatment. In Eigen, this happens when using SparseMatrix or PermutationMatrix for which 32 bits signed integers are used by default so save memory usage. All these conversions are explicitly handled by a convert_index<To,From>(From x) helper function with assertion checking. To overcome the signed conversion overhead, we could add a convert_positive_index<To,From>(From x) variant asserting that x>0 and enforcing a cheap conversion (as in the unsigned case).

Then regarding Eigen moving to unsigned integers (or supporting unsigned integers, that's the same), that's not gonna happen because there are numerous places where we truly need signed integers, and as previously stated by others this would mean that for every use of the current Index type we would have to carefully think whether it should be signed or unsigned (considering possible future usages for which negative indices could make sense), and then be extremely careful for every operations (addition, comparison, assignment,etc) involving two Index types to be sure they are both signed or both unsigned. We have enough subtleties to take of.. Sorry.

Regarding the "extra bit" argument, it does make sense for the StorageIndex type when the number of non-zeros is in [2^31..2^32] on a 64 bits system. For instance, SparseMatrix<double,0,unsigned int> would save 1/4 of memory consumption compared to SparseMatrix<double,0,Index>. I'm sure this justify all the pain of supporting both signed and unsigned types as the StorageIndex.

gael

**Follow-Ups**:**Re: [eigen] Signed or unsigned indexing***From:*Francois Fayard

**References**:**[eigen] Signed or unsigned indexing***From:*Henrik Mannerström

**Re: [eigen] Signed or unsigned indexing***From:*Benoit Jacob

**Re: [eigen] Signed or unsigned indexing***From:*Francois Fayard

**Re: [eigen] Signed or unsigned indexing***From:*Mark Borgerding

**Re: [eigen] Signed or unsigned indexing***From:*Martin Beeger

**Re: [eigen] Signed or unsigned indexing***From:*François Fayard

**Messages sorted by:**[ date | thread ]- Prev by Date:
**Re: [eigen] Signed or unsigned indexing** - Next by Date:
**Re: [eigen] Signed or unsigned indexing** - Previous by thread:
**Re: [eigen] Signed or unsigned indexing** - Next by thread:
**Re: [eigen] Signed or unsigned indexing**

Mail converted by MHonArc 2.6.19+ | http://listengine.tuxfamily.org/ |