C ++中的向量化计算,如Matlab(Matlab到C ++)中的那些? [英] vectorized computation in C++ such as those in Matlab (Matlab toC++)?

查看:88
本文介绍了C ++中的向量化计算,如Matlab(Matlab到C ++)中的那些?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的,


C ++ / STL / Boost可以像在Matlab中那样进行矢量化计算吗?


例如,在下面的代码,我真正想做的是

发送你的矢量。


所有其他参数,如t,l1, l2,l3等是标量......


但你是一个向量。


因此,t6成为一个向量。 />

t9是元素乘法...


以下代码实际上是从Matlab转换而来的。


如果没有方便矢量化计算,那么我必须将这个

函数称为数百万次。


但是如果向量化计算没问题,那么我可以一次性发送带有批处理元素的
向量。


我在Matlab中有很多这样的代码需要转换成C ++与

矢量化。


有什么想法吗?


谢谢!


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);

解决方案

亲爱的,


>

C ++ / STL / Boost可以像在Matlab中那样进行矢量化计算吗?

例如,在下面的代码中,我真正想做的是

发送你的矢量。


所有其他参数,如t,l1,l2,l3等都是标量...


但你是一个向量。


因此,t6成为一个向量。


t9是元素乘法...


以下代码是实际上是从Matlab转换而来的。


如果不方便矢量化计算,那么我必须将这个

函数称为数百万次。

但是如果矢量化计算没问题,那么我可以用批量元素一次性发送au

向量。


我有很多这样的需要将Matlab中的代码转换为带有

矢量化的C ++。


有什么想法吗?


谢谢!


* * * * * * * *双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);



嗨。


我认为matlab提供了一个c ++ api。你看过了吗?还有用于一般代数计算的矩阵模板库。

。你

可能会觉得它很有用。

-

Leandro TC Melo


我不认为Matlab的C ++ API可以做到这一点。我认为这只是一个C

界面。它没有STL,Boost等。


另外,我们不是在谈论像高速这样复杂的事情。\\ n \\ n
矩阵计算,它只是矢量化计算......


7月22日11:00 * am,Leandro Melo< ltcm ... @ gmail.comwrote:


亲爱的所有人,


C ++ / STL / Boost能否进行矢量化计算Matlab的?


例如,在下面的代码中,我真正想做的是

发送你的向量'的。


所有其他参数如t,l1,l2,l3等都是标量...


但是你是一个向量。


因此,t6成为一个向量。


t9是元素乘法...


以下代码实际上是从Matlab转换而来的。


如果不方便矢量化计算,那么我必须将这个

函数称为数百万次。


但是如果矢量化计算没问题,那么我可以一次性发送带有批量元素的u

向量。


我在Matlab中有很多这样的代码需要转换成带有

向量化的C ++。


有什么想法吗?


谢谢!


* * * * * * * * 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);



嗨。


我认为matlab提供了一个c ++ api。你看过了吗?还有用于一般代数计算的矩阵模板库。

。你好b / b
可能会觉得它很有用。


-

Leandro TC Melodt



2008年7月22日星期二07:08:38 -0700,Luna Moon写道:


亲爱的,


C ++ / STL / Boost可以像Matlab那样进行矢量化计算吗?



你用matlab中的矢量化计算究竟是什么意思??

你只是说Matlab有一个本机向量用你的类型来计算和b
计算,或者你是否建议Matlab以某种特殊方式处理
向量,而C ++不能这样做?


Matlab,AFAIK,使用BLAS

库进行了大量的矩阵/向量运算,例如dot

乘积和矩阵 - 矩阵或矩阵向量乘法 - 这是高度优化的线性代数代码(通常在Fortran中写成
) - 可以通过C ++访问,因为C ++有一个定义明确的

接口(C,真的)和Fortran。您很有可能

已经在您的系统上安装了BLAS库;如果没有,有开放的源码(例如,ATLAS项目)以及供应商提供的版本

(例如英特尔,AMD等供应BLAS库)。


Matlab也可能会使用特定于机器的优化
优化,例如sse / mmx进行浮点计算。如果你能说服你的编译器去玩球,你也可以使用这些来自C ++的这些。


底线是Matlab没有什么可以做的你不能用C ++做
,同样(如果不是更多)有效。这更像是一个问题

方便:Matlab专为矢量/矩阵设计

操作 - C ++是一种通用编程语言。
< blockquote class =post_quotes>
例如,在下面的代码中,我真正想做的是在你的向量中发送




所有其他参数,如t,l1,l2,l3等都是标量...


但是你是一个向量。


因此,t6成为一个向量。


t9是元素乘法...


以下代码实际上是从Matlab转换而来的。


如果不方便矢量化计算,那么我必须将这个

函数称为数百万次。


但是如果矢量化计算没问题,那么我可以用批量元素一次性发送au

向量。



我真的不太清楚你的意思。


C ++中最接近Matlab矢量的东西可能是

std :: valarray< doubleclass,虽然看起来有点像一个bodge因此

相当不受欢迎。 std :: vector< doubleclass很可能会给你很好的b $ b;它没有实现像元素那样的功能

乘法,所以你必须自己做 - 但这很漂亮

简单。


还有各种矩阵/矢量C ++库可供您查看(例如

Blitz ++)。


在效率方面,如果你正在做很多大型矩阵

乘法或更复杂的线性代数a matlab,那么

你可能想调查一下BLAS可能还有LAPACK(线性

代数包),但我怀疑你的情况可能有些过分。并且

它很难看。


FWIW,我最近将很多Matlab代码移植到C ++中并且不得不说

C ++通常在效率方面踢Matlab's *但是没有在b / b
编码的容易程度(Matlab似乎从很多内容复制中获得了很好的性能)你可以用手工编写的C ++来消除它。


我在Matlab中有很多这样的代码需要转换成C ++并带有

矢量化。


有什么想法吗?


谢谢!


双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


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);

解决方案

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


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




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<doubleclass, although it seems a bit of a bodge and hence
rather unpopular. The std::vector<doubleclass 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


这篇关于C ++中的向量化计算,如Matlab(Matlab到C ++)中的那些?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