f



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

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);
0
lunamoonmoon (257)
7/22/2008 2:08:38 PM
comp.soft-sys.matlab 211265 articles. 25 followers. lunamoonmoon (257) is leader. Post Follow

26 Replies
1721 Views

Similar Articles

[PageSpeed] 48

> 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!
>
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 double t5, t6, t7, t9, t11, t13, t16, t20=
, t23, t27, t32, t34, t36,
> t37, t38, t42,
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 t44, t47, t48, t51, t52, =
t54, t59, t60, t61, t66, t67, t69, t74,
> t75, t76, t81,
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 t82, t84, t87, t105, t106=
, t110, t112;
>
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 t5 =3D exp(-t * l1 - t * l2 - t * l3);
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 t6 =3D t * u;
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 t7 =3D mu1 * mu1;
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 t9 =3D u * u;
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 t11 =3D kappa * kappa;
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 t13 =3D 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
0
ltcmelo (61)
7/22/2008 3:00:51 PM
Luna Moon <lunamoonmoon@gmail.com> wrote in message 
<e0f8d278-54bf-4826-bcca-
bbbd88bfaec6@79g2000hsk.googlegroups.com>...
> 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);

Write some Matrix algebra classes with overloaded operators 
that mimic Matlab's behaviour.  Then you'll find code 
conversion becomes simple.
0
7/22/2008 3:20:05 PM
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=A0am, 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!
>
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 double t5, t6, t7, t9, t11, t13, t16, t=
20, t23, t27, t32, t34, t36,
> > t37, t38, t42,
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 t44, t47, t48, t51, t52=
, t54, t59, t60, t61, t66, t67, t69, t74,
> > t75, t76, t81,
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 t82, t84, t87, t105, t1=
06, t110, t112;
>
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 t5 =3D exp(-t * l1 - t * l2 - t * l3);
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 t6 =3D t * u;
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 t7 =3D mu1 * mu1;
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 t9 =3D u * u;
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 t11 =3D kappa * kappa;
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 t13 =3D 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





0
lunamoonmoon (257)
7/22/2008 3:38:21 PM
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?

No. The computations are done element-wise, but you can write wrapper
functions which let you as user view things as vector operations.

> For example, in the following code, what I really want to do is to
> send in a vector of u's.
....
> 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 assume you have a few million elements you want to send as argument
to the computations, and that each operation on a scalar will produce
a scalar result. So the vector is only a container for the input and
output data. Here is an example on how to do things (not a complete
program):

#include <vector>                 // You need this one to use the
vector class
#include <math>                   // You need this to get access to
some
                                  // maths functions
std::vector<double> input;        // Declare a vector for the input

double myfunction(double x)
{
  // Implement your computations here, e.g.
  y=sin(x);
}

std::vector<double> myfunction(std::vector<double> x)
{
    // Wrapper function that lets you call the function
    // for each element in a vector

    std::vector<double> y;  // Declare the vector for results
    y.reserve(x.size());    // You already know how many elements y
                            // needs to hold, so reserve the space
                            // up front

    for (int n=0;n<x.size();++n)  // Now the loop.
    {
        y[n]=myfunction(x[n]);    // Note the same name on the
functions!
    }
    return y;
}

and there you are. When you want to use this you just write

    std::vector<double> x;
    // Initialize x
    std::vector<double> y = myfunction(x);

as you would in matlab. You just tell the compiler what you want and
the compiler is assigned the task of picking exactly which variant
of 'myfunction' is used, based on the type of the argument you
call it with.

Try this:

    std::vector<double> x;
    // Initialize x
    std::vector<double> y = myfunction(x);
    double a = 3.14;
    double b;
    b = myfunction(a);

Two functions with the same name are used, but since the argument
types are different, the compiler figures out which variant to
use in each case.

Rune
0
allnor (8509)
7/22/2008 3:54:14 PM
On 22 Jul, 17:54, Rune Allnor <all...@tele.ntnu.no> wrote:

Saw a typo just after I sent the first message...

> double myfunction(double x)
> {
> =A0 // Implement your computations here, e.g.
> =A0 y=3Dsin(x);
    return (y);   // << =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D  Missed thi=
s line....
> }

Rune
0
allnor (8509)
7/22/2008 3:59:06 PM
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/
0
bart855 (270)
7/22/2008 4:36:31 PM
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
0
me4 (19624)
7/22/2008 4:37:22 PM
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:

http://groups.google.no/group/comp.soft-sys.matlab/msg/e9699dcd19dcbe49?hl=no&dmode=source

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
0
allnor (8509)
7/22/2008 5:16:08 PM
Bart van Ingen Schenau <bart@ingen.ddns.info> wrote in 
message <9740147.dvEKTklcxX@ingen.ddns.info>...
> 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

I've always found it easier to define my own classes which 
use STL vectors (etc) to store the data and have overloads 
to work on the data (& sometimes specialised conversion 
constructor to convert types).  Operating directly on the 
STL vectors in main code gets messy.

0
7/23/2008 8:08:01 AM
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
0
me4 (19624)
7/23/2008 9:26:54 AM
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
0
allnor (8509)
7/23/2008 12:26:08 PM
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!
>
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 double t5, t6, t7, t9, t11, t13, t16, t20=
, t23, t27, t32, t34, t36,
> t37, t38, t42,
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 t44, t47, t48, t51, t52, =
t54, t59, t60, t61, t66, t67, t69, t74,
> t75, t76, t81,
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 t82, t84, t87, t105, t106=
, t110, t112;
>
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 t5 =3D exp(-t * l1 - t * l2 - t * l3);
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 t6 =3D t * u;
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 t7 =3D mu1 * mu1;
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 t9 =3D u * u;
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 t11 =3D kappa * kappa;
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 t13 =3D 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
0
7/23/2008 1:29:32 PM
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
0
7/25/2008 1:20:52 AM
Luna Moon <lunamoonmoon@gmail.com> wrote:
<snip>
> 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);

This is out of my element, but I have written some vector code in GCC, just
for my own edification. I'm not sure how you would translate the above
code... I don't exactly see the vectors, unless I'm misinterpreting. Anyhow:

http://gcc.gnu.org/onlinedocs/gcc-4.3.0/gcc/Vector-Extensions.html#Vector-Extensions

It's exceedingly simple. ICC and MSC support similar, though incompatible
syntax. With GCC, make sure to manually specify -march, otherwise the
generator won't have access to SSE instructions (or whatever your platform
has).

The biggest initial syntax gotcha I encountered was initializing the vector;
the vector can be treated like an array, IIRC, but I encountered some
hangups. Second, GCC has to load all the SSE registers, and other sourcery I
wasn't acquainted with. There just seems like there'd be lots of headaches
keeping the pipeline chugging along, depending on your data set, and where
it comes from.

