Hi,
I’m trying to call a kernel from an C++ source to substitute the existing CPU-Code through a kernel. As I’m new to CUDA, problems are hard-wired. I’ve tried to follow some examples found in the internet, but most were demo-like and i was not able to take them to my problem.
On my current try, I’ve following files:
kernel_nomm.h which contains the necessary imports
#ifndef KERNEL_NOMM_H_
#define KERNEL_NOMM_H_
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cuda.h>
#include <cuda_runtime.h>
//extern void kernel_wrapper(int*);
#endif /* CUDAHEADER_H_ */
kernel_nomm.cu which contains a demo-kernel with a dummy functionality
#include "kernel_nomm.h"
#include <stdio.h>
extern "C" void kernel_wrapper(int *a);
__global__ void kernel(int *a)
{
int tx = threadIdx.x;
switch( tx )
{
case 0:
a[tx] = a[tx] + 2;
break;
case 1:
a[tx] = a[tx] + 3;
break;
}
}
void kernel_wrapper(int *a)
{
int *d_a;
dim3 threads( 2, 1 );
dim3 blocks( 1, 1 );
cudaMalloc( (void **)&d_a, sizeof(int) * 2 );
cudaMemcpy( d_a, a, sizeof(int) * 2, cudaMemcpyHostToDevice );
printf("Kernel starts");
kernel<<< blocks, threads >>>( d_a );
printf("Kernel ends");
cudaMemcpy( a, d_a, sizeof(int) * 2, cudaMemcpyDeviceToHost );
printf( "Finish kernel wrapper\n" );
cudaFree(d_a);
}
/*
int main(int argc, char *argv[])
{
int *a = (int *)malloc(sizeof(int) * 2);
a[0] = 2;
a[1] = 3;
printf( "a[0]: %d, a[1]: %d\n", a[0], a[1] );
kernel_wrapper(a);
printf( "a[0]: %d, a[1]: %d\n", a[0], a[1] );
free(a);
return 0;
}*/
And a cpp class which should call the function:
ebwt_search.cpp (only snippets)
//include kernels
#include "kernel_nomm.h"
#ifdef CHUD_PROFILING
#include <CHUD/CHUD.h>
#endif
using namespace std;
using namespace seqan;
extern void kernel_wrapper(int *a);
template<typename TStr>
static void driver(const char * type,
const string& ebwtFileBase,
const string& query,
const vector<string>& queries,
const vector<string>& qualities,
const string& outfile)
{
...
//#### MH Test Kernel to run ###
int *a = (int *)malloc(sizeof(int) * 2);
a[0] = 2;
a[1] = 3;
kernel_wrapper(a);
Eclipse underlines the last line and says: undefined reference to `kernel_wrapper(int*)', but its possible to go to the definition by Ctrl+Click. If I try to compile:
ebwt_search.cpp:2511: undefined reference to `kernel_wrapper(int*)'
Because the Makefile is very complex, I would prefer to make something like a library or object that i can give g++ as an argument, to keep the current Makefile as much as possible. My very simple (and maybe stupid) approach was to tell g++ to use the object file as a library -L kernel_nomm.o (generated by nvcc -c kernel_nomm.cu)
ANY Ideas are welcome,
thx miccim
######################
Solved - see other Posts