- Notifications
You must be signed in to change notification settings - Fork7
Opencl interop#52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Open
sakchal wants to merge12 commits intoarrayfire:masterChoose a base branch fromsakchal:opencl_interop
base:master
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Uh oh!
There was an error while loading.Please reload this page.
Open
Changes fromall commits
Commits
Show all changes
12 commits Select commitHold shift + click to select a range
25fbfd0 utility functions for tests
8c7d809 Fixed utility function merge conflict
2fbaace added unit tests for range function
1bc0f3a Readability changes to cosntants tests
bc96cbc readability changes pt.2
a3d0511 sync() default value fix
afb263e Merge branch 'master' of https://github.com/arrayfire/arrayfire-binar…
sakchal7f97198 "update local master branch"
sakchalf84366d added opencl functionality and tests within wrapper
sakchal4a505dc fixed test_opencl mypy issues
sakchal32c5bf2 added interop functionality and tests, edited str_to_dtype function i…
sakchal51f16ce added return types for interop tests
sakchalFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
2 changes: 1 addition & 1 deletionarrayfire_wrapper/dtypes.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletionsarrayfire_wrapper/lib/interface_functions/interop.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| import ctypes | ||
| import numpy as np | ||
| import pyopencl.array as cl # type: ignore[import-untyped] | ||
| from arrayfire_wrapper.defines import AFArray, CShape | ||
| from arrayfire_wrapper.dtypes import c_api_value_to_dtype, str_to_dtype | ||
| from arrayfire_wrapper.lib._utility import call_from_clib | ||
| from arrayfire_wrapper.lib.create_and_modify_array.manage_array import create_array, get_data_ptr, get_dims, get_type | ||
| def numpy_to_af_array(np_arr: np.ndarray) -> AFArray: | ||
| out = AFArray(0) | ||
| shape = np_arr.shape | ||
| c_shape = CShape(*shape) | ||
| c_type = np.ctypeslib.as_ctypes_type(np_arr.dtype) | ||
| dtype = str_to_dtype(c_type) | ||
| call_from_clib( | ||
| create_array.__name__, | ||
| ctypes.pointer(out), | ||
| np_arr.ctypes.data_as(ctypes.c_void_p), | ||
| c_shape.original_shape, | ||
| ctypes.pointer(c_shape.c_array), | ||
| dtype.c_api_value, | ||
| ) | ||
| return out | ||
| def af_to_numpy_array(af_arr: AFArray) -> np.ndarray: | ||
| shape = get_dims(af_arr) | ||
| dtype = c_api_value_to_dtype(get_type(af_arr)) | ||
| typecode = dtype.typecode | ||
| out = np.empty(shape, typecode, "F") | ||
| call_from_clib(get_data_ptr.__name__, ctypes.c_void_p(out.ctypes.data), af_arr) | ||
| return out | ||
| def pyopencl_to_af_array(pycl_arr: cl.Array) -> AFArray: | ||
| out = AFArray(0) | ||
| np_arr = pycl_arr.get() | ||
| shape = np_arr.shape | ||
| c_shape = CShape(*shape) | ||
| c_type = np.ctypeslib.as_ctypes_type(np_arr.dtype) | ||
| dtype = str_to_dtype(c_type) | ||
| call_from_clib( | ||
| create_array.__name__, | ||
| ctypes.pointer(out), | ||
| np_arr.ctypes.data_as(ctypes.c_void_p), | ||
| c_shape.original_shape, | ||
| ctypes.pointer(c_shape.c_array), | ||
| dtype.c_api_value, | ||
| ) | ||
| return out |
101 changes: 100 additions & 1 deletionarrayfire_wrapper/lib/interface_functions/opencl.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,100 @@ | ||
| import ctypes | ||
| from enum import Enum | ||
| from arrayfire_wrapper.lib._utility import call_from_clib | ||
| class DeviceType(Enum): | ||
| CPU = 2 | ||
| GPU = 4 | ||
| ACC = 8 | ||
| UNKNOWN = -1 | ||
| class PlatformType(Enum): | ||
| AMD = 0 | ||
| APPLE = 1 | ||
| INTEL = 2 | ||
| NVIDIA = 3 | ||
| BEIGNET = 4 | ||
| POCL = 5 | ||
| UNKNOWN = -1 | ||
| def get_context(retain: bool = False) -> ctypes.c_void_p: | ||
| """ | ||
| source: https://arrayfire.org/docs/group__opencl__mat.htm#gad42de383f405b3e38d6eb669c0cbe2e3 | ||
| """ | ||
| out = ctypes.c_void_p() | ||
| call_from_clib(get_context.__name__, ctypes.pointer(out), retain, clib_prefix="afcl") | ||
| return out # type: ignore[return-value] | ||
| def get_queue(retain: bool = False) -> ctypes.c_void_p: | ||
| """ | ||
| source: https://arrayfire.org/docs/group__opencl__mat.htm#gab1701ef4f2b68429eb31c1e21c88d0bc | ||
| """ | ||
| out = ctypes.c_void_p() | ||
| call_from_clib(get_queue.__name__, ctypes.pointer(out), retain, clib_prefix="afcl") | ||
| return out # type: ignore[return-value] | ||
| def get_device_id() -> int: | ||
| """ | ||
| source: https://arrayfire.org/docs/group__opencl__mat.htm#gaf7258055284e65a8647a49c3f3b9feee | ||
| """ | ||
| out = ctypes.c_void_p() | ||
| call_from_clib(get_device_id.__name__, ctypes.pointer(out), clib_prefix="afcl") | ||
| return out # type: ignore[return-value] | ||
| def set_device_id(idx: int) -> None: | ||
| """ | ||
| source: https://arrayfire.org/docs/group__opencl__mat.htm#ga600361a20ceac2a65590b67fc0366314 | ||
| """ | ||
| call_from_clib(set_device_id.__name__, ctypes.c_int64(idx), clib_prefix="afcl") | ||
| return None | ||
| def add_device_context(dev: int, ctx: int, que: int) -> None: | ||
| """ | ||
| source: https://arrayfire.org/docs/group__opencl__mat.htm#ga49f596a4041fb757f1f5a75999cf8858 | ||
| """ | ||
| call_from_clib( | ||
| add_device_context.__name__, ctypes.c_int64(dev), ctypes.c_int64(ctx), ctypes.c_int64(que), clib_prefix="afcl" | ||
| ) | ||
| return None | ||
| def set_device_context(dev: int, ctx: int) -> None: | ||
| """ | ||
| source: https://arrayfire.org/docs/group__opencl__mat.htm#ga975661f2b06dddb125c5d1757160b02c | ||
| """ | ||
| call_from_clib(set_device_context.__name__, ctypes.c_int64(dev), ctypes.c_int64(ctx), clib_prefix="afcl") | ||
| return None | ||
| def delete_device_context(dev: int, ctx: int) -> None: | ||
| """ | ||
| source: https://arrayfire.org/docs/group__opencl__mat.htm#ga1a56dcf05099d6ac0a3b7701f7cb23f8 | ||
| """ | ||
| call_from_clib(delete_device_context.__name__, ctypes.c_int64(dev), ctypes.c_int64(ctx), clib_prefix="afcl") | ||
| return None | ||
| def get_device_type() -> DeviceType: | ||
| """ | ||
| source: https://arrayfire.org/docs/group__opencl__mat.htm#ga5e360e0fe0eb55d0046191bc3fd6f81d | ||
| """ | ||
| res = ctypes.c_void_p() | ||
| call_from_clib(get_device_type.__name__, ctypes.pointer(res), clib_prefix="afcl") | ||
| return DeviceType(res.value) | ||
| def get_platform() -> PlatformType: | ||
| """ | ||
| source: https://arrayfire.org/docs/group__opencl__mat.htm#ga5e360e0fe0eb55d0046191bc3fd6f81d&gsc.tab=0 | ||
| """ | ||
| res = ctypes.c_void_p() | ||
| call_from_clib(get_platform.__name__, ctypes.pointer(res), clib_prefix="afcl") | ||
| return PlatformType(res.value) |
72 changes: 72 additions & 0 deletionstests/test_interop.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| import numpy as np | ||
| import pyopencl as cl # type: ignore | ||
| import pyopencl.array as cl_array # type: ignore | ||
| import arrayfire_wrapper.lib as wrapper | ||
| from arrayfire_wrapper.defines import AFArray | ||
| from arrayfire_wrapper.dtypes import int16 | ||
| from arrayfire_wrapper.lib.create_and_modify_array.manage_array import get_dims, get_numdims | ||
| from arrayfire_wrapper.lib.interface_functions.interop import ( # noqa: E501 | ||
| af_to_numpy_array, | ||
| numpy_to_af_array, | ||
| pyopencl_to_af_array, | ||
| ) | ||
| # flake8: noqa: E203 | ||
| def test_numpy_to_af_array_type() -> None: | ||
| arr = np.array([1, 2, 3, 4]) | ||
| af_array = numpy_to_af_array(arr) | ||
| assert isinstance(af_array, AFArray) | ||
| def test_af_to_numpy_array_type() -> None: | ||
| arr = wrapper.constant(2, (5, 5), int16) | ||
| np_arr = af_to_numpy_array(arr) | ||
| assert isinstance(np_arr, np.ndarray) | ||
| def test_pyopencl_to_af_array_type() -> None: | ||
| ctx = cl.create_some_context() | ||
| queue = cl.CommandQueue(ctx) | ||
| host_array = np.array([1, 2, 3, 4]) | ||
| cl_array_device = cl_array.to_device(queue, host_array) | ||
| af_array = pyopencl_to_af_array(cl_array_device) | ||
| assert isinstance(af_array, AFArray) | ||
| def test_numpy_to_af_array_shape() -> None: | ||
| np_arr = np.array([1, 2, 3, 4]) | ||
| af_arr = numpy_to_af_array(np_arr) | ||
| assert get_dims(af_arr)[0 : get_numdims(af_arr)] == np_arr.shape[0 : get_numdims(af_arr)] | ||
| def test_af_to_numpy_array_shape() -> None: | ||
| af_arr = wrapper.constant(2, (5, 5), int16) | ||
| np_arr = af_to_numpy_array(af_arr) | ||
| assert np_arr.shape[0 : get_numdims(af_arr)] == get_dims(af_arr)[0 : get_numdims(af_arr)] | ||
| def test_pyopencl_to_af_array_shape() -> None: | ||
| ctx = cl.create_some_context() | ||
| queue = cl.CommandQueue(ctx) | ||
| host_array = np.array([1, 2, 3, 4]) | ||
| cl_arr = cl_array.to_device(queue, host_array) | ||
| af_arr = pyopencl_to_af_array(cl_arr) | ||
| assert cl_arr.shape[0 : get_numdims(af_arr)] == get_dims(af_arr)[0 : get_numdims(af_arr)] |
29 changes: 29 additions & 0 deletionstests/test_opencl.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import ctypes | ||
| import arrayfire_wrapper.lib.interface_functions.opencl as cl | ||
| def test_get_context_type() -> None: | ||
| ptr = cl.get_context() | ||
| assert isinstance(ptr, ctypes.c_void_p) | ||
| def test_get_queue_type() -> None: | ||
| assert isinstance(cl.get_queue(), ctypes.c_void_p) | ||
| def test_get_device_id() -> None: | ||
| assert isinstance(cl.get_device_id(), int) | ||
| def test_set_device_id() -> None: | ||
| cl.set_device_id(0) | ||
| assert cl.get_device_id() == 0 | ||
| def test_get_device_type() -> None: | ||
| assert cl.get_device_type() == cl.DeviceType.GPU # change according to device | ||
| def test_get_platform() -> None: | ||
| assert cl.get_platform() == cl.PlatformType.INTEL # change according to platform |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.