Skip to content

Commit

Permalink
Add dpnp.byte_bounds implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
ekomarova committed Nov 7, 2024
1 parent 0bcd49d commit 9cdf55d
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 0 deletions.
4 changes: 4 additions & 0 deletions dpnp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,13 @@
from dpnp.dpnp_iface_types import *
from dpnp.dpnp_iface import *
from dpnp.dpnp_iface import __all__ as _iface__all__
from dpnp.dpnp_iface_utils import *
from dpnp.dpnp_iface_utils import __all__ as _ifaceutils__all__
from dpnp._version import get_versions

__all__ = _iface__all__
__all__ += _ifaceutils__all__


__version__ = get_versions()["version"]
del get_versions
70 changes: 70 additions & 0 deletions dpnp/dpnp_iface_utils.py
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
42 changes: 42 additions & 0 deletions tests/test_usm_utils.py
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)

0 comments on commit 9cdf55d

Please sign in to comment.