22
33import re
44import six
5- import numpy as np
65
76from six .moves import zip
87
98from pytools import ImmutableRecord
109from loopy .diagnostic import LoopyError
11- from loopy .types import NumpyType
1210
1311from loopy .kernel .instruction import (MultiAssignmentBase , CInstruction ,
1412 _DataObliviousInstruction )
@@ -85,115 +83,6 @@ def copy(self, dtype=None, mem_scope=None, shape=None, dim_tags=None):
8583# }}}
8684
8785
88- # {{{ c with types
89-
90- def c_with_types (name , arg_id_to_dtype ):
91-
92- # Specializing the type of the math function once they agree upon the
93- # function signature.
94-
95- if name in ["abs" , "acos" , "asin" , "atan" , "cos" , "cosh" , "sin" , "sinh" ,
96- "tanh" , "exp" , "log" , "log10" , "sqrt" , "ceil" , "floor" , "tan" ]:
97- for id , dtype in arg_id_to_dtype .items ():
98- if not - 1 <= id <= 0 :
99- raise LoopyError ("%s can take only one argument." % name )
100-
101- dtype = arg_id_to_dtype [0 ].numpy_dtype
102-
103- if dtype .kind == 'f' :
104- # generic type resolve we can go ahead and specialize
105- pass
106- elif dtype .kind in ['u' , 'i' ]:
107- # int and unsigned are casted into float32
108- dtype = np .float32
109- else :
110- raise LoopyError ("%s function cannot take arguments of the type %s"
111- % (name , dtype ))
112-
113- # Done specializing. Returning the intended arg_id_to_dtype
114- dtype = NumpyType (dtype )
115- return {- 1 : dtype , 0 : dtype }
116-
117- # binary functions
118- elif name in ["max" , "min" ]:
119- for id , dtype in arg_id_to_dtype .items ():
120- if not - 1 <= id <= 1 :
121- raise LoopyError ("%s can take only two arguments." % name )
122-
123- # finding the common type for all the dtypes involved
124- dtype = np .find_common_type (
125- [], [dtype .numpy_dtype for dtype in arg_id_to_dtype ])
126-
127- if dtype .kind == 'f' :
128- # generic type resolve we can go ahead and specialize
129- pass
130- elif dtype .kind in ['u' , 'i' ]:
131- # int and unsigned are implicitly casted into float32
132- dtype = np .float32
133- else :
134- raise LoopyError ("%s function cannot take arguments of the type %s"
135- % (name , dtype ))
136-
137- # Specialized into one of the known types
138- return {- 1 : NumpyType (dtype ), 0 : arg_id_to_dtype [0 ], 1 : arg_id_to_dtype [1 ]}
139-
140- else :
141- # could not specialize the function within the C namespace
142- # this would help when checking for OpenCL/CUDA function which are not
143- # present in C
144- return None
145-
146- # }}}
147-
148-
149- # {{{ opencl with_types
150-
151- def opencl_with_types (name , arg_id_to_dtype ):
152- new_arg_id_to_dtype = c_with_types (name , arg_id_to_dtype )
153- if new_arg_id_to_dtype is None :
154- # could not locate the function within C's namespace. Searching in
155- # OpenCL specific namespace
156-
157- # FIXME: Need to add these functions over here
158- new_arg_id_to_dtype = None
159-
160- return new_arg_id_to_dtype
161-
162- # }}}
163-
164-
165- # {{{ pyopencl with_types
166-
167- def pyopencl_with_types (name , arg_id_to_dtype ):
168- new_arg_id_to_dtype = opencl_with_types (name , arg_id_to_dtype )
169- if new_arg_id_to_dtype is None :
170- # could not locate the function within C's namespace. Searching in
171- # PyOpenCL specific namespace
172-
173- # FIXME: Need to add these functions over here
174- new_arg_id_to_dtype = None
175-
176- return new_arg_id_to_dtype
177-
178- # }}}
179-
180-
181- # {{{ cuda with_types
182-
183- def cuda_with_types (name , arg_id_to_dtype ):
184- new_arg_id_to_dtype = c_with_types (name , arg_id_to_dtype )
185- if new_arg_id_to_dtype is None :
186- # could not locate the function within C's namespace. Searching in
187- # CUDA specific namespace
188-
189- # FIXME: Need to add these extra functions over here
190- new_arg_id_to_dtype = None
191-
192- return new_arg_id_to_dtype
193-
194- # }}}
195-
196-
19786# {{{ kw_to_pos
19887
19988def get_kw_pos_association (kernel ):
@@ -243,7 +132,7 @@ class InKernelCallable(ImmutableRecord):
243132 """
244133
245134 def __init__ (self , name , subkernel = None , arg_id_to_dtype = None ,
246- arg_id_to_descr = None ):
135+ arg_id_to_descr = None , name_in_target = None ):
247136
248137 # {{{ sanity checks
249138
@@ -252,10 +141,14 @@ def __init__(self, name, subkernel=None, arg_id_to_dtype=None,
252141
253142 # }}}
254143
144+ if name_in_target is not None and subkernel is not None :
145+ subkernel = subkernel .copy (name = name_in_target )
146+
255147 super (InKernelCallable , self ).__init__ (name = name ,
256148 subkernel = subkernel ,
257149 arg_id_to_dtype = arg_id_to_dtype ,
258- arg_id_to_descr = arg_id_to_descr )
150+ arg_id_to_descr = arg_id_to_descr ,
151+ name_in_target = name_in_target )
259152
260153 def with_types (self , arg_id_to_dtype , target ):
261154 """
@@ -285,37 +178,15 @@ def with_types(self, arg_id_to_dtype, target):
285178 raise LoopyError ("Overwriting a specialized"
286179 " function is illegal--maybe start with new instance of"
287180 " InKernelCallable?" )
288- # TODO: Check if the arguments match. If yes then just
289- # return self.copy()
290181
291182 # {{{ attempt to specialize using scalar functions
292183
293184 if self .name in target .get_device_ast_builder ().function_identifiers ():
294- from loopy .target .c import CTarget
295- from loopy .target .opencl import OpenCLTarget
296- from loopy .target .pyopencl import PyOpenCLTarget
297- from loopy .target .cuda import CudaTarget
298-
299- # FIXME: Push this into the target
300- if isinstance (target , CTarget ):
301- new_arg_id_to_dtype = c_with_types (self .name , arg_id_to_dtype )
302-
303- elif isinstance (target , OpenCLTarget ):
304- new_arg_id_to_dtype = opencl_with_types (self .name , arg_id_to_dtype )
305-
306- elif isinstance (target , PyOpenCLTarget ):
307- new_arg_id_to_dtype = pyopencl_with_types (self .name , arg_id_to_dtype )
308-
309- elif isinstance (target , CudaTarget ):
310- new_arg_id_to_dtype = cuda_with_types (self .name , arg_id_to_dtype )
311-
312- else :
313- raise NotImplementedError ("InKernelCallable.with_types() for"
314- " %s target" % target )
315-
316- if new_arg_id_to_dtype is not None :
317- # got our speciliazed function
318- return self .copy (arg_id_to_dtype = new_arg_id_to_dtype )
185+ new_in_knl_callable = target .get_device_ast_builder ().with_types (
186+ self , arg_id_to_dtype )
187+ if new_in_knl_callable is None :
188+ new_in_knl_callable = self .copy ()
189+ return new_in_knl_callable
319190
320191 # }}}
321192
@@ -444,7 +315,8 @@ def with_iname_tag_usage(self, unusable, concurrent_shape):
444315 def is_ready_for_code_gen (self ):
445316
446317 return (self .arg_id_to_dtype is not None and
447- self .arg_id_to_descr is not None )
318+ self .arg_id_to_descr is not None and
319+ self .name_in_target is not None )
448320
449321 # {{{ code generation
450322
@@ -453,16 +325,10 @@ def generate_preambles(self, target):
453325 """
454326 raise NotImplementedError ()
455327
456- def get_target_specific_name (self , target ):
457-
458- if self .subkernel is None :
459- return self .name
460- else :
461- return self .subkernel .name
328+ def emit_call (self , expression_to_code_mapper , expression , target ):
462329
463- raise NotImplementedError ()
330+ assert self . is_ready_for_code_gen ()
464331
465- def emit_call (self , expression_to_code_mapper , expression , target ):
466332 if self .subkernel :
467333 raise NotImplementedError ()
468334
@@ -484,10 +350,12 @@ def emit_call(self, expression_to_code_mapper, expression, target):
484350 expression .parameters , par_dtypes , arg_dtypes ))
485351
486352 from pymbolic import var
487- return var (self .get_target_specific_name ( target ) )(* processed_parameters )
353+ return var (self .name_in_target )(* processed_parameters )
488354
489355 def emit_call_insn (self , insn , target , expression_to_code_mapper ):
490356
357+ assert self .is_ready_for_code_gen ()
358+
491359 from loopy .kernel .instruction import CallInstruction
492360 from pymbolic .primitives import CallWithKwargs
493361
@@ -507,7 +375,7 @@ def emit_call_insn(self, insn, target, expression_to_code_mapper):
507375 parameters .append (kw_parameters [pos_to_kw [i ]])
508376 par_dtypes .append (self .arg_id_to_dtype [pos_to_kw [i ]])
509377
510- # TODO: currently no suppport for insn keywords.
378+ # TODO: currently no suppport for assignee keywords.
511379 parameters = parameters + list (assignees )
512380 par_dtypes = par_dtypes + [self .arg_id_to_dtype [- i - 1 ] for i , _ in
513381 enumerate (assignees )]
@@ -523,7 +391,7 @@ def emit_call_insn(self, insn, target, expression_to_code_mapper):
523391 parameters , par_dtypes )]
524392
525393 from pymbolic import var
526- return var (self .get_target_specific_name ( target ) )(* c_parameters )
394+ return var (self .name_in_target )(* c_parameters )
527395
528396 # }}}
529397
@@ -718,12 +586,10 @@ def register_pymbolic_calls_to_knl_callables(kernel,
718586
719587 # book-keeping of the functions and names mappings for later use
720588 if in_knl_callable .subkernel is not None :
721- # changing the name of the subkenrel so that it emits a function
722- # with the name same as the name being used in the
723- # scoped_function.
724- new_subkernel = in_knl_callable .subkernel .copy (
725- name = unique_name )
726- in_knl_callable = in_knl_callable .copy (subkernel = new_subkernel )
589+ # for array calls the name in the target is the name of the
590+ # scoped funciton
591+ in_knl_callable = in_knl_callable .copy (
592+ name_in_target = unique_name )
727593 scoped_names_to_functions [unique_name ] = in_knl_callable
728594 scoped_functions_to_names [in_knl_callable ] = unique_name
729595
0 commit comments