Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fast-track @threads when nthreads() == 1 #32181

Closed
wants to merge 1 commit into from

Commits on Jul 6, 2019

  1. Fast-track @threads when nthreads() == 1

    This avoids overhead when threading is disabled, Example benchmark:
    
    ```
    using BenchmarkTools, Base.Threads, Test
    
    function func(val, N)
        sums = [0*(1 .^ val) for thread_idx in 1:nthreads()]
        for idx in 1:N
            sums[threadid()] += idx.^val
        end
        return sum(sums)
    end
    
    function func_threaded(val, N)
        sums = [0*(1 .^ val) for thread_idx in 1:nthreads()]
        @threads for idx in 1:N
            sums[threadid()] += idx.^val
        end
        return sum(sums)
    end
    
    @test func(2.0, 1<<10) == func_threaded(2.0, 1<<10)
    
    @show @benchmark func(2.0, 1<<10)
    @show @benchmark func_threaded(2.0, 1<<10)
    ```
    
    Running the benchmarks as:
    ```
    for JULIA in julia-master ./julia; do
        for T in 1 2; do
            echo "$JULIA with $T threads:"
            JULIA_NUM_THREADS=$T $JULIA speedtest.jl
        done
    done
    ```
    
    Before this commit:
    ```
    julia-master with 1 threads:
    julia-master with 2 threads:
    ```
    
    After this commit:
    ```
    ./julia with 1 threads:
    ./julia with 2 threads:
    ```
    staticfloat committed Jul 6, 2019
    Configuration menu
    Copy the full SHA
    baa94c8 View commit details
    Browse the repository at this point in the history