def get_predefined_types(precomp): """Return a dictionary that can be used by a CythonGenerator for the precomputed symbols. """ result = {'dt': 0.0, 't': 0.0, 'dst': KnownType('object'), 'src': KnownType('ParticleArrayWrapper')} for sym, value in precomp.items(): result[sym] = value.context[sym] return result
def test_array_declarations_with_known_types(self): # Given g = self.group known_types = {'d_x': KnownType('float*')} # When result = g.get_array_declarations(['d_x'], known_types) # Then. expect = 'cdef float* d_x' self.assertEqual(result, expect)
def test_method_with_known_types(self): cg = CythonGenerator( known_types={ 'WIJ': 0.0, 'DWIJ': [0.0, 0.0, 0.0], 'user': KnownType('ndarray'), 'd_user': KnownType('long*'), 's_user': KnownType('int*'), }) cg.parse(EqWithKnownTypes()) expect = dedent(""" cdef class EqWithKnownTypes: def __init__(self, **kwargs): for key, value in kwargs.items(): setattr(self, key, value) cdef inline void some_func(self, long d_idx, double* d_p, double WIJ, double* DWIJ, ndarray user, long* d_user, int* s_user): d_p[d_idx] = WIJ*DWIJ[0] """) self.assert_code_equal(cg.get_code().strip(), expect.strip())
def get_header(self): object = self.object headers = [] headers.extend(get_code(object.kernel)) # get headers from the Equations for equation in object.all_group.equations: headers.extend(get_code(equation)) # Kernel wrappers. cg = CythonGenerator(known_types=self.known_types) cg.parse(object.kernel) headers.append(cg.get_code()) # Equation wrappers. self.known_types['KERNEL'] = KnownType( object.kernel.__class__.__name__) headers.append(object.all_group.get_equation_wrappers( self.known_types)) return '\n'.join(headers)
def get_equation_wrappers(self, known_types={}): classes = defaultdict(lambda: 0) eqs = {} for equation in self.equations: cls = equation.__class__.__name__ n = classes[cls] equation.var_name = '%s%d' % ( camel_to_underscore(equation.name), n ) classes[cls] += 1 eqs[cls] = equation wrappers = [] predefined = dict(get_predefined_types(self.pre_comp)) predefined.update(known_types) predefined['NBRS'] = KnownType('__global unsigned int*') code_gen = OpenCLConverter(known_types=predefined) ignore = ['reduce'] for cls in sorted(classes.keys()): src = code_gen.parse_instance(eqs[cls], ignore_methods=ignore) wrappers.append(src) return '\n'.join(wrappers)
def test_that_all_types_are_detected_correctly(self): x = np.linspace(0, 1, 10) pa = ParticleArray(name='f', x=x) pa.remove_property('pid') info = get_all_array_names([pa]) result = get_known_types_for_arrays(info) expect = { 'd_gid': KnownType("unsigned int*"), 'd_tag': KnownType("int*"), 'd_x': KnownType("double*"), 's_gid': KnownType("unsigned int*"), 's_tag': KnownType("int*"), 's_x': KnownType("double*") } for key in expect: self.assertEqual(repr(result[key]), repr(expect[key]))
def get_known_types_for_arrays(array_names): """Given all the array names from `get_all_array_names` this creates known types for each of them so that the code generators can use this type information when needed. Note that known type info is generated for both source and destination style arrays. Parameters ---------- array_names: dict A dictionary produced by `get_all_array_names`. Examples -------- A simple example would be:: >>> x = np.linspace(0, 1, 10) >>> pa = ParticleArray(name='f', x=x) >>> pa.remove_property('pid') >>> info = get_all_array_names([pa]) >>> get_known_types_for_arrays(info) {'d_gid': KnownType("unsigned int*"), 'd_tag': KnownType("int*"), 'd_x': KnownType("double*"), 's_gid': KnownType("unsigned int*"), 's_tag': KnownType("int*"), 's_x': KnownType("double*")} """ result = {} for arr_type, arrays in array_names.items(): c_type = getattr(carray, arr_type)().get_c_type() for arr in arrays: known_type = KnownType(c_type + '*') result['s_' + arr] = known_type result['d_' + arr] = known_type return result
def _get_self_type(self): return KnownType('__global %s*' % self._class_name)