Example #1
0
def test_from_backend_contexts_cuda_single_device(mock_backend_pycuda,
                                                  take_ownership):
    # CUDA style - a context per device

    backend = mock_backend_pycuda

    backend.add_devices(['Device1', 'Device2'])

    backend_context = backend.pycuda_driver.Device(1).make_context()

    if not take_ownership:
        # backend context can stay in the stack
        context = Context.from_backend_contexts(backend_context,
                                                take_ownership=False)

    else:
        # forgot to pop the backend context off the stack - error
        with pytest.raises(
                ValueError,
                match="The given context is already in the context stack"):
            context = Context.from_backend_contexts(backend_context,
                                                    take_ownership=True)

        backend.pycuda_driver.Context.pop()
        context = Context.from_backend_contexts(backend_context,
                                                take_ownership=True)

    # CUDA has no concept of platforms, so the platform name in the mock will be ignored
    assert context.platform.name == 'nVidia CUDA'

    assert [device.name for device in context.devices] == ['Device2']
Example #2
0
def test_from_backend_contexts_opencl(mock_backend_pyopencl):
    # OpenCL style - one context, many devices

    backend = mock_backend_pyopencl

    backend.add_platform_with_devices('Platform1', ['Device1'])
    backend.add_platform_with_devices('Platform2', ['Device2', 'Device3'])

    backend_devices = backend.pyopencl.get_platforms()[1].get_devices()
    backend_context = backend.pyopencl.Context(backend_devices)

    backend_context2 = backend.pyopencl.Context(backend_devices)
    with pytest.raises(
            ValueError,
            match="Cannot make one OpenCL context out of several contexts"):
        Context.from_backend_contexts([backend_context, backend_context2])

    context = Context.from_backend_contexts(backend_context)

    assert context.platform.name == 'Platform2'
    assert [device.name
            for device in context.devices] == ['Device2', 'Device3']

    with pytest.raises(TypeError):
        Context.from_backend_contexts(1)
Example #3
0
def test_from_backend_contexts_cuda_multi_device(mock_backend_pycuda):
    # CUDA style - a context per device

    backend = mock_backend_pycuda

    backend.add_devices(['Device1', 'Device2'])

    backend_context1 = backend.pycuda_driver.Device(0).make_context()
    backend.pycuda_driver.Context.pop()

    backend_context2 = backend.pycuda_driver.Device(1).make_context()
    backend.pycuda_driver.Context.pop()

    # Grunnur mast have ownership
    error_msg = "When dealing with multiple CUDA contexts, Grunnur must be the one managing them"
    with pytest.raises(ValueError, match=error_msg):
        Context.from_backend_contexts([backend_context1, backend_context2])

    context = Context.from_backend_contexts(
        [backend_context1, backend_context2], take_ownership=True)

    # CUDA has no concept of platforms, so the platform name in the mock will be ignored
    assert context.platform.name == 'nVidia CUDA'

    assert [device.name
            for device in context.devices] == ['Device1', 'Device2']
Example #4
0
def test_deactivate(mock_backend_pyopencl, mock_backend_pycuda):

    mock_backend_pyopencl.add_platform_with_devices('Platform1', ['Device1'])
    mock_backend_pycuda.add_devices(['Device1'])

    api = API.from_api_id(mock_backend_pyopencl.api_id)
    context = Context.from_devices(api.platforms[0].devices[0])
    with pytest.raises(RuntimeError,
                       match="`deactivate\\(\\)` only works for CUDA API"):
        context.deactivate()

    backend_context = mock_backend_pycuda.pycuda_driver.Device(
        0).make_context()
    backend_context.pop()

    api = API.from_api_id(mock_backend_pycuda.api_id)
    context = Context.from_backend_contexts(backend_context,
                                            take_ownership=True)
    assert backend_context.is_stacked()
    context.deactivate()
    assert not backend_context.is_stacked()