HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppMinor

Vectors assignations and operations in a loop and parallelization with OpenMP

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
assignationsvectorsoperationswithloopopenmpparallelizationand

Problem

I use this piece of code to compute a short-time Fourier transform:

// Output pre-allocation
std::vector > > y(nFft, std::vector >(nFrames, 0.0));

std::vector xt;
std::vector > yt;
xt.reserve(nFft);
yt.reserve(nFft);

#pragma omp parallel for private(xt,yt)
for (unsigned int t = 0; t  n) {
        xt.assign(x.begin() + offset, x.end());
        xt.resize(wSize, 0.0);
    }
    else
        xt.assign(x.begin() + offset, x.begin() + offset + wSize);

    // Apply window to current frame
    std::transform(xt.begin(), xt.end(), w.begin(), xt.begin(), std::multiplies());

    // Zero padding
    std::rotate(xt.begin(), xt.begin() + wSize/2, xt.end());
    xt.insert(xt.begin() + wSize/2, nFft - wSize, 0.0);

    yt = fft(xt);  // Perform the FFT!

    #pragma omp critical
    {
        for (unsigned int f = 0; f < nFft; ++f)
            y[f][t] = yt[f];
    }
}


Is there something that I can improve, either in design style or performances, without sacrificing readability?
Of course the point is to improve my coding skills without using any external library!

Regarding the xt and yt vectors, I put them outside the loop to improve the performances in mono-threaded (when OpenMP is disabled). In multi-threaded, there are copied for each thread anyway (hence the use of the private close).

Solution

The only thing worth commenting on is the identifier names could be more meaningful.

  • Short identifiers are harder to find when maintaining the code (more false positives).



  • Short identifiers make it hard to put meaning to the identifier.

Context

StackExchange Code Review Q#6980, answer score: 2

Revisions (0)

No revisions yet.