Skip to content

Threads.nthreads() not a compile-time constant. #34902

@MasonProtter

Description

@MasonProtter

Since the number of threads can't be changed without restarting julia, I find this behavior quite surprising:

julia> @code_llvm Threads.nthreads()
;  @ threadingconstructs.jl:19 within `nthreads'
define i64 @julia_nthreads_20335() {
top:
; ┌ @ pointer.jl:105 within `unsafe_load' @ pointer.jl:105
   %0 = load i32, i32* inttoptr (i64 140333758273848 to i32*), align 8
; └
; ┌ @ boot.jl:707 within `Int64'
; │┌ @ boot.jl:626 within `toInt64'
    %1 = sext i32 %0 to i64
; └└
  ret i64 %1
}

In general, I think it would be useful to do something like

const NUM_THREADS = Int(unsafe_load(cglobal(:jl_n_threads, Cint)))
nthreads() = NUM_THREADS

so that branches / dispatches which depend on the number of threads can be eliminated.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions