######################################################## Copyright (c) 2015, ArrayFire# All rights reserved.## This file is distributed under 3-clause BSD license.# The complete license agreement can be obtained at:# http://arrayfire.com/licenses/BSD-3-Clause########################################################"""Functions specific to OpenCL backend.This module provides interoperability with other OpenCL libraries."""from.utilimport*from.libraryimport(_Enum,_Enum_Type)[docs]classDEVICE_TYPE(_Enum):""" ArrayFire wrapper for CL_DEVICE_TYPE """CPU=_Enum_Type(1<<1)GPU=_Enum_Type(1<<2)ACC=_Enum_Type(1<<3)UNKNOWN=_Enum_Type(-1) [docs]classPLATFORM(_Enum):""" ArrayFire enum for common platforms """AMD=_Enum_Type(0)APPLE=_Enum_Type(1)INTEL=_Enum_Type(2)NVIDIA=_Enum_Type(3)BEIGNET=_Enum_Type(4)POCL=_Enum_Type(5)UNKNOWN=_Enum_Type(-1) [docs]defget_context(retain=False):""" Get the current OpenCL context being used by ArrayFire. Parameters ---------- retain : bool. optional. Default: False. Specifies if the context needs to be retained by arrayfire before returning. Returns ----------- context : integer denoting the context id. """importctypesasctfrom.utilimportsafe_callassafe_callfrom.libraryimportbackendif(backend.name()!="opencl"):raiseRuntimeError("Invalid backend loaded")context=c_void_ptr_t(0)safe_call(backend.get().afcl_get_context(c_pointer(context),retain))returncontext.value [docs]defget_queue(retain):""" Get the current OpenCL command queue being used by ArrayFire. Parameters ---------- retain : bool. optional. Default: False. Specifies if the context needs to be retained by arrayfire before returning. Returns ----------- queue : integer denoting the queue id. """importctypesasctfrom.utilimportsafe_callassafe_callfrom.libraryimportbackendif(backend.name()!="opencl"):raiseRuntimeError("Invalid backend loaded")queue=c_int_t(0)safe_call(backend.get().afcl_get_queue(c_pointer(queue),retain))returnqueue.value [docs]defget_device_id():""" Get native (unsorted) OpenCL device ID Returns -------- idx : int. Specifies the `cl_device_id` of the device. """importctypesasctfrom.utilimportsafe_callassafe_callfrom.libraryimportbackendif(backend.name()!="opencl"):raiseRuntimeError("Invalid backend loaded")idx=c_int_t(0)safe_call(backend.get().afcl_get_device_id(c_pointer(idx)))returnidx.value [docs]defset_device_id(idx):""" Set native (unsorted) OpenCL device ID Parameters ---------- idx : int. Specifies the `cl_device_id` of the device. """importctypesasctfrom.utilimportsafe_callassafe_callfrom.libraryimportbackendif(backend.name()!="opencl"):raiseRuntimeError("Invalid backend loaded")safe_call(backend.get().afcl_set_device_id(idx))return [docs]defadd_device_context(dev,ctx,que):""" Add a new device to arrayfire opencl device manager Parameters ---------- dev : cl_device_id ctx : cl_context que : cl_command_queue """importctypesasctfrom.utilimportsafe_callassafe_callfrom.libraryimportbackendif(backend.name()!="opencl"):raiseRuntimeError("Invalid backend loaded")safe_call(backend.get().afcl_add_device_context(dev,ctx,que)) [docs]defset_device_context(dev,ctx):""" Set a device as current active device Parameters ---------- dev : cl_device_id ctx : cl_context """importctypesasctfrom.utilimportsafe_callassafe_callfrom.libraryimportbackendif(backend.name()!="opencl"):raiseRuntimeError("Invalid backend loaded")safe_call(backend.get().afcl_set_device_context(dev,ctx)) [docs]defdelete_device_context(dev,ctx):""" Delete a device Parameters ---------- dev : cl_device_id ctx : cl_context """importctypesasctfrom.utilimportsafe_callassafe_callfrom.libraryimportbackendif(backend.name()!="opencl"):raiseRuntimeError("Invalid backend loaded")safe_call(backend.get().afcl_delete_device_context(dev,ctx)) _to_device_type={DEVICE_TYPE.CPU.value:DEVICE_TYPE.CPU,DEVICE_TYPE.GPU.value:DEVICE_TYPE.GPU,DEVICE_TYPE.ACC.value:DEVICE_TYPE.ACC,DEVICE_TYPE.UNKNOWN.value:DEVICE_TYPE.UNKNOWN}_to_platform={PLATFORM.AMD.value:PLATFORM.AMD,PLATFORM.APPLE.value:PLATFORM.APPLE,PLATFORM.INTEL.value:PLATFORM.INTEL,PLATFORM.NVIDIA.value:PLATFORM.NVIDIA,PLATFORM.BEIGNET.value:PLATFORM.BEIGNET,PLATFORM.POCL.value:PLATFORM.POCL,PLATFORM.UNKNOWN.value:PLATFORM.UNKNOWN}[docs]defget_device_type():""" Get opencl device type """importctypesasctfrom.utilimportsafe_callassafe_callfrom.libraryimportbackendif(backend.name()!="opencl"):raiseRuntimeError("Invalid backend loaded")res=c_int_t(DEVICE_TYPE.UNKNOWN.value)safe_call(backend.get().afcl_get_device_type(c_pointer(res)))return_to_device_type[res.value] [docs]defget_platform():""" Get opencl platform """importctypesasctfrom.utilimportsafe_callassafe_callfrom.libraryimportbackendif(backend.name()!="opencl"):raiseRuntimeError("Invalid backend loaded")res=c_int_t(PLATFORM.UNKNOWN.value)safe_call(backend.get().afcl_get_platform(c_pointer(res)))return_to_platform[res.value]