Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiling and Linking pure C and CUDA code [warning: implicit declaration of function]

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.

like image 648
anupshrestha Avatar asked Aug 16 '26 00:08

anupshrestha


1 Answers

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

like image 174
user3629249 Avatar answered Aug 18 '26 15:08

user3629249



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!