|
| 1 | +####################################################### |
| 2 | +# Copyright (c) 2015, ArrayFire |
| 3 | +# All rights reserved. |
| 4 | +# |
| 5 | +# This file is distributed under 3-clause BSD license. |
| 6 | +# The complete license agreement can be obtained at: |
| 7 | +# http://arrayfire.com/licenses/BSD-3-Clause |
| 8 | +######################################################## |
| 9 | + |
| 10 | +""" |
| 11 | +Functions specific to OpenCL backend. |
| 12 | +
|
| 13 | +This module provides interoperability with other OpenCL libraries. |
| 14 | +""" |
| 15 | + |
| 16 | +def get_context(retain=False): |
| 17 | + """ |
| 18 | + Get the current OpenCL context being used by ArrayFire. |
| 19 | +
|
| 20 | + Parameters |
| 21 | + ---------- |
| 22 | +
|
| 23 | + retain : bool. optional. Default: False. |
| 24 | + Specifies if the context needs to be retained by arrayfire before returning. |
| 25 | +
|
| 26 | + Returns |
| 27 | + ----------- |
| 28 | + context : integer denoting the context id. |
| 29 | + """ |
| 30 | + |
| 31 | + import ctypes as ct |
| 32 | + from .util import safe_call as safe_call |
| 33 | + from .library import backend as backend |
| 34 | + |
| 35 | + if (backend.name() != "opencl"): |
| 36 | + raise RuntimeError("Invalid backend loaded") |
| 37 | + |
| 38 | + context = ct.c_void_p(0) |
| 39 | + safe_call(backend.get().afcl_get_context(ct.pointer(context), retain)) |
| 40 | + return context.value |
| 41 | + |
| 42 | +def get_queue(retain): |
| 43 | + """ |
| 44 | + Get the current OpenCL command queue being used by ArrayFire. |
| 45 | +
|
| 46 | + Parameters |
| 47 | + ---------- |
| 48 | +
|
| 49 | + retain : bool. optional. Default: False. |
| 50 | + Specifies if the context needs to be retained by arrayfire before returning. |
| 51 | +
|
| 52 | + Returns |
| 53 | + ----------- |
| 54 | + queue : integer denoting the queue id. |
| 55 | + """ |
| 56 | + |
| 57 | + import ctypes as ct |
| 58 | + from .util import safe_call as safe_call |
| 59 | + from .library import backend as backend |
| 60 | + |
| 61 | + if (backend.name() != "opencl"): |
| 62 | + raise RuntimeError("Invalid backend loaded") |
| 63 | + |
| 64 | + queue = ct.c_int(0) |
| 65 | + safe_call(backend.get().afcl_get_queue(ct.pointer(queue), retain)) |
| 66 | + return queue.value |
| 67 | + |
| 68 | +def get_device_id(): |
| 69 | + """ |
| 70 | + Get native (unsorted) OpenCL device ID |
| 71 | +
|
| 72 | + Returns |
| 73 | + -------- |
| 74 | +
|
| 75 | + idx : int. |
| 76 | + Specifies the `cl_device_id` of the device. |
| 77 | + """ |
| 78 | + |
| 79 | + import ctypes as ct |
| 80 | + from .util import safe_call as safe_call |
| 81 | + from .library import backend as backend |
| 82 | + |
| 83 | + if (backend.name() != "opencl"): |
| 84 | + raise RuntimeError("Invalid backend loaded") |
| 85 | + |
| 86 | + idx = ct.c_int(0) |
| 87 | + safe_call(backend.get().afcl_get_device_id(ct.pointer(idx))) |
| 88 | + return idx.value |
| 89 | + |
| 90 | +def set_device_id(idx): |
| 91 | + """ |
| 92 | + Set native (unsorted) OpenCL device ID |
| 93 | +
|
| 94 | + Parameters |
| 95 | + ---------- |
| 96 | +
|
| 97 | + idx : int. |
| 98 | + Specifies the `cl_device_id` of the device. |
| 99 | + """ |
| 100 | + |
| 101 | + import ctypes as ct |
| 102 | + from .util import safe_call as safe_call |
| 103 | + from .library import backend as backend |
| 104 | + |
| 105 | + if (backend.name() != "opencl"): |
| 106 | + raise RuntimeError("Invalid backend loaded") |
| 107 | + |
| 108 | + safe_call(backend.get().afcl_set_device_id(idx)) |
| 109 | + return |
0 commit comments