Using Runtime compiled C++ for runtime compiling cuda code!!

77 views
Skip to first unread message

Daniel Moodie

unread,
Jun 18, 2015, 10:42:15 PM6/18/15
to runtimecompi...@googlegroups.com
Hey guys,

I love RCC and I just wanted to post about how I got RCC to work with writing cuda code.

Getting this to run on windows is currently WIP, but I just got it working on Linux so I thought I would add a how to.

Cuda's NVCC can be treated almost exactly like any other native compiler, except that we just need to add a few more flags.  

Here are the following modifications I added to use NVCC instead of gcc:

In Compiler_PlatformPosix.cpp: 202:
std::string compileString = compilerLocation + " " + "-g --compiler-options '-fPIC -fvisibility=hidden -shared' ";

NVCC doesn't natively understand -fPIC and -fvisibility, so we need to throw the --compiler-options flag behind it so nvcc knows to pass those to the native compiler.

Then just set the compiler path with IRuntimeObjectSystem::SetCompilerLocation to nvcc.

Now the problem I was running into was I can't really put complicated macros or includes in any .cu or .cuh files because nvcc would have to compile it.  Thus to get around that I define any cuda code with simple interfaces in a .cuh file, with the actual cuda implementation in a .cu file.
After that I set the .cuh and .cu files as source dependencies of my .cpp file.  This way I can edit a .cu file, then notify rcc of a negligible change to the .cpp file, which will rebuild a new module with the new cuda code.

I simply extended the runtime source dependency concept to allow any file to be added as a source dependency.  For this to work I needed the current implementation to not replace the extension of registered files.

In RuntimeObjectSystem.cpp: 556
if(pathSrc.Extension() == ".cu" || pathSrc.Extension() == ".cuh")
 {
  }else{
  pathSrc.ReplaceExtension( ".cpp" );
 }


Finally I needed a macro to add the .cu files as a dependency of my .cpp file:
In RuntimeSourceDependency.h, add the following:
#define RUNTIME_COMPILER_ADDITIONAL_SOURCE_DEPENDENCY( filename )     namespace { RUNTIME_COMPILER_SOURCEDEPENDENCY_BASE( filename, __COUNTER__ ) }

Now in your .cpp file just add 
RUNTIME_COMPILER_ADDITIONAL_SOURCE_DEPENDENCY("full file path of your cuda files")




Doug Binks

unread,
Jun 19, 2015, 4:07:18 AM6/19/15
to runtimecompi...@googlegroups.com
That sounds excellent - do you have a code repo I can point people to?

I could consider adding CUDA support to the main repo at some point - however I'd need a way to tell when to use NVCC vs another compiler.

--
You received this message because you are subscribed to the Google Groups "Runtime-Compiled C++" group.
To unsubscribe from this group and stop receiving emails from it, send an email to runtimecompiledcpl...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Daniel Moodie

unread,
Jun 29, 2015, 9:27:27 AM6/29/15
to runtimecompi...@googlegroups.com
Hi Doug,

I'm currently working on merging all of my changes into a fork of your main repo.  Previously I was working on a much more outdated version, so it's missing a lot of your newer changes.

It currently doesn't have the NVCC update that I just got working, but it will soon.

As an aside, nvcc automatically knows if a file contains cuda code or not, and will default to the native compiler the source file doesn't contain cuda code.  You can change the native compiler with the -ccbin flag when passed into nvcc.

To unsubscribe from this group and stop receiving emails from it, send an email to runtimecompiledcplusplus+unsub...@googlegroups.com.

Doug Binks

unread,
Jun 29, 2015, 9:45:18 AM6/29/15
to runtimecompi...@googlegroups.com
Sounds good! Many thanks.

To unsubscribe from this group and stop receiving emails from it, send an email to runtimecompiledcpl...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Runtime-Compiled C++" group.
To unsubscribe from this group and stop receiving emails from it, send an email to runtimecompiledcpl...@googlegroups.com.

Daniel Moodie

unread,
Jun 30, 2015, 3:37:21 PM6/30/15
to runtimecompi...@googlegroups.com
Hi Doug,

I just finished merging the Linux code and extending the windows code.

It's currently a little hacky but I'll explain what I've done and how someone else can add it into their own project.

All of my projects are CMake based, so I made a top level CMakeLists.txt file which sets up rcc and integrates it into an external project, it can be found in the root of my fork.
To include it into an external project, the scripts requires some help for linking, and includes.
The following variables need to be set, in the parent cmake file that includes the rcc cmake file.

RCC_INCLUDE_DEPENDENCIES - Include directories used during compile
RCC_LIBRARY_DIRS         -  Directories to be used during linking
IObject_INCLUDE                - Include directory where the user defined IObject.h file can be found

The rcc cmake file now searches for cuda and automatically adds a NVCC_PATH definition.

Typically cuda code is setup in a similar structure to C++ code except with the extension ".cuh" and ".cu" instead of ".h" and ".cpp".  So I made some modifications to the source dependency code to replace ".cuh" extensions with ".cu" instead of ".cpp" and everything kinda fell into place.
All that is needed in a cuda ".cuh" file is the following:

#include "RuntimeInclude.h"
#include "RuntimeSourceDependency.h"
RUNTIME_COMPILER_SOURCEDEPENDENCY
RUNTIME_MODIFIABLE_INCLUDE

Now any ".cpp" files that include the ".cuh" file will recompile with the updated ".cu" code.  So to make a runtime cuda change, just edit the .cu file, then make a trivial edit to the .cpp file interfacing it with the rest of the system.

On my todo list next is to figure out why RCC wont trigger an appropriate recompile to an edit of the ".cu" code, it notices the file change but something is going on with the dependency searching such that it doesn't realize other files are dependent on that ".cu" file.

RCC is used in my EagleEye project to provide recompilation of computer vision processing nodes, which can be found here , example RCC integration can be found in this subfolder.
An example RCC cuda module can be found here
Sounds good! Many thanks.

To unsubscribe from this group and stop receiving emails from it, send an email to runtimecompiledcplusplus+unsub...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Runtime-Compiled C++" group.
To unsubscribe from this group and stop receiving emails from it, send an email to runtimecompiledcplusplus+unsub...@googlegroups.com.

Doug Binks

unread,
Jul 1, 2015, 3:51:50 AM7/1/15
to Runtime-Compiled C++
Sounds excellent.

I'll blog about some of this as soon as I get time, and have a look at whether it can be integrated easily into the main repo. I won't get time for that before next week however.

Sounds good! Many thanks.

To unsubscribe from this group and stop receiving emails from it, send an email to runtimecompiledcpl...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Runtime-Compiled C++" group.
To unsubscribe from this group and stop receiving emails from it, send an email to runtimecompiledcpl...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Runtime-Compiled C++" group.
To unsubscribe from this group and stop receiving emails from it, send an email to runtimecompiledcpl...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages