简体   繁体   English

编译的Simulink / Matlab x Fortran-性能

[英]Compiled Simulink/Matlab x Fortran - Performance

I have to prove to my client that Fortran is faster than Matlab/Simulink. 我必须向客户证明Fortran比Matlab / Simulink快。 He is considering migrating a code from fortran to Matlab. 他正在考虑将代码从fortran迁移到Matlab。 The code is mainly logic and "procedural" subroutines. 该代码主要是逻辑和“过程”子例程。 It does not use any native matrix operations or mathematical functions (eigenvalues, non linear equations, etc) I think that the question of who is faster is already answered considering several references over the internet and the "intrinsic characteristics" of each language, but I need concrete data. 它不使用任何本机矩阵运算或数学函数(特征值,非线性方程式等),我认为考虑到互联网上的多个参考文献以及每种语言的“内在特征”,已经回答了谁的速度更快的问题,但是我需要具体数据。

All charts that I found compare Matlab/Simulink x Fortran but do not specify if the Matlab code is compiled or not (using matlab coder toolbox). 我发现的所有图表都比较Matlab / Simulink x Fortran,但未指定是否已编译Matlab代码(使用matlab编码器工具箱)。 I think that it is a critical issue. 我认为这是一个关键问题。

I´m not saying that compiling the code will make matlab faster than fortran, but in order to really convince someone I would like to see the results. 我并不是说编译代码会使matlab的速度比fortran快,但是要说服某个人,我希望看到结果。

A good start would be: 一个好的开始将是:

  1. Performance - Matlab (.m) compiled (Matlab coder toolbox) X Intel Fortran 性能-Matlab(.m)编译(Matlab编码器工具箱)X Intel Fortran
  2. Performance - Simulink compiled (Realtime toolbox) X Intel Fortran 性能-Simulink编译(实时工具箱)X Intel Fortran

Does anyone have already tested this scenario? 有没有人已经测试过这种情况?

Matlab code that I recently "compiled" using the Matlab Coder produced a speed-up of x20 (!). 我最近使用Matlab Coder“编译”的Matlab代码产生的速度提高了20倍(!)。 The actual expected speedup depends on many things. 实际的预期加速取决于许多因素。 If your Matlab code is highly vectorized and uses mainly linear-algebra routines, then the Coder is unlikely to produce much speedup. 如果您的Matlab代码是高度矢量化的,并且主要使用线性代数例程,则Coder不太可能产生很大的加速。 But if you have multiple loops and conditionals in your algorithm then you can indeed achieve order-of-magnitude speedup as in my example above. 但是,如果您的算法中包含多个循环和条件,则确实可以实现幅度级加速,如上面的示例所示。

Under the hood, Matlab's linear-algebra uses BLAS/LAPACK (via the MKL/ACML libraries), that use highly-optimized Fortran code. 在底层,Matlab的线性代数使用BLAS / LAPACK(通过MKL / ACML库),后者使用高度优化的Fortran代码。 So unless you write extremely efficient Fortran, it is not likely that you will be able to outperform Matlab (despite the function-call overheads) for highly-vectorized Matlab linear-algebra/math algos. 因此,除非您编写非常高效的Fortran,否则对于高度矢量化的Matlab线性代数/数学算法,您不可能胜过Matlab(尽管有函数调用开销)。 However, if your code uses conditionals/loops and similar non-math programming constructs, then the picture might change. 但是,如果您的代码使用条件/循环和类似的非数学编程构造,则情况可能会发生变化。 In short, there's no simple answer - it depends on your specific algorithm/program. 简而言之,没有简单的答案-这取决于您的特定算法/程序。

Putting performance aside for the moment, Matlab has numerous other benefits over Fortran, including a vast array of tested built-in functions and enabling a rapid development cycle. 目前暂不考虑性能,Matlab与Fortran相比还有许多其他好处,包括大量经过测试的内置功能,并能加快开发周期。

