@@ -11,20 +11,27 @@ ID `1`.
1111"""
1212threadid () = Int (ccall (:jl_threadid , Int16, ())+ 1 )
1313
14+ # lower bound on the largest threadid()
1415"""
15- Threads.nthreads([:default|:interactive] ) -> Int
16+ Threads.maxthreadid( ) -> Int
1617
17- Get the number of threads (across all thread pools or within the specified
18- thread pool) available to Julia. The number of threads across all thread
19- pools is the inclusive upper bound on [`threadid()`](@ref).
18+ Get a lower bound on the number of threads (across all thread pools) available
19+ to the Julia process, with atomic-acquire semantics. The result will always be
20+ greater than or equal to [`threadid()`](@ref) as well as `threadid(task)` for
21+ any task you were able to observe before calling `maxthreadid`.
22+ """
23+ maxthreadid () = Int (Core. Intrinsics. atomic_pointerref (cglobal (:jl_n_threads , Cint), :acquire ))
2024
21- See also: `BLAS.get_num_threads` and `BLAS.set_num_threads` in the
22- [`LinearAlgebra`](@ref man-linalg) standard library, and `nprocs()` in the
23- [`Distributed`](@ref man-distributed) standard library.
2425"""
25- function nthreads end
26+ Threads. nthreads(:default | :interactive) -> Int
2627
27- nthreads () = Int (unsafe_load (cglobal (:jl_n_threads , Cint)))
28+ Get the current number of threads within the specified thread pool. The threads in default
29+ have id numbers `1:nthreads(:default)`.
30+
31+ See also `BLAS.get_num_threads` and `BLAS.set_num_threads` in the [`LinearAlgebra`](@ref
32+ man-linalg) standard library, and `nprocs()` in the [`Distributed`](@ref man-distributed)
33+ standard library and [`Threads.maxthreadid()`](@ref).
34+ """
2835function nthreads (pool:: Symbol )
2936 if pool === :default
3037 tpid = Int8 (0 )
@@ -35,6 +42,7 @@ function nthreads(pool::Symbol)
3542 end
3643 return _nthreads_in_pool (tpid)
3744end
45+
3846function _nthreads_in_pool (tpid:: Int8 )
3947 p = unsafe_load (cglobal (:jl_n_threads_per_pool , Ptr{Cint}))
4048 return Int (unsafe_load (p, tpid + 1 ))
@@ -57,10 +65,20 @@ Returns the number of threadpools currently configured.
5765"""
5866nthreadpools () = Int (unsafe_load (cglobal (:jl_n_threadpools , Cint)))
5967
68+ """
69+ Threads.threadpoolsize()
70+
71+ Get the number of threads available to the Julia default worker-thread pool.
72+
73+ See also: `BLAS.get_num_threads` and `BLAS.set_num_threads` in the
74+ [`LinearAlgebra`](@ref man-linalg) standard library, and `nprocs()` in the
75+ [`Distributed`](@ref man-distributed) standard library.
76+ """
77+ threadpoolsize () = Threads. _nthreads_in_pool (Int8 (0 ))
6078
6179function threading_run (fun, static)
6280 ccall (:jl_enter_threaded_region , Cvoid, ())
63- n = nthreads ()
81+ n = threadpoolsize ()
6482 tasks = Vector {Task} (undef, n)
6583 for i = 1 : n
6684 t = Task (() -> fun (i)) # pass in tid
@@ -93,7 +111,7 @@ function _threadsfor(iter, lbody, schedule)
93111 tid = 1
94112 len, rem = lenr, 0
95113 else
96- len, rem = divrem (lenr, nthreads ())
114+ len, rem = divrem (lenr, threadpoolsize ())
97115 end
98116 # not enough iterations for all the threads?
99117 if len == 0
@@ -185,7 +203,7 @@ assumption may be removed in the future.
185203This scheduling option is merely a hint to the underlying execution mechanism. However, a
186204few properties can be expected. The number of `Task`s used by `:dynamic` scheduler is
187205bounded by a small constant multiple of the number of available worker threads
188- ([`nthreads ()`](@ref Threads.nthreads )). Each task processes contiguous regions of the
206+ ([`Threads.threadpoolsize ()`](@ref)). Each task processes contiguous regions of the
189207iteration space. Thus, `@threads :dynamic for x in xs; f(x); end` is typically more
190208efficient than `@sync for x in xs; @spawn f(x); end` if `length(xs)` is significantly
191209larger than the number of the worker threads and the run-time of `f(x)` is relatively
@@ -222,15 +240,15 @@ julia> function busywait(seconds)
222240
223241julia> @time begin
224242 Threads.@spawn busywait(5)
225- Threads.@threads :static for i in 1:Threads.nthreads ()
243+ Threads.@threads :static for i in 1:Threads.threadpoolsize ()
226244 busywait(1)
227245 end
228246 end
2292476.003001 seconds (16.33 k allocations: 899.255 KiB, 0.25% compilation time)
230248
231249julia> @time begin
232250 Threads.@spawn busywait(5)
233- Threads.@threads :dynamic for i in 1:Threads.nthreads ()
251+ Threads.@threads :dynamic for i in 1:Threads.threadpoolsize ()
234252 busywait(1)
235253 end
236254 end
0 commit comments