def __init__(self, domain_or_context=DefaultMemory): create_vcl_context_from = None # set this later if domain_or_context is None: domain_or_context = DefaultMemory if isinstance(domain_or_context, Context): self.domain = domain_or_context.domain self.vcl_context = domain_or_context.vcl_context self.sub_context = domain_or_context.sub_context #if self.domain is OpenCLMemory: # vcl.set_active_context(self) return if WITH_OPENCL: if isinstance(domain_or_context, cl.Context): self.domain = OpenCLMemory self.sub_context = domain_or_context create_vcl_context_from = vcl.get_viennacl_object(self.sub_context) try: domain_or_context_is_domain = issubclass(domain_or_context, MemoryDomain) except TypeError: domain_or_context_is_domain = False if domain_or_context_is_domain: # cf default arg self.domain = domain_or_context if domain_or_context is OpenCLMemory: self.sub_context = vcl.default_context create_vcl_context_from = vcl.get_viennacl_object(self.sub_context) else: create_vcl_context_from = self.domain.vcl_memory_type if create_vcl_context_from is None: raise TypeError("Cannot handle argument of type %s. Note: WITH_OPENCL is %s." % (type(domain_or_context), WITH_OPENCL)) self.vcl_context = _v.context(create_vcl_context_from) if self.domain is OpenCLMemory: for device in self.devices: if not self.queues[device]: self.add_queue(device) if not self.cache_path: new_path = appdirs.user_data_dir if not os.path.isdir(new_path): try: os.makedirs(new_path) except: pass try: new_path = os.path.join(new_path, '') open(os.path.join(new_path, 'permission_test'), 'a+') except OSError as e: log.warning("Could not open cache path '%s' for writing, disabling kernel cache. Exception was: %s" % (new_path, e)) new_path = '' self.cache_path = new_path
def __init__(self, domain_or_context = DefaultMemory): try: if issubclass(domain_or_context, MemoryDomain): self.domain = domain_or_context self.vcl_context = _v.context(self.domain.vcl_memory_type) if domain_or_context is OpenCLMemory: self.vcl_sub_context = self.vcl_context.opencl_context self.sub_context = vcl.get_pyopencl_object(self.vcl_sub_context) vcl.set_active_context(self) return except TypeError: pass if isinstance(domain_or_context, Context): self.domain = domain_or_context.domain self.vcl_context = domain_or_context.vcl_context self.sub_context = domain_or_context.sub_context self.vcl_sub_context = domain_or_context.vcl_sub_context return if WITH_OPENCL: if isinstance(domain_or_context, cl.Context): self.domain = OpenCLMemory self.sub_context = domain_or_context self.vcl_sub_context = vcl.get_viennacl_object(domain_or_context) self.vcl_context = _v.context(self.vcl_sub_context) vcl.set_active_context(self) return
def switch_queue(self, queue): """ TODO docstring """ if self.domain is not OpenCLMemory: raise TypeError("Only the OpenCL backend currently supports queues") if queue not in self.queues[queue.device]: self.add_queue(queue.device, queue) vcl_queue = vcl.get_viennacl_object(queue, self.sub_context) self.vcl_sub_context.switch_queue(vcl_queue)
def vcl_sub_context(self): if self.domain is not OpenCLMemory: raise TypeError("Only OpenCL sub-context supported currently") return vcl.get_viennacl_object(self.sub_context, self)