Search code examples
c++tensorflowmakefilegoogle-colaboratory

How to compile custom cpp files on Google Colab


I'm trying to replicate the result of this github repo using Google Colab since I don't want to install all the requirements on my local machine and to take advantage of the GPU on Google Colab

However, one of the things I need to do (as indicated in the repo's README) is to first compile a cpp makefile. The instruction of the makefile is included below. Obvious I can't follow this instruction since I don't know Google Colab's directories of ncvv, cudalib and tensorflow library

cd latent_3d_points/external

with your editor modify the first three lines of the makefile to point to 
your nvcc, cudalib and tensorflow library.

make

Is there a way for me to compile the files included in the makefile (because those functions are needed to run the model) either using the makefile directly or compile each cpp file individually? I included the content of the makefile below to avoid having you to click around in the repo looking for it

nvcc = /usr/local/cuda-8.0/bin/nvcc
cudalib =  /usr/local/cuda-8.0/lib64
tensorflow = /orions4-zfs/projects/optas/Virt_Env/tf_1.3/lib/python2.7/site-packages/tensorflow/include

all: tf_approxmatch_so.so tf_approxmatch_g.cu.o tf_nndistance_so.so tf_nndistance_g.cu.o


tf_approxmatch_so.so: tf_approxmatch_g.cu.o tf_approxmatch.cpp
    g++ -std=c++11 tf_approxmatch.cpp tf_approxmatch_g.cu.o -o tf_approxmatch_so.so -shared -fPIC -I $(tensorflow) -lcudart -L $(cudalib) -O2 -D_GLIBCXX_USE_CXX11_ABI=0


tf_approxmatch_g.cu.o: tf_approxmatch_g.cu
    $(nvcc) -D_GLIBCXX_USE_CXX11_ABI=0 -std=c++11 -c -o tf_approxmatch_g.cu.o tf_approxmatch_g.cu -I $(tensorflow) -DGOOGLE_CUDA=1 -x cu -Xcompiler -fPIC -O2


tf_nndistance_so.so: tf_nndistance_g.cu.o tf_nndistance.cpp
    g++ -std=c++11 tf_nndistance.cpp tf_nndistance_g.cu.o -o tf_nndistance_so.so -shared -fPIC -I $(tensorflow) -lcudart -L $(cudalib) -O2 -D_GLIBCXX_USE_CXX11_ABI=0


tf_nndistance_g.cu.o: tf_nndistance_g.cu
    $(nvcc) -D_GLIBCXX_USE_CXX11_ABI=0 -std=c++11 -c -o tf_nndistance_g.cu.o tf_nndistance_g.cu -I $(tensorflow) -DGOOGLE_CUDA=1 -x cu -Xcompiler -fPIC -O2


clean:
    rm tf_approxmatch_so.so
    rm tf_nndistance_so.so
    rm  *.cu.o 

Solution

  • You can install the required version of Cuda in google colab. For eg.

    For Cuda 9.2 you can try

    !apt-get --purge remove cuda nvidia* libnvidia-*
    !dpkg -l | grep cuda- | awk '{print $2}' | xargs -n1 dpkg --purge
    !apt-get remove cuda-*
    !apt autoremove
    !apt-get update
    
    !wget https://developer.nvidia.com/compute/cuda/9.2/Prod/local_installers/cuda-repo-ubuntu1604-9-2-local_9.2.88-1_amd64 -O cuda-repo-ubuntu1604-9-2-local_9.2.88-1_amd64.deb
    !dpkg -i cuda-repo-ubuntu1604-9-2-local_9.2.88-1_amd64.deb
    !apt-key add /var/cuda-repo-9-2-local/7fa2af80.pub
    !apt-get update
    !apt-get install cuda-9.2
    

    Similarly, you can find a way to install Cuda 8.2.

    For gcc

    !apt-get install -qq gcc-5 g++-5 -y
    !ln -s /usr/bin/gcc-5 
    !ln -s /usr/bin/g++-5 
    
    !sudo apt-get update
    !sudo apt-get upgrade
    

    Then you can compile it or make it by running make, if your installation has a custom make file.

    !make