How to run GPU functions?

My goal is to have ROOT use GPU for part of our code.

With my students, we created compiled a simple program with nvcc, and tried to load it from within ROOT, but this did not work. Compiling the simple program with ROOT instead did work. Details can be found on a question I wrote on StackOverflow.

What can we do in order to compile some of the GPU code with nvcc, and run it from ROOT?

Looking at your StackOverflow question, the first problem is that you do not create appropriate “CINT/CLING dictionary” for your C/C++ functions / classes / …, which you afterwards try to use in CINT/CLING interpreted macros.
When you use ACLiC to pre-compile your macro (e.g. “root TestCompiled.C++”) then it automatically creates the required dictionary “on the fly”.

The simplest trial that you can perform is …
… create a “TestCompiled.h” interface file:

#ifdef __cplusplus
extern "C" {
#endif
  
  void TestCompiled(void);
  
#ifdef __cplusplus
} /* end of extern "C" */
#endif

… modify your “Test.C” macro:

{ // Test.C ROOT CINT/CLING unnamed macro (interpreted)
  Int_t check, error;
  check = gROOT->LoadMacro("TestCompiled_C.so", &error);
  std::cout << "_C.so check " << check << " error " << error << std::endl;
  check = gROOT->LoadMacro("TestCompiled.h++", &error);
  std::cout << "_h.so check " << check << " error " << error << std::endl;
  // execute the compiled function
#if ROOT_VERSION_CODE >= ROOT_VERSION(6,00,00)
  gROOT->ProcessLine("TestCompiled();"); // ROOT 6 (CLING)
  // gInterpreter->ProcessLine("TestCompiled();"); // ROOT 6 (CLING)
#else
  TestCompiled(); // ROOT 5 (CINT)
#endif
}

… and then try it:

root -b -l -n -q Test.C

BTW. You can easily test the above idea without “nvcc”. Just create a simple “TestCompiled.C” file:

#include <cstdio>
extern "C" void TestCompiled(void) { printf("test\n"); }

and compile it with, for example, “g++”:

g++ -fPIC -shared -o TestCompiled_C.so TestCompiled.C

BTW. As “nvcc” is a C++ compiler driver which will possibly internally call the same real C++ compiler as ROOT uses, you could maybe get completely rid of any extern "C".

This worked. Thank you so much!

Feel free to post this answer on StackOverflow. Otherwise I’ll do it and cross-reference your answer here.

PS: I posted a version of this answer at stackoverflow.com/questions/2380 … 3#24116393.

hello,

i think i got the point but actually i don’t understand why it’s not working in my case. So I have my cuda program

#include <stdio.h>

int main(void) {
 printf("Hello, World!\n");
return 0;

}

I compile it by doing

nvcc --compiler-options '-fPIC' -shared -o TestCompiled_C.so TestCompiled.cu

I create my interface file TestCompiled.h++

#ifdef __cplusplus
extern "C" {
#endif

  void TestCompiled(void);

#ifdef __cplusplus
} /* end of extern "C" */
#endif

then I have my macro TestRoot.C

#include "TF1.h"


void TestRoot(){

  TF1 *f1=new TF1("f","gaus(0)",-10,10);
  f1->SetParameters(100,0,2);
  f1->Draw();
  Int_t check, error;
  check = gROOT->LoadMacro("TestCompiled_C.so", &error);
  std::cout << "_C.so check " << check << " error " << error << std::endl;
  check = gROOT->LoadMacro("TestCompiled.h++", &error);
  std::cout << "_h.so check " << check << " error " << error << std::endl;
  TestCompiled();

}

when i execute my root macro i got

error: use of undeclared identifier 'TestCompile'
  TestCompiled();

thanks for your help

ema

Your “TestCompiled.cu” should look like:

#include <cstdio>
extern "C" void TestCompiled(void) {
  printf("Hello, World!\n");
  return;
}

You also need to improve your “TestRoot.C” macro (to make it aware of ROOT 5 versus ROOT 6 differences) -> see the "Test.C” macro in my first post above.

Thanks for your reply, now it seems to be ok, but anyway it doesn’t execute the program

IncrementalExecutor::executeFunction: symbol ‘TestCompiled’ unresolved while linking [cling interface function]!

Try to run the full example from my first post above (first use “g++” instead of “nvcc” as I did, then try to switch to “nvcc”).

Still the same problem

IncrementalExecutor::executeFunction: symbol ‘TestCompiled’ unresolved while linking [cling interface function]!

i’ve no idea of what is missing