Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to define a utilization function which can be called both by a CUDA kernel and a regular C++ function
    primarykey
    data
    text
    <p>I'm working on a project which involves a lot of mathematics. For a single target problem( for example, gradient calculation), we always have two versions of implementations: one CPU version and one CUDA version. </p> <p>Now the CPU version is written in regular C++ and the kernel version is written in CUDA. If I want to define a small function, for example, vec_weight which returns the weight of a vector, I have to write one for the CPU compiled by g++ for the CPU version and one cuda version which has an "__<strong>device__</strong>" before it compiled by nvcc.</p> <p>I'm not trying to define a "__<strong>device__</strong> __<strong>host__</strong>" function here. What I want is kind of a library which can be called by a regular C++ function and a CUDA kernel. I tried to use the "<strong>__CUDACC__</strong>" macro but it didn't work. </p> <p>Because we will have a lot of small utilization functions needed by both CPU version and GPU version, I think it is reasonable to combine them in to one.</p> <p>Writing the CPU version in .cu instead of .cpp may solve our problem but this is not what we want. </p> <p>So what should I do?</p> <p>Here is the code segment:</p> <p>head.h:</p> <pre><code> 1 #ifndef HEAD_H 2 #define HEAD_H 3 #ifdef __cplusplus 4 extern "C"{ 5 #endif 6 __device__ __host__ void myprint(); 7 #ifdef __cplusplus 8 } 9 #endif 10 #endif </code></pre> <p>head.cu:</p> <pre><code> 1 #include "head.h" 2 #include &lt;stdio.h&gt; 3 void myprint(){ 4 // do something here 5 } </code></pre> <p>main.cpp</p> <pre><code> 1 #include "head.h" 2 int main(){ 3 myprint(); 4 } </code></pre> <p>I compiled the head.cu by:</p> <pre><code>nvcc -c head.cu </code></pre> <p>Link them together by :</p> <pre><code>g++ main.cpp head.o -o main ( The reason that I didn't use nvcc here is that we are using the PGI's pgcpp in our project and we need it to talk to the PGI's OMP library. But I'm sure that there is something wrong here but I don't know how to fix that. ) </code></pre> <p>The error messages:</p> <pre><code>In file included from main.cpp:18: head.h:6: error: ‘__device__’ does not name a type main.cpp: In function ‘int main()’: main.cpp:20: error: ‘myprint’ was not declared in this scope </code></pre> <p>So I'm pretty sure that g++ couldn't recognize the "__<strong>device__</strong>" prefix here. But our project demands us to use PGCPP to compile the cpp file because this is the only way we can have omp directives works fine both in Fortran and C( Our project mixes C/C++, Fortran and CUDA). But here even the g++ can not work, so I think we have fix this first. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload