Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Shared Data with QVector



  • Hi,
    i work with the FFTW library for complex to complex transformations. For the execution of the FFT you have to "bake" a plan, where all the internal buffers are created with the set input and output vectors. In my case I use QVectors to hold the input and output data.

    The first time the plan is created with the set input data, the output data holds the correct results. If I want to use the same plan again but with changed input data, the output is incorrect.

    A simple example:

    QVector<fftw_complex> input(12);
    QVector<fftw_complex> output(12);
    
    // fill input with data
    
    fftw_plan plan = fftw_plan_dft_1d(input.size(), input.data(), output.data(), FFTW_FORWARD, FFTW_ESTIMATE);
    
    fftw_execute(plan);
    
    // output = correct
    
    // change the data of the input vector
    input[0] = ...;
    input[1] = ...;
    ...
    
    fftw_execute(plan);
    
    // output = incorrect
    

    If I use an array fftw_complex input[12] instead of the QVector, the output is also correct after changing the input data.

    I think it's because of the implicit shared data of the QVector, but I'm not sure.

    Performance is a big issue in my case, so "baking" the plan again after changing the input data is no option (25 threads run 300.000 512x512 2D-FFTs, each).

    Is there a way to use the QVector (or another Qt class) in such a case?
    Thanks!


  • Qt Champions 2017

    @beecksche said in Shared Data with QVector:

    Is there a way to use the QVector (or another Qt class) in such a case?

    Make sure you don't detach the vector. "Baking" the plans isn't reentrant anyway, so you're stuck to doing it before anything else anyway. After that enforce constness on the vector and it should be okay.



  • @kshegunov

    I'm sorry, the example i wrote works fine. I found the mistake in my algorithm, the input buffer was recreated, instead of the entries

    ...
    input = QVector<fftw_complex>(12); // this doesn't work!
    ...
    

    Thanks anyway!

    EDIT:

    To complete the answer, the FFTW library has a "New-Array Execution" function, where the input and output buffer can be set when executing the plan.

    And with QVarLengthArray you can handle the arrays easily with high performance. QVarLengthArray won't check if the data is shared.


Log in to reply