11
22
3- # RECTANGULAR WORKSPACES
3+ # Import ChunkSplitters for better threading
4+ using ChunkSplitters: chunks
45
6+ # RECTANGULAR WORKSPACES
57
6- # default workspaces are immutable, so just forward the type
7- wrapserialworkspace (w, tid) = w
88
99struct CarClenshawCurtisProfileWorkspace{T,A<: AbstractArray{T,2} } <: AbstractProfileWorkspace{T}
1010 sin_α:: A
@@ -50,13 +50,16 @@ function profile_grid(model::AbstractGNFW{T}, logθs, redshifts, logMs) where T
5050 N_logθ, N_z, N_logM = length (logθs), length (redshifts), length (logMs)
5151 A = zeros (T, (N_logθ, N_z, N_logM))
5252
53- Threads. @threads :static for im in 1 : N_logM
54- logM = logMs[im]
55- M = 10 ^ (logM)
56- for (iz, z) in enumerate (redshifts)
57- for iθ in 1 : N_logθ
58- θ = exp (logθs[iθ])
59- A[iθ, iz, im] = max (zero (T), model (θ, M, z))
53+ # Use ChunkSplitters for better load balancing
54+ Threads. @threads for chunk in chunks (1 : N_logM; n= Threads. nthreads ())
55+ for im in chunk
56+ logM = logMs[im]
57+ M = 10 ^ (logM)
58+ for (iz, z) in enumerate (redshifts)
59+ for iθ in 1 : N_logθ
60+ θ = exp (logθs[iθ])
61+ A[iθ, iz, im] = max (zero (T), model (θ, M, z))
62+ end
6063 end
6164 end
6265 end
325328function profile_paint! (m:: Enmap{T, 2, Matrix{T}, Gnomonic{T}} ,
326329 workspace:: GnomonicProfileWorkspace , model, Mh, z, α₀, δ₀,
327330 θmax, normalization= 1 ) where T
328- profile_paint_generic! (m, model, workspace , Mh, z, α₀, δ₀, θmax, normalization)
331+ profile_paint_generic! (m, workspace, model , Mh, z, α₀, δ₀, θmax, normalization)
329332end
330333
331334
@@ -417,25 +420,15 @@ function paint!(m, workspace, model, masses, redshifts, αs, δs;
417420 zerobeforepainting && _fillzero! (m)
418421
419422 N_sources = length (masses)
420- chunksize = ceil (Int, N_sources / (2 Threads. nthreads ()))
421- chunks = chunk (N_sources, chunksize)
422-
423+
423424 if N_sources < 2 Threads. nthreads () # don't thread if there are not many sources
424- return paintrange! (1 : N_sources, m, wrapserialworkspace ( workspace, 1 ) ,
425+ return paintrange! (1 : N_sources, m, workspace,
425426 model, masses, redshifts, αs, δs)
426427 end
427428
428- Threads. @threads for ti in 1 : Threads. nthreads ()
429- chunk_i = 2 ti
430- i1, i2 = chunks[chunk_i]
431- paintrange! (i1: i2, m, wrapserialworkspace (workspace, ti),
432- model, masses, redshifts, αs, δs)
433- end
434-
435- Threads. @threads for ti in 1 : Threads. nthreads ()
436- chunk_i = 2 ti - 1
437- i1, i2 = chunks[chunk_i]
438- paintrange! (i1: i2, m, wrapserialworkspace (workspace, ti),
429+ # Use ChunkSplitters for better load balancing
430+ Threads. @threads for chunk in chunks (1 : N_sources; n= 2 * Threads. nthreads ())
431+ paintrange! (chunk, m, workspace,
439432 model, masses, redshifts, αs, δs)
440433 end
441434end
@@ -460,25 +453,15 @@ function paint!(m, workspace, model, masses, redshifts, αs, δs, proj_v_over_c;
460453 zerobeforepainting && _fillzero! (m)
461454
462455 N_sources = length (masses)
463- chunksize = ceil (Int, N_sources / (2 Threads. nthreads ()))
464- chunks = chunk (N_sources, chunksize)
465-
456+
466457 if N_sources < 2 Threads. nthreads () # don't thread if there are not many sources
467- return paintrange! (1 : N_sources, m, wrapserialworkspace (workspace, 1 ),
468- model, masses, redshifts, αs, δs, proj_v_over_c)
469- end
470-
471- Threads. @threads for ti in 1 : Threads. nthreads ()
472- chunk_i = 2 ti
473- i1, i2 = chunks[chunk_i]
474- paintrange! (i1: i2, m, wrapserialworkspace (workspace, ti),
458+ return paintrange! (1 : N_sources, m, workspace,
475459 model, masses, redshifts, αs, δs, proj_v_over_c)
476460 end
477461
478- Threads. @threads for ti in 1 : Threads. nthreads ()
479- chunk_i = 2 ti - 1
480- i1, i2 = chunks[chunk_i]
481- paintrange! (i1: i2, m, wrapserialworkspace (workspace, ti),
462+ # Use ChunkSplitters for better load balancing
463+ Threads. @threads for chunk in chunks (1 : N_sources; n= 2 * Threads. nthreads ())
464+ paintrange! (chunk, m, workspace,
482465 model, masses, redshifts, αs, δs, proj_v_over_c)
483466 end
484467end
488471# function paint!(m, workspace::HealpixSerialProfileWorkspace, model, masses, redshifts, αs, δs;
489472# zerobeforepainting=true)
490473# zerobeforepainting && _fillzero!(m)
491- # return paintrange!(1:length(masses), m, wrapserialworkspace( workspace, 1) ,
474+ # return paintrange!(1:length(masses), m, workspace,
492475# model, masses, redshifts, αs, δs)
493476# end
0 commit comments