|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# ***************************************************************************** |
| 3 | +# Copyright (c) 2025, Intel Corporation |
| 4 | +# All rights reserved. |
| 5 | +# |
| 6 | +# Redistribution and use in source and binary forms, with or without |
| 7 | +# modification, are permitted provided that the following conditions are met: |
| 8 | +# - Redistributions of source code must retain the above copyright notice, |
| 9 | +# this list of conditions and the following disclaimer. |
| 10 | +# - Redistributions in binary form must reproduce the above copyright notice, |
| 11 | +# this list of conditions and the following disclaimer in the documentation |
| 12 | +# and/or other materials provided with the distribution. |
| 13 | +# |
| 14 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 15 | +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 16 | +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 17 | +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 18 | +# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 19 | +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 20 | +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 21 | +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 22 | +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 23 | +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 24 | +# THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | +# ***************************************************************************** |
| 26 | + |
| 27 | +import dpctl.tensor as dpt |
| 28 | +from dpctl.memory import MemoryUSMDevice as DPCTLMemoryUSMDevice |
| 29 | +from dpctl.memory import MemoryUSMHost as DPCTLMemoryUSMHost |
| 30 | +from dpctl.memory import MemoryUSMShared as DPCTLMemoryUSMShared |
| 31 | + |
| 32 | + |
| 33 | +def _add_ptr_property(cls): |
| 34 | + _storage_attr = "_ptr" |
| 35 | + |
| 36 | + @property |
| 37 | + def ptr(self): |
| 38 | + return getattr(self, _storage_attr, None) |
| 39 | + |
| 40 | + @ptr.setter |
| 41 | + def ptr(self, value): |
| 42 | + setattr(self, _storage_attr, value) |
| 43 | + |
| 44 | + cls.ptr = ptr |
| 45 | + return cls |
| 46 | + |
| 47 | + |
| 48 | +@_add_ptr_property |
| 49 | +class MemoryUSMDevice(DPCTLMemoryUSMDevice): |
| 50 | + pass |
| 51 | + |
| 52 | + |
| 53 | +@_add_ptr_property |
| 54 | +class MemoryUSMHost(DPCTLMemoryUSMHost): |
| 55 | + pass |
| 56 | + |
| 57 | + |
| 58 | +@_add_ptr_property |
| 59 | +class MemoryUSMShared(DPCTLMemoryUSMShared): |
| 60 | + pass |
| 61 | + |
| 62 | + |
| 63 | +def create_data(x): |
| 64 | + """ |
| 65 | + Create an instance of :class:`.MemoryUSMDevice`, :class:`.MemoryUSMHost`, |
| 66 | + or :class:`.MemoryUSMShared` class depending on the type of USM allocation. |
| 67 | +
|
| 68 | + Parameters |
| 69 | + ---------- |
| 70 | + x : usm_ndarray |
| 71 | + Input array of :class:`dpctl.tensor.usm_ndarray` type. |
| 72 | +
|
| 73 | + Returns |
| 74 | + ------- |
| 75 | + out : {MemoryUSMDevice, MemoryUSMHost, MemoryUSMShared} |
| 76 | + A data object with a reference on USM memory. |
| 77 | +
|
| 78 | + """ |
| 79 | + |
| 80 | + dispatch = { |
| 81 | + DPCTLMemoryUSMDevice: MemoryUSMDevice, |
| 82 | + DPCTLMemoryUSMHost: MemoryUSMHost, |
| 83 | + DPCTLMemoryUSMShared: MemoryUSMShared, |
| 84 | + } |
| 85 | + |
| 86 | + if not isinstance(x, dpt.usm_ndarray): |
| 87 | + raise TypeError( |
| 88 | + f"An array must be any of supported type, but got {type(x)}" |
| 89 | + ) |
| 90 | + usm_data = x.usm_data |
| 91 | + |
| 92 | + cls = dispatch.get(type(usm_data), None) |
| 93 | + if cls: |
| 94 | + data = cls(usm_data) |
| 95 | + # `ptr`` is expecting to point at the start of the array's data, |
| 96 | + # while `usm_data._pointer` is a pointer at the start of memory buffer |
| 97 | + data.ptr = x._pointer |
| 98 | + return data |
| 99 | + raise TypeError(f"Expected USM memory, but got {type(usm_data)}") |
0 commit comments