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



On 22 Jul, 16:08, Luna Moon <lunamoonm...@xxxxxxxxx> 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
.



Relevant Pages

  • Re: back online again...
    ... I'd then have the compiler automatically fill in "dummy" ... dynamically compiled code uses this convention as if it were ... a custom prolog and epilog or wrapper function for calling SysV ... internal C functions and the SysV calling convention for external C ...
    (alt.lang.asm)
  • Re: Superstitious learning in Computer Architecture
    ... but rather a special-purpose neural network ... A good compiler may cost you 4:1 in performance!!! ... in '90!)) involved fairly repetitive simulation-step sorts of processes. ... coordinating widely separated units (e.g. vector operations). ...
    (comp.arch.arithmetic)
  • repost: problem compiling Managed Sink Wrapper for SMTP
    ... (I'm sorry to keep posting this question, but I only just figured out that I ... Unfortunaltely this resulted in compiler errors due to some ... datatype mismatches in the calls from the wrapper class to the interop DLL. ...
    (microsoft.public.exchange2000.development)
  • problem compiling Managed Sink Wrapper for SMTP
    ... (I've already posted wthis question to a couple of other newsgroups but have ... Our team needs to recieve SMTP mail from within a C# app, ... Unfortunaltely this resulted in compiler errors due to some ... datatype mismatches in the calls from the wrapper class to the interop DLL. ...
    (microsoft.public.exchange2000.development)
  • problem compiling Managed Sink Wrapper for SMTP
    ... Unfortunaltely this resulted in compiler errors due to some datatype mismatches in the calls from the wrapper class to the interop DLL. ... 'Microsoft.Exchange.Transport.EventInterop.IMailMsgPropertyBag.GetProperty(uint, uint, out uint, out byte)' has some invalid arguments: ... Is there something wrong with the interop dll, or the wrapper code? ...
    (microsoft.public.dotnet.framework.interop)