gogins / csound-extended

Extensions for Csound including algorithmic composition, Android app, and WebAssembly.
GNU Lesser General Public License v2.1
40 stars 1 forks source link

Efficiency of Faust for Csound #187

Closed gogins closed 3 years ago

gogins commented 3 years ago

Which runs faster, a Faust DSP algorithm implemented using run-time compilation with Csound's "faustgen" opcodes, or the same algorithm implemented using "faust2csound" as a C++ native plugin opcode?

gogins commented 3 years ago

Results

faustgen | faust2csound -- | -- 4.4607 | 5.0320 4.4475 | 5.0350 4.2371 | 5.0270 4.4839 | 4.9800 4.4357 | 5.0240 4.3795 | 5.0490 4.4235 | 4.9984 4.3682 | 4.9811 4.4395 | 5.0729 4.3618 | 5.0629 4.3783 | 5.0464 4.4142 | 5.0570 4.3713 | 5.0134 4.3180 | 5.0053 4.4177 | 5.0275 4.3958 | 5.0275 87.44% |   Student's t test |   3.81E-15 |  
gogins commented 3 years ago

Discussion

  1. There is a real difference in efficiency, the faustgen opcodes run the same .dsp source code in 87% of the time taken by the same .dsp code compiled to C++ with faust2csound (which compiles with -O3 ) as a Csound plugin opcode. But the difference in efficiency is not so large and one wonders if this might vary for different algorithms.
  2. With the faustgen opodes, there is significant overhead before audio begins, no doubt caused by the LLVM compilation of the Faust code.
  3. Of course, if Faust is not configured with LLVM on the host computer, only the native opcodes can be used.
  4. And of course, even if Faust is not installed at all on the host computer, precompiled native opcodes can still be used.
sletz commented 3 years ago

By default the C++ compiler compiles for a "generic" CPU, where the LLVM backend is configurated to produce code for the "native" CPU (that one where the code will actually run). This usually explains this kind of differences. You can try to use -march=native and also '-Ofast' in the C++ compiler, to have the same options the LLVM backend is using.

sletz commented 3 years ago

Some general explanations to get the most performances: https://faustdoc.grame.fr/manual/optimizing/#optimizing-the-c-or-llvm-code

gogins commented 3 years ago

More Results

faustgen | faust2csound | faust2csound g++ native | faust2csound clang++ native -- | -- | -- | -- 4.4607 | 5.0320 | 4.6015 | 3.8385 4.4475 | 5.0350 | 4.6022 | 3.8239 4.2371 | 5.0270 | 4.6325 | 3.8237 4.4839 | 4.9800 |   | 3.8610 4.4357 | 5.0240 |   | 3.8010 4.3795 | 5.0490 |   | 3.8296 4.4235 | 4.9984 |   | 3.8701 4.3682 | 4.9811 |   | 3.8213 4.4395 | 5.0729 |   | 3.8716 4.3618 | 5.0629 |   | 3.8693 4.3783 | 5.0464 |   | 3.8726 4.4142 | 5.0570 |   | 3.8365 4.3713 | 5.0134 |   | 3.8666 4.3180 | 5.0053 |   | 3.8410 4.4177 | 5.0275 |   | 3.8287 4.3958 | 5.0275 | 4.6121 | 3.8437 % of faustgen | 114.37% | 95.31% | 76.45% Student's t test |   |   |   3.81E-15 |   |   |  
gogins commented 3 years ago

More Discussion

OK, this is now more complete and it is also more unequivocal.

It is clear that using Faust to generate C++ code for a Csound plugin opcode, then compiling the generated C++ using clang++ with -march=native -Ofast, produces the fastest results. This seems to be because clang++ generates faster machine code than g++.

Best Practices

gogins commented 3 years ago

Yes, of course you are right!

I redid my experiment with -march=mative -Ofast, using clang++ instead of g++, and now the native opcode runs in 76% of the time as the "faustgen" opcodes. This is a useful advantage.

Updated details here: https://github.com/gogins/csound-extended/issues/187#issuecomment-914267546

I have no idea why this did not occur to me, as I already am building Csound with -march=native. I will try switching to clang++ for Csound as well.

Thanks, Mike


Michael Gogins Irreducible Productions http://michaelgogins.tumblr.com Michael dot Gogins at gmail dot com

On Tue, Sep 7, 2021 at 5:19 AM Stéphane Letz @.***> wrote:

Some general explanations to get the most performances: https://faustdoc.grame.fr/manual/optimizing/#optimizing-the-c-or-llvm-code

— You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub https://github.com/gogins/csound-extended/issues/187#issuecomment-914139744, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABQIGJPKJZLN3ZTRV3PBL7TUAXKLXANCNFSM5DQSJH4Q .