示例#1
0
def test_context_multi_device():
    try:
        d = dpctl.SyclDevice("cpu")
    except ValueError:
        pytest.skip()
    if d.default_selector_score < 0:
        pytest.skip()
    n = d.max_compute_units
    n1 = n // 2
    n2 = n - n1
    if n1 == 0 or n2 == 0:
        pytest.skip()
    d1, d2 = d.create_sub_devices(partition=(n1, n2))
    ctx = dpctl.SyclContext((d1, d2))
    assert ctx.device_count == 2
    assert type(repr(ctx)) is str
    q1 = dpctl.SyclQueue(ctx, d1)
    q2 = dpctl.SyclQueue(ctx, d2)
    import dpctl.memory as dpmem

    shmem_1 = dpmem.MemoryUSMShared(256, queue=q1)
    shmem_2 = dpmem.MemoryUSMDevice(256, queue=q2)
    shmem_2.copy_from_device(shmem_1)
    # create context for single sub-device
    ctx1 = dpctl.SyclContext(d1)
    q1 = dpctl.SyclQueue(ctx1, d1)
    shmem_1 = dpmem.MemoryUSMShared(256, queue=q1)
    cap = ctx1._get_capsule()
    cap2 = ctx1._get_capsule()
    del ctx1
    del cap2  # exercise deleter of non-renamed capsule
    ctx2 = dpctl.SyclContext(cap)
    q2 = dpctl.SyclQueue(ctx2, d1)
    shmem_2 = dpmem.MemoryUSMDevice(256, queue=q2)
    shmem_2.copy_from_device(shmem_1)
示例#2
0
def test_sycl_timer():
    try:
        q = dpctl.SyclQueue(property="enable_profiling")
    except dpctl.SyclQueueCreationError:
        pytest.skip("Queue creation of default device failed")
    timer = dpctl.SyclTimer()
    m1 = dpctl_mem.MemoryUSMDevice(256 * 1024, queue=q)
    m2 = dpctl_mem.MemoryUSMDevice(256 * 1024, queue=q)
    with timer(q):
        # device task
        m1.copy_from_device(m2)
        # host task
        [x**2 for x in range(1024)]
    host_dt, device_dt = timer.dt
    assert host_dt > device_dt
    q_no_profiling = dpctl.SyclQueue()
    assert q_no_profiling.has_enable_profiling is False
    with pytest.raises(ValueError):
        timer(queue=q_no_profiling)
    with pytest.raises(TypeError):
        timer(queue=None)
示例#3
0
def test_context_multi_device():
    try:
        d = dpctl.SyclDevice("cpu")
    except ValueError:
        pytest.skip()
    if d.default_selector_score < 0:
        pytest.skip()
    n = d.max_compute_units
    n1 = n // 2
    n2 = n - n1
    if n1 == 0 or n2 == 0:
        pytest.skip()
    d1, d2 = d.create_sub_devices(partition=(n1, n2))
    ctx = dpctl.SyclContext((d1, d2))
    assert ctx.device_count == 2
    q1 = dpctl.SyclQueue(ctx, d1)
    q2 = dpctl.SyclQueue(ctx, d2)
    import dpctl.memory as dpmem

    shmem_1 = dpmem.MemoryUSMShared(256, queue=q1)
    shmem_2 = dpmem.MemoryUSMDevice(256, queue=q2)
    shmem_2.copy_from_device(shmem_1)
示例#4
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()
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("")
    print(
        "An expected exception was raised during attempted construction of memoryview from USM-device memory object."
    )
    print("\t", e)
示例#6
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
示例#7
0
#    http://www.apache.org/licenses/LICENSE-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 host to device copy functions using dpctl.memory.
"""

import dpctl
import dpctl.memory as dpmem
import numpy as np

ms = dpmem.MemoryUSMShared(32)
md = dpmem.MemoryUSMDevice(32)

host_buf = np.random.randint(0, 42, dtype=np.uint8, size=32)

# copy host byte-like object to USM-device buffer
md.copy_from_host(host_buf)

# copy USM-device buffer to USM-shared buffer in parallel (using sycl::queue::memcpy)
ms.copy_from_device(md)

# build numpy array reusing host-accessible USM-shared memory
X = np.ndarray((len(ms), ), buffer=ms, dtype=np.uint8)

# Display Python object NumPy ndarray is viewing into
print("numpy.ndarray.base: ", X.base)
print("")