Skip to content

Commit e689ddc

Browse files
Closes #629 (#632)
Implement dpctl.utils.get_execition_queue( queue_list )
1 parent e4fe7ed commit e689ddc

File tree

5 files changed

+152
-0
lines changed

5 files changed

+152
-0
lines changed

.flake8

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ per-file-ignores =
2525
dpctl/tensor/_usmarray.pyx: E999, E225, E226, E227
2626
dpctl/tensor/numpy_usm_shared.py: F821
2727
dpctl/tests/_cython_api.pyx: E999, E225, E227, E402
28+
dpctl/utils/_compute_follows_data.pyx: E999, E225, E227
2829
examples/cython/sycl_buffer/_buffer_example.pyx: E999, E225, E402
2930
examples/cython/sycl_direct_linkage/_buffer_example.pyx: E999, E225, E402
3031
examples/cython/usm_memory/blackscholes.pyx: E999, E225, E226, E402

dpctl/tests/test_utils.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Data Parallel Control (dpctl)
2+
#
3+
# Copyright 2020-2021 Intel Corporation
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
""" Defines unit test cases for utility functions.
18+
"""
19+
20+
import pytest
21+
22+
import dpctl
23+
import dpctl.utils
24+
25+
26+
def test_get_execution_queue_input_validation():
27+
with pytest.raises(TypeError):
28+
dpctl.utils.get_execution_queue(dict())
29+
30+
31+
def test_get_execution_queue():
32+
try:
33+
q = dpctl.SyclQueue()
34+
q2 = dpctl.SyclQueue()
35+
except dpctl.SyclQueueCreationError:
36+
pytest.skip("Queue could not be create for default device")
37+
38+
exec_q = dpctl.utils.get_execution_queue(())
39+
assert exec_q is None
40+
41+
exec_q = dpctl.utils.get_execution_queue([q])
42+
assert exec_q is q
43+
44+
exec_q = dpctl.utils.get_execution_queue([q, q, q, q])
45+
assert exec_q is q
46+
47+
exec_q = dpctl.utils.get_execution_queue((q, q, None, q))
48+
assert exec_q is None
49+
50+
exec_q = dpctl.utils.get_execution_queue(
51+
(
52+
q,
53+
q2,
54+
q,
55+
)
56+
)
57+
assert exec_q is q
58+
59+
60+
def test_get_execution_queue_nonequiv():
61+
try:
62+
q = dpctl.SyclQueue("cpu")
63+
d1, d2 = q.sycl_device.create_sub_devices(partition=[1, 1])
64+
ctx = dpctl.SyclContext([q.sycl_device, d1, d2])
65+
q1 = dpctl.SyclQueue(ctx, d1)
66+
q2 = dpctl.SyclQueue(ctx, d2)
67+
except dpctl.SyclQueueCreationError:
68+
pytest.skip("Queue could not be create for default device")
69+
70+
exec_q = dpctl.utils.get_execution_queue((q, q1, q2))
71+
assert exec_q is None

dpctl/utils/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from ._compute_follows_data import get_execution_queue
2+
3+
__all__ = [
4+
"get_execution_queue",
5+
]

dpctl/utils/_compute_follows_data.pyx

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Data Parallel Control (dpctl)
2+
#
3+
# Copyright 2020-2021 Intel Corporation
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
# distutils: language = c++
18+
# cython: language_level=3
19+
# cython: linetrace=True
20+
21+
"""This file implements Python buffer protocol using Sycl USM shared and host
22+
allocators. The USM device allocator is also exposed through this module for
23+
use in other Python modules.
24+
"""
25+
26+
27+
import dpctl
28+
29+
from .._sycl_queue cimport SyclQueue
30+
31+
__all__ = ["get_execution_queue", ]
32+
33+
34+
cdef bint queue_equiv(SyclQueue q1, SyclQueue q2):
35+
""" Queues are equivalent if contexts are the same,
36+
devices are the same, and properties are the same."""
37+
return (
38+
(q1 is q2) or
39+
(
40+
(q1.sycl_context == q2.sycl_context) and
41+
(q1.sycl_device == q2.sycl_device) and
42+
(q1.is_in_order == q2.is_in_order) and
43+
(q1.has_enable_profiling == q2.has_enable_profiling)
44+
)
45+
)
46+
47+
48+
def get_execution_queue(qs):
49+
""" Given a list of :class:`dpctl.SyclQueue` objects
50+
returns the execution queue under compute follows data paradigm,
51+
or returns `None` if queues are not equivalent.
52+
"""
53+
if not isinstance(qs, (list, tuple)):
54+
raise TypeError(
55+
"Expected a list or a tuple, got {}".format(type(qs))
56+
)
57+
if len(qs) == 0:
58+
return None
59+
elif len(qs) == 1:
60+
return qs[0] if isinstance(qs[0], dpctl.SyclQueue) else None
61+
for q1, q2 in zip(qs, qs[1:]):
62+
if not isinstance(q1, dpctl.SyclQueue):
63+
return None
64+
elif not isinstance(q2, dpctl.SyclQueue):
65+
return None
66+
elif not queue_equiv(<SyclQueue> q1, <SyclQueue> q2):
67+
return None
68+
return qs[0]

setup.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,13 @@ def extensions():
214214
],
215215
**extension_args,
216216
),
217+
Extension(
218+
"dpctl.utils._compute_follows_data",
219+
[
220+
os.path.join("dpctl", "utils", "_compute_follows_data.pyx"),
221+
],
222+
**extension_args,
223+
),
217224
Extension(
218225
"dpctl.tensor._usmarray",
219226
[

0 commit comments

Comments
 (0)