@@ -33,6 +33,8 @@ function unwrap(x::Chunk)
3333 @assert root_worker_id (x. processor) == myid ()
3434 MemPool. poolget (x. handle)
3535end
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 " ))
3638function move! (dep_mod, to_space:: MemorySpace , from_space:: MemorySpace , to:: Chunk , from:: Chunk )
3739 to_w = root_worker_id (to_space)
3840 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
4446 end
4547 return
4648end
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
4753function move! (dep_mod, to_space:: MemorySpace , from_space:: MemorySpace , to:: AbstractArray{T,N} , from:: AbstractArray{T,N} ) where {T,N}
4854 move! (to_space, from_space, dep_mod (to), dep_mod (from))
4955end
6672
6773# ## Aliasing and Memory Spans
6874
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+
6992may_alias (:: MemorySpace , :: MemorySpace ) = true
7093may_alias (space1:: CPURAMMemorySpace , space2:: CPURAMMemorySpace ) = space1. owner == space2. owner
7194
@@ -104,8 +127,71 @@ memory_spans(::NoAliasing) = MemorySpan{CPURAMMemorySpace}[]
104127struct UnknownAliasing <: AbstractAliasing end
105128memory_spans (:: UnknownAliasing ) = [MemorySpan {CPURAMMemorySpace} (C_NULL , typemax (UInt))]
106129
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+
107165aliasing (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 ()
109195aliasing (x:: Chunk , T) = remotecall_fetch (root_worker_id (x. processor), x, T) do x, T
110196 aliasing (unwrap (x), T)
111197end
@@ -129,6 +215,7 @@ function aliasing(x::Array{T}) where T
129215 else
130216 # FIXME : Also ContiguousAliasing of container
131217 # return IteratedAliasing(x)
218+ warn_unknown_aliasing (T)
132219 return UnknownAliasing ()
133220 end
134221end
@@ -173,6 +260,7 @@ function aliasing(x::SubArray{T,N,A}) where {T,N,A<:Array}
173260 else
174261 # FIXME : Also ContiguousAliasing of container
175262 # return IteratedAliasing(x)
263+ warn_unknown_aliasing (T)
176264 return UnknownAliasing ()
177265 end
178266end
0 commit comments