How to Run CUDA C or C++ on Jupyter (Google Colab)
How to Run CUDA C or C++ on Jupyter (Google Colab)
CUDA is NVIDIA’s parallel computing architecture that enables dramatic increases in computing performance by harnessing the power of the GPU. With Colab, you can work with CUDA C/C++ on the GPU for free.
Steps

Create a new Notebook. Click: here.

Click on New Python 3 Notebook at the bottom right corner of the window.

Click on Runtime > Change runtime type.

Select GPU from the drop down menu and click on Save.

Uninstall any previous versions of CUDA completely. (The '!' added at the beginning of a line allows it to be executed as a command line command.) !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

Install CUDA Version 9. !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

Check your version using this code: !nvcc --version This should print something like this: nvcc: NVIDIA (R) Cuda compiler driver Copyright (c) 2005-2018 NVIDIA Corporation Built on Wed_Apr_11_23:16:29_CDT_2018 Cuda compilation tools, release 9.2, V9.2.88

Execute the given command to install a small extension to run nvcc from Notebook cells. !pip install git+git://github.com/andreinechaev/nvcc4jupyter.git

Load the extension using this code: %load_ext nvcc_plugin

Execute the code below to check if CUDA is working. To run CUDA C/C++ code in your notebook, add the %%cu extension at the beginning of your code. %%cu #include #include __global__ void add(int *a, int *b, int *c) { *c = *a + *b; } int main() { int a, b, c; // host copies of variables a, b & c int *d_a, *d_b, *d_c; // device copies of variables a, b & c int size = sizeof(int); // Allocate space for device copies of a, b, c cudaMalloc((void **)&d_a, size); cudaMalloc((void **)&d_b, size); cudaMalloc((void **)&d_c, size); // Setup input values c = 0; a = 3; b = 5; // Copy inputs to device cudaMemcpy(d_a, &a, size, cudaMemcpyHostToDevice); cudaMemcpy(d_b, &b, size, cudaMemcpyHostToDevice); // Launch add() kernel on GPU add<<<1,1>>>(d_a, d_b, d_c); // Copy result back to host cudaError err = cudaMemcpy(&c, d_c, size, cudaMemcpyDeviceToHost); if(err!=cudaSuccess) { printf("CUDA error copying to Host: %s\n", cudaGetErrorString(err)); } printf("result is %d\n",c); // Cleanup cudaFree(d_a); cudaFree(d_b); cudaFree(d_c); return 0; } If all went well this code should output: result is 8\n.

What's your reaction?

Comments

https://kapitoshka.info/assets/images/user-avatar-s.jpg

0 comment

Write the first comment for this!