# Add support for linking with in-memory PTX code

**URL:** <https://numba.discourse.group/t/add-support-for-linking-with-in-memory-ptx-code/2414>\
**Category:** Development\
**Created:** [February 23, 2024, 4:18pm UTC](https://numba.discourse.group/t/add-support-for-linking-with-in-memory-ptx-code/2414 "2024-02-23T16:18:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ed-o-saurus](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/ed-o-saurus/32/995_2.png) [@ed-o-saurus](https://numba.discourse.group/u/ed-o-saurus)\
**Post date:** [February 23, 2024, 4:18pm UTC](https://numba.discourse.group/t/add-support-for-linking-with-in-memory-ptx-code/2414/1 "2024-02-23T16:18:22Z")

</div>

I’d like to create a PR to allow the `@cuda.jit` decorator’s `link` field to accommodate in-memory PTX.

Currently, if a user want to create PTX code dynamically, they need to create a temporary file, and pass the path to the `link` field of the `@cuda.jit` decorator. I propose creating a new class (`PTXCode`) that will store code as a string. Such an object could be added to the `link` list.

The only other change needed to existing code would be at line 183 of `numba/cuda/codegen.py`

```auto
        for path in self._linking_files:
            if isinstance(path, PTXCode):
                linker.add_ptx(path._ptx.encode(encoding='ascii'))
            else :
                linker.add_file_guess_ext(path)

```

Does this sound like a good and/or desirable improvement?  
It’s also not clear to me where the best place to put the new `PTXCode` class is. Any guidance on that would be appreciated.

---

<div class="post-metadata">

**Author:** ![chococandy63](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/chococandy63/32/1102_2.png) [@chococandy63](https://numba.discourse.group/u/chococandy63)\
**Post date:** [July 20, 2024, 3:35pm UTC](https://numba.discourse.group/t/add-support-for-linking-with-in-memory-ptx-code/2414/2 "2024-07-20T15:35:19Z")

</div>

Hi @ed-o-saurus,  
I am looking for something like this too. Are you working on it, if yes then could you point me to your work ?

---

<div class="post-metadata">

**Author:** ![ed-o-saurus](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/ed-o-saurus/32/995_2.png) [@ed-o-saurus](https://numba.discourse.group/u/ed-o-saurus)\
**Post date:** [July 20, 2024, 4:17pm UTC](https://numba.discourse.group/t/add-support-for-linking-with-in-memory-ptx-code/2414/3 "2024-07-20T16:17:12Z")

</div>

Support has been added to the pynvjitlink project. Take a look at :

> <https://github.com/numba/numba/pull/9470#issuecomment-2186254416>
>
> @ed-o-saurus Apologies for the confusion here - are you using Linux and CUDA 12.…0 or later? If so, the workaround is to add \[\`pynvjitlink\`\](https://github.com/rapidsai/pynvjitlink) as a dependency and call
> 
> \`\`\`python
> from pynvjitlink import patch
> patch.patch\_numba\_linker()
> \`\`\`
> 
> then you can use \`cuda.PTXSource\` and the other classes (e.g. \`cuda.CUSource\`, etc.).
> 
> If you're on Windows, we have not yet implemented support for Windows but are working on it. If you're on Windows and need support right now, you could take the \`LinkableCode\` classes from pynvjitlink and adapt the patch to patch just the part of the Numba linker that handles them in your project.

I believe the plan to to incorporate this into numba in a future release.

```
           -Ed

```

---

<div class="post-metadata">

**Author:** ![chococandy63](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/chococandy63/32/1102_2.png) [@chococandy63](https://numba.discourse.group/u/chococandy63)\
**Post date:** [July 20, 2024, 4:32pm UTC](https://numba.discourse.group/t/add-support-for-linking-with-in-memory-ptx-code/2414/4 "2024-07-20T16:32:57Z")

</div>

@ed-o-saurus thanks for the link!  
Could you please look at this issue `https://numba.discourse.group/t/extending-numba-for-cuda-inside-cppyy/2644` and guide me further.
