# Can a ptx file contain multiple kernels ?

**URL:** <https://forums.developer.nvidia.com/t/can-a-ptx-file-contain-multiple-kernels/23190>\
**Category:** CUDA Programming and Performance\
**Created:** [June 23, 2011, 3:18am UTC](https://forums.developer.nvidia.com/t/can-a-ptx-file-contain-multiple-kernels/23190 "2011-06-23T03:18:13Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Skybuck](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/skybuck/32/9717_2.png) [@Skybuck](https://forums.developer.nvidia.com/u/Skybuck)\
**Post date:** [June 23, 2011, 3:18am UTC](https://forums.developer.nvidia.com/t/can-a-ptx-file-contain-multiple-kernels/23190/1 "2011-06-23T03:18:13Z")

</div>

Can a ptx file contain multiple kernels ?

---

<div class="post-metadata">

**Author:** ![E.D\_Riedijk](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@E.D\_Riedijk](https://forums.developer.nvidia.com/u/E.D_Riedijk)\
**Post date:** [June 23, 2011, 4:44am UTC](https://forums.developer.nvidia.com/t/can-a-ptx-file-contain-multiple-kernels/23190/2 "2011-06-23T04:44:35Z")

</div>

yes, it can

---

<div class="post-metadata">

**Author:** ![Skybuck](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/skybuck/32/9717_2.png) [@Skybuck](https://forums.developer.nvidia.com/u/Skybuck)\
**Post date:** [June 23, 2011, 6:06pm UTC](https://forums.developer.nvidia.com/t/can-a-ptx-file-contain-multiple-kernels/23190/3 "2011-06-23T18:06:25Z")

</div>

It would be helpfull if the CUDA Driver API had a function/API to return how many kernels a module contains.

It would also be helpfull if it would automatically extract the kernel entry names… and kernel handles.

Especially kernel entry names, since they seemed mangled.

IT would be even more helpfull if the entry names weren’t mangled at all [External Image](http://forums.nvidia.com/public/style_emoticons/<#EMO_DIR#>/wink.gif "Image hosted on another site. Click to open in a new tab.").

---

<div class="post-metadata">

**Author:** ![seibert](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/seibert/32/520688_2.png) [@seibert](https://forums.developer.nvidia.com/u/seibert)\
**Post date:** [June 23, 2011, 11:47pm UTC](https://forums.developer.nvidia.com/t/can-a-ptx-file-contain-multiple-kernels/23190/4 "2011-06-23T23:47:27Z")

</div>

> [@](#):
>
> Especially kernel entry names, since they seemed mangled.
> 
> IT would be even more helpfull if the entry names weren’t mangled at all [External Image](http://forums.nvidia.com/public/style_emoticons/<#EMO_DIR#>/wink.gif "Image hosted on another site. Click to open in a new tab.").

Unfortunately, I think we’re stuck with this in the general case as long as C++ function overloading is allowed for **global** functions. Some kind of mangling is required to capture the type information.

You can prevent mangling if you don’t need overloading by wrapping your **global** function like this:

```auto
extern "C" {

__global__ void mykernel(int *stuff) {

}

// etc etc

}

```

This is what PyCUDA does to aid in fetching kernels from compiled modules.

---

<div class="post-metadata">

**Author:** ![Skybuck](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/skybuck/32/9717_2.png) [@Skybuck](https://forums.developer.nvidia.com/u/Skybuck)\
**Post date:** [June 24, 2011, 3:10am UTC](https://forums.developer.nvidia.com/t/can-a-ptx-file-contain-multiple-kernels/23190/5 "2011-06-24T03:10:45Z")

</div>

Wow that’s pretty cool ! And very handy ! Because the entry names seemed to change after some code changes (?), so this will prevent that which is real nice.

I do wonder though:

Will this still allow c++ classes to be used inside the **global**?

I don’t know what extern “C” normally does… it almost seems as if it’s saying: “only c allowed” “no C++” ! [External Image](http://forums.nvidia.com/public/style_emoticons/<#EMO_DIR#>/wink.gif "Image hosted on another site. Click to open in a new tab.") =D (But that’s probably just me ! [External Image](http://forums.nvidia.com/public/style_emoticons/<#EMO_DIR#>/wink.gif "Image hosted on another site. Click to open in a new tab.") [External Image](http://forums.nvidia.com/public/style_emoticons/<#EMO_DIR#>/smile2.gif "Image hosted on another site. Click to open in a new tab.")

I have seen this before sometimes… I think it’s actually for exporting dll function names as well, kinda makes sense ! [External Image](http://forums.nvidia.com/public/style_emoticons/<#EMO_DIR#>/wink.gif "Image hosted on another site. Click to open in a new tab.") =D

If those dll names would change all the time that would be a real problem for dynamic loading ! [External Image](http://forums.nvidia.com/public/style_emoticons/<#EMO_DIR#>/wink.gif "Image hosted on another site. Click to open in a new tab.") =D

---

<div class="post-metadata">

**Author:** ![seibert](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/seibert/32/520688_2.png) [@seibert](https://forums.developer.nvidia.com/u/seibert)\
**Post date:** [June 24, 2011, 5:19pm UTC](https://forums.developer.nvidia.com/t/can-a-ptx-file-contain-multiple-kernels/23190/6 "2011-06-24T17:19:06Z")

</div>

> [@](#):
>
> Will this still allow c++ classes to be used inside the **global**?
> 
> I don’t know what extern “C” normally does… it almost seems as if it’s saying: “only c allowed” “no C++” ! [External Image](http://forums.nvidia.com/public/style_emoticons/<#EMO_DIR#>/wink.gif "Image hosted on another site. Click to open in a new tab.") =D (But that’s probably just me ! [External Image](http://forums.nvidia.com/public/style_emoticons/<#EMO_DIR#>/wink.gif "Image hosted on another site. Click to open in a new tab.") [External Image](http://forums.nvidia.com/public/style_emoticons/<#EMO_DIR#>/smile2.gif "Image hosted on another site. Click to open in a new tab.")
> 
> I have seen this before sometimes… I think it’s actually for exporting dll function names as well, kinda makes sense ! [External Image](http://forums.nvidia.com/public/style_emoticons/<#EMO_DIR#>/wink.gif "Image hosted on another site. Click to open in a new tab.") =D
> 
> If those dll names would change all the time that would be a real problem for dynamic loading ! [External Image](http://forums.nvidia.com/public/style_emoticons/<#EMO_DIR#>/wink.gif "Image hosted on another site. Click to open in a new tab.") =D

extern “C” is only used to indicate to the compiler that you would like C-style linkage, i.e. the symbol name is the function name. You can still use C++ classes, but specifically function overloading won’t work since multiple functions with the same name and different parameters will get the same symbol name in the linker.

---

<div class="post-metadata">

**Author:** ![hyqneuron](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/hyqneuron/32/9902_2.png) [@hyqneuron](https://forums.developer.nvidia.com/u/hyqneuron)\
**Post date:** [June 25, 2011, 9:34am UTC](https://forums.developer.nvidia.com/t/can-a-ptx-file-contain-multiple-kernels/23190/7 "2011-06-25T09:34:22Z")

</div>

> [@](#):
>
> It would be helpfull if the CUDA Driver API had a function/API to return how many kernels a module contains.
> 
> It would also be helpfull if it would automatically extract the kernel entry names… and kernel handles.
> 
> Especially kernel entry names, since they seemed mangled.

Just to add on a bit, these are entirely doable with an ELF library. Just reading through the section header table as well as some entries in .shstrtab and you’ll find all the names (prefixed with .text.). It would be nice, however, to have these functions straight from the driver API.
