# USM-shared and USM-host pointers are host-accessible,
# meaning they are accessible from Python, therefore
# they implement Pyton buffer protocol

# allocate 1K of USM-shared buffer
ms = dpmem.MemoryUSMShared(1024)

# create memoryview into USM-shared buffer
msv = memoryview(ms)

# populate buffer from host one byte at a type
for i in range(len(ms)):
    ir = i % 256
    msv[i] = ir**2 % 256

mh = dpmem.MemoryUSMHost(64)
mhv = memoryview(mh)

# copy content of block of USM-shared buffer to
# USM-host buffer
mhv[:] = msv[78:78 + len(mh)]

print("Byte-values of the USM-host buffer")
print(list(mhv))

# USM-device buffer is not host accessible
md = dpmem.MemoryUSMDevice(16)
try:
    mdv = memoryview(md)
except Exception as e:
    print("")
Exemplo n.º 2
0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Demonstrates SYCL USM memory usage in Python using dpctl.memory.
"""

import dpctl.memory as dpmem

# allocate USM-shared byte-buffer
ms = dpmem.MemoryUSMShared(16)

# allocate USM-device byte-buffer
md = dpmem.MemoryUSMDevice(16)

# allocate USM-host byte-buffer
mh = dpmem.MemoryUSMHost(16)

# specify alignment
mda = dpmem.MemoryUSMDevice(128, alignment=16)

# allocate using given queue,
# i.e. on the device and bound to the context stored in the queue
mdq = dpmem.MemoryUSMDevice(256, queue=mda.sycl_queue)

# information about device associate with USM buffer
print("Allocation performed on device:")
mda.sycl_queue.print_device_info()
Exemplo n.º 3
0
def as_usm_obj(obj, queue=None, usm_type="shared", copy=True):
    """
    Determine and return a SYCL device accesible object.

    We try to determine if the provided object defines a valid
    __sycl_usm_array_interface__ dictionary.
    If not, we create a USM memory of `usm_type` and try to copy the data
    `obj` holds. Only numpy.ndarray is supported currently as `obj` if
    the object is not already allocated using USM.

    Args:
        obj: Object to be tested and data copied from.
        usm_type: USM type used in case obj is not already allocated using USM.
        queue (dpctl.SyclQueue): SYCL queue to be used to allocate USM
            memory in case obj is not already USM allocated.
        copy (bool): Flag to determine if we copy data from obj.

    Returns:
        A Python object allocated using USM memory.

    Raises:
        TypeError:
            1. If obj is not allocated on USM memory or is not of type
               numpy.ndarray, TypeError is raised.
            2. If queue is not of type dpctl.SyclQueue.
        ValueError:
            1. In case obj is not USM allocated, users need to pass
               the SYCL queue to be used for creating new memory. ValuieError
               is raised if queue argument is not provided.
            2. If usm_type is not valid.
            3. If dtype of the passed ndarray(obj) is not supported.
    """
    usm_mem = has_usm_memory(obj)

    if queue is None:
        raise ValueError(
            "Queue can not be None. Please provide the SYCL queue to be used.")
    if not isinstance(queue, dpctl.SyclQueue):
        raise TypeError("queue has to be of dpctl.SyclQueue type. Got %s" %
                        (type(queue)))

    if usm_mem is None:
        if not isinstance(obj, np.ndarray):
            raise TypeError("Obj is not USM allocated and is not of type "
                            "numpy.ndarray. Obj type: %s" % (type(obj)))

        if obj.dtype not in [np.dtype(typ) for typ in supported_numpy_dtype]:
            raise ValueError("dtype is not supprted. Supported dtypes "
                             "are: %s" % (supported_numpy_dtype))

        size = np.prod(obj.shape)
        if usm_type == "shared":
            usm_mem = dpctl_mem.MemoryUSMShared(size * obj.dtype.itemsize,
                                                queue=queue)
        elif usm_type == "device":
            usm_mem = dpctl_mem.MemoryUSMDevice(size * obj.dtype.itemsize,
                                                queue=queue)
        elif usm_type == "host":
            usm_mem = dpctl_mem.MemoryUSMHost(size * obj.dtype.itemsize,
                                              queue=queue)
        else:
            raise ValueError("Supported usm_type are: 'shared', "
                             "'device' and 'host'. Provided: %s" % (usm_type))

        if copy:
            # Copy data from numpy.ndarray
            copy_from_numpy_to_usm_obj(usm_mem, obj)

    return usm_mem