Bui Thanh Binh
Bui Thanh Binh

Reputation: 103

CUDA C++ Linking error undefined reference threadIdx.x

Hello trying to parallelize a Lattice Boltzmann Solver on CUDA. Somehow i am getting an error while linking the object files together. The objects compiles without an error.

LBMSolver.o: In function >LBMSolver::calcMoments_GPU(LBMUniformGrid2D::LBMGridNode**, int)': tmpxft_00004b33_00000000-3_LBMSolver.cudafe1.cpp:(.text+0x939): undefined >reference toblockDim' tmpxft_00004b33_00000000-3_LBMSolver.cudafe1.cpp:(.text+0x93f): undefined >reference to blockIdx' tmpxft_00004b33_00000000-3_LBMSolver.cudafe1.cpp:(.text+0x948): undefined >reference tothreadIdx' tmpxft_00004b33_00000000-3_LBMSolver.cudafe1.cpp:(.text+0x953): undefined >reference to blockDim' tmpxft_00004b33_00000000-3_LBMSolver.cudafe1.cpp:(.text+0x959): undefined >reference toblockIdx' tmpxft_00004b33_00000000-3_LBMSolver.cudafe1.cpp:(.text+0x962): undefined >reference to `threadIdx'

Here is my Makefile:

OBJS = main.o LBMSolver.o LBMUniformGrid2D.o LBMBoundaryConditions.o   writeVTK.o
CC = g++
NVCC = nvcc
CFLAGS = -Wall -c 
LFLAGS = -Wall
XCOMPILER = -Xcompiler "-Wall"
NVCFLAGS = -c -G -g -ccbin=$(CC) $(XCOMPILER)
NVLFLAGS = -G -g -ccbin=$(CC) $(XCOMPILER)


main : $(OBJS)
    $(NVCC) $(NVLFLAGS) $(OBJS) -o main 

main.o : main.cu LBMSolver.h writeVTK.h
    $(NVCC) $(NVCFLAGS) main.cu

LBMSolver.o : LBMSolver.cu LBMSolver.h LBMUniformGrid2D.h LBMBoundaryConditions.h
    $(NVCC) $(NVCFLAGS) LBMSolver.cu

LBMBoundaryConditions.o : LBMBoundaryConditions.cu LBMBoundaryConditions.h LBMSolver.h
    $(NVCC) $(NVCFLAGS) LBMBoundaryConditions.cu

LBMUniformGrid2D.o : LBMUniformGrid2D.cpp LBMUniformGrid2D.h
    $(CC) $(CFLAGS) LBMUniformGrid2D.cpp

writeVTK.o : writeVTK.h writeVTK.cpp
    $(CC) $(CFLAGS) writeVTK.cpp

clean_obj: 
    \rm *.o main

Function and headerfile that gives the error:

#pragma once
#include "LBMUniformGrid2D.h"
#include <cuda.h>

#ifdef __CUDACC__
#define CUDA_HOSTDEV __host__ __device__
#else
#define CUDA_HOSTDEV
#endif

class LBMSolver{
...
CUDA_HOSTDEV void calcMoments_GPU(LBMUniformGrid2D::LBMGridNode **field, int nx);

__host__ __device__ void LBMSolver::calcMoments_GPU(LBMUniformGrid2D::LBMGridNode **field, int nx){
    int x = blockDim.x * blockIdx.x + threadIdx.x;
    int y = blockDim.y * blockIdx.y + threadIdx.y;

    LBMUniformGrid2D::LBMGridNode *node;
    node = field[x+y*nx];
    calc_rho_GPU(node);
    calc_ux_GPU(node);
    calc_uy_GPU(node);
    calc_v_GPU(node);
}

Upvotes: 1

Views: 2841

Answers (1)

talonmies
talonmies

Reputation: 72350

You cannot define that function containing device code as __host__, because device specific features are not supported in host code. Remove that and things will probably compile correctly.

Upvotes: 1

Related Questions