-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,70 @@ | ||
# -*- coding: utf-8 -*- | ||
# ***************************************************************************** | ||
# Copyright (c) 2016-2024, Intel Corporation | ||
# All rights reserved. | ||
# | ||
# Redistribution and use in source and binary forms, with or without | ||
# modification, are permitted provided that the following conditions are met: | ||
# - Redistributions of source code must retain the above copyright notice, | ||
# this list of conditions and the following disclaimer. | ||
# - Redistributions in binary form must reproduce the above copyright notice, | ||
# this list of conditions and the following disclaimer in the documentation | ||
# and/or other materials provided with the distribution. | ||
# | ||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | ||
# THE POSSIBILITY OF SUCH DAMAGE. | ||
# ***************************************************************************** | ||
|
||
""" | ||
Interface of the utils function of the DPNP | ||
Notes | ||
----- | ||
This module is a face or public interface file for the library | ||
""" | ||
|
||
import dpnp | ||
|
||
__all__ = ["byte_bounds"] | ||
|
||
|
||
def byte_bounds(a): | ||
""" | ||
Returns a 2-tuple with pointers to the end-points of the array. | ||
For full documentation refer to | ||
:obj:`dpctl.tensor.usm_ndarray._byte_bounds`. | ||
Parameters | ||
---------- | ||
a : usm_ndarray | ||
Input array | ||
Returns | ||
------- | ||
(beg, end) : tuple of 2 integers | ||
The first integer is the first byte of the array, the second | ||
integer is just past the last byte of the array | ||
Examples | ||
-------- | ||
>>> import dpnp as np | ||
>>> x = np.ones((3, 10, 7)) | ||
>>> y = np.flip(x[:, 1::2], axis=1) | ||
>>> beg_p, end_p = np.byte_bounds(y) | ||
Bytes taken to store this array | ||
>>> bytes_extent = end_p - beg_p | ||
""" | ||
|
||
return dpnp.get_usm_ndarray(a)._byte_bounds |
This file contains 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,42 @@ | ||
import dpctl.tensor as dpt | ||
from numpy.testing import assert_equal | ||
|
||
import dpnp | ||
|
||
|
||
def test_byte_bounds(): | ||
a = dpt.usm_ndarray((3, 4), dtype="int64", buffer="shared") | ||
values = dpt.arange(12, dtype="int64") | ||
for i in range(3): | ||
a[i, :] = values[i * 4 : (i + 1) * 4] | ||
low, high = dpnp.byte_bounds(a) | ||
assert_equal(high - low, a.size * a.itemsize) | ||
|
||
|
||
def test_unusual_order_positive_stride(): | ||
a = dpt.usm_ndarray((3, 4), dtype="int64", buffer="shared") | ||
values = dpt.arange(12, dtype="int64") | ||
for i in range(3): | ||
a[i, :] = values[i * 4 : (i + 1) * 4] | ||
b = a.T | ||
low, high = dpnp.byte_bounds(b) | ||
assert_equal(high - low, b.size * b.itemsize) | ||
|
||
|
||
def test_unusual_order_negative_stride(): | ||
a = dpt.usm_ndarray((3, 4), dtype="int64", buffer="shared") | ||
values = dpt.arange(12, dtype="int64") | ||
for i in range(3): | ||
a[i, :] = values[i * 4 : (i + 1) * 4] | ||
b = a.T[::-1] | ||
low, high = dpnp.byte_bounds(b) | ||
assert_equal(high - low, b.size * b.itemsize) | ||
|
||
|
||
def test_strided(): | ||
a = dpt.usm_ndarray(12, dtype="int64", buffer="shared") | ||
a[:] = dpt.arange(12, dtype="int64") | ||
b = a[::2] | ||
low, high = dpnp.byte_bounds(b) | ||
expected_byte_diff = b.size * 2 * b.itemsize - b.itemsize | ||
assert_equal(high - low, expected_byte_diff) |