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

Add ability to link CUDA functions with in-memory PTX. #9470

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions docs/source/cuda-reference/kernel.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ be configured and launched:
.. autofunction:: numba.cuda.jit


.. _cuda-ptx-code:

The ``cuda.PTXCode`` class is used to store in-memory PTX code to be linked
with a CUDA dispatcher object

.. autoclass:: numba.cuda.PTXCode


Dispatcher objects
------------------

Expand Down
17 changes: 16 additions & 1 deletion numba/cuda/codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@
CUDA_TRIPLE = 'nvptx64-nvidia-cuda'


class PTXCode :
"""
Store in-memory PTX code to link with a compiled function.
"""

def __init__(self, ptx) :
self._ptx = ptx

def __str__(self) :
return self._ptx


def run_nvdisasm(cubin, flags):
# nvdisasm only accepts input from a file, so we need to write out to a
# temp file and clean up afterwards.
Expand Down Expand Up @@ -169,7 +181,10 @@ def get_cubin(self, cc=None):
for ptx in ptxes:
linker.add_ptx(ptx.encode())
for path in self._linking_files:
linker.add_file_guess_ext(path)
if isinstance(path, PTXCode):
linker.add_ptx(path._ptx.encode(encoding='ascii'))
else :
linker.add_file_guess_ext(path)
if self.needs_cudadevrt:
linker.add_file_guess_ext(get_cudalib('cudadevrt', static=True))

Expand Down
4 changes: 2 additions & 2 deletions numba/cuda/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ def jit(func_or_sig=None, device=False, inline=False, link=[], debug=None,
.. note:: A kernel cannot have any return value.
:param device: Indicates whether this is a device function.
:type device: bool
:param link: A list of files containing PTX or CUDA C/C++ source to link
with the function
:param link: A list of files containing PTX or CUDA C/C++ source or
:class:`PTXCode <numba.cuda.PTXCode>` to link with the function.
:type link: list
:param debug: If True, check for exceptions thrown when executing the
kernel. Since this degrades performance, this should only be used for
Expand Down
1 change: 1 addition & 0 deletions numba/cuda/device_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from .errors import KernelRuntimeError

from .decorators import jit, declare_device
from .codegen import PTXCode
from .api import *
from .api import _auto_device
from .args import In, Out, InOut
Expand Down