def extract_prototype(src: str) -> KernelPrototype: """ Extract OpenCL kernel prototype from preprocessed file. Parameters ---------- src : str OpenCL source. Returns ------- KernelPrototype Prototype instance. """ idxs = text.get_substring_idxs('__kernel void ', src) if len(idxs) != 1: raise PrototypeException("Invalid number of kernels found: {}".format( len(idxs))) src = get_cl_kernel(src, idxs[0]) try: index = src.index('{') + 1 prototype = src[:index] except ValueError: raise PrototypeException("malformed seed") return KernelPrototype(prototype)
def get_cl_kernels(src: str) -> List[str]: """ Return OpenCL kernels. Parameters ---------- src : str OpenCL source. Returns ------- List[str] OpenCL kernels. """ idxs = text.get_substring_idxs('__kernel', src) kernels = [get_cl_kernel(src, i) for i in idxs] return kernels
def strip_attributes(src: str) -> str: """ Remove attributes from OpenCL source. Parameters ---------- src : str OpenCL source. Returns ------- str OpenCL source, with ((attributes)) removed. """ # get list of __attribute__ substrings idxs = sorted(text.get_substring_idxs('__attribute__', src)) # get ((attribute)) ranges attribute_ranges = [get_attribute_range(src, i) for i in idxs] # remove ((atribute)) ranges for r in reversed(attribute_ranges): src = src[:r[0]] + src[r[1]:] return src
def test_get_substring_idxs(self): self.assertEqual([0, 2], text.get_substring_idxs('a', 'aba')) self.assertEqual([], text.get_substring_idxs('a', 'bb'))
def test_get_substring_idxs(): assert [0, 2] == text.get_substring_idxs('a', 'aba') assert not text.get_substring_idxs('a', 'bb')