Also, I have no idea if the syntax carriers over to C++. And perhaps GCC can
already accomplish similar optimizations with valarrays. Either way, you'll
definitely want to use the latest GCC 4.3 version, which AFAIK is at the
moment king of the hill regarding auto-vectorization.

0
William
7/27/2008 1:29:16 AM
Hello,

my question is pretty much related to this topic so I continue
the thread instead of opening another one.
I'm a former Matlab coder and a wannabe C++ coder.

I've read about std::valarray<> in the Stroustrup's book
"The C++ Programming Language", discovering that some operators
(like the multiplication *), and some basic math functions
like sin and cos, are overloaded in order to behave very similar
to Matlab ones when dealing with valarrays (mainly, they act
component-wise).

I'm aware of Matlab "vectorization" techniques; I use them to
avoid for-loops.
But when I do that I _don't_ do linear algebra: I just do
component-wise operations between matrices, in order to
save Matlab from doing serial calls to the same routine (via for-
loops).
I dont't think such code gains anything from using BLAS or
similarities.
I mean: taking the inverse of a matrix is linear algebra,
but multiplying two vectors component-wise is just... multiplying.

rocksportrocker
> If it is for performance: Writing native loops in C/C++ with some
> optimization flags will give you best performance in most cases.

Best performance than Matlab, or best performance than
"vectorized" C++?

Rune Allnor:
> 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.

My question:
When writing C++ code, do you thing I can have faster code
if I use std::valarray<> in "the Matlab way", instead
of using, say, std::vector<> and for-loops?

I must admit that this curiosity comes from my previous Matlab
experiences, when I used to think to for-loops as being the devil,
but from a naive point of view I can image that "vectorized"
operations on std::valarray<> are optimized by smart compilation
techniques... after all the programmer doesn't specify the
order with which the operation as to be done, like in a foor-loop...
Is this just fantasy?
--NOTE that the scenario I have in mind is a single core machine.

Regards,
Giovanni Gherdovich
0
gherdovich (40)
8/6/2008 12:54:29 PM
On 6 Aug, 14:54, Giovanni Gherdovich
<gherdov...@students.math.unifi.it> wrote:
> Hello,
>
> my question is pretty much related to this topic so I continue
> the thread instead of opening another one.
> I'm a former Matlab coder and a wannabe C++ coder.
>
> I've read about std::valarray<> in the Stroustrup's book
> "The C++ Programming Language", discovering that some operators
> (like the multiplication *), and some basic math functions
> like sin and cos, are overloaded in order to behave very similar
> to Matlab ones when dealing with valarrays (mainly, they act
> component-wise).
>
> I'm aware of Matlab "vectorization" techniques; I use them to
> avoid for-loops.

That's a *matlab* problem. 'Vectorization' is a concept
exclusive to matlab, which historically was caused by
what I consider to be bugs in the matlab interpreter.

> But when I do that I _don't_ do linear algebra: I just do
> component-wise operations between matrices, in order to
> save Matlab from doing serial calls to the same routine (via for-
> loops).
> I dont't think such code gains anything from using BLAS or
> similarities.
> I mean: taking the inverse of a matrix is linear algebra,
> but multiplying two vectors component-wise is just... multiplying.
>
> rocksportrocker
>
> > If it is for performance: Writing native loops in C/C++ with some
> > optimization flags will give you best performance in most cases.
>
> Best performance than Matlab,

Depending on exactly what you do, matlab *can* get very
close to best-possible performance since it uses highly
tuned low-level libraries. If your operation is covered by
such a function, you might find it difficult to beat matlab.
If not, don't be surprised if C++ code beats matlab by
a factor 5-10 or more.

> or best performance than
> "vectorized" C++?

There is no such thing as 'vectorized C++.'

> Rune Allnor:
>
> > 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.
>
> My question:
> When writing C++ code, do you thing I can have faster code
> if I use std::valarray<> in "the Matlab way", instead
> of using, say, std::vector<> and for-loops?

I don't know. I haven't used std::valarray<>. I know I have
seen some comment somewhere that std::valarray<> was an early
attempt at a standardized way to handle numbercrunching in C++,
which was, well, not quite as successful as one might have
whished for.

> I must admit that this curiosity comes from my previous Matlab
> experiences, when I used to think to for-loops as being the devil,
> but from a naive point of view I can image that "vectorized"
> operations on std::valarray<> are optimized by smart compilation
> techniques...

You would be surprised: There are for-loops at the core of all
those libraries, even the BLAS libraries matlab is based on.
There are smart compilation techniques involved, but to *optimize*
the for-loops, not to *eliminate* them.

> after all the programmer doesn't specify the
> order with which the operation as to be done, like in a foor-loop...

I wrote a sketch to illustrate how this is done in a previous
post in this thread, which was posted only to comp.soft-sys.matlab:

http://groups.google.no/group/comp.soft-sys.matlab/msg/2ab57a27d663e1fa?hl=no

As you can see, the 'vector' version myfunction(std::vector<double>)
calls the scalar version myfunction(double) in a for-loop. This is
essentially what is done in all the libraries you use, including
matlab.

The for-loops are at the core, and the smart compiler techniques
optimize the executable code to avoid any unnecessary run-time
overhead.

> Is this just fantasy?

You might want to have a look at the basic texts on modern C++.
Try "Accelerated C++" by Koenig & Moo, or "You can do it!" by
Glassborow. Or both.

Rune
0
allnor (8509)
8/6/2008 1:27:42 PM
In article <6d972a8d-8bc3-44d7-9a40-40044c35131c@x41g2000hsb.googlegroups.com>,
Rune Allnor  <allnor@tele.ntnu.no> wrote:
>On 6 Aug, 14:54, Giovanni Gherdovich
><gherdov...@students.math.unifi.it> wrote:

>> I'm aware of Matlab "vectorization" techniques; I use them to
>> avoid for-loops.
>
>That's a *matlab* problem. 'Vectorization' is a concept
>exclusive to matlab, which historically was caused by
>what I consider to be bugs in the matlab interpreter.

To describe this property of matlab as "buggy" is inordinately harsh.

It's an inherent property of interpreters: If you're interpreting a
loop, you have to look at the loop condition code, and the loop
bookkeeping code, and the code inside the loop, every time through.
Unless you go out of your way to make this fast, you end up having to
do a lookup-decode-process for each of those steps.
Compiling to native code lets you do the lookup-decode at compile time,
and for typical loops only generates a few machine-code instructions
for the loop bookkeeping and condition checking, which substantially
reduces the total amount of work the processor is doing.  But making an
interpreter clever enough to do interpreted loops that fast is a Much
Harder Problem.

