# How to run GPU functions?

**URL:** <https://root-forum.cern.ch/t/how-to-run-gpu-functions/17720>\
**Category:** ROOT\
**Created:** [June 4, 2014, 4:30am UTC](https://root-forum.cern.ch/t/how-to-run-gpu-functions/17720 "2014-06-04T04:30:58Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![lebigot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/lebigot/32/11597_2.png) [@lebigot](https://root-forum.cern.ch/u/lebigot)\
**Post date:** [June 4, 2014, 4:30am UTC](https://root-forum.cern.ch/t/how-to-run-gpu-functions/17720/1 "2014-06-04T04:30:58Z")

</div>

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](http://stackoverflow.com/questions/23807905/compiling-part-of-a-c-program-for-gpu).

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

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [June 4, 2014, 7:27am UTC](https://root-forum.cern.ch/t/how-to-run-gpu-functions/17720/2 "2014-06-04T07:27:51Z")

</div>

Looking at [your StackOverflow question](http://stackoverflow.com/questions/23807905/compiling-part-of-a-c-program-for-gpu), 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:

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

```

… modify your “Test.C” macro:

```auto
{ // 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:

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

```

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

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

```

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

```auto
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"`.

---

<div class="post-metadata">

**Author:** ![lebigot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/lebigot/32/11597_2.png) [@lebigot](https://root-forum.cern.ch/u/lebigot)\
**Post date:** [June 5, 2014, 1:06am UTC](https://root-forum.cern.ch/t/how-to-run-gpu-functions/17720/3 "2014-06-05T01:06:24Z")

</div>

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](http://stackoverflow.com/questions/23807905/compiling-part-of-a-c-program-for-gpu/24116393#24116393).

---

<div class="post-metadata">

**Author:** ![pappo](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/9d8465/32.png) [@pappo](https://root-forum.cern.ch/u/pappo)\
**Post date:** [March 1, 2018, 9:31am UTC](https://root-forum.cern.ch/t/how-to-run-gpu-functions/17720/4 "2018-03-01T09:31:57Z")

</div>

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

```auto
#include <stdio.h>

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

}

```

I compile it by doing

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

```

I create my interface file TestCompiled.h++

```auto
#ifdef __cplusplus
extern "C" {
#endif

  void TestCompiled(void);

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

```

then I have my macro TestRoot.C

```auto
#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

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

```

thanks for your help

ema

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [March 1, 2018, 10:01am UTC](https://root-forum.cern.ch/t/how-to-run-gpu-functions/17720/5 "2018-03-01T10:01:56Z")

</div>

Your “TestCompiled.cu” should look like:

```auto
#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.

---

<div class="post-metadata">

**Author:** ![pappo](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/9d8465/32.png) [@pappo](https://root-forum.cern.ch/u/pappo)\
**Post date:** [March 1, 2018, 10:27am UTC](https://root-forum.cern.ch/t/how-to-run-gpu-functions/17720/6 "2018-03-01T10:27:20Z")

</div>

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]!

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [March 1, 2018, 10:43am UTC](https://root-forum.cern.ch/t/how-to-run-gpu-functions/17720/7 "2018-03-01T10:43:23Z")

</div>

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”).

---

<div class="post-metadata">

**Author:** ![pappo](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/9d8465/32.png) [@pappo](https://root-forum.cern.ch/u/pappo)\
**Post date:** [March 1, 2018, 10:53am UTC](https://root-forum.cern.ch/t/how-to-run-gpu-functions/17720/8 "2018-03-01T10:53:13Z")

</div>

Still the same problem

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

i’ve no idea of what is missing
