Skip to content

Commit 71fb308

Browse files
authored
Merge pull request #39 from omlins/thread-bounds-check
Make kernel launch parameter computation performance-negligable (also for small problems)
2 parents 40060f3 + dc763ca commit 71fb308

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

src/ParallelKernel/parallel.jl

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -367,13 +367,15 @@ promote_maxsize(maxsize::MAXSIZE_TYPE_2D) = (maxsize..., 1)
367367
promote_maxsize(maxsize::MAXSIZE_TYPE) = maxsize
368368
promote_maxsize(maxsize) = @ModuleInternalError("maxsize must be a Tuple of Integer of size 1, 2 or 3 (obtained: $maxsize; its type is: $(typeof(maxsize))).")
369369

370+
maxsize(A::T) where T<:AbstractArray = (size(A,1),size(A,2),size(A,3)) # NOTE: using size(A,dim) three times instead of size(A) ensures to have a tuple of length 3.
371+
maxsize(a::T) where T<:Number = (1, 1, 1)
372+
maxsize(x) = @ArgumentError("automatic detection of ranges not possible in @parallel call: some kernel arguments are neither arrays nor scalars. Specify ranges or nthreads and nblocks manually.")
373+
maxsize(x, args...) = merge(maxsize(x), maxsize(args...)) # NOTE: maxsize is implemented as a recursive function, which results in optimal code; otherwise, the function is not performance-negligable for small problems.
374+
merge(a::Tuple, b::Tuple) = max.(a,b)
375+
370376
function get_ranges(args...)
371-
complex_args = [x for x in args if !(isa(x, AbstractArray) || isa(x, Number))]
372-
if length(complex_args) > 0 @ArgumentError("automatic detection of ranges not possible in @parallel call: some kernel arguments are neither arrays nor scalars. Specify ranges or nthreads and nblocks manually.") end
373-
arrays = [x for x in args if isa(x, AbstractArray)];
374-
if (length(arrays) == 0) @ArgumentError("automatic detection of ranges not possible: no arrays detected in kernel arguments. Specify ranges or nthreads and nblocks manually.") end
375-
maxsize = ( maximum([size(A,1) for A in arrays]), maximum([size(A,2) for A in arrays]), maximum([size(A,3) for A in arrays]) )
376-
return (1:max(maxsize[1],1), 1:max(maxsize[2],1), 1:max(maxsize[3],1))
377+
maxsizes = maxsize(args...)
378+
return (1:maxsizes[1], 1:maxsizes[2], 1:maxsizes[3])
377379
end
378380

379381
function compute_ranges(maxsize)

0 commit comments

Comments
 (0)