(So, the answer to the OP's question is (as already noted): Don't worry
about vectorizing, write loops and ask the compiler to optimize it, and
you'll probably come close enough to Matlab's performance that you
won't be able to tell the difference.)

Since Matlab is targeting numerical work with large arrays anyways,
there's not much benefit to speeding up this part of the interpreter;
if the program is spending most of its time inside the large-matrix
code (which is compiled to native code, aggressively optimized by the
compiler, and probably hand-tuned for speed), then speeding up the
interpreter's handling of the loop won't gain you any noticeable
speedup anyways.  If you're writing loopy code to do things Matlab has
primitives for, you're probably better off vectorizing it anyways,
since that will make it both clearer and faster.
So (unlike with general-purpose interpreted languages that don't have
primitives that replace common loop idioms) there's no real benefit to
speeding up the Matlab interpreter's loop handling, and there are
obvious costs (development time, increased complexity, more potential
for bugs), so there are good reasons not to bother.

If you do have code that doesn't fit Matlab's vectorization model, you
can always write it in C or Fortran and wrap it up in a Matlab FFI
wrapper; Matlab's FFI is not hard to use on the compiled-to-native-code
side, and looks exactly like a Matlab function on the Matlab code side,
so it's almost always the Right Tool For The Job in that case.
(At my day job, I've been asked to do this for the Matlab programmers a
few times, and for hard-to-vectorize loopy code getting a speedup of
two or three orders of magnitude just by doing a reasonably direct
translation into C and compiling to native code with an optimizing
compiler is pretty much expected.)



dave

-- 
Dave Vandervies                               dj3vande at eskimo dot com
Erm... wouldn't clock(), used with Bill Godfrey's follow-up, ignoring my
follow-up to him (as suggested in your follow-up to me), do the trick
quite nicely?                           --Joona I Palaste in comp.lang.c
0
dj3vande3 (264)
8/6/2008 5:37:47 PM
On 6 Aug, 19:37, dj3va...@csclub.uwaterloo.ca.invalid wrote:
> In article <6d972a8d-8bc3-44d7-9a40-40044c351...@x41g2000hsb.googlegroups=
..com>,
> Rune Allnor =A0<all...@tele.ntnu.no> wrote:
>
> >On 6 Aug, 14:54, Giovanni Gherdovich
> ><gherdov...@students.math.unifi.it> wrote:
> >> I'm aware of Matlab "vectorization" techniques; I use them to
> >> avoid for-loops.
>
> >That's a *matlab* problem. 'Vectorization' is a concept
> >exclusive to matlab, which historically was caused by
> >what I consider to be bugs in the matlab interpreter.
>
> To describe this property of matlab as "buggy" is inordinately harsh.

Here is a test example I first made ten years ago:

http://groups.google.no/group/comp.soft-sys.matlab/msg/e9699dcd19dcbe49?hl=
=3Dno

Do you think an interpreter overhead on the order of 100-200x
is acceptable? Note that in recent versions of matlab the same
example is only 5-10 slower than compiled code. I don't know
what has changed, but that sort of improvement is almost
certainly not possible unless there were one or more serious
bugs there in the first place.

Rune
0
allnor (8509)
8/6/2008 6:01:27 PM
In article <cad30e01-6396-479d-ad51-ae01abb0e49e@34g2000hsh.googlegroups.com>,
Rune Allnor  <allnor@tele.ntnu.no> wrote:
>On 6 Aug, 19:37, dj3va...@csclub.uwaterloo.ca.invalid wrote:
>> In article
><6d972a8d-8bc3-44d7-9a40-40044c351...@x41g2000hsb.googlegroups.com>,
>> Rune Allnor  <all...@tele.ntnu.no> wrote:

>> >That's a *matlab* problem. 'Vectorization' is a concept
>> >exclusive to matlab, which historically was caused by
>> >what I consider to be bugs in the matlab interpreter.
>>
>> To describe this property of matlab as "buggy" is inordinately harsh.
>
>Here is a test example I first made ten years ago:
>
>http://groups.google.no/group/comp.soft-sys.matlab/msg/e9699dcd19dcbe49?hl=no

So you can, by deliberately avoiding features that are in the language
and run in vanishingly small amounts of time, write code that exercises
the interpreter in ways that slow it down.

Do you write your own matrix multiplication code too?


>Do you think an interpreter overhead on the order of 100-200x
>is acceptable?

It depends on how the interpreter is used and how difficult it is to
avoid the overhead in cases where speed matters.
For Matlab?  Certainly, especially since it's often trivial and rarely
difficult to run compiled-to-native code from the interpreter in cases
where it matters.  The language is built as an interpreted shell that
calls native-code routines for primitives like matrix multiplication,
and when most of the running time in basic code is inside the primitives
anyways, when you want less-basic operations it makes sense to work
with the model that you already have - it's not broken.
If you really want to do something inside Matlab that doesn't reduce
nicely to primitives that are already in the language, you can always
write your own native code routine and call it from the interpreter.
Matlab's FFI is easy to use on the foreign side and completely
transparent on the Matlab side, so there's no good reason not to use
it.
(Of course, if what you're really trying to accomplish is writing loops
that run as fast as possible, you should be using Fortran anyways.
There's a reason we have more than one programming language.)


Exercise for the reader:
A native-code sum loop can be written in four instructions with one
cache-friendly memory access per iteration:
--------
top:
    addl (%ebx,%edx,4),%eax
    incl %edx
    cmpl %edx,%ecx
    jne top
--------
Write an interpreter that can run a comparable interpreted loop using
fewer than 400-800 instructions per iteration.  Keep in mind that
memory access is expensive; cache misses (including for interpreter
code) count as no less than a few dozen instructions and possibly a few
hundred.


>     Note that in recent versions of matlab the same
>example is only 5-10 slower than compiled code. I don't know
>what has changed, but that sort of improvement is almost
>certainly not possible unless there were one or more serious
>bugs there in the first place.

Bullshit.  There are plenty of good reasons why it might have improved
that much without having any bugs, or even bad engineering tradeoffs,
in the original version.

It's not beyond the realm of possibility that hardware capabilities
have increased in ways that disproportionately affect interpreted code,
even without any changes to the Matlab system.  (Or, depending on how
carefully you did your measurements, possibly the only thing that's
changed is that setup and measurement overhead are now big enough to
skew the numbers).

A more likely effect of increasing hardware capabilities is that
interpreter cleverness like pre-compiling to bytecode became
cost-effective.  When you have a slow computer running interpreted code
that's mostly calls into primitives implemented as well-tuned native
code, the additional startup cost of making the interpreter cleverer
might be near-impossible to make up in improved running times once it's
started, but if the same startup cost happens ten or twenty times as
fast, it could very well fall below the threshold of what people
notice, and thereby become worth doing.

Another possible motivation for interpreter improvements is that the
primitives have improved (because of better tuning and/or new processor
instructions that can run them faster) to the point where the
interpreter speed actually makes a noticeable effect on properly
written code, which increases the benefits of improving it to the point
where they exceed the costs of doing so.

It's also not impossible that one of the motivations behind improving
it was that the Matlab people got sick of listening to idiots who can't
tell the difference between a hammer and a screwdriver whinging about
how hard it was to pound in nails with their screwdriver, and decided
that it was easier to add nail-pounding capabilities than to continue
to deal with the complaints.


And that's just off the top of my head; people who actually build and
work with interpreters could probably give you a rather longer list.


dave

-- 
Dave Vandervies                          dj3vande at eskimo dot com
>[An] optimizing compiler is no substitute for a good programmer
A good programmer is also no substitute for an optimizing compiler.
               --Nils O. Selesdal and Keith Thompson in comp.lang.c
0
dj3vande3 (264)
8/7/2008 3:34:10 AM
On 7 Aug, 05:34, dj3va...@csclub.uwaterloo.ca.invalid wrote:
> In article <cad30e01-6396-479d-ad51-ae01abb0e...@34g2000hsh.googlegroups.=
com>,
> Rune Allnor =A0<all...@tele.ntnu.no> wrote:
>
> >On 6 Aug, 19:37, dj3va...@csclub.uwaterloo.ca.invalid wrote:
> >> In article
> ><6d972a8d-8bc3-44d7-9a40-40044c351...@x41g2000hsb.googlegroups.com>,
> >> Rune Allnor =A0<all...@tele.ntnu.no> wrote:
> >> >That's a *matlab* problem. 'Vectorization' is a concept
> >> >exclusive to matlab, which historically was caused by
> >> >what I consider to be bugs in the matlab interpreter.
>
> >> To describe this property of matlab as "buggy" is inordinately harsh.
>
> >Here is a test example I first made ten years ago:
>
> >http://groups.google.no/group/comp.soft-sys.matlab/msg/e9699dcd19dcbe...
>
> So you can, by deliberately avoiding features that are in the language
> and run in vanishingly small amounts of time, write code that exercises
> the interpreter in ways that slow it down.
>
> Do you write your own matrix multiplication code too?
>
> >Do you think an interpreter overhead on the order of 100-200x
> >is acceptable?
>
> It depends on how the interpreter is used and how difficult it is to
> avoid the overhead in cases where speed matters.
> For Matlab? =A0Certainly, especially since it's often trivial and rarely
> difficult to run compiled-to-native code from the interpreter in cases
> where it matters. =A0

It seems you might not be aware how matlab is used these days.
For the last decade or so the focus has veered from "Matrix
laboraory" to "scientific computing language." People use
matlab not as the "MATrix LABoratory" it was originally
designed to be, but as a development environment and
programming language on a par with Java.

In my day job I have met people who used poorly implemented
matlab code at key points in their business, only to find
that where matlab is used is a bankrupcy-inducing bottleneck.

The data processing took so long that only the benevolence
from their main customer kept them in business - 99% of
customers would have claimed violation of contract and
sued for damage reimbursements.

When I showed the processors the test, every single one of
them went a whiter shade of pale, "I though matlab was the
absolutely best there was!"

> The language is built as an interpreted shell that
> calls native-code routines for primitives like matrix multiplication,
> and when most of the running time in basic code is inside the primitives
> anyways, when you want less-basic operations it makes sense to work
> with the model that you already have - it's not broken.

The matlab engine has two parts: The parser and the interpreter.
The parser checks for syntax, undeclared variables and those
sorts of things. The interpreter translates code that passed
parser into executable statements. Both steps must necesarily
introduce run-time overhead compared to compiled code, I have
no issue with that.

But do take another look at the code I posted: What on earth
could cause an interpreter overhead of ~150x in that simple
code?

One possible answer - and I have no if this is how things is
done presently or if they were done like this in the past - is
that the code inside the for-loop is parsed and interpreted
everey single time the interpreter enters the loop. The same
piece of code which doesn't change during execution is
interpreted 100000000 times.

Why is that acceptable to you? Once the code has been checked
by the parser, the inerpreter ought to know all the blocks
of code present. There is no need to interpret the code inside
that for-loop more than once. It will not change during the
execution of the script.

This is a very simple argument which *might* have been the
cause of the traditionally poor performance of matlab with
loops. Whether this is a 'bug' or a 'poor design' is a
matter of semantics; the damage has been done over the past
couple of decades, as students who try and learn programming
with matlab as their first language have been conditioned to
believe that 'for-loops are evil.'

Rune
0
allnor (8509)
8/7/2008 8:42:04 AM
On 7 Aug, 05:34, dj3va...@csclub.uwaterloo.ca.invalid wrote:
> In article <cad30e01-6396-479d-ad51-ae01abb0e...@34g2000hsh.googlegroups.=
com>,

> It's also not impossible that one of the motivations behind improving
> it was that the Matlab people got sick of listening to idiots who can't
> tell the difference between a hammer and a screwdriver whinging about
> how hard it was to pound in nails with their screwdriver, and decided
> that it was easier to add nail-pounding capabilities than to continue
> to deal with the complaints.

This may certainly have been a contributing factor, but if you look
into the history of matlab you will find that it is in fact the other
way around: For the past 15 years or so The Mathworks have purposedly
expanded the target customer base from traditional educational
and R&D institutions to the general computer industry.

The main slogan in the marketing material from 1995 vintage matlab 4
was 'rapid prototyping.' On the printed material accompanying my 2005
vintage matlab release, the keywords on the front page are:

 - Computation
 - Visualization
 - Programming
 - Modeling
 - Simulation
 - Implementation

No hint about 'development' or 'prototyping.' An unsuspecting
customer meeting matlab for the first time might very well
interpret this as if matlab is a tool for designing and
implementing production code.

If you go to today's on-line product description at

http://www.mathworks.com/products/matlab/description1.html

there is an item for "Developing algorithms and applications"
which is about the only clue to matlab's origins as a developer's
tool. I find the term 'rapid prototyping' buried deep in an
online page on 'Programming and Application Development':

http://www.mathworks.com/applications/tech_computing/description/progapp.ht=
ml

Some cut'n paste from that page:

"The MATLAB=AE environment is well suited to rapid prototyping and
application development."

We already agreed about matlab as a prototyping tool. For some
reason I think matlab  being presented as a tool for 'application
development' might come as a surprise to you.

Another one from the same page:

"MATLAB is the most productive development environment for creating
scientific and engineering applications because it offers powerful
tools for every step in the process to reduce your overall
development time."

Again, a definite (and maybe surprising) focus on *applications*,
not *prototypes*. And of course, it is the misplaced focus on
*development* time, not *run* time, which leaves the unsuspecting
down-the-line customers who use matlab-based applications in their
time-critical productions lines, hanging high and dry.

Rune
0
allnor (8509)
8/7/2008 10:04:52 AM
 Giovanni Gherdovich schrieb:
> Hello,
>
> my question is pretty much related to this topic so I continue
> the thread instead of opening another one.
> I'm a former Matlab coder and a wannabe C++ coder.
>
> I've read about std::valarray<> in the Stroustrup's book
> "The C++ Programming Language", discovering that some operators
> (like the multiplication *), and some basic math functions
> like sin and cos, are overloaded in order to behave very similar
> to Matlab ones when dealing with valarrays (mainly, they act
> component-wise).
>
> I'm aware of Matlab "vectorization" techniques; I use them to
> avoid for-loops.
> But when I do that I _don't_ do linear algebra: I just do
> component-wise operations between matrices, in order to
> save Matlab from doing serial calls to the same routine (via for-
> loops).
> I dont't think such code gains anything from using BLAS or
> similarities.
> I mean: taking the inverse of a matrix is linear algebra,
> but multiplying two vectors component-wise is just... multiplying.
>
yes. but you can do some enrollment or other
access patterns for optimizing cache access.
this is a broad field, look at:
http://en.wikipedia.org/wiki/Loop_transformation

>
>> If it is for performance: Writing native loops in C/C++ with some
>> optimization flags will give you best performance in most cases.
>>
>
> Best performance than Matlab, or best performance than
> "vectorized" C++?
>
best performance compared to vectorized code.
optimization flags of your compiler can force
loopoptimisation and other strategies.

>
>> 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.
>>
>
> My question:
> When writing C++ code, do you thing I can have faster code
> if I use std::valarray<> in "the Matlab way", instead
> of using, say, std::vector<> and for-loops?
>
I do not know how optimized valarray<> is. You
should compare it using  different matrix-/vector-sizes
and different optimization flags
of your compiler and post your results.

If you use GNU compilers, the flags are -O?
afaik -O0 up to -O3

And you should compare it to
     http://math-atlas.sourceforge.net/

which is supposed to gain very good performance.

> I must admit that this curiosity comes from my previous Matlab
> experiences, when I used to think to for-loops as being the devil,
> but from a naive point of view I can image that "vectorized"
> operations on std::valarray<> are optimized by smart compilation
> techniques... after all the programmer doesn't specify the
> order with which the operation as to be done, like in a foor-loop...
> Is this just fantasy?
>
In matlab for-loops are devil, because the interpreter
has to handle the loops, which slows things down.
If you make a navieve C implementation, you for-loops
are compiled to machinecode, which runs much faster
than the interpreted matlab for-loop.
vectorization gives matlab the ability to put the
operation into a optimized C routine, where the
essential and fast looping happens.

Greetings, Uwe

--
Dr. rer. nat. Uwe Schmitt
F&E Mathematik

mineway GmbH
Science Park 2
D-66123 Saarbr=FCcken

Telefon: +49 (0)681 8390 5334
Telefax: +49 (0)681 830 4376

uschmitt@mineway.de
www.mineway.de

Gesch=E4ftsf=FChrung: Dr.-Ing. Mathias Bauer
Amtsgericht Saarbr=FCcken HRB 12339


0
8/7/2008 2:37:25 PM
Hello,

thank you for your answers.

Rune Allnor:
> Depending on exactly what you do, matlab *can* get very
> close to best-possible performance since it uses highly
> tuned low-level libraries. If your operation is covered by
> such a function, you might find it difficult to beat matlab.
> If not, don't be surprised if C++ code beats matlab by
> a factor 5-10 or more.

dave:
> (So, the answer to the OP's question is (as already noted): Don't worry
> about vectorizing, write loops and ask the compiler to optimize it, and
> you'll probably come close enough to Matlab's performance that you
> won't be able to tell the difference.)

Uwe:
> I mean: taking the inverse of a matrix is linear algebra,
> but multiplying two vectors component-wise is just... multiplying.
>
> >  yes. but you can do some enrollment or other
> >  access patterns for optimizing cache access.
> >  this is a broad field, look at:
> >  http://en.wikipedia.org/wiki/Loop_transformation

Rune Allnor:
> You would be surprised: There are for-loops at the core of all
> those libraries, even the BLAS libraries matlab is based on.
> There are smart compilation techniques involved, but to *optimize*
> the for-loops, not to *eliminate* them.

I was among the user who are "conditioned to believe that the
problem lies with for-loops as such, and not with matlab",
to use Rune's words.
Thank you all to point it out.

About the performance of numerical computation done using
std::valarray<>'s features:

Uwe:
>  My question:
> When writing C++ code, do you thing I can have faster code
> if I use std::valarray<> in "the Matlab way", instead
> of using, say, std::vector<> and for-loops?
>
> >  I do not know how optimized valarray<> is. You
> >  should compare it using  different matrix-/vector-sizes
> >  and different optimization flags
> >  of your compiler and post your results.

Rune Allnor:
> I don't know. I haven't used std::valarray<>. I know I have
> seen some comment somewhere that std::valarray<> was an early
> attempt at a standardized way to handle numbercrunching in C++,
> which was, well, not quite as successful as one might have
> whished for.

It seems that nobody knows if it's worth to use std::valarray<>
and related "vectorized" operators (provided by the standard
library) to do numerical computing in C++.

Googling this topic, I've found this interesting thread in
a forum of a site called "www.velocityreviews.com"
http://www.velocityreviews.com/forums/t277285-p-c-stl-valarrays-vs-vectors.html

One of the poster, who (like me) took the chapter "Vector Arithmetic"
on Stroustrup's book as The Truth, says that with valarray<>
you can do math at the speed of light, blah blah optimization
blah blah vectorization and so on.

Another user answers with what I find a more reasonable argument:
std::valarray<> was designed to meet the characteristic of vector
machines, like the Cray. If you don't have the Cray, there is
no point in doing math with valarray<> and related operators.

Anyway, as soon as I have some spare time I will check it on
my own, comparing the results with ATLAS as Uwe suggests.

Regards,
Giovanni Gherdovich
0
gherdovich (40)
8/7/2008 4:48:11 PM
In article <a47fac46-bb33-4608-bd97-
f75cd419340f@d77g2000hsb.googlegroups.com>, 
gherdovich@students.math.unifi.it says...

[ ... ]

> Another user answers with what I find a more reasonable argument:
> std::valarray<> was designed to meet the characteristic of vector
> machines, like the Cray. If you don't have the Cray, there is
> no point in doing math with valarray<> and related operators.

In theory that's right: the basic idea was to provide something that 
could be implemented quite efficiently on vector machines. In fact, I've 
never heard of anybody optimizing the code for a vector machine, so it 
may be open to question whether it provides any real advantage on them.

OTOH, valarray _can_ make some code quite readable, so it's not always a 
complete loss anyway.

-- 
    Later,
    Jerry.

The universe is a figment of its own imagination.
0
jcoffin (2241)
8/8/2008 4:38:18 AM
Jerry Coffin wrote:
> In article <a47fac46-bb33-4608-bd97-
> f75cd419340f@d77g2000hsb.googlegroups.com>, 
> gherdovich@students.math.unifi.it says...
> 
> [ ... ]
> 
>> Another user answers with what I find a more reasonable argument:
>> std::valarray<> was designed to meet the characteristic of vector
>> machines, like the Cray. If you don't have the Cray, there is
>> no point in doing math with valarray<> and related operators.
> 
> In theory that's right: the basic idea was to provide something that 
> could be implemented quite efficiently on vector machines. In fact, I've 
> never heard of anybody optimizing the code for a vector machine, so it 
> may be open to question whether it provides any real advantage on them.

During the mid-90s both C and C++ were involved in adding features that 
would support numerically intense programming. Unfortunately a couple of 
years later the companies whose numerical experts doing the grunt work 
withdrew support. By hindsight it might have been better to have shelved 
the work but both WG14 and WG21 opted to continue hoping that they would 
still produce something useful.

It is not obvious to those outside the numerical fields that actually 
developing things like complex number support, support for array 
arithmetic etc. is fraught with subtle traps.


> 
> OTOH, valarray _can_ make some code quite readable, so it's not always a 
> complete loss anyway.
> 

-- 
Note that robinton.demon.co.uk addresses are no longer valid.
0
8/8/2008 12:12:48 PM
Hello,

> During the mid-90s both C and C++ were involved in adding features that
> would support numerically intense programming. Unfortunately a couple of
> years later the companies whose numerical experts doing the grunt work
> withdrew support.

Just for the sake of historical investigation, I found a thread on
this newsgroup from the far 1991, where Walter Bright (who might
be the same Walter Bright who designed the D programming language,
http://www.walterbright.com/
http://en.wikipedia.org/wiki/Walter_Bright , but I'm not sure)
lists some shortcomings for the C++ numerical programmer,
and item #6 is

"Optimization of array operations is inhibited by the 'aliasing'
problems."
http://en.wikipedia.org/wiki/Aliasing_(computing)

(retrieved from
http://groups.google.com/group/comp.lang.c++/browse_thread/thread/3df3ab6920fb71f5/feb0ec8ea7b24189?lnk=gst&q=numerical+analysis#feb0ec8ea7b24189

Then he mentions some solutions to this (two libraries, which
might be completely out of date nowaday).
Just to say that the Original Poster isn't the first to
address this issue...

> By hindsight it might have been better to have shelved
> the work but both WG14 and WG21 opted to continue hoping that they would
> still produce something useful.

Mmmh... I skimmed over the pages of Working Groups 14 and 21
http://www.open-std.org/jtc1/sc22/wg14
http://www.open-std.org/jtc1/sc22/wg21
and they don't seem to have vector arithmetic among their priorities.
Anyway, from what I've learned from this thread, the overall
theme can very well make no sense, because of CPUs characteristics.

Regards,
Giovanni Gh.
0
gherdovich (40)
8/8/2008 4:27:02 PM
Reply:

Similar Artilces:

C/C++ in matlab and matlab in C/C++ ?
Is it possible to read C/C++ code in matlab and the other way around too? Hi, as long as C or C++ are written in text files you can edit them in the matlab editor. To see the special character of C or C++ coding language, in the editor go to menus: file>preferences>Edito/Debugger>language and on popup "Language" select "C/C++". This does not allow you to run the C/C++ code, just to view it i nicer way... regards, chris saneman <asdfsdf@asd.com> wrote in message <frl846 $f8i$2@news.net.uni-c.dk>... > Is it possible to read C/C...

methods to call matlab in C C++ and call C C++ in matlab
Hi, I am reading the help documents about how to program matlab and C C++ together and my questions are a little bit general. It seems that are several ways to call matlab in C C++ and another several ways to call C C++ in matlab. Please correct me if I am wrong or missing something when I try to list them. To call matlab code in C C++, are there two methods available? The first one is Matlab engine and the second one is to use MATLAB Compiler mcc to create C or C++ shared libraries from your MATLAB code. To call C C++ in matlab, are there also two methods? The first one is to call funct...

matlab beginner: calling matlab from C/C++
I'm relatively new to the world of Matlab. I want to call my matlab own code from a C/C++ program on a machine with matlab installed. Do I need to compile the matlab code using matlab compiler tool or the matlab engine is enough for this? Thanks a lot for your help On Nov 28, 10:03 am, "Paeo Campigotto" <campigo...@dit.unitn.it> wrote: > I'm relatively new to the world of Matlab. > I want to call my matlab own code from a C/C++ program on a > machine with matlab installed. > Do I need to compile the matlab code using matlab compiler > tool or the matla...

C/C++ in MATALB or MATLAB in C/C++?
Hi I've been asking on the net how to use MEX properly so that I can use C/C++ in MATLAB, but information is not forthcoming. There seems to be a lot on using MATLAB in C/C++ but not the other way around. I understand Mathworks is trying very hard to sell the Matlab Compiler, but even so, why is it SO hard to get information about MEX? Or is it that I've missed something very fundamental, meaning that I'm struggling over something that is actually quite easy? Any thoughts and info would be great. Thanks BullDog BullDog wrote: > Hi > > I've been asking on the net ...

Conversion from MATLAB to C/C++ using MATLAB compiler
I am working on conversion of .m files to c/c++. I am using the MATLAB compiler version 3.0 and MATLAB version 6.5.1. There seem to be a few functions which do not seem to get converted. The error i got while using functions like TRAIN, NETWORK and INIT (all neural-networks related functions used in the general neural network programs, newpnn.m etc) is The MATLAB compiler does not currently support MATLAB object oriented programming and the functions TRAIN , NETWORK and INIT would produce a run-time error. Could anyone please tell me as to how i can circumnavigate this problem or solve this...

matlab beginner: calling matlab from C/C++ #2
I'm relatively new to the world of Matlab. I want to call my matlab own code from a C/C++ program on a machine with matlab installed. Do I need to compile the matlab code using matlab compiler tool or the matlab engine is enough for this? Thanks a lot for your help ...

Create (Matlab) object inside C/C++ and pass it to Matlab
I'm trying to create an object inside C++ and then pass it to Matlab via en= gPutVariable. It seems I can create a struct // Create a float array mxArray *acq_data =3D mxCreateNumericMatrix(0, 0, mxSINGLE_CLASS); float *data =3D (float *)mxCalloc(M*N, sizeof(float)); mxSetData(acq_data, data); mxSetM(acq_data, M); mxSetN(acq_data, N); // Convert to struct const char *fieldname =3D "data"; mxArray *acq_struct =3D mxCreateStructMatrix(1,1,1, &fieldname); mxSetField(acq_struct,i,"data",acq_data); // Convert to class ?? Does it work? if (j=3D=3D1...

C/C++ and MatLab
I am trying to call and debug matlab code via C/C++. i am able to run the engdemo.c program but I cannot stop the debugger in a test MatLab file to see if this can be done. I would like the C/C++ program to spawn the MatLab environment so that I may have access to all the MatLab debugging tools but be able to read any inputs from the C/C++ calling function and return any modifications to those values back to the C/C++ function as well. I am running MatLab 6.5.1.199709 v 13. Does MatLab 7 have any added capability that I would need to do this? Reginald Robertson wrote: > > > I am tr...

C/C++ to matlab
Hello, I have a code. I need to change that code for matlab. I am wandering if there is an article so I can read before start. Thank you "Ongun Palaoglu" <ongun@mac.com> wrote in message <hae0r2$erv$1@fred.mathworks.com>... > Hello, I have a code. I need to change that code for matlab. I am wandering if there is an article so I can read before start. Thank you check matlab help and read mex file creation. its pretty simple to call c from matlab. "Maya " <priyaravi_22@yahoo.co.in> wrote in message <hae4mr$nf6$1@fred.mathworks.com>... > "Ongun Palaoglu" <ongun@mac.com> wrote in message <hae0r2$erv$1@fred.mathworks.com>... > > Hello, I have a code. I need to change that code for matlab. I am wandering if there is an article so I can read before start. Thank you > > check matlab help and read mex file creation. its pretty simple to call c from matlab. Dear Maya, I do not want to call the function. I want to change the language to Matlabs own language . "Ongun Palaoglu" <ongun@mac.com> wrote in message <hae5r1$59l$1@fred.mathworks.com>... > "Maya " <priyaravi_22@yahoo.co.in> wrote in message <hae4mr$nf6$1@fred.mathworks.com>... > > "Ongun Palaoglu" <ongun@mac.com> wrote in message <hae0r2$erv$1@fred.mathworks.com>... > > > Hello, I have a code. I need to change that code for matlab. I am wandering if th...

Control of Matlab event loop from C or Matlab
I was wondering if there is a way to control Matlab event loop? Is there a way to make Matlab be able to respond to key presses in Matlab GUI or call from C (via engine) and to either pause or stop its execution. I know that Matlab can execute items on its 'event queue' when pause() or drawnow() etc are called, but that means that I have to explicitly put those in the code. Is there some way that would be more elegant? Second question is about the 'event queue.' How does one add things to the event queue? How can I add a call from, say, C engine such as evalString() to the ev...

Matlab command to convert Matlab script to C
I know that this has probably been asked before but I desperately need to do this. I have simple scripts in Matlab and I would like to see what they look like in C language. Can anyone help me with this? I'm not a great C programer so this would help me immensly if I could see various examples of things that I have written in Matlab in C Thank you. Hi finiteguy, check the MATLAB help for the MATLAB Compiler, if you have. Regards, Stefan Hi Stefan, I tried that and got no help. Nothing about Matlab compiler. I kew that years ago there was a simple command but I lost it. On Mon, 30...

Using MatLab in C/C++
Is it possible to call MatLab or a MatLab function in C or C++? If so, where can I find info on how to do that? Mike On Feb 11, 8:57 am, "Michael Stachowsky" <mstachow...@gmail.com> wrote: > Is it possible to call MatLab or a MatLab function in C or > C++? If so, where can I find info on how to do that? > > Mike See the Matlab engine http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_external/f38569.html Chris "Michael Stachowsky" <mstachowsky@gmail.com> wrote in message <fopk7d$bs2$1@fred.mathworks.com>... > Is it possibl...

Matlab for C/C++ programmers
well i attended the seminar this morning and found it really useful. so i decided to try the stuff out myself. I am using the same program that is provided in the slides of the seminar as below. #include "C:\MATLAB7\extern\include\engine.h" void main() { Engine *ep; double data_c[10] = {8,1,2,3,2,5,-1,7,8,3}; mxArray *data_ml; data_ml = mxCreateDoubleMatrix(1,10,mxREAL); memcpy((char *)mxGetPr(data_ml),(char *)data_c,10*sizeof(double)); ep = engOpen(NULL); // Transfer to MATLAB engPutVariable(ep,"data",data_ml); } everything seems to be working fine except th...

Calling matlab from C/C++
SUBJECT : Using matlab Engine. (Or Calling matlab from C). I have a problem in Calling matlab from C. Did any body used it before. I'm getting problem while Linking at Runtime.(During Build.. not while compiling). It Shows The Following Error Message. Error LNK2001 unresolved External Symbol. Even I couldn't Run the example program given by matlab. I tried engdemo.c and engwindemo.c given in $matlab/extern/examples/eng_mat/ I will truely, apprieciate all your help in this regard. Thanks, Kiran Chintal. hello, which compiler do you use? I tried BloodShed, and it didn't co...

MATLAB to C/C++ conversion
Hi. I would like to convert a short .m file to C or C++ code. I have MATLAB compiler version 4.6. I used the following commands: mbuild -setup mcc -c MyFunction.m I recived the following error messages: "??? Unable to determine application type, since no wrapper function was specified. Please use the -W switch or specify application type via - m or -l. Type 'mcc -?' for further assistance." "Error: File "MyFunction" is a script M-file and cannot be compiled with the current Compiler. You may be able to correct this by converting this script into a fu...

Matlab & C/C++
Good day everyone, i'm already developed a system in matlab's fuzzy environment and i wrote the Mfile.Which i later compiled using the matlab compiler into both c and c++ codes..after setting the linker options in the visual c++ environment, i get the error below ------ Build started: Project: Bio, Configuration: Debug Win32 ------ Linking... LINK : fatal error LNK1104: cannot open file 'C:\Bio\Bio.obj' Build log was saved at "file://c:\Bio\Bio\Bio\Debug\BuildLog.htm" Bio - 1 error(s), 0 warning(s) ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ====...

MATLAB C/C++ Library
Hi, I have installed MATLAB7 which includes MATLAB Compiler. It's mentioned that the MATLAB Compiler now includes MATLAB C/C++ Math and Graphics Libraries. In my case, there is no directory called "cmath" and "cppmath" in $MATLAB/extern/examples/. That means it is not installed. Should I need a License for it or same license is applicable. Where to get these libraries? Any idea. Thanks & Regards, Ardhendu Hi Ardhendu, MATLAB Compiler 4 dosen't include MATLAB C/C++ Math Library. To know what version you have, type command in MATLAB Command Workspace: >&...

matlab c/c++ compiler
Hi, I'm student and i'm working on wavelet foveation, my first version of m filles takes more than 4 hours to calculate results using 512*512 image, i tried to use matlab c ++ compiler in order to optimise time calculation (making full or partial stend alone application), problem : i had a lot of warnings and errors ! my question is about efficient manuals or books about making stand alone applications using matlab compiler (with visual interfaces if its posible, generating mex files under linux or dll's under windows). Thanks. Marie. On Sat, 15 Nov 2003 11:49:24 +0...

How to pass C pointers when interfacing Matlab MCR from C/C++
We have a signal processing application for doing frequency analysis of acoustic signals. The original signal is captured in a C struct: -------------------------------------------- typedef struct _sig { unsigned long length; float *data; } Sig; -------------------------------------------- Typical length is 10M, so the size of the data is 10M*sizeof(float)=40M. We want to apply FIR filter on this signal using Matlab MCR. I have built a Matlab DLL using the deploytool from the Compiler toolbox. In the Matlab DLL there is Filt.m that includes somethin...

C++ to MATLAB and debugging in MATLAB w edit/debugger
I am currently trying to create a C++ - Matlab function call(on a Windows platform) where I am able to debug the Matlab function via the edit/debugger. Yes, I have used the MatLab Engine calls and the COM interface. However when i attempt to debug via the edit/debugger, I get the following error message sent to STDOUT: buffer 0x0012fda4 "Warning: Debug commands not available when using MATLAB as Engine. > In <a href="error:c:\matlab7\bin\win32\cMatlabTest.m,4,1">cMatlabTest at 4</a> Checked all 17 cache memory tables. Found no corrupted tables. We also at...

Call C code from Matlab -> Matlab crash
Hello, I have a problem calling C code from Matlab. The mex file compilation is ok. When I run the program Matlab (7.3) crashes. Below is the mexFunction code. Any idea is welcome. (I think the main part of the code (middle part) is not useful for the crash problem, but I put it anyway). //########################################################### #include "math.h" #include "mex.h" void mexFunction (int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { // Standard variables int p,r,h,i,j,f,w; // Indices for loop int MAX_NBM = 1000; int MAX_DIM = 10; int MAX_NB_CLUST = 20; double *SF, *km, *new_data; int size, dim, K, nbm, aux_size, count_bis, count_aux; double diam_max, diam, vec1_p, vec2_p, dist_min, dis; int combi[1000000][2]; int dist_aux[1000000][2]; /* Check for proper number of arguments. */ if (nrhs != 5) { mexErrMsgTxt("5 input required."); } else if (nlhs > 1) { mexErrMsgTxt("Too many output arguments"); } // Input parameter (MATLAB) new_data = mxGetPr(prhs[0]); size = (int)mxGetScalar(prhs[1]); dim = (int)mxGetScalar(prhs[2]); K = (int)mxGetScalar(prhs[3]); km = mxGetPr(prhs[4]); // Output parameter (MATLAB) plhs[0] = mxCreateDoubleMatrix(1,1,mxREAL); SF = mxGetPr(plhs[0]); // Other variables nbm = size/dim; // Number of points printf("Taille: %d\n",nbm); printf("Dimension: %d\n",dim); //#############################...

MATLAB algorithm to MATLAB function code or C code
hallo firends, Can any body help me write this below code into MATLAB object oriented code or C code form please? M=50; % total number of periods. (choose for 1s duration) f0 = 50; % fundamental AC frequency T0 = 1/f0; %fundamental AC period T = M*T0; %Time for M periods (integer M) N=30; % sample points per period. dt = T0/N; % Sample at N points per period (integer N > 20) t = dt*[1:M*N-1]; % Sampling time array A0=120/2; % amplitude of AC signal. Divide by 2 or maybe sqrt (2) ? AC=A0*sin(f0*t*2*pi); % create AC signal. DC0=50; % DC amplitude DC=DC0*ones(1,length(t)); % create a DC signal that is constant of length t S=AC+DC; % total signal is AC signal + DC signal figure(1); plot(t,S); % plot signal. xlabel('time');ylabel('Signal'); title('Total Signal'); % filter components fAC=S-mean(S); % here is the AC component fDC=mean(S); % here is the DC component figure(2); plot(t,fAC,'r',t,fDC,'b'); % plot AC and DC signals legend('AC','DC'); xlabel('time');ylabel('Signal'); title('Signal with only AC component'); figure(3); plot(t,fDC,'b'); % plot DC signal legend('DC'); xlabel('time');ylabel('Signal'); title('Signal with only DC component'); figure(4); % best to plot in Fourier domain u=abs(fftshift(fft(S))); NumPoints=length(t); freq=(1/dt)*[1:NumPoints]/NumPoints; % create frequency axis freq=fre...

Converting Matlab code to C using Matlab Compiler
Hello All, Please can I get some guidance on how to Convert a structure from Matlab code to C code using Matlab Compiler. Kind regards Sammy ...

matlab 7.1 use matlab C++ math library
I wanna use matlab C++ math library into VC++6.0, but my matlab is 7.1. if i wanna use matlab C++ math library to programme, how can i setup my VC link?? why matlab7.1 doesn't have "extern->include->cpp" file??? In using matlab C++ math library , what difference between matlab 6.X and matlab7.1??? ...

Web resources about - vectorized computation in C++ such as those in Matlab (Matlab to C++)? - comp.soft-sys.matlab

Computation - Wikipedia, the free encyclopedia
that follows a well-defined model understood and expressed as, for example, an algorithm , or a protocol . The study of computation is paramount ...

Evolutionary computation - Wikipedia, the free encyclopedia
In computer science , evolutionary computation is a subfield of artificial intelligence (more particularly computational intelligence ) that ...

bit-player - An amateur's outlook on computation and mathematics
In a few days bit-player will celebrate its seventh birthday. (The first published post was dated January 9, 2006.) The original design for the ...

Bubble Math – Place Value Computation Tactics Educational Game for kids on the App Store on iTunes
Get Bubble Math – Place Value Computation Tactics Educational Game for kids on the App Store. See screenshots and ratings, and read customer ...

All sizes - A Beautiful Computation - Flickr - Photo Sharing!
Flickr is almost certainly the best online photo management and sharing application in the world. Show off your favorite photos and videos to ...

Electrical Efficiency Trends of Computation over Time - YouTube
Jonathan Koomey [Stanford University] Abstract: This talk will describe long-term trends in the electrical efficiency of computation that enabled ...

Big data under the spotlight at Curtin Institute for Computation
Curtin University has opened the Curtin Institute for Computation (CIC) in Western Australia to focus on computation and data analytics research. ...

How to get faster fiber-optic pipes through computation
Calculation compensates for fiber imperfection, may increase data rates.

Luis von Ahn: The Pioneer of "Human Computation" - Businessweek
MacArthur Fellow Luis von Ahn's programs harness human abilities to solve large-scale, complex problems The New York Times (NYT) has published ...

Wolfram doesn’t use computation in URS dispute
A lot of brain power missed the connection. Wolfram Research, Inc. has lost a Uniform Rapid Suspension (URS) case for mathematica.guru and wolfram.ceo, ...

Resources last updated: 3/15/2016 10:06:55 AM