JuliaSIMD / JuliaSIMD/Polyester.jl
Large overhead using @batch
- Dominant language
- Julia
- Stars
- 285
- Forks
- 20
- PR merge metrics
- No merged PRs in 30d
Description
When I try to use `@batch` to improve the performance with multithreading, I found it has a large overhead which is similar to `Threads.@threads`.
Here is the code I tested which is the task of applying the X-gate on the first qubit in quantum simulation. And the functions `serial_X1`, `batch_X1`, and `threads_X1` are single-thread, multi-thread with `@batch`, and multi-threads with `Threads.@threads` version of X-gate on the first qubit respectly.
```julia
using Polyester
using BenchmarkTools
@inline function scheduler(L::Integer, tid, nthreads)
N = L ÷ 2
bsize = N ÷ nthreads
tid < nthreads && return (tid-1)*bsize*2:2:tid*bsize*2-2
return (tid-1)*bsize*2:2:N*2-2
end
@inline function kernel_vec(x, iter)
for i in iter
@inbounds begin
temp = x[i+1]
x[i+1] = x[i+2]
x[i+2] = temp
end
end
return x
end
@inline function kernel_mat(x, iter)
bs = size(x, 1)
for i in iter
for b in 1:bs
@inbounds begin
temp = x[b, i+1]
x[b, i+1] = x[b, i+2]
x[b, i+2] = temp
end
end
end
return x
end
function seriel_X1(x::Vector{T}) where T
for i in 0:2:length(x)-2
@inbounds begin
temp = x[i+1]
x[i+1] = x[i+2]
x[i+2] = temp
end
end
return x
end
function batch_X1(x::Vector{T}) where T
nthreads = Threads.nthreads()
N = length(x)
@batch for tid = 1:nthreads
kernel_vec(x, scheduler(N, tid, nthreads))
end
return x
end
function threads_X1(x::Vector{T}) where T
nthreads = Threads.nthreads()
N = length(x)
Threads.@threads for tid = 1:nthreads
kernel_vec(x, scheduler(N, tid, nthreads))
end
return x
end
function seriel_X1(x::Matrix{T}) where T
batch_size, state_size = size(x)
for i in 0:2:state_size-2
@inbounds for b in 1:batch_size
temp = x[b, i+1]
x[b, i+1] = x[b, i+2]
x[b, i+2] = temp
end
end
return x
end
function batch_X1(x::Matrix{T}) where T
nthreads = Threads.nthreads()
batch_size, state_size = size(x)
@batch per=thread for tid in 1:nthreads
kernel_mat(x, scheduler(state_size, tid, nthreads))
end
return x
end
function threads_X1(x::Matrix{T}) where T
nthreads = Threads.nthreads()
batch_size, state_size = size(x)
Threads.@threads for tid in 1:nthreads
kernel_mat(x, scheduler(state_size, tid, nthreads))
end
return x
end
N = 29
M = 8
B = 1<
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.