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