Skip to content

Subdevices work #326

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 28 commits into from
Closed

Subdevices work #326

wants to merge 28 commits into from

Conversation

1e-to
Copy link
Contributor

@1e-to 1e-to commented Mar 17, 2021

Closes #290

@1e-to 1e-to marked this pull request as draft March 17, 2021 14:18
@1e-to
Copy link
Contributor Author

1e-to commented Mar 22, 2021

2 notes being worked on

  • Need to change the vector in DPCTLDevice_CreateSubDevicesByCounts to what?
  • Add tests to DPCTLDevice_CreateSubDevicesByAffinity on all info :: partition_affinity_domain

@diptorupd
Copy link
Contributor

* Need to change the vector in DPCTLDevice_CreateSubDevicesByCounts to what?

Yes, the pitfalls of our C API design. You can pass __dpctl_keep size_t* counts, size_t ncounts to the function.

@diptorupd diptorupd mentioned this pull request Mar 22, 2021
8 tasks
@diptorupd diptorupd added this to the 0.7.0 milestone Mar 23, 2021
@1e-to
Copy link
Contributor Author

1e-to commented Mar 26, 2021

Work on:

  • Find out a way how to convert python array to pointer in create_sub_devices_by_counts (May be use memset(a.data.as_voidptr, 0, len(a) * sizeof(int))?)
  • Make _partition_affinity_domain_type enum visible in python

@1e-to 1e-to marked this pull request as ready for review March 26, 2021 17:41
@1e-to 1e-to changed the title WIP subdevices work Subdevices work Mar 29, 2021
@1e-to
Copy link
Contributor Author

1e-to commented Mar 29, 2021

@diptorupd , @oleksandr-pavlyk could you please take a look and write what need to be changed?

@oleksandr-pavlyk
Copy link
Contributor

@diptorupd , @oleksandr-pavlyk could you please take a look and write what need to be changed?

In [3]: d

Out[3]: <dpctl.SyclDevice [backend_type.opencl, device_type.cpu,  Intel(R) Core(TM) i7-10710U CPU @ 1.10GHz] at 0x7ffa8d81f070>

In [4]: d.create_sub_devices(partition=6)  # returns None ?

This kind of worked:

In [8]: import numpy as np

In [9]: d.create_sub_devices_by_counts(np.array([2, 2, 2, 2, 2, 2], dtype=np.uint64), 6)
Out[9]:
[<dpctl.SyclDevice [backend_type.opencl, device_type.cpu,  Intel(R) Core(TM) i7-10710U CPU @ 1.10GHz] at 0x7ffa9fa9a2f0>,
 <dpctl.SyclDevice [backend_type.opencl, device_type.cpu,  Intel(R) Core(TM) i7-10710U CPU @ 1.10GHz] at 0x7ffa9fa9a5f0>,
 <dpctl.SyclDevice [backend_type.opencl, device_type.cpu,  Intel(R) Core(TM) i7-10710U CPU @ 1.10GHz] at 0x7ffab44157b0>,
 <dpctl.SyclDevice [backend_type.opencl, device_type.cpu,  Intel(R) Core(TM) i7-10710U CPU @ 1.10GHz] at 0x7ffab4415770>,
 <dpctl.SyclDevice [backend_type.opencl, device_type.cpu,  Intel(R) Core(TM) i7-10710U CPU @ 1.10GHz] at 0x7ffab4415cb0>,
 <dpctl.SyclDevice [backend_type.opencl, device_type.cpu,  Intel(R) Core(TM) i7-10710U CPU @ 1.10GHz] at 0x7ffab4415830>]

but it should be unnecessary to specify length 6 as the second argument. It also should not be needed to use numpy array for this. Using list should be supported, so I would have expected d.create_sub_devices_by_counts([2, 2, 2, 2, 2, 2]) to work.

create by partition should raise an error here, rather than return an empty list

In [12]: d.max_compute_units
Out[12]: 12

In [13]: d.create_sub_devices_equally(12)
Native API failed. Native API returns: -18 (CL_DEVICE_PARTITION_FAILED) -18 (CL_DEVICE_PARTITION_FAILED)
Out[13]: []

This worked fine.

In [14]: d.create_sub_devices_equally(6)
Out[14]:
[<dpctl.SyclDevice [backend_type.opencl, device_type.cpu,  Intel(R) Core(TM) i7-10710U CPU @ 1.10GHz] at 0x7ffa8d0c48b0>,
 <dpctl.SyclDevice [backend_type.opencl, device_type.cpu,  Intel(R) Core(TM) i7-10710U CPU @ 1.10GHz] at 0x7ffa8d0c4270>]

I thought we discussed that create_sub_devices_equally, create_sub_devices_by_partition and create_sub_devices_by_counts methods will be cdef only, and the only Python visible interface will be create_sub_devices(partition=...) interface.

Copy link
Contributor

@diptorupd diptorupd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost there. Looking good.

cdef DPCTLDeviceVectorRef DVRef = NULL
DVRef = DPCTLDevice_CreateSubDevicesByAffinity(self._device_ref, domain)
if DVRef is NULL:
self._raise_sub_devices_creation_error("DPCTLSubDeviceCreationError", -1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@oleksandr-pavlyk Will Cython ever raise this exception? Should the cdef function have except + added to the signature?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@1e-to can you check if Cython raises this exception? Would be good to have a negative test case to see if an exception is raised.

Refer Sasha's comments in #35 (comment)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it raises

>>> d = dpctl.SyclDevice("cpu")
>>> d.create_sub_devices("L2_cache")

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "dpctl/_sycl_device.pyx", line 268, in dpctl._sycl_device.SyclDevice.create_sub_devices
    return self.create_sub_devices_by_affinity(domain_type)
  File "dpctl/_sycl_device.pyx", line 241, in dpctl._sycl_device.SyclDevice.create_sub_devices_by_affinity
    self._raise_sub_devices_creation_error("DPCTLSubDeviceCreationError", -1)
  File "dpctl/_sycl_device.pyx", line 195, in dpctl._sycl_device.SyclDevice._raise_sub_devices_creation_error
    raise e
dpctl._sycl_device.SubDeviceCreationError: Sub-devices were not created.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@1e-to Please add some negative test cases where we check if the exception is raised.

if DVRef is NULL:
self._raise_sub_devices_creation_error("DPCTLSubDeviceCreationError", -1)
cdef list devices = _get_devices(DVRef)
free(counts_buff)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if memory was allocated, but DVRef was NULL, the memory would never be freed.

I fixed this issue in #343

e = SubDeviceCreationError("Sub-devices were not created.")
e.fname = fname
e.code = errcode
raise e
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function serves no purpose. It is deleted in #343

Object e derives from Exception, which does not have attributes fname or code. Lines 193-194 dynamically add these attributes to the exception instance, but they go unused.

Calls self._raise_sub_devices_creation_error("DPCTLSubDeviceCreationError", -1) have been replaced with raise SubDeviceCreationError("Sub-devices were not created.") in #343

is created.
"""
cdef size_t ncounts = len(counts)
cdef size_t *counts_buff = <size_t *> malloc(ncounts * sizeof(size_t))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Must check that pointer counts_buff is not NULL, and raise MemoryError if it is.

In any case, null pointer should not be dereferenced. Fixed in #343

def create_sub_devices(self, partition):
if isinstance(partition, int) and partition > 0:
return self.create_sub_devices_equally(partition)
elif isinstance(partition, list) and all([i > 0 for i in partition]):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all([i>0 for i in partition]) is redundant. It is being checked as part of conversion from element of partition to size_t on lines 223-224.

else:
raise Exception('Unsupported type of domain')
return self.create_sub_devices_by_affinity(domain_type)
else:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reworked this dispatch in #343 to require keyword partition, so that device.create_sub_devices(4) must become device.create_sub_devices(partition=4) to work.

Changes allow allow for list/tuples/numpy arrays for specify partition by counts.


In [1]: import dpctl, numpy as np

In [2]: [di.max_compute_units for di in dpctl.SyclDevice("cpu").create_sub_devices(partition=np.array([2, 6])) ]
Out[2]: [2, 6]

In [1]: import dpctl, numpy as np

In [2]: [di.max_compute_units for di in dpctl.SyclDevice("cpu").create_sub_devices(partition=np.array([2, 6])) ]
Out[2]: [2, 6]

In [4]: [di.max_compute_units for di in dpctl.SyclDevice("cpu").create_sub_devices(partition = [2, 6, 4]) ]
Out[4]: [2, 6, 4]

In [5]: [di.max_compute_units for di in dpctl.SyclDevice("cpu").create_sub_devices(partition = (2, 6, 4)) ]
Out[5]: [2, 6, 4]

Also use of NumPy integral types is supported:

In [6]: dpctl.SyclDevice("cpu").create_sub_devices(partition=np.int32(4))
Out[6]:
[<dpctl.SyclDevice [backend_type.opencl, device_type.cpu,  Intel(R) Core(TM) i7-10710U CPU @ 1.10GHz] at 0x7f1f698727b0>,
 <dpctl.SyclDevice [backend_type.opencl, device_type.cpu,  Intel(R) Core(TM) i7-10710U CPU @ 1.10GHz] at 0x7f1f69872c30>,
 <dpctl.SyclDevice [backend_type.opencl, device_type.cpu,  Intel(R) Core(TM) i7-10710U CPU @ 1.10GHz] at 0x7f1f69872d30>]

@oleksandr-pavlyk
Copy link
Contributor

Closing this PR, since the superseding PR #326 has been merged.

diptorupd pushed a commit to diptorupd/dpctl that referenced this pull request Apr 2, 2021
diptorupd pushed a commit to diptorupd/dpctl that referenced this pull request Apr 2, 2021
@1e-to 1e-to deleted the sub_devices branch November 3, 2021 09:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement support for sub-devices
3 participants