grame-cncm / faust

Functional programming language for signal processing and sound synthesis
http://faust.grame.fr
Other
2.58k stars 322 forks source link

Does Faust support in-place buffers in general (re-using inputs as outputs)? #443

Closed bluenote10 closed 4 years ago

bluenote10 commented 4 years ago

I'm wondering if Faust in general has "in-place semantics", i.e., whether it is always okay to re-use input buffers as output?

I've only looked at a few examples, but the pattern often is (using the C++ code generator as an example):

virtual void compute(int count, FAUSTFLOAT** inputs, FAUSTFLOAT** outputs) {
  FAUSTFLOAT* input0 = inputs[0];
  FAUSTFLOAT* input1 = inputs[1];
  FAUSTFLOAT* output0 = outputs[0];
  for (int i = 0; (i < count); i = (i + 1)) {
    float fTemp0 = float(input0[i]);
    // ...
    output0[i] = ...;
  }
}

In such a case it would be okay to use the same memory for both input and output, because the generated code never needs to re-read an input value.

Is this a general property that the code generation always fulfills?

In case this is guaranteed:

Background: I'm using Faust in a context where I instantiate several small Faust components as "building blocks", passing signals manually between various compute functions. In most cases there is no need to preserve the inputs, i.e., components could be chained in an in-place fashion, which would be more cache efficient and save memory.

sletz commented 4 years ago
sletz commented 4 years ago

A bit of improvement in the doc https://github.com/grame-cncm/faustdoc/commit/b24980ddde03bea5285108d4773d4e1534887464

bluenote10 commented 4 years ago

Perfect thanks! Somehow I had missed the documentation on the -inpl mode.

So, regarding the Rust backend, I assume that it is currently scalar-only anyway (so a possible candidate for -inpl processing), but probably lacks an explicit handling of in-place mode. The question is if the compute interface should already account for the possibility of in-place processing. I'll have to experiment with the idea and the implications a bit more to see if that makes sense.

Since the documentation aspect is covered, we may as well close the ticket and address the Rust aspect separately?

sletz commented 4 years ago