@@ -23,86 +23,85 @@ for i in 1:4 ta[i] = i end # assign
23
23
Array(ta) # convert to 4-element Array{Int64,1}: [1, 2, 3, 4]
24
24
```
25
25
"""
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 ())
29
30
end
30
31
31
- TArray {T} () where T = TArray (T, d)
32
32
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" )
35
35
TArray {T,N} (dim:: NTuple{N,Int} ) where {T,N} = TArray (T, dim)
36
36
37
37
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
43
43
end
44
44
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... ]
57
52
end
58
53
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
69
64
end
70
65
71
66
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)
80
75
end
81
76
82
77
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)
91
86
end
92
87
93
88
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
98
93
end
99
94
100
95
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
104
99
end
105
100
101
+ function Base. display (S:: TArray )
102
+ arr = S. orig_task. storage[S. ref][2 ]
103
+ display (arr)
104
+ end
106
105
107
106
Base. show (io:: IO , S:: TArray ) = Base. show (io:: IO , task_local_storage (S. ref)[2 ])
108
107
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])
112
111
# Base.get(t::Task, S::TArray) = (t.storage[S.ref][2])
113
112
Base. get (S:: TArray ) = (current_task (). storage[S. ref][2 ])
114
113
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)
115
127
116
128
# #########
117
129
# tzeros #
@@ -135,13 +147,37 @@ Array(tz) # convert to 4-element Array{Int64,1}: [0, 0, 0, 0]
135
147
```
136
148
"""
137
149
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
143
155
end
144
156
145
157
tzeros (:: Type{T} , d1:: Integer , drest:: Integer... ) where T = tzeros (T, convert (Dims, tuple (d1, drest... )))
146
158
tzeros (d1:: Integer , drest:: Integer... ) = tzeros (Float64, convert (Dims, tuple (d1, drest... )))
147
159
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
0 commit comments