Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I call CUDA runtime function from C++ code not compiled by nvcc?

Is there any way I can call CUDA runtime function calls such as

cudaMemcpy(...);

in a .cpp file, compiled with a regular C++ compiler?

like image 338
small_potato Avatar asked Sep 28 '10 09:09

small_potato


People also ask

What is Nvcc used for?

NVCC is a compiler driver which works by invoking all the necessary tools and compilers like cudacc, g++, cl, etc. NVCC can output either C code (CPU Code) that must then be compiled with the rest of the application using another tool or PTX or object code directly.

Can Nvcc compile cpp files?

cu files, since NVCC 2. x actually compiles these files as c++ files. c++ compilation enforces a different syntax for function symbols in object code, which resolved in linking errors of the type “undefined reference” when linking .


1 Answers

EDIT: There was an example here but it's not longer found, but most of the example was copied below.

The caller C (but could be C++)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cuda.h>

extern void kernel_wrapper(int *a, int *b);

int main(int argc, char *argv[])
{
   int a = 2;
   int b = 3;

   kernel_wrapper(&a, &b);

   return 0;
}

The Callee (CUDA)

__global__ void kernel(int *a, int *b)
{
   int tx = threadIdx.x;

   switch( tx )
   {
case 0:
    *a = *a + 10;
    break;
case 1:
    *b = *b + 3;
    break;
default:
    break;
   }
}

void kernel_wrapper(int *a, int *b)
{
   int *d_1, *d_2;
   dim3 threads( 2, 1 );
   dim3 blocks( 1, 1 );

   cudaMalloc( (void **)&d_1, sizeof(int) );
   cudaMalloc( (void **)&d_2, sizeof(int) );

   cudaMemcpy( d_1, a, sizeof(int), cudaMemcpyHostToDevice );
   cudaMemcpy( d_2, b, sizeof(int), cudaMemcpyHostToDevice );

   kernel<<< blocks, threads >>>( a, b );

   cudaMemcpy( a, d_1, sizeof(int), cudaMemcpyDeviceToHost );
   cudaMemcpy( b, d_2, sizeof(int), cudaMemcpyDeviceToHost );

   cudaFree(d_1);
   cudaFree(d_2);
}
like image 54
Preet Sangha Avatar answered Nov 15 '22 15:11

Preet Sangha