Search code examples
c++cudagtkgladensight

Using Glade with CUDA compiler (Nvidia Eclipse Nsight) -> which NVCC linker settings?


I'm starting a new project which involves CUDA, and I would like to use Glade (v3) for the GUI.

I'm trying to implement a Glade project in a basic CUDA template project, and here is my code so far:

#include <iostream>
#include <numeric>
#include <stdlib.h>
#include <gtk/gtk.h>

static void CheckCudaErrorAux (const char *, unsigned, const char *, cudaError_t);
#define CUDA_CHECK_RETURN(value) CheckCudaErrorAux(__FILE__,__LINE__, #value, value)

/**
 * CUDA kernel that computes reciprocal values for a given vector
 */
__global__ void reciprocalKernel(float *data, unsigned vectorSize) {
    unsigned idx = blockIdx.x*blockDim.x+threadIdx.x;
    if (idx < vectorSize)
        data[idx] = 1.0/data[idx];
}

/**
 * Host function that copies the data and launches the work on GPU
 */
float *gpuReciprocal(float *data, unsigned size)
{
    float *rc = new float[size];
    float *gpuData;

    CUDA_CHECK_RETURN(cudaMalloc((void **)&gpuData, sizeof(float)*size));
    CUDA_CHECK_RETURN(cudaMemcpy(gpuData, data, sizeof(float)*size, cudaMemcpyHostToDevice));

    static const int BLOCK_SIZE = 256;
    const int blockCount = (size+BLOCK_SIZE-1)/BLOCK_SIZE;
    reciprocalKernel<<<blockCount, BLOCK_SIZE>>> (gpuData, size);

    CUDA_CHECK_RETURN(cudaMemcpy(rc, gpuData, sizeof(float)*size, cudaMemcpyDeviceToHost));
    CUDA_CHECK_RETURN(cudaFree(gpuData));
    return rc;
}

float *cpuReciprocal(float *data, unsigned size)
{
    float *rc = new float[size];
    for (unsigned cnt = 0; cnt < size; ++cnt) rc[cnt] = 1.0/data[cnt];
    return rc;
}


void initialize(float *data, unsigned size)
{
    for (unsigned i = 0; i < size; ++i)
        data[i] = .5*(i+1);
}

int main(int argc, char *argv[])
{
    static const int WORK_SIZE = 65530;
    float *data = new float[WORK_SIZE];

    initialize (data, WORK_SIZE);

    GtkBuilder      *builder;
    GtkWidget       *window;

    gtk_init(&argc, &argv);

    builder = gtk_builder_new();
    gtk_builder_add_from_file (builder, "GladeTest01.glade", NULL);

    window = GTK_WIDGET(gtk_builder_get_object(builder, "applicationwindow1"));
    gtk_builder_connect_signals(builder, NULL);

    g_object_unref(builder);

    gtk_widget_show(window);
    gtk_main();

    /*
    float *recCpu = cpuReciprocal(data, WORK_SIZE);
    float *recGpu = gpuReciprocal(data, WORK_SIZE);
    float cpuSum = std::accumulate (recCpu, recCpu+WORK_SIZE, 0.0);
    float gpuSum = std::accumulate (recGpu, recGpu+WORK_SIZE, 0.0);

    /* Verify the results *//*
    std::cout<<"gpuSum = "<<gpuSum<< " cpuSum = " <<cpuSum<<std::endl;

    /* Free memory *//*
    delete[] data;
    delete[] recCpu;
    delete[] recGpu;
*/
    return 0;
}

// called when window is closed
void on_QuitApp()
{
    gtk_main_quit();
}

At compilation, I'm unable to understand why I got these errors :

