Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Libraries usually contain the code compiled for the target processor (CPU or GPU) - hence you need to compile it through NVCC. Hence, you may as well put it in .cu files.</p> <p>If you can release sources then you can put the code in headers and include them into .cpp or .cu files.</p> <p><strong>Update</strong></p> <p>This is what I did in my code (<code>hdf</code> function can be called from the host and device):</p> <p>File <code>devhost.h</code></p> <pre><code>#ifndef FUNCTIONS_H_ #define FUNCTIONS_H_ int myhost_functin(int); #endif </code></pre> <p>File <code>cu.cu</code>:</p> <pre><code>__host__ __device__ int hdf(int a) { return a + 4; } int myhost_function(int a) { return hdf(a); } __global__ void kern(int *data) { data[threadIdx.x] = hdf(data[threadIdx.x]); } </code></pre> <p>File <code>cpp.cpp</code>:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include "devhost.h" int main() { printf ("%d\n", myhost_function(5)); return 0; } </code></pre> <p>This is how I compile and link it:</p> <pre><code>nvcc -c cu.cu gcc -c cpp.cpp gcc cpp.o cu.o -lcudart -L/usr/local/cuda-5.5/lib64 </code></pre> <p>Note that you need to link with CUDART as CU file has device call.</p> <p><strong>Update 2</strong></p> <p>Slightly less elegant approach that still seems to compile is having something like following in your header file:</p> <pre><code>#ifdef __CUDACC__ __host__ __device__ #endif static int anotherfunction(int a) { return a * 50; } </code></pre> <p>In this case you will have a copy of your code in every translation unit, this will increase your compilation time and might increase the executable size.</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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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