From bd7467b47baf15cb8ed049ad177f34b097b1c4aa Mon Sep 17 00:00:00 2001 From: ksimpson Date: Thu, 23 Jan 2025 10:41:35 -0800 Subject: [PATCH 1/4] first draft --- cuda_bindings/docs/source/tips_and_tricks.rst | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/cuda_bindings/docs/source/tips_and_tricks.rst b/cuda_bindings/docs/source/tips_and_tricks.rst index d979ea85..1393012d 100644 --- a/cuda_bindings/docs/source/tips_and_tricks.rst +++ b/cuda_bindings/docs/source/tips_and_tricks.rst @@ -7,3 +7,12 @@ Getting the address of underlying C objects from the low-level bindings All CUDA C types are exposed to Python as Python classes. For example, the :class:`~cuda.bindings.driver.CUstream` type is exposed as a class with methods :meth:`~cuda.bindings.driver.CUstream.getPtr()` and :meth:`~cuda.bindings.driver.CUstream.__int__()` implemented. There is an important distinction between the ``getPtr()`` method and the behaviour of ``__int__()``. If you need to get the pointer address *of* the underlying ``CUstream`` C object wrapped in the Python class, you can do so by calling ``int(instance_of_CUstream)``, which returns the address as a Python `int`, while calling ``instance_of_CUstream.getPtr()`` returns the pointer *to* the ``CUstream`` C object (that is, ``&CUstream``) as a Python `int`. + + +Getting and setting attributes of extension types +================================================= + +While the bindings outwardly presents the attributes of extension types in a Pythonic way, they can't always be interacted +with in a Pythonic style. Often the getters/setters (__getitem__(), __setitem__()) are actually a translation step to convert values between +Python and C. For example, in some cases, attempting to modify an attribute in place, will lead to unexpected behavior due to the constraints +of the underlying implementation. For this reason, users should use the getters and setters directly when interacting with extension types. \ No newline at end of file From 2ea3971e7dc97e03bee84faa8386e7b6752c0a7b Mon Sep 17 00:00:00 2001 From: ksimpson Date: Thu, 23 Jan 2025 10:41:58 -0800 Subject: [PATCH 2/4] first draft --- cuda_bindings/docs/source/tips_and_tricks.rst | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/cuda_bindings/docs/source/tips_and_tricks.rst b/cuda_bindings/docs/source/tips_and_tricks.rst index 1393012d..51a02194 100644 --- a/cuda_bindings/docs/source/tips_and_tricks.rst +++ b/cuda_bindings/docs/source/tips_and_tricks.rst @@ -12,7 +12,4 @@ There is an important distinction between the ``getPtr()`` method and the behavi Getting and setting attributes of extension types ================================================= -While the bindings outwardly presents the attributes of extension types in a Pythonic way, they can't always be interacted -with in a Pythonic style. Often the getters/setters (__getitem__(), __setitem__()) are actually a translation step to convert values between -Python and C. For example, in some cases, attempting to modify an attribute in place, will lead to unexpected behavior due to the constraints -of the underlying implementation. For this reason, users should use the getters and setters directly when interacting with extension types. \ No newline at end of file +While the bindings outwardly presents the attributes of extension types in a Pythonic way, they can't always be interacted with in a Pythonic style. Often the getters/setters (__getitem__(), __setitem__()) are actually a translation step to convert values between Python and C. For example, in some cases, attempting to modify an attribute in place, will lead to unexpected behavior due to the constraints of the underlying implementation. For this reason, users should use the getters and setters directly when interacting with extension types. \ No newline at end of file From db8186bfd9bcc31e933eed6f76f2a91a228add90 Mon Sep 17 00:00:00 2001 From: ksimpson Date: Thu, 23 Jan 2025 19:14:49 -0800 Subject: [PATCH 3/4] expland the explanation with an example and reword --- cuda_bindings/docs/source/tips_and_tricks.rst | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/cuda_bindings/docs/source/tips_and_tricks.rst b/cuda_bindings/docs/source/tips_and_tricks.rst index 51a02194..9b7d7f4d 100644 --- a/cuda_bindings/docs/source/tips_and_tricks.rst +++ b/cuda_bindings/docs/source/tips_and_tricks.rst @@ -12,4 +12,22 @@ There is an important distinction between the ``getPtr()`` method and the behavi Getting and setting attributes of extension types ================================================= -While the bindings outwardly presents the attributes of extension types in a Pythonic way, they can't always be interacted with in a Pythonic style. Often the getters/setters (__getitem__(), __setitem__()) are actually a translation step to convert values between Python and C. For example, in some cases, attempting to modify an attribute in place, will lead to unexpected behavior due to the constraints of the underlying implementation. For this reason, users should use the getters and setters directly when interacting with extension types. \ No newline at end of file +While the bindings outwardly presents the attributes of extension types in a Pythonic way, they can't always be interacted with in a Pythonic style. Often the getters/setters (__getitem__(), __setitem__()) are actually a translation step to convert values between Python and C. For example, in some cases, attempting to modify an attribute in place, will lead to unexpected behavior due to the design of the underlying implementation. For this reason, users should use the getters and setters directly when interacting with extension types. + +An example of this is the :class:`~cuda.bindings.driver.CULaunchConfig` type. + +.. code-block:: python + + cfg = cuda.CUlaunchConfig() + + cfg.numAttrs += 1 + attr = cuda.CUlaunchAttribute() + + ... + + # This works. We are passing the attribute to the setter + drv_cfg.attrs = [attr] + + # This does not work. We are trying to modify the attribute in place + drv_cfg.attrs.append(attr) + From b37750571873a1eae230c0e479faf4a3bf3a6bcb Mon Sep 17 00:00:00 2001 From: ksimpson Date: Fri, 24 Jan 2025 10:18:46 -0800 Subject: [PATCH 4/4] address comments --- cuda_bindings/docs/source/tips_and_tricks.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cuda_bindings/docs/source/tips_and_tricks.rst b/cuda_bindings/docs/source/tips_and_tricks.rst index 9b7d7f4d..f4dea6e8 100644 --- a/cuda_bindings/docs/source/tips_and_tricks.rst +++ b/cuda_bindings/docs/source/tips_and_tricks.rst @@ -12,7 +12,7 @@ There is an important distinction between the ``getPtr()`` method and the behavi Getting and setting attributes of extension types ================================================= -While the bindings outwardly presents the attributes of extension types in a Pythonic way, they can't always be interacted with in a Pythonic style. Often the getters/setters (__getitem__(), __setitem__()) are actually a translation step to convert values between Python and C. For example, in some cases, attempting to modify an attribute in place, will lead to unexpected behavior due to the design of the underlying implementation. For this reason, users should use the getters and setters directly when interacting with extension types. +While the bindings outwardly present the attributes of extension types in a pythonic way, they can't always be interacted with in a Pythonic style. Often the getters/setters (__getitem__(), __setitem__()) are actually a translation step to convert values between Python and C. For example, in some cases, attempting to modify an attribute in place, will lead to unexpected behavior due to the design of the underlying implementation. For this reason, users should use the getters and setters directly when interacting with extension types. An example of this is the :class:`~cuda.bindings.driver.CULaunchConfig` type. @@ -25,9 +25,9 @@ An example of this is the :class:`~cuda.bindings.driver.CULaunchConfig` type. ... - # This works. We are passing the attribute to the setter + # This works. We are passing the new attribute to the setter drv_cfg.attrs = [attr] - # This does not work. We are trying to modify the attribute in place + # This does not work. We are only modifying the returned attribute in place drv_cfg.attrs.append(attr)