@@ -33,6 +33,8 @@ function unwrap(x::Chunk)
33
33
@assert root_worker_id (x. processor) == myid ()
34
34
MemPool. poolget (x. handle)
35
35
end
36
+ move! (dep_mod, to_space:: MemorySpace , from_space:: MemorySpace , to:: T , from:: F ) where {T,F} =
37
+ throw (ArgumentError (" No `move!` implementation defined for $F -> $T " ))
36
38
function move! (dep_mod, to_space:: MemorySpace , from_space:: MemorySpace , to:: Chunk , from:: Chunk )
37
39
to_w = root_worker_id (to_space)
38
40
remotecall_wait (to_w, dep_mod, to_space, from_space, to, from) do dep_mod, to_space, from_space, to, from
@@ -44,6 +46,10 @@ function move!(dep_mod, to_space::MemorySpace, from_space::MemorySpace, to::Chun
44
46
end
45
47
return
46
48
end
49
+ function move! (dep_mod, to_space:: MemorySpace , from_space:: MemorySpace , to:: Base.RefValue{T} , from:: Base.RefValue{T} ) where {T}
50
+ to[] = from[]
51
+ return
52
+ end
47
53
function move! (dep_mod, to_space:: MemorySpace , from_space:: MemorySpace , to:: AbstractArray{T,N} , from:: AbstractArray{T,N} ) where {T,N}
48
54
move! (to_space, from_space, dep_mod (to), dep_mod (from))
49
55
end
66
72
67
73
# ## Aliasing and Memory Spans
68
74
75
+ type_may_alias (:: Type{String} ) = false
76
+ type_may_alias (:: Type{Symbol} ) = false
77
+ type_may_alias (:: Type{<:Type} ) = false
78
+ type_may_alias (:: Type{C} ) where C<: Chunk{T} where T = type_may_alias (T)
79
+ function type_may_alias (:: Type{T} ) where T
80
+ if isbitstype (T)
81
+ return false
82
+ elseif ismutabletype (T)
83
+ return true
84
+ elseif isstructtype (T)
85
+ for FT in fieldtypes (T)
86
+ type_may_alias (FT) && return true
87
+ end
88
+ end
89
+ return false
90
+ end
91
+
69
92
may_alias (:: MemorySpace , :: MemorySpace ) = true
70
93
may_alias (space1:: CPURAMMemorySpace , space2:: CPURAMMemorySpace ) = space1. owner == space2. owner
71
94
@@ -104,8 +127,71 @@ memory_spans(::NoAliasing) = MemorySpan{CPURAMMemorySpace}[]
104
127
struct UnknownAliasing <: AbstractAliasing end
105
128
memory_spans (:: UnknownAliasing ) = [MemorySpan {CPURAMMemorySpace} (C_NULL , typemax (UInt))]
106
129
130
+ warn_unknown_aliasing (T) =
131
+ @warn " Cannot resolve aliasing for object of type $T \n Execution may become sequential"
132
+
133
+ struct CombinedAliasing <: AbstractAliasing
134
+ sub_ainfos:: Vector{AbstractAliasing}
135
+ end
136
+ function memory_spans (ca:: CombinedAliasing )
137
+ # FIXME : Don't hardcode CPURAMMemorySpace
138
+ all_spans = MemorySpan{CPURAMMemorySpace}[]
139
+ for sub_a in ca. sub_ainfos
140
+ append! (all_spans, memory_spans (sub_a))
141
+ end
142
+ return all_spans
143
+ end
144
+ Base.:(== )(ca1:: CombinedAliasing , ca2:: CombinedAliasing ) =
145
+ ca1. sub_ainfos == ca2. sub_ainfos
146
+ Base. hash (ca1:: CombinedAliasing , h:: UInt ) =
147
+ hash (ca1. sub_ainfos, hash (CombinedAliasing, h))
148
+
149
+ struct ObjectAliasing <: AbstractAliasing
150
+ ptr:: Ptr{Cvoid}
151
+ sz:: UInt
152
+ end
153
+ function ObjectAliasing (x:: T ) where T
154
+ @nospecialize x
155
+ ptr = pointer_from_objref (x)
156
+ sz = sizeof (T)
157
+ return ObjectAliasing (ptr, sz)
158
+ end
159
+ function memory_spans (oa:: ObjectAliasing )
160
+ rptr = RemotePtr {Cvoid} (oa. ptr)
161
+ span = MemorySpan {CPURAMMemorySpace} (rptr, oa. sz)
162
+ return [span]
163
+ end
164
+
107
165
aliasing (x, T) = aliasing (T (x))
108
- aliasing (x) = isbits (x) ? NoAliasing () : UnknownAliasing ()
166
+ function aliasing (x:: T ) where T
167
+ if isbits (x)
168
+ return NoAliasing ()
169
+ elseif isstructtype (T)
170
+ as = AbstractAliasing[]
171
+ # If the object itself is mutable, it can alias
172
+ if ismutabletype (T)
173
+ push! (as, ObjectAliasing (x))
174
+ end
175
+ # Check all object fields (recursive)
176
+ for field in fieldnames (T)
177
+ sub_as = aliasing (getfield (x, field))
178
+ if sub_as isa NoAliasing
179
+ continue
180
+ elseif sub_as isa CombinedAliasing
181
+ append! (as, sub_as. sub_ainfos)
182
+ else
183
+ push! (as, sub_as)
184
+ end
185
+ end
186
+ return CombinedAliasing (as)
187
+ else
188
+ warn_unknown_aliasing (T)
189
+ return UnknownAliasing ()
190
+ end
191
+ end
192
+ aliasing (:: String ) = NoAliasing () # FIXME : Not necessarily true
193
+ aliasing (:: Symbol ) = NoAliasing ()
194
+ aliasing (:: Type ) = NoAliasing ()
109
195
aliasing (x:: Chunk , T) = remotecall_fetch (root_worker_id (x. processor), x, T) do x, T
110
196
aliasing (unwrap (x), T)
111
197
end
@@ -129,6 +215,7 @@ function aliasing(x::Array{T}) where T
129
215
else
130
216
# FIXME : Also ContiguousAliasing of container
131
217
# return IteratedAliasing(x)
218
+ warn_unknown_aliasing (T)
132
219
return UnknownAliasing ()
133
220
end
134
221
end
@@ -173,6 +260,7 @@ function aliasing(x::SubArray{T,N,A}) where {T,N,A<:Array}
173
260
else
174
261
# FIXME : Also ContiguousAliasing of container
175
262
# return IteratedAliasing(x)
263
+ warn_unknown_aliasing (T)
176
264
return UnknownAliasing ()
177
265
end
178
266
end
0 commit comments