Skip to content

Commit 0b0a606

Browse files
committed
Make load/store! increment size of the loaded/stored types.
1 parent f1012a9 commit 0b0a606

7 files changed

+48
-30
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
Manifest.toml
55
/docs/build/
66
src/*~
7+
*#*

Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "ThreadingUtilities"
22
uuid = "8290d209-cae3-49c0-8002-c8c24d57dab5"
33
authors = ["Chris Elrod <[email protected]> and contributors"]
4-
version = "0.2.5"
4+
version = "0.3.0"
55

66
[deps]
77
VectorizationBase = "3d5dd08c-fd9d-11e8-17fa-ed2836048c2f"

src/ThreadingUtilities.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module ThreadingUtilities
22

33
using VectorizationBase:
4-
pause, StaticInt, StridedPointer, offsets, cache_linesize, align, vload, vstore!, num_threads, assume
4+
pause, StaticInt, StridedPointer, offsets, cache_linesize, align, __vload, __vstore!, num_threads, assume, False, register_size
55

66
@enum ThreadState::UInt begin
77
SPIN = 0 # 0: spinning
@@ -11,7 +11,7 @@ using VectorizationBase:
1111
STUP = 4 # 4: problem being setup. Any reason to have two lock flags?
1212
end
1313
const TASKS = Task[]
14-
const THREADBUFFERSIZE = 32
14+
const THREADBUFFERSIZE = 64
1515
const THREADPOOL = UInt[]
1616
const THREADPOOLPTR = Ref{Ptr{UInt}}(C_NULL);
1717

src/threadtasks.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ function wake_thread!(tid)
4444
end
4545

4646
# 1-based tid
47-
@inline function __wait(tid)
48-
p = taskpointer(tid)
47+
@inline __wait(tid::Integer) = __wait(taskpointer(tid))
48+
@inline function __wait(p::Ptr{UInt})
4949
# note: based on relative values (SPIN = 0, WAIT = 1)
5050
# thus it should spin for as long as the task is doing anything else
5151
while reinterpret(UInt, _atomic_load(p)) > reinterpret(UInt, WAIT)

src/utils.jl

+22-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,24 @@
11
# To add support for loading/storing...
2-
@inline function load(p::Ptr{UInt}, ::Type{T}) where {T}
3-
reinterpret(T, vload(p))
2+
@inline function load(p::Ptr{UInt}, ::Type{T}) where {T<:Ptr}
3+
reinterpret(T, __vload(p, False(), register_size()))
44
end
5-
@inline function store!(p::Ptr{UInt}, x)
6-
vstore!(p, reinterpret(UInt, x))
5+
@inline function load(p::Ptr{UInt32}, ::Type{T}) where {T<:Union{UInt32,Int32,Float32}}
6+
reinterpret(T, __vload(p, False(), register_size()))
77
end
8+
@inline function load(p::Ptr{UInt64}, ::Type{T}) where {T<:Union{UInt64,Int64,Float64}}
9+
reinterpret(T, __vload(p, False(), register_size()))
10+
end
11+
@inline load(p::Ptr{UInt}, ::Type{T}) where {T} = unsafe_load(Base.unsafe_convert(Ptr{T}, p))
12+
@inline function store!(p::Ptr{UInt}, x::T) where {T <: Ptr}
13+
__vstore!(p, reinterpret(UInt, x), False(), False(), False(), register_size())
14+
end
15+
@inline function store!(p::Ptr{UInt32}, x::T) where {T <: Union{UInt32,Int32,Float32}}
16+
__vstore!(p, reinterpret(UInt, x), False(), False(), False(), register_size())
17+
end
18+
@inline function store!(p::Ptr{UInt64}, x::T) where {T <: Union{UInt64,Int64,Float64}}
19+
__vstore!(p, reinterpret(UInt, x), False(), False(), False(), register_size())
20+
end
21+
@inline store!(p::Ptr{UInt}, x::T) where {T} = (unsafe_store!(Base.unsafe_convert(Ptr{T}, p), x); nothing)
822

923
@inline load(p::Ptr{UInt}, ::Type{StaticInt{N}}, i) where {N} = i, StaticInt{N}()
1024
@inline store!(p::Ptr{UInt}, ::StaticInt, i) = i
@@ -53,12 +67,11 @@ end
5367
end
5468

5569
@inline function load(p::Ptr{UInt}, ::Type{T}, i) where {T}
56-
i += 1
57-
i, load(p + i * sizeof(UInt), T)
70+
i + sizeof(T), load(p + i, T)
5871
end
5972
@inline function store!(p::Ptr{UInt}, x, i)
60-
store!(p + sizeof(UInt)*(i += 1), x)
61-
i
73+
store!(p + i, x)
74+
i + sizeof(x)
6275
end
6376

6477
@generated function load(p::Ptr{UInt}, ::Type{T}, i) where {T<:Tuple}
@@ -81,4 +94,5 @@ end
8194
store!(p, first(tup), i)
8295
end
8396
@inline store!(p::Ptr{UInt}, tup::Tuple{}, i) = i
97+
@inline store!(p::Ptr{UInt}, tup::Nothing, i) = i
8498

test/internals.jl

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
@testset "Internals" begin
22
@test ThreadingUtilities.store!(pointer(UInt[]), (), 1) == 1
3+
@test ThreadingUtilities.store!(pointer(UInt[]), nothing, 1) == 1
34
end

test/threadingutilities.jl

+19-17
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
struct Copy{P} end
22
function (::Copy{P})(p::Ptr{UInt}) where {P}
3-
_, (ptry,ptrx,N) = ThreadingUtilities.load(p, P, 1)
3+
_, (ptry,ptrx,N) = ThreadingUtilities.load(p, P, 2*sizeof(UInt))
44
N > 0 || throw("This function throws if N == 0 for testing purposes.")
55
@simd ivdep for n 1:N
66
vstore!(ptry, vload(ptrx, (n,)), (n,))
@@ -18,7 +18,7 @@ function setup_copy!(p, y, x)
1818
py = stridedpointer(y)
1919
px = stridedpointer(x)
2020
fptr = copy_ptr(py, px)
21-
offset = ThreadingUtilities.store!(p, fptr, 0)
21+
offset = ThreadingUtilities.store!(p, fptr, sizeof(UInt))
2222
ThreadingUtilities.store!(p, (py,px,N), offset)
2323
end
2424

@@ -46,21 +46,23 @@ function test_copy(tid, N = 100_000)
4646
x = similar(a) .= NaN;
4747
y = similar(b) .= NaN;
4848
z = similar(c) .= NaN;
49-
launch_thread_copy!(tid, x, a)
50-
yield()
51-
# sleep(1e-3)
52-
ThreadingUtilities.__wait(tid)
53-
launch_thread_copy!(tid, y, b)
54-
yield()
55-
# sleep(1e-3)
56-
ThreadingUtilities.__wait(tid)
57-
launch_thread_copy!(tid, z, c)
58-
yield()
59-
# sleep(1e-3)
60-
ThreadingUtilities.__wait(tid)
61-
@test a == x
62-
@test b == y
63-
@test c == z
49+
GC.@preserve a b c x y z begin
50+
launch_thread_copy!(tid, x, a)
51+
yield()
52+
# sleep(1e-3)
53+
ThreadingUtilities.__wait(tid)
54+
launch_thread_copy!(tid, y, b)
55+
yield()
56+
# sleep(1e-3)
57+
ThreadingUtilities.__wait(tid)
58+
launch_thread_copy!(tid, z, c)
59+
yield()
60+
# sleep(1e-3)
61+
ThreadingUtilities.__wait(tid)
62+
@test a == x
63+
@test b == y
64+
@test c == z
65+
end
6466
end
6567

6668
@testset "ThreadingUtilities.jl" begin

0 commit comments

Comments
 (0)