Why I am observing no speedp in a parallelised for loop in Julia

Viewed 91

THERE WAS AN ERROR IN MY CODE. SORRY EVERYBODY. THE REASON WAS THAT THE TIME NEEDED FOR LARGER VALUE OF X IS LARGER.

This is my code:

fit_s = rand(100)
fit_d = rand(300) 
LIM = 100
x = [2^i for i in range(-2,2,40)]
y = zeros(length(x))
Threads.@threads for i in 1:length(x)
    y[i] = analytic(x[i], fit_s, fit_d, LIM)
end
print(i)

my_function uses the two inputs to produce the output. The problem is that it does not speed up the execution. With 1 threads it takes 27 s while with 8 threads 21 s(I'm considering only the time of the for loop). What can I do ? Thanks in advice.

Function

function alpha(i,n,m)
    ((n-i)/n)^m - ((n-i-1)/n)^m
end

function beta(i,n,m)
    (1-i/n)^(m)
end

function gamma(s, d, fit_s, fit_d, LIM)

    n_s = length(fit_s)    
    n_d = length(fit_d)

    app = sort(fit_d, rev=true)[1:LIM]
    F_DN = 0.

    for i in 1:LIM
        j = sum(fit_s .> app[i])
        F_DN += alpha(i-1,n_d,d)*beta(j,n_s,s)
    end
    
    F_DN
end

function mine_poisson(m,i)
    f = Poisson(m)
    pdf(f, i)
end

function analytic(m, fit_s, fit_d, LIM)
    Z = 1 - exp(-2*m)
    F_DN = 0.
    intm = trunc(Int, m + 0.5)
    if m < 20
        intervallo = 1:(intm + 20)
    else
        app = trunc(Int,m^0.5)*4
        intervallo = (intm - app) : (intm+app)
    end

    for j in intervallo
        for k in intervallo
            p = mine_poisson(m, k)*mine_poisson(m, j) / Z
            F_DN += gamma(j, k, fit_s, fit_d, LIM)*p
        end
    end
    

    F_DN += exp(-m)*(1-exp(-m))/Z
end
1 Answers

You code looks correct.

There are the following reasons why you might not observe the speedup:

  • you forgot the -t parameter when starting Julia. Use Threads.nthreads() to check the actual number of threads loaded
  • your operating system is heavily loaded with other tasks
  • [most likely] your function my_function is using parallelized code via BLAS. In order to find out the BLAS configuration try:
    using LinearAlgebra  
    LinearAlgebra.BLAS.get_num_threads()
    
    BLAS is used for linear algebra operations such as matrix multiplications. In that case the call my_function would be using most of threads already and hence small speedup is observed
  • some other libraries such as DataFrames are utilizing threading support for selected operations
  • other issues such as false sharing or race condition (does not look to be the case in your code)
Related