/home/fabio/AppTests/GladeTest01/Debug/../src/GladeTest.cu:72: undefined reference to `gtk_init'
/home/fabio/AppTests/GladeTest01/Debug/../src/GladeTest.cu:74: undefined reference to `gtk_builder_new'
/home/fabio/AppTests/GladeTest01/Debug/../src/GladeTest.cu:75: undefined reference to `gtk_builder_add_from_file'
/home/fabio/AppTests/GladeTest01/Debug/../src/GladeTest.cu:77: undefined reference to `gtk_widget_get_type'
/home/fabio/AppTests/GladeTest01/Debug/../src/GladeTest.cu:77: undefined reference to `gtk_builder_get_object'
/home/fabio/AppTests/GladeTest01/Debug/../src/GladeTest.cu:77: undefined reference to `g_type_check_instance_cast'
/home/fabio/AppTests/GladeTest01/Debug/../src/GladeTest.cu:78: undefined reference to `gtk_builder_connect_signals'
/home/fabio/AppTests/GladeTest01/Debug/../src/GladeTest.cu:80: undefined reference to `g_object_unref'
/home/fabio/AppTests/GladeTest01/Debug/../src/GladeTest.cu:82: undefined reference to `gtk_widget_show'
/home/fabio/AppTests/GladeTest01/Debug/../src/GladeTest.cu:83: undefined reference to `gtk_main'
./src/GladeTest.o: In function `on_QuitApp()':
/home/fabio/AppTests/GladeTest01/Debug/../src/GladeTest.cu:105: undefined reference to `gtk_main_quit'
collect2: error: ld returned 1 exit status
make: *** [GladeTest] Error 1

As everything seems to be defined in gtk.h and all the other includes in it... What am I missing here ?

Is somebody able to give me some directions or to tell me what I am doing wrong?

Edit : Here is the compilation commands used :

Invoking: NVCC Compiler
/usr/local/cuda-7.5/bin/nvcc -I/usr/include/gtk-3.0 -I/usr/include/atk-1.0 -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/cairo -I/usr/include/pango-1.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I/usr/include/glib-2.0 -G -g -O3 -gencode arch=compute_52,code=sm_52 -m64 -odir "src" -M -o "src/GladeTest.d" "../src/GladeTest.cu"
/usr/local/cuda-7.5/bin/nvcc -I/usr/include/gtk-3.0 -I/usr/include/atk-1.0 -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/cairo -I/usr/include/pango-1.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I/usr/include/glib-2.0 -G -g -O3 --compile --relocatable-device-code=false -gencode arch=compute_52,code=compute_52 -gencode arch=compute_52,code=sm_52 -m64  -x cu -o  "src/GladeTest.o" "../src/GladeTest.cu"
Finished building: ../src/GladeTest.cu

Building target: GladeTest
Invoking: NVCC Linker
/usr/local/cuda-7.5/bin/nvcc --cudart static --relocatable-device-code=false -gencode arch=compute_52,code=compute_52 -gencode arch=compute_52,code=sm_52 -m64 -link -o  "GladeTest"  ./src/GladeTest.o    

If I understood correctly the answers, all the -I options should be in the nvcc linker call and not in the nvcc compiler command ?

Edit 2 : Here is one of the many variants I tried. With the same results everytime :

Invoking: NVCC Compiler
/usr/local/cuda-7.5/bin/nvcc -I/usr/include/gtk-3.0 -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I/usr/include/pango-1.0 -I/usr/include/cairo -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/atk-1.0 -G -g -O3 -gencode arch=compute_52,code=sm_52 -m64 -odir "src" -M -o "src/GladeTest.d" "../src/GladeTest.cu"
/usr/local/cuda-7.5/bin/nvcc -I/usr/include/gtk-3.0 -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I/usr/include/pango-1.0 -I/usr/include/cairo -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/atk-1.0 -G -g -O3 --compile --relocatable-device-code=false -gencode arch=compute_52,code=compute_52 -gencode arch=compute_52,code=sm_52 -m64  -x cu -o  "src/GladeTest.o" "../src/GladeTest.cu"
Finished building: ../src/GladeTest.cu

Building target: GladeTest
Invoking: NVCC Linker
/usr/local/cuda-7.5/bin/nvcc --cudart static -L/usr/include/gtk-3.0 -L/usr/include/glib-2.0 -L/usr/lib/x86_64-linux-gnu/glib-2.0/include -L/usr/include/pango-1.0 -L/usr/include/cairo -L/usr/include/gdk-pixbuf-2.0 -L/usr/include/atk-1.0 -lGL -lGLU -lglut  --relocatable-device-code=false -gencode arch=compute_52,code=compute_52 -gencode arch=compute_52,code=sm_52 -m64 -link -o  "GladeTest"  ./src/GladeTest.o

