Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   vectorized computation in C++ such as those in Matlab (Matlab toC++)? (http://www.velocityreviews.com/forums/t627023-vectorized-computation-in-c-such-as-those-in-matlab-matlab-toc.html)

 Luna Moon 07-22-2008 02:08 PM

vectorized computation in C++ such as those in Matlab (Matlab toC++)?

Dear all,

Can C++/STL/Boost do the vectorized calculation as those in Matlab?

For example, in the following code, what I really want to do is to
send in a vector of u's.

All other parameters such as t, l1, l2, l3, etc. are scalars...

But u is a vector.

Thus, t6 becomes a vector.

t9 is an element-wise multiplication...

The following code was actually converted from Matlab.

If vectorized computation is not facilitated, then I have to call this
function millions of times.

But if vectorized computation is okay, then I can send in just a u
vector with batch elements a time.

I have many such code in Matlab need to be converted into C++ with
vectorization.

Any thoughts?

Thank you!

double t5, t6, t7, t9, t11, t13, t16, t20, t23, t27, t32, t34, t36,
t37, t38, t42,
t44, t47, t48, t51, t52, t54, t59, t60, t61, t66, t67, t69, t74,
t75, t76, t81,
t82, t84, t87, t105, t106, t110, t112;

t5 = exp(-t * l1 - t * l2 - t * l3);
t6 = t * u;
t7 = mu1 * mu1;
t9 = u * u;
t11 = kappa * kappa;
t13 = 0.1e1 / (t9 * t7 + t11);

 Leandro Melo 07-22-2008 03:00 PM

Re: vectorized computation in C++ such as those in Matlab (Matlab toC++)?

> Dear all,
>
> Can C++/STL/Boost do the vectorized calculation as those in Matlab?
>
> For example, in the following code, what I really want to do is to
> send in a vector of u's.
>
> All other parameters such as t, l1, l2, l3, etc. are scalars...
>
> But u is a vector.
>
> Thus, t6 becomes a vector.
>
> t9 is an element-wise multiplication...
>
> The following code was actually converted from Matlab.
>
> If vectorized computation is not facilitated, then I have to call this
> function millions of times.
>
> But if vectorized computation is okay, then I can send in just a u
> vector with batch elements a time.
>
> I have many such code in Matlab need to be converted into C++ with
> vectorization.
>
> Any thoughts?
>
> Thank you!
>
> * * * * * * * * double t5, t6, t7, t9, t11, t13, t16, t20, t23, t27, t32, t34, t36,
> t37, t38, t42,
> * * * * * * * * * * * * t44, t47, t48, t51, t52, t54, t59, t60, t61, t66, t67, t69, t74,
> t75, t76, t81,
> * * * * * * * * * * * * t82, t84, t87, t105, t106, t110, t112;
>
> * * * * * * * * t5 = exp(-t * l1 - t * l2 - t * l3);
> * * * * * * * * t6 = t * u;
> * * * * * * * * t7 = mu1 * mu1;
> * * * * * * * * t9 = u * u;
> * * * * * * * * t11 = kappa * kappa;
> * * * * * * * * t13 = 0.1e1 / (t9 * t7 + t11);

Hi.

I think matlab provides a c++ api. Have you checked it out? There's
also the matrix template library for general algebra computations. You
might find it useful.

--
Leandro T. C. Melo

 Luna Moon 07-22-2008 03:38 PM

Re: vectorized computation in C++ such as those in Matlab (Matlab toC++)?

I don't think Matlab's C++ API can do that. I think it is just a C
interface. It does not have STL, Boost etc.

Also, we are not talking about things as complicated as high speed
matrix computation, it's just vectorized computation...

On Jul 22, 11:00*am, Leandro Melo <ltcm...@gmail.com> wrote:
> > Dear all,

>
> > Can C++/STL/Boost do the vectorized calculation as those in Matlab?

>
> > For example, in the following code, what I really want to do is to
> > send in a vector of u's.

>
> > All other parameters such as t, l1, l2, l3, etc. are scalars...

>
> > But u is a vector.

>
> > Thus, t6 becomes a vector.

>
> > t9 is an element-wise multiplication...

>
> > The following code was actually converted from Matlab.

>
> > If vectorized computation is not facilitated, then I have to call this
> > function millions of times.

>
> > But if vectorized computation is okay, then I can send in just a u
> > vector with batch elements a time.

>
> > I have many such code in Matlab need to be converted into C++ with
> > vectorization.

>
> > Any thoughts?

>
> > Thank you!

>
> > * * * * * * * * double t5, t6, t7, t9, t11, t13, t16, t20, t23, t27, t32, t34, t36,
> > t37, t38, t42,
> > * * * * * * * * * * * * t44, t47, t48, t51, t52, t54, t59, t60, t61, t66, t67, t69, t74,
> > t75, t76, t81,
> > * * * * * * * * * * * * t82, t84, t87, t105, t106, t110, t112;

>
> > * * * * * * * * t5 = exp(-t * l1 - t * l2 - t * l3);
> > * * * * * * * * t6 = t * u;
> > * * * * * * * * t7 = mu1 * mu1;
> > * * * * * * * * t9 = u * u;
> > * * * * * * * * t11 = kappa * kappa;
> > * * * * * * * * t13 = 0.1e1 / (t9 * t7 + t11);

>
> Hi.
>
> I think matlab provides a c++ api. Have you checked it out? There's
> also the matrix template library for general algebra computations. You
> might find it useful.
>
> --
> Leandro T. C. Melodt

 Bart van Ingen Schenau 07-22-2008 04:36 PM

Re: vectorized computation in C++ such as those in Matlab (Matlab to C++)?

Luna Moon wrote:

> Dear all,
>
> Can C++/STL/Boost do the vectorized calculation as those in Matlab?

I don't know what Boost has in the field of matrix & vector
computations, but standard C++ does not have anything even remotely
resembling the capabilities of Matlab.

The closest you can get with standard C++ is to use std::valarray<>,
which was intended to facilitate computations that can potentially be
executed in parallel.

Bart v Ingen Schenau
--
a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq
c.l.c FAQ: http://c-faq.com/
c.l.c++ FAQ: http://www.parashift.com/c++-faq-lite/

 Lionel B 07-22-2008 04:37 PM

Re: vectorized computation in C++ such as those in Matlab (MatlabtoC++)?

On Tue, 22 Jul 2008 07:08:38 -0700, Luna Moon wrote:

> Dear all,
>
> Can C++/STL/Boost do the vectorized calculation as those in Matlab?

What exactly do you mean by "vectorized calculation as those in Matlab"?
Do you just mean that Matlab has a native vector type and does
calculations with it, or were you suggesting that Matlab processes
vectors in some special way that C++ cannot?

Matlab, AFAIK, does a lot of its matrix/vector arithmetic, such as dot
products and matrix-matrix or matrix-vector multiplication, using a BLAS
library - that is highly optimised linear algebra code (generally written
in Fortran) - which is accessible via C++, since there is a well-defined
interface for C++ (C, really) and Fortran. There is a good chance you
will already have a BLAS library on your system; if not, there are open
source (e.g,. the ATLAS project) as well as vendor-supplied versions
(e.g. Intel, AMD, etc supply BLAS libraries).

It is possible that Matlab will also make use of very machine-specific
optimisations such as sse/mmx for floating point computation. You can use
these too from C++ if you can persuade your compiler to play ball.

The bottom line is that there's nothing Matlab can do that you can't do
in C++, equally (if not more) efficiently. It's more a question of
convenience: Matlab is designed specifically for vector/matrix
manipulation - C++ is a general-purpose programming language.

> For example, in the following code, what I really want to do is to send
> in a vector of u's.
>
> All other parameters such as t, l1, l2, l3, etc. are scalars...
>
> But u is a vector.
>
> Thus, t6 becomes a vector.
>
> t9 is an element-wise multiplication...
>
> The following code was actually converted from Matlab.
>
> If vectorized computation is not facilitated, then I have to call this
> function millions of times.
>
> But if vectorized computation is okay, then I can send in just a u
> vector with batch elements a time.

I'm really not quite sure what you mean here.

The closest thing in C++ to a Matlab vector is probably the
std::valarray<double> class, although it seems a bit of a bodge and hence
rather unpopular. The std::vector<double> class will probably do you
quite well; it doesn't implement functionality such as element-wise
multiplication, so you will have to do that yourself - but that's pretty
simple.

There are also various matrix/vector C++ libraries knocking around (e.g.
Blitz++) that you might want to look at.

In terms of efficiency, if you are doing a lot of large matrix
multiplications or more sophisticated linear algebra a la Matlab, then
you might want to investigate the BLAS and possibly LAPACK (Linear
Algebra Package), but I suspect that might be overkill in your case. And
it is ugly.

FWIW, I recently ported a lot of Matlab code to C++ and have to say that
C++ generally kicks Matlab's a*se in terms of efficiency - but not in
ease of coding (Matlab appears to suffer performance-wise from a lot of
internal copying which you can eliminate in hand-coded C++).

> I have many such code in Matlab need to be converted into C++ with
> vectorization.
>
> Any thoughts?
>
> Thank you!
>
> double t5, t6, t7, t9, t11, t13, t16, t20, t23, t27, t32,

t34, t36,
> t37, t38, t42,
> t44, t47, t48, t51, t52, t54, t59, t60, t61, t66,

t67, t69, t74,
> t75, t76, t81,
> t82, t84, t87, t105, t106, t110, t112;
>
> t5 = exp(-t * l1 - t * l2 - t * l3);
> t6 = t * u;
> t7 = mu1 * mu1;
> t9 = u * u;
> t11 = kappa * kappa;
> t13 = 0.1e1 / (t9 * t7 + t11);

--
Lionel B

 Rune Allnor 07-22-2008 05:16 PM

Re: vectorized computation in C++ such as those in Matlab (Matlab toC++)?

On 22 Jul, 18:37, Lionel B <m...@privacy.net> wrote:
> On Tue, 22 Jul 2008 07:08:38 -0700, Luna Moon wrote:
> > Dear all,

>
> > Can C++/STL/Boost do the vectorized calculation as those in Matlab?

>
> What exactly do you mean by "vectorized calculation as those in Matlab"?
> Do you just mean that Matlab has a native vector type and does
> calculations with it, or were you suggesting that Matlab processes
> vectors in some special way that C++ cannot?

It is a common misconception amongst matlab users that there is
something special about vectors. Matlab has historically been
very slow when executing explicit for-loops and while-loops etc.
The 'standard' matlab way to deal with this is to bypass the
interpreter and call compiled code, often from BLAS or LAPACK,
by 'vectorizing' the matlab code. I commented on that just a
few days ago on comp.soft-sys.matlab:

The problem is that users who only know matlab and no other
programming
languages are conditioned to believe that the problem lies with for-
loops
as such, and not with matlab.

Rune

 Lionel B 07-23-2008 09:26 AM

Re: vectorized computation in C++ such as those in Matlab (MatlabtoC++)?

On Tue, 22 Jul 2008 10:16:08 -0700, Rune Allnor wrote:

> On 22 Jul, 18:37, Lionel B <m...@privacy.net> wrote:
>> On Tue, 22 Jul 2008 07:08:38 -0700, Luna Moon wrote:
>> > Dear all,

>>
>> > Can C++/STL/Boost do the vectorized calculation as those in Matlab?

>>
>> What exactly do you mean by "vectorized calculation as those in
>> Matlab"? Do you just mean that Matlab has a native vector type and does
>> calculations with it, or were you suggesting that Matlab processes
>> vectors in some special way that C++ cannot?

>
> It is a common misconception amongst matlab users that there is
> something special about vectors. Matlab has historically been very slow
> when executing explicit for-loops and while-loops etc. The 'standard'
> matlab way to deal with this is to bypass the interpreter and call
> compiled code, often from BLAS or LAPACK, by 'vectorizing' the matlab
> code. I commented on that just a few days ago on comp.soft-sys.matlab:

Indeed. And if you look inside any BLAS or LAPACK you'll see... loops.
Cleverely structured loops, to be sure - to exploit processor
architecture features such as cache structure and special floating point
facilities, maybe even some true parallelization if you're on a
multiprocessor system - but loops nonetheless. How could it be otherwise
on a serial processing CPU?

> The problem is that users who only know matlab and no other programming
> languages are conditioned to believe that the problem lies with for-
> loops as such, and not with matlab.

Compilers are getting pretty clever these days and can often achieve
similar optimizations as Matlab no doubt deploys - as well as some of the
more sophisticated optimizations implemented in modern BLAS and LAPACK
libraries - with C++ (or C or Fortran) loops through vectors. [Intel's
compilers in particular are pretty impressive with floating point
optimization, GCC and Microsoft not far behind].

A recent experience of mine involved re-writing some Matlab code in C++.
For straightforward vector and matrix operations (essentially BLAS levels
1 & 2) I used explicit for loops while for matrix-matrix multiplication
(BLAS level 3) and higher order linear algebra calculations (like SVD and
eigenvalues) I plugged into a BLAS/LAPACK library (the same BLAS/LAPACK
that my Matlab installation uses). The resultant code (compiled by a
recent GCC) ran on the order of 10-20x faster than the Matlab code. My
suspicion is that Matlab's extra overhead was incurred through unecessary
(from an algorithmic perspective) copying of large vectors and matrices.

--
Lionel B

 allnor@tele.ntnu.no 07-23-2008 12:26 PM

Re: vectorized computation in C++ such as those in Matlab (Matlab toC++)?

On 23 Jul, 11:26, Lionel B <m...@privacy.net> wrote:
> On Tue, 22 Jul 2008 10:16:08 -0700, Rune Allnor wrote:
> > On 22 Jul, 18:37, Lionel B <m...@privacy.net> wrote:
> >> On Tue, 22 Jul 2008 07:08:38 -0700, Luna Moon wrote:
> >> > Dear all,

>
> >> > Can C++/STL/Boost do the vectorized calculation as those in Matlab?

>
> >> What exactly do you mean by "vectorized calculation as those in
> >> Matlab"? Do you just mean that Matlab has a native vector type and does
> >> calculations with it, or were you suggesting that Matlab processes
> >> vectors in some special way that C++ cannot?

>
> > It is a common misconception amongst matlab users that there is
> > something special about vectors. Matlab has historically been very slow
> > when executing explicit for-loops and while-loops etc. The 'standard'
> > matlab way to deal with this is to bypass the interpreter and call
> > compiled code, often from BLAS or LAPACK, by 'vectorizing' the matlab
> > code. I commented on that just a few days ago on comp.soft-sys.matlab:

>
> Indeed. And if you look inside any BLAS or LAPACK you'll see... loops.

Exactly. I attended a conference on underwater acoustics many years
ago, where one of the presentations dealt with 'efficient
computation.'
In effect, the matlab code was rewritten from readable code (i.e. for-
loops) to 'vectorized' matlab code. That presentation was, in fact,
the inspiration for making the test I pointed to yesterday.

Rune

 rocksportrocker 07-23-2008 01:29 PM

Re: vectorized computation in C++ such as those in Matlab (Matlab toC++)?

On 22 Jul., 16:08, Luna Moon <lunamoonm...@gmail.com> wrote:
> Dear all,
>
> Can C++/STL/Boost do the vectorized calculation as those in Matlab?
>
> For example, in the following code, what I really want to do is to
> send in a vector of u's.
>
> All other parameters such as t, l1, l2, l3, etc. are scalars...
>
> But u is a vector.
>
> Thus, t6 becomes a vector.
>
> t9 is an element-wise multiplication...
>
> The following code was actually converted from Matlab.
>
> If vectorized computation is not facilitated, then I have to call this
> function millions of times.
>
> But if vectorized computation is okay, then I can send in just a u
> vector with batch elements a time.
>
> I have many such code in Matlab need to be converted into C++ with
> vectorization.
>
> Any thoughts?
>
> Thank you!
>
> * * * * * * * * double t5, t6, t7, t9, t11, t13, t16, t20, t23, t27, t32, t34, t36,
> t37, t38, t42,
> * * * * * * * * * * * * t44, t47, t48, t51, t52, t54, t59, t60, t61, t66, t67, t69, t74,
> t75, t76, t81,
> * * * * * * * * * * * * t82, t84, t87, t105, t106, t110, t112;
>
> * * * * * * * * t5 = exp(-t * l1 - t * l2 - t * l3);
> * * * * * * * * t6 = t * u;
> * * * * * * * * t7 = mu1 * mu1;
> * * * * * * * * t9 = u * u;
> * * * * * * * * t11 = kappa * kappa;
> * * * * * * * * t13 = 0.1e1 / (t9 * t7 + t11);

Why do you want that ? Is it because the code is easier to read, or do
you hope
to get better performance ?

If it is for performance: Writing native loops in C/C++ with some
optimization flags
will give you best performance in most cases. Sometimes optimized BLAS
like
ATLAS will improve performance further.

Vectorized code in Matlab is faster than looping code, because in the
latter the
loops are interpreted which slows things down. Internally Matlab works
as described
above.

Greetings, Uwe

 Bo Schwarzstein 07-25-2008 01:20 AM

Re: vectorized computation in C++ such as those in Matlab (Matlab toC++)?

On Jul 22, 10:08 pm, Luna Moon <lunamoonm...@gmail.com> wrote:
> Dear all,
>
> Can C++/STL/Boost do the vectorized calculation as those in Matlab?
>
> For example, in the following code, what I really want to do is to
> send in a vector of u's.
>
> All other parameters such as t, l1, l2, l3, etc. are scalars...
>
> But u is a vector.
>
> Thus, t6 becomes a vector.
>
> t9 is an element-wise multiplication...
>
> The following code was actually converted from Matlab.
>
> If vectorized computation is not facilitated, then I have to call this
> function millions of times.
>
> But if vectorized computation is okay, then I can send in just a u
> vector with batch elements a time.
>
> I have many such code in Matlab need to be converted into C++ with
> vectorization.
>
> Any thoughts?
>
> Thank you!
>
> double t5, t6, t7, t9, t11, t13, t16, t20, t23, t27, t32, t34, t36,
> t37, t38, t42,
> t44, t47, t48, t51, t52, t54, t59, t60, t61, t66, t67, t69, t74,
> t75, t76, t81,
> t82, t84, t87, t105, t106, t110, t112;
>
> t5 = exp(-t * l1 - t * l2 - t * l3);
> t6 = t * u;
> t7 = mu1 * mu1;
> t9 = u * u;
> t11 = kappa * kappa;
> t13 = 0.1e1 / (t9 * t7 + t11);

GSL,GNU Octave,boost

All times are GMT. The time now is 06:03 AM.