473,756 Members | 8,108 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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);
Jul 22 '08 #1
14 3143
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
Jul 22 '08 #2
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. comwrote:
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


Jul 22 '08 #3
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<d oubleclass, although it seems a bit of a bodge and hence
rather unpopular. The std::vector<dou bleclass 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
Jul 22 '08 #4
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/
Jul 22 '08 #5
On 22 Jul, 18:37, Lionel B <m...@privacy.n etwrote:
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.s...o&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
Jul 22 '08 #6
On 23 Jul, 11:26, Lionel B <m...@privacy.n etwrote:
On Tue, 22 Jul 2008 10:16:08 -0700, Rune Allnor wrote:
On 22 Jul, 18:37, Lionel B <m...@privacy.n etwrote:
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
Jul 23 '08 #7
On 22 Jul., 16:08, Luna Moon <lunamoonm...@g mail.comwrote:
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
Jul 23 '08 #8
On Jul 22, 10:08 pm, Luna Moon <lunamoonm...@g mail.comwrote:
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
Jul 25 '08 #9
Luna Moon <lu**********@g mail.comwrote:
<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....tor-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.

Jul 27 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
16926
by: Mathias | last post by:
Dear NG, I currently ty to switch from matlab to python/scipy but have a lot of trouble with images. What I need is a function for subsequently displaying a number of 2D-matrices as an image. I tried: - Image.show() which is nice but slow and I don't seem to be able to either close the window nor draw again to the same window, so I end up with 100 pictures on the screen... - scipy.xplt.imagesc() and scipy.plt.imagesc() crash
4
2762
by: dataangel | last post by:
I'm a student who's considering doing a project for a Machine Learning class on pathing (bots learning to run through a maze). The language primarily used by the class has been Matlab. I would prefer to do the bulk of the project in python because I'm familiar with pygame (for the visuals) but I already have a lot of AI code written in Matlab. I'd like to be able to call Matlab code from within python. I'm not sure this is possible. My...
3
4005
by: Mohammed Smadi | last post by:
Hi; Does anyone know if we can call matlab for a python or bash script while feeding the matlab script some command line arguments? I have an interactive matlab script which i want to automate by feeding the args from a script. I know this is probably more suitable to a matlab group but any ideas will be appreciated.
6
22260
by: Lars Christiansen | last post by:
Hi! I am a master student in (geo)physics at the University of Copenhagen and part of a study group on C++ as a scientific programming language. I, and the other students in the group, have previously used MatLab in different courses to solve problems, and find it very easy to use. This has led us to the discussion: When should we use C++ instead of MatLab? When is C++ superior to MatLab - and when is it the other way
53
4378
by: Michael Tobis | last post by:
Someone asked me to write a brief essay regarding the value-add proposition for Python in the Fortran community. Slightly modified to remove a few climatology-related specifics, here it is. I would welcome comments and corrections, and would be happy to contribute some version of this to the Python website if it is of interest. ===
2
6852
by: Quina | last post by:
Hi there, i'm building an windows application to manage my disc collection. I also want to enable it with cd recognition features on audio CDs. In order to do this I must submit the CD info (discID, ...) to freedb.org. The problem is that I don't know how to retrieve the Table Of Contents (toc) from the disc. The information provided by freedb tells how to calculate the DiscID, based on the toc, but it doesn't explain how to get the...
9
5296
by: Carl | last post by:
I am desperately looking for a way to call Python from Matlab. I have become used to Python's rich syntax and large number of libraries, and feel ridiculously clumsy being stuck with Matlab's rather restricted facilities for doing other things than standard mathematical work. Does anyone know of good techniques (or readily available software) for achieving a cross-language support between Python and Matlab? Carl
10
5261
by: Chao | last post by:
I've been trying to develop some numerical codes with python, however got disappointed. A very simple test, a = 1.0 for i in range(1000): for j in range(1000): a = a+1
4
13752
by: itcecsa | last post by:
Hi, I am implementing a small Python project, what I am going to do is to open Matlab and run some M-files, and get some output from Matlab command prompt. I have no idea how to open Matlab from Python! Any suggestions would be appriciated!
0
9456
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10040
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9873
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9846
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8713
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6534
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3806
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2666
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.