# Parallel resampling columnwise

**URL:** <https://discourse.julialang.org/t/parallel-resampling-columnwise/113203>\
**Category:** Signal and Image Processing\
**Tags:** distributed, dsp\
**Created:** [April 18, 2024, 4:53pm UTC](https://discourse.julialang.org/t/parallel-resampling-columnwise/113203 "2024-04-18T16:53:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jafagervik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jafagervik/32/208594_2.png) [@jafagervik](https://discourse.julialang.org/u/jafagervik)\
**Post date:** [April 18, 2024, 4:53pm UTC](https://discourse.julialang.org/t/parallel-resampling-columnwise/113203/1 "2024-04-18T16:53:27Z")

</div>

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:

```julia
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

```julia
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.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [April 20, 2024, 6:19pm UTC](https://discourse.julialang.org/t/parallel-resampling-columnwise/113203/2 "2024-04-20T18:19:11Z")

</div>

Linking a [related thread](https://discourse.julialang.org/t/shared-memory-parallelization-with-large-matrix/29076).

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [April 20, 2024, 10:48pm UTC](https://discourse.julialang.org/t/parallel-resampling-columnwise/113203/3 "2024-04-20T22:48:43Z")

</div>

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:

```julia
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

```
