Help me process this data in parallel

Hi everyone.
I have a simple function that turns each row of a matrix into a seismic trace (Seis.jl format) and filter it.

Can you guys help me turn this into a parallel function? Like several cores filtering one trace at the same time? I have 8 cores i guess I can run this is several of them to gain some speed!

Thanks a lot.

function bpdas(strainrate, time, offset, f1, f2; poles=2)
# Begin
bpstrainrate=copy(strainrate);
set up some trace information
b=0.0;
delta=time[2]-time[1];
# Bandpass Filtering Loop
for i =1:size(offset,1)
dastrace=Trace(b, delta, strainrate[i,:]) write the data to trace format
bandpass!(dastrace, f1, f2; poles) #filter in place
bpstrainrate[i,:]=trace(dastrace); #safe to global variable
end
return bpstrainrate
end

function bpdas(strainrate, time, offset, f1, f2; poles=2)
# Begin
    bpstrainrate=copy(strainrate);
#Set up some trace information
    b=0.0;
    delta=time[2]-time[1];
# Bandpass Filtering Loop
    Base.Threads.@threads for i =1:size(offset,1)
        dastrace=Trace(b, delta, strainrate[i,:]) #write the data to trace format
        bandpass!(dastrace, f1, f2; poles) #filter in place
        bpstrainrate[i,:]=trace(dastrace); #safe to global variable
    end
    return bpstrainrate
end

Provided I understand the data.

I will give it a go :).
Is it necesary to start julia like this for it to work

julia -p 6

Just it case, all the rows strainrate[i,:] are independent from each other so every loop repetition is also independet. If that helps hehehe

This is multiprocessing not multithreading (which is what Threads.@threads uses). You should only really use one or the other on a single machine.

In your case, you would need to start with more threads, e.g:

julia --threads=auto

Or to start with 4 threads

julia --threads=4

This can also be done with an environment variable, or there’s even a setting in VS code for it too.

If I want to process each i in a different hardware (processor, thread, worker, i don’t know)… is the macro doing this?

Use Threads.@threads on the front of a for loop. If you want to use functions like map & reduce there is a package called ThreadsX.jl which provides parallel implementations of these base library functions as well.

E.g.

function double!(array)
    Threads.@threads for i in eachindex(array)
        array[i] = array[i] * 2
    end
    return nothing
end
1 Like