3

I am trying to compile and link .c and .cu files and I am getting a warning

 warning: implicit declaration of function

I have a function in the .cu file that I need to call from the .c file. The .c file is compiled using gcc and .cu file is compiled using nvcc compiler. Since the header file for the .cu file contains built in cuda data types I cannot include that in the .c file. I am still able to compile and link all the files but I want to get rid of the warning which I am not able to. The basic structure of the code is:

gpu.cu
    void fooInsideCuda();

cpu.c
    fooInsideCuda(); //calling function in gpu.cu

Any help or suggestion would be much appreciated.

anupshrestha
  • 236
  • 5
  • 19
  • 3
    On the face of it, you could simply use `extern void fooInsideCuda(void);` either in `cpu.c` itself or in a header that it includes. Ideally, the header (possibly a new one) would be used in `gpu.cu` as well as in `cpu.c` to ensure cross-checking. – Jonathan Leffler May 14 '15 at 21:33

1 Answers1

2

this link: https://devtalk.nvidia.com/default/topic/388072/calling-cuda-functions-from-a-c-file/

answers your question:,. basically:

in the .c file

#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;
}

and in the .cu file;

__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);
}

then a .h file similar to this:

#ifndef __B__
#define __B__

#include "cuda.h"
#include "cuda_runtime.h"

extern "C" void kernel_wrapper(int *a, int *b);
#endif

also note that the .cu compiler uses C++ conventions

so will need something like the following in the .cu file:

extern "C" void A(void)
{
    .......
}

so 'C' conventions are used

user3629249
  • 16,402
  • 1
  • 16
  • 17