Skip to content

Commit be8b10c

Browse files
authored
Merge pull request #8 from cpfiffer/master
TArray Updates
2 parents 859a2e6 + 90b1f9d commit be8b10c

File tree

4 files changed

+102
-62
lines changed

4 files changed

+102
-62
lines changed

src/Libtask.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module Libtask
22

3-
export consume, produce, TArray, get, tzeros
3+
export consume, produce, TArray, get, tzeros, tfill
44

55
include("../deps/deps.jl"); check_deps();
66
include("taskcopy.jl")

src/tarray.jl

Lines changed: 97 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -23,86 +23,85 @@ for i in 1:4 ta[i] = i end # assign
2323
Array(ta) # convert to 4-element Array{Int64,1}: [1, 2, 3, 4]
2424
```
2525
"""
26-
struct TArray{T,N} <: DenseArray{T,N}
27-
ref :: Symbol # object_id
28-
TArray{T,N}() where {T,N} = new(gensym())
26+
struct TArray{T,N} <: AbstractArray{T,N}
27+
ref :: Symbol # object_id
28+
orig_task :: Task
29+
TArray{T,N}() where {T,N} = new(gensym(), current_task())
2930
end
3031

31-
TArray{T}() where T = TArray(T, d)
3232
TArray{T,1}(d::Integer) where T = TArray(T, d)
33-
TArray{T}(d::Integer...) where T = TArray(T, convert(Tuple{Vararg{Int}}, d))
34-
TArray{T,N}(d::Integer...) where {T,N} = length(d)==N ? TArray(T, convert(Tuple{Vararg{Int}}, d)) : error("malformed dims")
33+
TArray{T}(d::Integer...) where T = TArray(T, d)
34+
TArray{T,N}(d::Integer...) where {T,N} = length(d)==N ? TArray(T,d) : error("Malformed dims")
3535
TArray{T,N}(dim::NTuple{N,Int}) where {T,N} = TArray(T, dim)
3636

3737
function TArray(T::Type, dim)
38-
res = TArray{T,length(dim)}();
39-
n = n_copies()
40-
d = Array{T}(undef, dim)
41-
task_local_storage(res.ref, (n,d))
42-
res
38+
res = TArray{T,length(dim)}();
39+
n = n_copies()
40+
d = Array{T}(undef, dim)
41+
task_local_storage(res.ref, (n,d))
42+
res
4343
end
4444

45-
# pass through getindex and setindex!
46-
# duplicate TArray if task id does not match current_task
47-
function Base.getindex(S::TArray, i::Real)
48-
t, d = task_local_storage(S.ref)
49-
newd = d
50-
# ct = current_task()
51-
# if t != ct
52-
# # println("[getindex]: $(S.ref ) copying data")
53-
# newd = deepcopy(d)
54-
# task_local_storage(S.ref, (ct, newd))
55-
# end
56-
getindex(newd, i)
45+
#
46+
# Indexing Interface Implementation
47+
#
48+
49+
function Base.getindex(S::TArray{T, N}, I::Vararg{Int,N}) where {T, N}
50+
t, d = task_local_storage(S.ref)
51+
return d[I...]
5752
end
5853

59-
function Base.setindex!(S::TArray, x, i::Real)
60-
n, d = task_local_storage(S.ref)
61-
cn = n_copies()
62-
newd = d
63-
if cn > n
64-
# println("[setindex!]: $(S.ref) copying data")
65-
newd = deepcopy(d)
66-
task_local_storage(S.ref, (cn, newd))
67-
end
68-
setindex!(newd, x, i)
54+
function Base.setindex!(S::TArray{T, N}, x, I::Vararg{Int,N}) where {T, N}
55+
n, d = task_local_storage(S.ref)
56+
cn = n_copies()
57+
newd = d
58+
if cn > n
59+
# println("[setindex!]: $(S.ref) copying data")
60+
newd = deepcopy(d)
61+
task_local_storage(S.ref, (cn, newd))
62+
end
63+
newd[I...] = x
6964
end
7065

7166
function Base.push!(S::TArray, x)
72-
n, d = task_local_storage(S.ref)
73-
cn = n_copies()
74-
newd = d
75-
if cn > n
76-
newd = deepcopy(d)
77-
task_local_storage(S.ref, (cn, newd))
78-
end
79-
push!(newd, x)
67+
n, d = task_local_storage(S.ref)
68+
cn = n_copies()
69+
newd = d
70+
if cn > n
71+
newd = deepcopy(d)
72+
task_local_storage(S.ref, (cn, newd))
73+
end
74+
push!(newd, x)
8075
end
8176

8277
function Base.pop!(S::TArray)
83-
n, d = task_local_storage(S.ref)
84-
cn = n_copies()
85-
newd = d
86-
if cn > n
87-
newd = deepcopy(d)
88-
task_local_storage(S.ref, (cn, newd))
89-
end
90-
pop!(d)
78+
n, d = task_local_storage(S.ref)
79+
cn = n_copies()
80+
newd = d
81+
if cn > n
82+
newd = deepcopy(d)
83+
task_local_storage(S.ref, (cn, newd))
84+
end
85+
pop!(d)
9186
end
9287

9388
function Base.convert(::Type{TArray}, x::Array)
94-
res = TArray{typeof(x[1]),ndims(x)}();
95-
n = n_copies()
96-
task_local_storage(res.ref, (n,x))
97-
res
89+
res = TArray{typeof(x[1]),ndims(x)}();
90+
n = n_copies()
91+
task_local_storage(res.ref, (n,x))
92+
return res
9893
end
9994

10095
function Base.convert(::Array, x::Type{TArray})
101-
n,d = task_local_storage(S.ref)
102-
c = deepcopy(d)
103-
return c
96+
n,d = task_local_storage(S.ref)
97+
c = deepcopy(d)
98+
return c
10499
end
105100

101+
function Base.display(S::TArray)
102+
arr = S.orig_task.storage[S.ref][2]
103+
display(arr)
104+
end
106105

107106
Base.show(io::IO, S::TArray) = Base.show(io::IO, task_local_storage(S.ref)[2])
108107
Base.size(S::TArray) = Base.size(task_local_storage(S.ref)[2])
@@ -112,6 +111,19 @@ Base.ndims(S::TArray) = Base.ndims(task_local_storage(S.ref)[2])
112111
# Base.get(t::Task, S::TArray) = (t.storage[S.ref][2])
113112
Base.get(S::TArray) = (current_task().storage[S.ref][2])
114113

114+
# Implements eltype, firstindex, lastindex, and iterate
115+
# functions.
116+
for F in (:eltype, :firstindex, :lastindex, :iterate)
117+
@eval Base.$F(a::TArray, args...) = $F(get(a), args...)
118+
end
119+
120+
#
121+
# Similarity implementation
122+
#
123+
124+
Base.similar(S::TArray) = tzeros(eltype(S), size(S))
125+
Base.similar(S::TArray, ::Type{T}) where {T} = tzeros(T, size(S))
126+
Base.similar(S::TArray, dims::Dims) = tzeros(eltype(S), dims)
115127

116128
##########
117129
# tzeros #
@@ -135,13 +147,37 @@ Array(tz) # convert to 4-element Array{Int64,1}: [0, 0, 0, 0]
135147
```
136148
"""
137149
function tzeros(T::Type, dim)
138-
res = TArray{T,length(dim)}();
139-
n = n_copies()
140-
d = zeros(T,dim)
141-
task_local_storage(res.ref, (n,d))
142-
res
150+
res = TArray{T,length(dim)}();
151+
n = n_copies()
152+
d = zeros(T,dim)
153+
task_local_storage(res.ref, (n,d))
154+
return res
143155
end
144156

145157
tzeros(::Type{T}, d1::Integer, drest::Integer...) where T = tzeros(T, convert(Dims, tuple(d1, drest...)))
146158
tzeros(d1::Integer, drest::Integer...) = tzeros(Float64, convert(Dims, tuple(d1, drest...)))
147159
tzeros(d::Dims) = tzeros(Float64, d)
160+
161+
"""
162+
tfill(val, dim, ...)
163+
164+
Construct a TArray of a specified value.
165+
166+
```julia
167+
tfill(val, dim)
168+
```
169+
170+
Example:
171+
172+
```julia
173+
tz = tfill(9.0, 4) # construct
174+
Array(tz) # convert to 4-element Array{Float64,1}: [9.0 9.0 9.0 9.0]
175+
```
176+
"""
177+
function tfill(val::Real, dim)
178+
res = TArray{typeof(val),length(dim)}();
179+
n = n_copies()
180+
d = fill(val,dim)
181+
task_local_storage(res.ref, (n,d))
182+
return res
183+
end

test/tarray.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ Base.@assert consume(a) == 4
2525
# Base.@assert TArray(Float64, 5)[1] != 0 REVIEW: can we remove this? (Kai)
2626
Base.@assert tzeros(Float64, 5)[1]==0
2727
Base.@assert tzeros(4)[1]==0
28+
Base.@assert tfill(5, 5)[1] == 5

test/tarray2.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,6 @@ for i in 1:4 ta6[i] = i / 10 end
2929
@test Array(ta6) == [0.1, 0.2, 0.3, 0.4]
3030

3131
ta7 = TArray{Int, 2}((2, 2)); # TODO: add test for use this multi-dim array
32+
33+
ta8 = tfill(19, (5,5,5,5))
34+
@test ta8[1,1,1,1] == 19

0 commit comments

Comments
 (0)