Re: [eigen] Vectorized Hamming distance |
[ Thread Index |
Date Index
| More lists.tuxfamily.org/eigen Archives
]
- To: eigen@xxxxxxxxxxxxxxxxxxx
- Subject: Re: [eigen] Vectorized Hamming distance
- From: Benoit Jacob <jacob.benoit.1@xxxxxxxxx>
- Date: Wed, 25 Nov 2009 07:21:10 -0500
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :date:message-id:subject:from:to:content-type :content-transfer-encoding; bh=GxOV+y/qb7EsFzbwHaagT0fcVXh0ytmer+cHtgWvfck=; b=CW4kT+aMOQUx+xT2IMa6kPO4Kt23d6fLiKkN66gXYKXbsz/HXQMup/ZthzbDX4o1Wp Vdn2cDqiS/aSMZG0QkRacbaJ0qeB+LeCknnnXr2c55VaSfP+przdamIFftFMX7ZeuZk5 44x1trFFqelj2Yg6XDm6q8Ke0Y6q4hWkPKS/E=
- Domainkey-signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; b=KJwmp1ASiGSUJ8M/AmGLL3E4faKUghFX2b/MnFcfrwrl3eRTo7wYWxbV7E5ilFTVan LuKDdfcAOXE+y+lQBTgLx2t3FF6GK4unB9SrRX0VRuS7qIeM2pv9eL1BZoyEb8nBi8j3 QHYIw+e+CgOzo4QgS6LoPxF62sRaIdRNtdMcw=
2009/11/25 rodrigo benenson <rodrigo.benenson@xxxxxxxxx>:
> Hello,
> I would like to use the Eigen librabry to write a vectorized (using
> SIMD instruction) version of the Hamming Distance between two
> Matrix<int, N, 1>.
> (following the spirit of Benoît Jacob's "Eigen2 as a vectorization
> library" blog post)
Coincidentally, the current thread "true Array support" is exactly about that.
>
> The hamming distance consists on doing XOR between two vectors and
> then counting the number of bits in the resulting vector.
>
> Any suggestion on how to do this ?
>
> Why is it that Eigen does not support boolean operations on int data types ?
>
> Anyone has tried something similar before ?
I don't recall any use case appearing before today, that's probably
why we don't have it. But with the increasing number of people using
Eigen with integer types, that seems like a reasonable feature
addition. And actually we already have the SIMD support for xor:
ei_pxor(a,b) in the development branch.
How to do this:
First of all I assume the development branch, not 2.0. It's not much
different, it's just that it already has SIMD xor support.
First see how to implement a custom binary operator:
http://eigen.tuxfamily.org/dox-devel/classEigen_1_1MatrixBase.html#ae022bf30f910d5d1b016c3d88f6af1ad
Then see this page:
http://eigen.tuxfamily.org/dox/CustomizingEigen.html#ExtendingMatrixBase
about how to add a new method in MatrixBase from outside Eigen, by
defining EIGEN_MATRIXBASE_PLUGIN. You can also put it in Cwise if you
want a cwise() syntax, using EIGEN_CWISE_PLUGIN. And your functor
should go in EIGEN_FUNCTORS_PLUGIN.
Then have a look at other functors to see how SIMD is done, for
example look at ei_scalar_sum_op in Core/Functors.h:
for example here's how division is implemented:
template<typename Scalar> struct ei_scalar_quotient_op EIGEN_EMPTY_STRUCT {
EIGEN_STRONG_INLINE const Scalar operator() (const Scalar& a, const
Scalar& b) const { return a / b; }
template<typename PacketScalar>
EIGEN_STRONG_INLINE const PacketScalar packetOp(const PacketScalar&
a, const PacketScalar& b) const
{ return ei_pdiv(a,b); }
};
template<typename Scalar>
struct ei_functor_traits<ei_scalar_quotient_op<Scalar> > {
enum {
Cost = 2 * NumTraits<Scalar>::MulCost,
PacketAccess = ei_packet_traits<Scalar>::size>1
#if (defined EIGEN_VECTORIZE_SSE)
&& NumTraits<Scalar>::HasFloatingPoint
#endif
};
};
You can see that basically SIMD is implemented in the packetOp()
method, and setting PacketAccess to 1 in the ei_functor_traits.
So in your case you'd use ei_pxor instead of ei_pdiv, and that's it.
Benoit
>
> Regards,
> rodrigob.
>
>
>