Parallel resampling columnwise

Hi, I have a large matrix I want to resample. Typical size is about 20_000 * 500.

The code I have for now looks like this:

using DSP: resample
function resample_parallel(signal::Matrix{Float32}, rate::Real)
    result = pmap(x_slices -> Float32.(resample(x_slices, rate)), eachcol(signal))

    return hcat(result...)
end

This is my other version

function test(signal::Matrix{Float32}, rate::Real)
    rows, cols = size(signal)

    new_rows = Int(floor(rows * rate))

    result = Matrix{Float32}(undef, new_rows, cols)
    
    slices = pmap(x_slices -> Float32.(resample(x_slices, rate)), eachcol(signal))

    for (i, slice) in enumerate(slices)
        @inbounds result[:, i] = slice
    end

    return result
end

In essence, I want resample each column in parallel, and gather the new result to a matrix. I have tried preallocating the matrix, but it does not work much faster.

Linking a related thread.

FWIW, after setting in VS Code the Julia Num Threads variable (= 14 for my Windows laptop), the following code using Threads.@threads seems to run 5x faster than the pmap version:

function test2(signal::Matrix{Float32}, rate::Real)
    rows, cols = size(signal)
    new_rows = Int(floor(rows * rate))
    result = Matrix{Float32}(undef, new_rows - 1, cols)
    Threads.@threads for i in axes(signal,2)
        @views @inbounds result[:, i] = resample(signal[:,i], rate, dims=1)
    end
    return result
end
1 Like