MATLAB Compiler will not make your code faster, it is intended for distributing your code to third party users that do not have MATLAB. MATLAB Compiler 不会使您的代码更快,它旨在将您的代码分发给没有MATLAB的第三方用户。 You need to provide, along with your compiled code, the MCR or MATLAB Component Runtime, which is essentially a headless version of MATLAB, and which you can distribute freely if you have a license of MATLAB Compiler. 您需要与编译后的代码一起提供MCR或MATLAB Component Runtime,它实际上是MATLAB的无头版本,并且如果您拥有MATLAB Compiler的许可证,则可以自由分发。

Now, if you use MATLAB Coder (or Simulink Coder for Simulink) to generate C code from your MATLAB code, then it is likely that you will get a speed up compared to interpreted MATLAB code. 现在,如果您使用MATLAB Coder(或Simulink的Simulink Coder)从MATLAB代码生成C代码,则与解释后的MATLAB代码相比,您可能会获得更快的速度。 Even then, that depends on the code in question. 即使这样,这仍取决于所讨论的代码。 Also, this only supports a subset of the MATLAB language, that is compatible with C code generation. 此外,这仅支持与C代码生成兼容的MATLAB语言的子集。

You would need to ask a more tightly defined question - there's no single answer to whether Fortran is faster than MATLAB/Simulink. 您需要提出一个更严格定义的问题-对于Fortran是否比MATLAB / Simulink更快,没有唯一的答案。

First of all, it's easy to write terrible, slow algorithms in either language. 首先,很容易用两种语言编写可怕的,缓慢的算法。 So you'd need to specify particular, well-written algorithms. 因此,您需要指定特定的,编写良好的算法。

Secondly, there are many things for which MATLAB will be faster than even very well-written Fortran (or C). 其次,在很多方面,MATLAB甚至会比编写出色的Fortran(或C)更快。 For example, if you want to multiply two big matrices together, or calculate some eigenvalues, or other linear algebra that is in MATLAB's sweet spot, you won't beat it. 例如,如果您想将两个大矩阵相乘,或者计算一些特征值或MATLAB的最佳位置中的其他线性代数,您将不会打败它。 On the other hand if you're doing something with a lot more logic, that can't be vectorised, Fortran is likely to be faster (as long as it's written well). 另一方面,如果您要执行的逻辑更多,无法向量化,则Fortran可能会更快(只要编写得好)。

When you introduce MATLAB Coder into the picture, these latter things are the ones that are most likely to benefit from a speedup by converting to C code (mostly because the former things really can't be sped up much, which is why you wouldn't beat them). 当您将MATLAB Coder引入图片中时,后面这些东西最有可能通过转换为C代码而从加速中受益(主要是因为前面的东西确实不能太多地加速,这就是为什么您不会这样做的原因)击败他们)。 But the speedup is variable - I've seen over 10-15x, but also sometimes only 1-2x. 但是加速是可变的-我见过10到15倍,但有时也只有1-2倍。

You don't mention where you found the charts you have comparing MATLAB to Fortran, but if you've found them on the internet I would think it's a pretty safe assumption that they don't involve C code generation with MATLAB Coder, and represent the performance of just MATLAB. 您没有提到在哪里可以找到将MATLAB与Fortran进行比较的图表,但是如果您在互联网上找到了图表,我认为这是一个非常安全的假设,即它们不涉及使用MATLAB Coder生成C代码,并且可以表示只是MATLAB的性能。

Finally - one other method of speeding up MATLAB is to parallelize it with Parallel Computing Toolbox (which enables you to parallelize things over the cores on your local machine) and possibly also with Distributed Computing Server (parallelization on cluster). 最后,另一种提高MATLAB速度的方法是使用Parallel Computing Toolbox(使您可以在本地计算机的内核上对事物进行并行化)和Distributed Computing Server(在群集上并行化)对其进行并行化。 It's typically a lot easier to do this with MATLAB code than it is to speed up by using MATLAB Coder to produce C code - so if you think it's critical to consider MATLAB Coder in your comparisons, you should probably also consider this as well. 使用MATLAB代码执行此操作通常比使用MATLAB Coder生成C代码来加速容易得多-因此,如果您认为在比较中考虑MATLAB Coder至关重要,那么您也应该考虑这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM