Example #1
0
def test_memory_construction_from_other_memory_objects():
    try:
        q = dpctl.SyclQueue()
    except dpctl.SyclQueueCreationError:
        pytest.skip("Default queue could not be created")
    m_sh = MemoryUSMShared(256, queue=q)
    m_de = MemoryUSMDevice(256, queue=q)
    m_ho = MemoryUSMHost(256, queue=q)
    with pytest.raises(ValueError):
        MemoryUSMDevice(m_sh)
    with pytest.raises(ValueError):
        MemoryUSMHost(m_de)
    with pytest.raises(ValueError):
        MemoryUSMShared(m_ho)
    m1 = MemoryUSMDevice(m_sh, copy=True)
    m2 = MemoryUSMHost(m_de, copy=True)
    m3 = MemoryUSMShared(m_de, copy=True)
    assert bytes(m1) == bytes(m_sh)
    assert bytes(m2) == bytes(m3)
Example #2
0
 def __new__(
     subtype,
     shape,
     dtype=float,
     buffer=None,
     offset=0,
     strides=None,
     order=None,
 ):
     # Create a new array.
     if buffer is None:
         dprint("numpy_usm_shared::ndarray __new__ buffer None")
         nelems = np.prod(shape)
         dt = np.dtype(dtype)
         isz = dt.itemsize
         # Have to use builtins.max explicitly since this module will
         # import numpy's max function.
         nbytes = int(isz * builtins.max(1, nelems))
         buf = MemoryUSMShared(nbytes)
         new_obj = np.ndarray.__new__(
             subtype,
             shape,
             dtype=dt,
             buffer=buf,
             offset=0,
             strides=strides,
             order=order,
         )
         if hasattr(new_obj, array_interface_property):
             dprint("buffer None new_obj already has sycl_usm")
         else:
             dprint("buffer None new_obj will add sycl_usm")
             setattr(
                 new_obj,
                 array_interface_property,
                 new_obj._getter_sycl_usm_array_interface_(),
             )
         return new_obj
     # zero copy if buffer is a usm backed array-like thing
     elif hasattr(buffer, array_interface_property):
         dprint(
             "numpy_usm_shared::ndarray __new__ buffer",
             array_interface_property,
         )
         # also check for array interface
         new_obj = np.ndarray.__new__(
             subtype,
             shape,
             dtype=dtype,
             buffer=buffer,
             offset=offset,
             strides=strides,
             order=order,
         )
         if hasattr(new_obj, array_interface_property):
             dprint("buffer None new_obj already has sycl_usm")
         else:
             dprint("buffer None new_obj will add sycl_usm")
             setattr(
                 new_obj,
                 array_interface_property,
                 new_obj._getter_sycl_usm_array_interface_(),
             )
         return new_obj
     else:
         dprint(
             "numpy_usm_shared::ndarray __new__ buffer not None "
             "and not sycl_usm"
         )
         nelems = np.prod(shape)
         # must copy
         ar = np.ndarray(
             shape,
             dtype=dtype,
             buffer=buffer,
             offset=offset,
             strides=strides,
             order=order,
         )
         nbytes = int(ar.nbytes)
         buf = MemoryUSMShared(nbytes)
         new_obj = np.ndarray.__new__(
             subtype,
             shape,
             dtype=dtype,
             buffer=buf,
             offset=0,
             strides=strides,
             order=order,
         )
         np.copyto(new_obj, ar, casting="no")
         if hasattr(new_obj, array_interface_property):
             dprint("buffer None new_obj already has sycl_usm")
         else:
             dprint("buffer None new_obj will add sycl_usm")
             setattr(
                 new_obj,
                 array_interface_property,
                 new_obj._getter_sycl_usm_array_interface_(),
             )
         return new_obj
 def _create_memory(self):
     nbytes = 1024
     queue = dpctl.get_current_queue()
     mobj = MemoryUSMShared(nbytes, alignment=64, queue=queue)
     return mobj
 def test_memory_create(self):
     nbytes = 1024
     queue = dpctl.get_current_queue()
     mobj = MemoryUSMShared(nbytes, alignment=64, queue=queue)
     self.assertEqual(mobj.nbytes, nbytes)
     self.assertTrue(hasattr(mobj, "__sycl_usm_array_interface__"))
Example #5
0
def _create_memory():
    nbytes = 1024
    queue = dpctl.SyclQueue()
    mobj = MemoryUSMShared(nbytes, alignment=64, queue=queue)
    return mobj
Example #6
0
def test_suai_invalid_suai():
    n_bytes = 2 * 3 * 5 * 128
    try:
        q = dpctl.SyclQueue()
    except dpctl.SyclQueueCreationError:
        pytest.skip("Could not create default queue")
    try:
        buf = MemoryUSMShared(n_bytes, queue=q)
    except Exception:
        pytest.skip("USM-shared allocation failed")

    # different syclobj values
    class DuckSyclObject:
        def __init__(self, syclobj):
            self.syclobj = syclobj

        def _get_capsule(self):
            return self.syclobj._get_capsule()

    ctx = q.sycl_context
    for syclobj in [
            q,
            DuckSyclObject(q),
            q._get_capsule(),
            ctx,
            DuckSyclObject(ctx),
            ctx._get_capsule(),
    ]:
        v = View(buf,
                 shape=(n_bytes, ),
                 strides=(1, ),
                 offset=0,
                 syclobj=syclobj)
        MemoryUSMShared(v)
        with pytest.raises(ValueError):
            MemoryUSMDevice(v)
        with pytest.raises(ValueError):
            MemoryUSMHost(v)

    # version validation
    def invalid_version(suai_iface):
        "Set version to invalid"
        suai_iface["version"] = 0
        return suai_iface

    v = View(buf,
             shape=(n_bytes, ),
             strides=(1, ),
             offset=0,
             transf_fn=invalid_version)
    with pytest.raises(ValueError):
        MemoryUSMShared(v)

    # data validation
    def invalid_data(suai_iface):
        "Set data to invalid"
        suai_iface["data"] = tuple()
        return suai_iface

    v = View(buf,
             shape=(n_bytes, ),
             strides=(1, ),
             offset=0,
             transf_fn=invalid_data)
    with pytest.raises(ValueError):
        MemoryUSMShared(v)
    # set shape to a negative value
    v = View(buf, shape=(-n_bytes, ), strides=(2, ), offset=0)
    with pytest.raises(ValueError):
        MemoryUSMShared(v)
    v = View(buf, shape=(-n_bytes, ), strides=None, offset=0)
    with pytest.raises(ValueError):
        MemoryUSMShared(v)
    # shape validation
    v = View(buf, shape=None, strides=(1, ), offset=0)
    with pytest.raises(ValueError):
        MemoryUSMShared(v)

    # typestr validation
    def invalid_typestr(suai_iface):
        suai_iface["typestr"] = "invalid"
        return suai_iface

    v = View(buf,
             shape=(n_bytes, ),
             strides=(1, ),
             offset=0,
             transf_fn=invalid_typestr)
    with pytest.raises(ValueError):
        MemoryUSMShared(v)

    def unsupported_typestr(suai_iface):
        suai_iface["typestr"] = "O"
        return suai_iface

    v = View(
        buf,
        shape=(n_bytes, ),
        strides=(1, ),
        offset=0,
        transf_fn=unsupported_typestr,
    )
    with pytest.raises(ValueError):
        MemoryUSMShared(v)
    # set strides to invalid value
    v = View(buf, shape=(n_bytes, ), strides=Ellipsis, offset=0)
    with pytest.raises(ValueError):
        MemoryUSMShared(v)