I am trying to compile a program that contains CUDA files and C (not C++) files using a CMakeLists.
My program is composed with some files .c (without CUDA), one file .c (which calls cuBLAS functions and basic CUDA functions like cudaMalloc and it works) and cuda.cu which contains the following code(I put all the #includes in case any are missing):
#include "cuda_runtime.h"
#include <stdio.h>
#include <stdlib.h>
#include "device_launch_parameters.h"
#include <device_functions.h>
#include <assert.h>
#include <cuda.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <float.h>
#include <sys/time.h>
//#include "cublas_kernels.h"
#define TILE_DIM 32
#define BLOCK_ROWS 8
#define GRID_SIZE 32
#define BLOCK_SIZE 32
__global__ void kernelfunction(float *a, float *b, int n) {
int i = threadIdx.x + blockIdx.x * blockDim.x;
int stride = blockDim.x * gridDim.x;
while (i < n)
{
b[i] = (a[i] > 0.f) ? 1.f : 0.f;
i += stride;
}
}
void function(float *a, float *b, int n)
{
dim3 dimGrid(GRID_SIZE, GRID_SIZE);
dim3 dimBlock(BLOCK_SIZE, BLOCK_SIZE);
kernelfunction<<<dimGrid, dimBlock>>>(a, b, n);
}
This is the CUDA part of the CMakeLists.txt file:
include(FindCUDA)
find_package(CUDA)
if(CUDA_FOUND)
target_link_libraries(myprogram PRIVATE ${CUDA_CUBLAS_LIBRARIES} dl)
target_link_libraries(myprogram PRIVATE ${CUDA_LIBRARIES} dl)
set_target_properties(myprogram PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
endif()
when I try to compile it with the CMakeLists file I get the following error:
cuda.cu: undefined reference to 'threadIdx'
cuda.cu: undefined reference to 'blockIdx'
cuda.cu: undefined reference to 'blockDim'
cuda.cu: undefined reference to 'gridDim'
I guess my makefile is incomplete since if I compile the file (cuda.cu) separately with nvcc it compiles without problems.
My nvcc version:
Cuda compilation tools, release 9.1, V9.1.85
My CMake version:
cmake version 3.10.2
CUDA is a first-class language in your version of cmake (iirc this was added in cmake 3.9). However, CUDA support is not enabled by default when you create a new project.
If your project is defined like project(my_project_name)
then simply specify the languages you're using there, e.g. project(my_objrect_name C CUDA)
. Then cmake will use the nvidia compiler to compile any file with a .cu
extension