Git Product home page Git Product logo

cuda-google-colab's Introduction

CUDA on Google Colab

The cuda code is mainly for nvidia hardware device. In this repo, I will show how to run your cuda c or cuda cpp code on the google colab platform for free.

How to run the cuda code on Google Colab

You can follow the steps below. The specific code is inside the example.ipynb file.

a. Setting the environment

  1. The Google Colab is initialized with no hardware as default. In that case, we need to first set our hardware to GPU.
Runtime > Change runtime type > Setting the Hardware accelerator to GPU > Save
  1. If we need to use the cuda, we have to have cuda tookit. The Google Colab has already installed that. We can use the following code to verify that. (The output will show the file under the /usr/local, which has several different version of cuda.)
!ls /usr/local/
  1. Then, we need to verify if the nvcc command is there in the $PATH environment to use. We can use the following code to verify that. It will show the full path of nvcc command.
!which nvcc
  1. The cuda driver is also important for us to use the cuda. The Google Colab also has already installed that. We can use the following code to verify that. It will show the property of the nvidia card, which means the driver is working properly.Remember the NVIDIA device you are using now, in my case, I use K80
!nvidia-smi

b. Write, Compile and Run the program.

  1. Write cuda code
%%writefile hello.cu

#include<stdio.h>
__global__ void hello(void)
{
    printf("GPU: Hello!\n");
}
int main(int argc,char **argv)
{
    printf("CPU: Hello!\n");
    hello<<<1,10>>>();
    cudaDeviceReset();
    return 0;
}
  1. Compile the code (It depends on the device, K80 use the Kepler arch which only supports CUDA 5 to CUDA 10. But the Google Colab has CUDA 11 as default. There are two ways to fix that. First is adding flag, sm_37 is for K80. Second is create a soft link to make the CUDA11 to CUDA10)
    NVIDIA arch
    GPU Compute Capability
# Method 1
!nvcc -arch=sm_37 -gencode=arch=compute_37,code=sm_37 hello.cu -o hello

# Method 2
!rm -rf /usr/local/cuda
!ln -s /usr/local/cuda-10.1 /usr/local/cuda
!nvcc hello.cu -o hello
  1. Run the program
!./hello

If you see 1 cpu hello and 10 gpu hello in the output. The program is working well.

Reference

  1. Cuda not working on Google Colab
  2. Matching CUDA arch and CUDA gencode for various NVIDIA architectures
  3. GPU Compute Capability

cuda-google-colab's People

Contributors

flin3500 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.