Home How To Run CUDA C/C++ Programs On Google Colab?
Post
Cancel

How To Run CUDA C/C++ Programs On Google Colab?

If you are learning High-Performance Computing(HPC), you might have come across Compute Unified Device Architecture(CUDA). CUDA is a proprietary technology developed by Nvidia Corporation for GPU computing. To use the CUDA framework for programming you need to have access to CUDA capable Nvidia GPU. If you do not have access to such GPU, you either have to rent such GPU on cloud platforms like Azure, AWS, and Google cloud or you can use free services like Google Colaboratory to run your CUDA C/C++ programs. In this article, we will show simple steps to run CUDA C/C++ programs on Google Colab notebook.

Requirements

  • Google account to access Google colab
  • Internet connection

Step 1

Go to Google colab and click on New Notebook.

Google colab Google colab project selection page

Step 2

Click on the Runtime menu and then select the Change runtime type option

Runtime menu Runtime menu

Step 3

Set the Hardware accelerator option to GPU

Hardware accelerator selection menu Hardware accelerator selection menu

Step 4

Run the following commands

1
2
3
4
5
6
7
8
# Check the nvcc version.
!nvcc --version

# Install the NVCC Plugin for Jupyter notebook.
!pip install git+git://github.com/andreinechaev/nvcc4jupyter.git

# Load the above extension in the notebook.
%load_ext nvcc_plugin

Note: Each command should be executed on a different cell.

Step 5

Now paste/write your code in a new cell and run the cell to execute your code.

1
2
3
4
5
6
7
8
9
%%cu
#include <iostream>
int main() {
    printf("CUDA is working\n");
    return 0;
}
// Here, only a simple C program has been executed.
// All the other complex CUDA programs can be executed in the
// Google colab environment by following these instructions.

Note: You have to add %%cu on tbe first line of your cell where your C/C++ code is written.

C program running on Colab C program running on Colab

Conclusion

In this way, we can easily run CUDA C/C++ programs on the Google colab platform for free. We hope this article helps you in your AI programming journey.

Top free resources to learn the MERN Stack

-

Comments powered by Disqus.