@@ -9,7 +9,7 @@ const LAUNCH_KWARGS = [:global_size, :local_size, :queue]
99
1010macro opencl (ex... )
1111 call = ex[end ]
12- kwargs = map (ex[1 : end - 1 ]) do kwarg
12+ kwargs = map (ex[1 : ( end - 1 ) ]) do kwarg
1313 if kwarg isa Symbol
1414 :($ kwarg = $ kwarg)
1515 elseif Meta. isexpr (kwarg, :(= ))
@@ -31,14 +31,14 @@ macro opencl(ex...)
3131 macro_kwargs, compiler_kwargs, call_kwargs, other_kwargs =
3232 split_kwargs (kwargs, MACRO_KWARGS, COMPILER_KWARGS, LAUNCH_KWARGS)
3333 if ! isempty (other_kwargs)
34- key,val = first (other_kwargs). args
34+ key, val = first (other_kwargs). args
3535 throw (ArgumentError (" Unsupported keyword argument '$key '" ))
3636 end
3737
3838 # handle keyword arguments that influence the macro's behavior
3939 launch = true
4040 for kwarg in macro_kwargs
41- key,val = kwarg. args
41+ key, val = kwarg. args
4242 if key == :launch
4343 isa (val, Bool) || throw (ArgumentError (" `launch` keyword argument to @opencl should be a constant value" ))
4444 launch = val:: Bool
@@ -56,7 +56,8 @@ macro opencl(ex...)
5656
5757 # convert the arguments, call the compiler and launch the kernel
5858 # while keeping the original arguments alive
59- push! (code. args,
59+ push! (
60+ code. args,
6061 quote
6162 $ f_var = $ f
6263 GC. @preserve $ (vars... ) $ f_var begin
@@ -69,13 +70,16 @@ macro opencl(ex...)
6970 end
7071 $ kernel
7172 end
72- end )
73+ end
74+ )
7375
74- return esc (quote
75- let
76- $ code
76+ return esc (
77+ quote
78+ let
79+ $ code
80+ end
7781 end
78- end )
82+ )
7983end
8084
8185
@@ -101,21 +105,23 @@ end
101105# Base.RefValue isn't GPU compatible, so provide a compatible alternative
102106# TODO : port improvements from CUDA.jl
103107struct CLRefValue{T} <: Ref{T}
104- x:: T
108+ x:: T
105109end
106110Base. getindex (r:: CLRefValue ) = r. x
107111Adapt. adapt_structure (to:: KernelAdaptor , r:: Base.RefValue ) = CLRefValue (adapt (to, r[]))
108112
109113# broadcast sometimes passes a ref(type), resulting in a GPU-incompatible DataType box.
110114# avoid that by using a special kind of ref that knows about the boxed type.
111115struct CLRefType{T} <: Ref{DataType} end
112- Base. getindex (r:: CLRefType{T} ) where T = T
113- Adapt. adapt_structure (to:: KernelAdaptor , r:: Base.RefValue{<:Union{DataType,Type}} ) =
116+ Base. getindex (r:: CLRefType{T} ) where {T} = T
117+ Adapt. adapt_structure (to:: KernelAdaptor , r:: Base.RefValue{<:Union{DataType, Type}} ) =
114118 CLRefType {r[]} ()
115119
116120# case where type is the function being broadcasted
117- Adapt. adapt_structure (to:: KernelAdaptor ,
118- bc:: Broadcast.Broadcasted{Style, <:Any, Type{T}} ) where {Style, T} =
121+ Adapt. adapt_structure (
122+ to:: KernelAdaptor ,
123+ bc:: Broadcast.Broadcasted{Style, <:Any, Type{T}}
124+ ) where {Style, T} =
119125 Broadcast. Broadcasted {Style} ((x... ) -> T (x... ), adapt (to, bc. args), bc. axes)
120126
121127"""
@@ -131,29 +137,30 @@ register methods for the the `OpenCL.KernelAdaptor` type.
131137The `pointers` argument is used to collect pointers to indirect SVM buffers, which need to
132138be registered with OpenCL before invoking the kernel.
133139"""
134- function clconvert (arg, pointers:: Vector{Ptr{Cvoid}} = Ptr{Cvoid}[])
135- adapt (KernelAdaptor (pointers), arg)
140+ function clconvert (arg, pointers:: Vector{Ptr{Cvoid}} = Ptr{Cvoid}[])
141+ return adapt (KernelAdaptor (pointers), arg)
136142end
137143
138144
139-
140145# # abstract kernel functionality
141146
142- abstract type AbstractKernel{F,TT} end
147+ abstract type AbstractKernel{F, TT} end
143148
144- @inline @generated function (kernel:: AbstractKernel{F,TT} )(args... ;
145- call_kwargs... ) where {F,TT}
149+ @inline @generated function (kernel:: AbstractKernel{F, TT} )(
150+ args... ;
151+ call_kwargs...
152+ ) where {F, TT}
146153 sig = Tuple{F, TT. parameters... } # Base.signature_type with a function type
147- args = (:(kernel. f), (:( clconvert (args[$ i], svm_pointers) ) for i in 1 : length (args)). .. )
154+ args = (:(kernel. f), (:(clconvert (args[$ i], svm_pointers)) for i in 1 : length (args)). .. )
148155
149156 # filter out ghost arguments that shouldn't be passed
150157 predicate = dt -> GPUCompiler. isghosttype (dt) || Core. Compiler. isconstType (dt)
151158 to_pass = map (! predicate, sig. parameters)
152- call_t = Type[x[1 ] for x in zip (sig. parameters, to_pass) if x[2 ]]
153- call_args = Union{Expr,Symbol}[x[1 ] for x in zip (args, to_pass) if x[2 ]]
159+ call_t = Type[x[1 ] for x in zip (sig. parameters, to_pass) if x[2 ]]
160+ call_args = Union{Expr, Symbol}[x[1 ] for x in zip (args, to_pass) if x[2 ]]
154161
155162 # replace non-isbits arguments (they should be unused, or compilation would have failed)
156- for (i,dt) in enumerate (call_t)
163+ for (i, dt) in enumerate (call_t)
157164 if ! isbitstype (dt)
158165 call_t[i] = Ptr{Any}
159166 call_args[i] = :C_NULL
@@ -163,17 +170,16 @@ abstract type AbstractKernel{F,TT} end
163170 # finalize types
164171 call_tt = Base. to_tuple_type (call_t)
165172
166- quote
173+ return quote
167174 svm_pointers = Ptr{Cvoid}[]
168175 $ cl. clcall (kernel. fun, $ call_tt, $ (call_args... ); svm_pointers, call_kwargs... )
169176 end
170177end
171178
172179
173-
174180# # host-side kernels
175181
176- struct HostKernel{F,TT} <: AbstractKernel{F,TT}
182+ struct HostKernel{F, TT} <: AbstractKernel{F, TT}
177183 f:: F
178184 fun:: cl.Kernel
179185end
183189
184190const clfunction_lock = ReentrantLock ()
185191
186- function clfunction (f:: F , tt:: TT = Tuple{}; kwargs... ) where {F,TT}
192+ function clfunction (f:: F , tt:: TT = Tuple{}; kwargs... ) where {F, TT}
187193 ctx = context ()
188194 dev = device ()
189195
@@ -200,10 +206,10 @@ function clfunction(f::F, tt::TT=Tuple{}; kwargs...) where {F,TT}
200206 kernel = get (_kernel_instances, h, nothing )
201207 if kernel === nothing
202208 # create the kernel state object
203- kernel = HostKernel {F,tt} (f, fun)
209+ kernel = HostKernel {F, tt} (f, fun)
204210 _kernel_instances[h] = kernel
205211 end
206- return kernel:: HostKernel{F,tt}
212+ return kernel:: HostKernel{F, tt}
207213 end
208214end
209215
0 commit comments