Solution

  • It is important to realize that the source of the errors you are seeing is linking, not compilation, and the reason will be that you are not linking the required GTK libraries.

    I was able to produce an executable from the code you posted (with a tiny modification) against GTK2 on a Ubuntu system like this:

    $ nvcc -arch=sm_52 -Xcompiler="`pkg-config --cflags gtk+-2.0`" cuda_glade.cu `pkg-config --libs gtk+-2.0`
    
    $ ldd a.out
        linux-vdso.so.1 =>  (0x00007ffdf351c000)
        libgtk-x11-2.0.so.0 => /usr/lib/x86_64-linux-gnu/libgtk-x11-2.0.so.0 (0x00007f85eece0000)
        libgobject-2.0.so.0 => /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0 (0x00007f85eea8e000)
        librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007f85ee886000)
        libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f85ee668000)
        libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f85ee464000)
        libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f85ee15f000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f85edd9a000)
        libgdk-x11-2.0.so.0 => /usr/lib/x86_64-linux-gnu/libgdk-x11-2.0.so.0 (0x00007f85edae7000)
        libgmodule-2.0.so.0 => /usr/lib/x86_64-linux-gnu/libgmodule-2.0.so.0 (0x00007f85ed8e2000)
        libpangocairo-1.0.so.0 => /usr/lib/x86_64-linux-gnu/libpangocairo-1.0.so.0 (0x00007f85ed6d5000)
        libX11.so.6 => /usr/lib/x86_64-linux-gnu/libX11.so.6 (0x00007f85ed3a0000)
        libXfixes.so.3 => /usr/lib/x86_64-linux-gnu/libXfixes.so.3 (0x00007f85ed199000)
        libatk-1.0.so.0 => /usr/lib/x86_64-linux-gnu/libatk-1.0.so.0 (0x00007f85ecf77000)
        libcairo.so.2 => /usr/lib/x86_64-linux-gnu/libcairo.so.2 (0x00007f85ecc6c000)
        libgdk_pixbuf-2.0.so.0 => /usr/lib/x86_64-linux-gnu/libgdk_pixbuf-2.0.so.0 (0x00007f85eca4a000)
        libgio-2.0.so.0 => /usr/lib/x86_64-linux-gnu/libgio-2.0.so.0 (0x00007f85ec6d7000)
        libpangoft2-1.0.so.0 => /usr/lib/x86_64-linux-gnu/libpangoft2-1.0.so.0 (0x00007f85ec4c2000)
        libpango-1.0.so.0 => /usr/lib/x86_64-linux-gnu/libpango-1.0.so.0 (0x00007f85ec274000)
        libfontconfig.so.1 => /usr/lib/x86_64-linux-gnu/libfontconfig.so.1 (0x00007f85ec038000)
        libglib-2.0.so.0 => /lib/x86_64-linux-gnu/libglib-2.0.so.0 (0x00007f85ebd30000)
        libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f85eba29000)
        libffi.so.6 => /usr/lib/x86_64-linux-gnu/libffi.so.6 (0x00007f85eb821000)
        /lib64/ld-linux-x86-64.so.2 (0x000055d54fa62000)
        libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f85eb60a000)
        libXrender.so.1 => /usr/lib/x86_64-linux-gnu/libXrender.so.1 (0x00007f85eb400000)
        libXinerama.so.1 => /usr/lib/x86_64-linux-gnu/libXinerama.so.1 (0x00007f85eb1fd000)
        libXi.so.6 => /usr/lib/x86_64-linux-gnu/libXi.so.6 (0x00007f85eafec000)
        libXrandr.so.2 => /usr/lib/x86_64-linux-gnu/libXrandr.so.2 (0x00007f85eade2000)
        libXcursor.so.1 => /usr/lib/x86_64-linux-gnu/libXcursor.so.1 (0x00007f85eabd8000)
        libXcomposite.so.1 => /usr/lib/x86_64-linux-gnu/libXcomposite.so.1 (0x00007f85ea9d4000)
        libXdamage.so.1 => /usr/lib/x86_64-linux-gnu/libXdamage.so.1 (0x00007f85ea7d1000)
        libXext.so.6 => /usr/lib/x86_64-linux-gnu/libXext.so.6 (0x00007f85ea5bf000)
        libfreetype.so.6 => /usr/lib/x86_64-linux-gnu/libfreetype.so.6 (0x00007f85ea31b000)
        libxcb.so.1 => /usr/lib/x86_64-linux-gnu/libxcb.so.1 (0x00007f85ea0fc000)
        libpixman-1.so.0 => /usr/lib/x86_64-linux-gnu/libpixman-1.so.0 (0x00007f85e9e53000)
        libpng12.so.0 => /lib/x86_64-linux-gnu/libpng12.so.0 (0x00007f85e9c2d000)
        libxcb-shm.so.0 => /usr/lib/x86_64-linux-gnu/libxcb-shm.so.0 (0x00007f85e9a2a000)
        libxcb-render.so.0 => /usr/lib/x86_64-linux-gnu/libxcb-render.so.0 (0x00007f85e9820000)
        libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007f85e9607000)
        libselinux.so.1 => /lib/x86_64-linux-gnu/libselinux.so.1 (0x00007f85e93e3000)
        libresolv.so.2 => /lib/x86_64-linux-gnu/libresolv.so.2 (0x00007f85e91c8000)
        libharfbuzz.so.0 => /usr/lib/x86_64-linux-gnu/libharfbuzz.so.0 (0x00007f85e8f73000)
        libthai.so.0 => /usr/lib/x86_64-linux-gnu/libthai.so.0 (0x00007f85e8d69000)
        libexpat.so.1 => /lib/x86_64-linux-gnu/libexpat.so.1 (0x00007f85e8b3f000)
        libpcre.so.3 => /lib/x86_64-linux-gnu/libpcre.so.3 (0x00007f85e8901000)
        libXau.so.6 => /usr/lib/x86_64-linux-gnu/libXau.so.6 (0x00007f85e86fc000)
        libXdmcp.so.6 => /usr/lib/x86_64-linux-gnu/libXdmcp.so.6 (0x00007f85e84f6000)
        libgraphite2.so.3 => /usr/lib/x86_64-linux-gnu/libgraphite2.so.3 (0x00007f85e82cf000)
        libdatrie.so.1 => /usr/lib/x86_64-linux-gnu/libdatrie.so.1 (0x00007f85e80c8000)
    

    Here I am using pkg-config to get the necessary compiler flags and linker dependencies and just passing them directly to nvcc. You could construct them by hand if you don't have access to pkg-config. The important point is putting library dependencies after your own code. The GNU linker reads dependencies left-to-right, and if you specify a library before code that depends on it, the linker will just discard the library and cause a linking error.


    EDIT: To do this in Nsight Eclipse edition

    Add the pkg-config commands in the Project properties for both the compiler and linker phases using $(shell pkg-config ...): nvcc compile flags nvcc linker flags

    Building the project with your code gives this build log:

    make all 
    Building file: ../src/glade_cuda.cu
    Invoking: NVCC Compiler
    /opt/cuda-7.5/bin/nvcc -O3 -Xcompiler -fPIC -Xcompiler -pthread -I/usr/include/gtk-2.0 -I/usr/lib/x86_64-linux-gnu/gtk-2.0/include -I/usr/include/atk-1.0 -I/usr/include/cairo -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/pango-1.0 -I/usr/include/gio-unix-2.0/ -I/usr/include/freetype2 -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I/usr/include/pixman-1 -I/usr/include/libpng12 -I/usr/include/harfbuzz   -gencode arch=compute_52,code=sm_52  -odir "src" -M -o "src/glade_cuda.d" "../src/glade_cuda.cu"
    /opt/cuda-7.5/bin/nvcc -O3 -Xcompiler -fPIC -Xcompiler -pthread -I/usr/include/gtk-2.0 -I/usr/lib/x86_64-linux-gnu/gtk-2.0/include -I/usr/include/atk-1.0 -I/usr/include/cairo -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/pango-1.0 -I/usr/include/gio-unix-2.0/ -I/usr/include/freetype2 -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I/usr/include/pixman-1 -I/usr/include/libpng12 -I/usr/include/harfbuzz   --compile --relocatable-device-code=false -gencode arch=compute_52,code=compute_52 -gencode arch=compute_52,code=sm_52  -x cu -o  "src/glade_cuda.o" "../src/glade_cuda.cu"
    Finished building: ../src/glade_cuda.cu
    
    Building target: libGTK test
    Invoking: NVCC Linker
    /opt/cuda-7.5/bin/nvcc --cudart static -shared -Xlinker -lgtk-x11-2.0 -lgdk-x11-2.0 -latk-1.0 -lgio-2.0 -lpangoft2-1.0 -lpangocairo-1.0 -lgdk_pixbuf-2.0 -lcairo -lpango-1.0 -lfontconfig -lgobject-2.0 -lglib-2.0 -lfreetype   --relocatable-device-code=false -gencode arch=compute_52,code=compute_52 -gencode arch=compute_52,code=sm_52 -link -o  "libGTK test"  ./src/glade_cuda.o   
    Finished building target: libGTK test
    
    
    12:57:43 Build Finished (took 3s.963ms)