def get_header(self): object = self.object transpiler = OpenCLConverter(known_types=self.known_types) headers = [] headers.extend(get_code(object.kernel, transpiler)) for equation in object.all_group.equations: headers.extend(get_code(equation, transpiler)) headers.append(transpiler.parse_instance(object.kernel)) cls_name = object.kernel.__class__.__name__ self.known_types['KERNEL'] = KnownType('__global %s*' % cls_name, base_type=cls_name) headers.append(object.all_group.get_equation_wrappers( self.known_types)) # This is to be done after the above as the equation names are assigned # only at this point. cpu_structs = self._cpu_structs h = CStructHelper(object.kernel) cpu_structs['kern'] = h.get_array() for eq in object.all_group.equations: self._equations[eq.var_name] = eq h.parse(eq) cpu_structs[eq.var_name] = h.get_array() return '\n'.join(headers)
def test_c_struct_helper(): # Given class Fruit(object): pass f = Fruit() f.apple = 1 f.banana = 2.0 f.pear = 1.5 h = CStructHelper(f) # When result = h.get_code() # Then expect = dedent(''' typedef struct Fruit { int apple; double banana; double pear; } Fruit; ''') assert result.strip() == expect.strip() # When/Then array = h.get_array() use_double = get_config().use_double fdtype = np.float64 if use_double else np.float32 expect = np.dtype([('apple', np.int32), ('banana', fdtype), ('pear', fdtype)]) assert array.dtype == expect assert array['apple'] == 1 assert array['banana'] == 2.0 assert array['pear'] == 1.5
def test_wrapping_class(): # Given class Dummy(object): '''Class Docstring''' def __init__(self, x=0, f=0.0, s=''): "Constructor docstring" self.x = x self.f = f self.s = s self._private = 1 def method(self): '''Method docstring. ''' pass obj = Dummy() # When c = CConverter() result = c.parse_instance(obj) # Then expect = dedent(''' typedef struct Dummy { double f; int x; } Dummy; void Dummy_method(Dummy* self) { ; } ''') assert result.strip() == expect.strip() # When h = CStructHelper(obj) use_double = get_config().use_double fdtype = np.float64 if use_double else np.float32 dtype = np.dtype([('f', fdtype), ('x', np.int32)]) expect = np.zeros(1, dtype) assert h.get_array() == expect
def test_c_struct_helper_empty_object(): # Given class Fruit(object): pass f = Fruit() h = CStructHelper(f) # When result = h.get_code() # Then expect = dedent(''' typedef struct Fruit { } Fruit; ''') assert result.strip() == expect.strip() # When/Then assert h.get_array() is None