Re: [eigen] Advanced vectorization |
[ Thread Index |
Date Index
| More lists.tuxfamily.org/eigen Archives
]
- To: eigen@xxxxxxxxxxxxxxxxxxx
- Subject: Re: [eigen] Advanced vectorization
- From: Benoit Jacob <jacob.benoit.1@xxxxxxxxx>
- Date: Mon, 6 Jun 2011 07:34:23 -0400
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:date :message-id:subject:from:to:content-type:content-transfer-encoding; bh=cDmbdDS7G7bNbK01UrS93k+IikXLu42+zGB4TcChnJk=; b=OCOhzUrwJXKclKW3VB6pIXYVpmXp9SH81JDSXQUkEv1werdLLpI8ztEIucKeJKJk6l mK188khgBzqsHHL9AR2QeQ32V+L3lcN6KXm2BSojhKWE2JTRz8FI9O1SmOQ8CGI1c7nT UI9EsnkmqlLMHyqtJYk2/O9h8jEHncdYkbneU=
- 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=t9+AZrRPNLImg0/zphRRm2IDScAiE1NCIOuba7Sf+4EzIyxu3DOykkCUWJGDglLfeD Xz4mO3IX9mOHJOVyiej22arv1irDmAE6NCPCzlvyM2bxi5l78KzgeOOjJkQKjBSjKkao P57EC6itBJQoMF3IuwN1QncPewpZ96w0S9Udk=
2011/6/6 Márton Danóczy <marton78@xxxxxxxxx>:
> Hi all,
>
> I'm trying to optimize the evaluation of a squared loss function and
> its gradient. That is, I'd like to calculate
>
> L = 0.5 ||Ax-b||^2
> dL/dx = A'(Ax-b)
>
> where A has more columns than rows, i.e. A'A would be huge and caching
> it would be infeasible.
>
> Right now, i have the following routine:
>
> Scalar objfunc(const Matrix<Scalar, Dynamic, 1>& x, Matrix<Scalar,
> Dynamic, 1>& g)
> {
> e.noalias() = A * x - b;
> g.noalias() = A.adjoint() * e;
> return Scalar(0.5) * e.squaredNorm();
> }
>
> where /e/ is pre-allocated as a class member. When hand coding this,
> instead of storing /e/, I would calculate it component-wise and
> accumulate its squared norm along the way, thus avoiding iterating
> twice. Is there a clever way to accomplish this with Eigen?
If A is of size n*n, then just reading it is n^2 memory accesses,
while you trick would be saving only n memory accesses (since e is a
vector of size n), so I'd say it's going to be negligible unless n is
very small. Also, by computing e coefficient-wise, you lose the
benefit of Eigen's cache-friendly matrix-vector product
implementation, which is important for large n.
Just benchmark it ;-)
Benoit
>
> Thanks,
> Marton
>
>
>