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

  1. 1
    Create a new Notebook. Click: here.
  2. 2
    Click on New Python 3 Notebook at the bottom right corner of the window.
    Advertisement
  3. 3
    Click on Runtime > Change runtime type.
  4. 4
    Select GPU from the drop down menu and click on Save.
  5. 5
    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
      
  6. 6
    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
      
  7. 7
    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
        
  8. 8
    Execute the given command to install a small extension to run nvcc from Notebook cells.
      !pip install git+git://github.com/andreinechaev/nvcc4jupyter.git
      
  9. 9
    Load the extension using this code:
      %load_ext nvcc_plugin
      
  10. 10
    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 <stdio.h>
      #include <stdlib.h>
      __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.
  11. Advertisement

Community Q&A

  • Question
    Why print ("\n") always print \n character not "enter"?
    Community Answer
    Community Answer
    \n is the newline character which is simply a line break.
  • Question
    Does it work on Windows 10?
    The logo Maker
    The logo Maker
    Community Answer
    Yes, it will. Be sure to have all required components used in the article.
  • Question
    How to give command line argument at runtime?
    Marrium Sharif
    Marrium Sharif
    Community Answer
    The Java command-line argument is an argument i.e. passed at the time of running the Java program. The arguments passed from the console can be received in the Java program and it can be used as an input. So, it provides a convenient way to check the behavior of the program for the different values. You can pass N (1,2,3 and so on) numbers of arguments from the command prompt.
Advertisement

About This Article

wikiHow is a “wiki,” similar to Wikipedia, which means that many of our articles are co-written by multiple authors. To create this article, volunteer authors worked to edit and improve it over time. This article has been viewed 36,469 times.
How helpful is this?
Co-authors: 3
Updated: June 3, 2019
Views: 36,469
Advertisement