如何在 Google Colab 上编译自定义 cpp 文件
How to compile custom cpp files on Google Colab
我正在尝试使用 Google Colab 复制此 github repo 的结果,因为我不想在我的本地计算机上安装所有要求并利用 GPU Google Colab
但是,我需要做的一件事(如 repo 的 README 中所示)是首先编译一个 cpp makefile。下面包含 makefile 的说明。很明显,我无法按照此说明进行操作,因为我不知道 Google Colab 的 ncvv、cudalib 和 tensorflow 库目录
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
有没有办法直接使用 makefile 或单独编译每个 cpp 文件来编译包含在 makefile 中的文件(因为 运行 模型需要这些函数)?我在下面包含了 makefile 的内容,以避免您在回购中四处点击寻找它
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
您可以在 google colab 中安装所需版本的 Cuda。例如
对于 Cuda 9.2 你可以试试
!apt-get --purge remove cuda nvidia* libnvidia-*
!dpkg -l | grep cuda- | awk '{print }' | 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
同样的,你可以想办法安装Cuda 8.2
对于 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
然后你可以编译它或者通过 运行 make 来制作它,如果你的安装有一个自定义的 make 文件。
!make
您可以通过在 colab 的单元格中添加 %%bash
来在您的电脑上使用 bash。
示例:
单元格一:写cpp文件
%%writefile welcome.cpp
#include <iostream>
int main()
{
std::cout << "Welcome To AI with Ashok's Blog\n";
return 0;
}
单元格二:编译和 运行
%%bash
g++ welcome.cpp -o welcome
./welcome
您也可以在colab内置的文本编辑器中打开cpp文件,以享受正确的亮点。当您从左侧的“文件”选项卡打开文本文件时它会打开,并且可以使用“ctr+s”快捷方式保存。
我正在尝试使用 Google Colab 复制此 github repo 的结果,因为我不想在我的本地计算机上安装所有要求并利用 GPU Google Colab
但是,我需要做的一件事(如 repo 的 README 中所示)是首先编译一个 cpp makefile。下面包含 makefile 的说明。很明显,我无法按照此说明进行操作,因为我不知道 Google Colab 的 ncvv、cudalib 和 tensorflow 库目录
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
有没有办法直接使用 makefile 或单独编译每个 cpp 文件来编译包含在 makefile 中的文件(因为 运行 模型需要这些函数)?我在下面包含了 makefile 的内容,以避免您在回购中四处点击寻找它
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
您可以在 google colab 中安装所需版本的 Cuda。例如
对于 Cuda 9.2 你可以试试
!apt-get --purge remove cuda nvidia* libnvidia-*
!dpkg -l | grep cuda- | awk '{print }' | 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
同样的,你可以想办法安装Cuda 8.2
对于 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
然后你可以编译它或者通过 运行 make 来制作它,如果你的安装有一个自定义的 make 文件。
!make
您可以通过在 colab 的单元格中添加 %%bash
来在您的电脑上使用 bash。
示例:
单元格一:写cpp文件
%%writefile welcome.cpp
#include <iostream>
int main()
{
std::cout << "Welcome To AI with Ashok's Blog\n";
return 0;
}
单元格二:编译和 运行
%%bash
g++ welcome.cpp -o welcome
./welcome
您也可以在colab内置的文本编辑器中打开cpp文件,以享受正确的亮点。当您从左侧的“文件”选项卡打开文本文件时它会打开,并且可以使用“ctr+s”快捷方式保存。