Hi everyone! I am currently trying to embed some cuda kernel in a C++ program using the wrapper function method.
Here is my testing code:
kernel_wrapper.cu
#include <stdio.h>
extern "C" void kernel_wrapper(int *a, int *b);
__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 >>>( d_1, d_2 );
cudaMemcpy( a, d_1, sizeof(int), cudaMemcpyDeviceToHost );
cudaMemcpy( b, d_2, sizeof(int), cudaMemcpyDeviceToHost );
cudaFree(d_1);
cudaFree(d_2);
}
main.cpp
#include <stdio.h>
#include <stdlib.h>
extern "C" void kernel_wrapper(int *a, int *b);
int main(int argc, char *argv[])
{
int a = 2;
int b = 3;
printf("Input: a = %d, b = %d\n",a,b);
kernel_wrapper(&a, &b);
printf("Ran: a = %d, b = %d\n",a,b);
return 0;
}
when i try to compile the .cu file, i get the following error
LINK : fatal error LNK1561: entry point must be defined
I did some resarch on the on internet and then understood that it has connection with the non existence of a main function. But if i do write a main function it would be impossible to call the wrapper function since its parameters a ant b won’t be recognized.
Any ideas ?