Skip to content
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

Documentation struct getters #413

Merged
merged 6 commits into from
Jan 24, 2025
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions cuda_bindings/docs/source/tips_and_tricks.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,27 @@ 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 design of the underlying implementation. For this reason, users should use the getters and setters directly when interacting with extension types.
leofang marked this conversation as resolved.
Show resolved Hide resolved

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
leofang marked this conversation as resolved.
Show resolved Hide resolved
drv_cfg.attrs = [attr]

# This does not work. We are trying to modify the attribute in place
leofang marked this conversation as resolved.
Show resolved Hide resolved
drv_cfg.attrs.append(attr)

Loading