How to Run CUDA C or C++ on Jupyter (Google Colab)

Introduction:

Introduction:

If you’re looking to leverage the power of GPU-accelerated computing for your CUDA C or C++ projects, running them on Jupyter Notebooks in Google Colab can be a convenient and efficient solution. Follow these steps to get started:

Step 1: Setting up Google Colab for CUDA

First, open Google Colab (https://colab.research.google.com/) and create a new notebook.

Step 2: Installing CUDA Toolkit

Install the CUDA toolkit by running the following commands in a code cell:

!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
!wget https://developer.nvidia.com/compute/cuda/9.2/Prod/local_installers/cuda-repo-ubuntu1604-9-2-local_9.2.88-1_amd64
!dpkg -i cuda-repo-ubuntu1604-9-2-local_9.2.88-1_amd64
!apt-key add /var/cuda-repo-9-2-local/7fa2af80.pub
!apt-get update
!apt-get install cuda-9.2

Step 3: Compiling and Running CUDA C/C++ Code

You can now compile and run your CUDA C or C++ code in the notebook. Just remember to set the runtime type to GPU by navigating to ‘Runtime’ -> ‘Change runtime type’ -> Select ‘GPU’ as the hardware accelerator.

Step 4: Running a Sample CUDA Program

As a quick test, you can run a sample CUDA program like the vector addition code below:

#include <stdio.h>

__global__ void add(int a, int b, int* c) {
  *c = a + b;
}

int main() {
  int a = 2, b = 7, c;
  int* d_c;
  cudaMalloc((void**)&d_c, sizeof(int));
  add<<<1, 1>>>(a, b, d_c);
  cudaMemcpy(&c, d_c, sizeof(int), cudaMemcpyDeviceToHost);
  printf("%d + %d = %d\n", a, b, c);
  cudaFree(d_c);

  return 0;
}

By following these steps, you can seamlessly run your CUDA C or C++ code on Jupyter Notebooks in Google Colab, harnessing the power of GPU acceleration for your projects.

0 Shares:
You May Also Like
Read More

How to Check if Your Gmail Account Has Been Hacked

With the rise in cyber threats, it’s essential to take proactive measures to safeguard your online accounts. Gmail, being one of the most widely used email services, is a common target for hackers. If you suspect that your Gmail account may have been hacked, it’s crucial to act fast to secure your information and prevent further damage.
Read More

How to Insert an Image Into a Shape on Adobe Illustrator

Adobe Illustrator is a powerful tool for creating stunning graphics, and inserting an image into a shape can add depth and creativity to your designs. Whether you want to place a photo inside a circle, square, or any other shape, Illustrator makes it easy to achieve this effect. Follow these simple steps to insert an image into a shape on Adobe Illustrator:
Read More

8 Easy Ways to Pin and Unpin a Text Message

In today’s fast-paced digital world, managing and organizing your text messages efficiently is crucial. Whether you use messaging apps on your smartphone or computer, knowing how to pin and unpin important messages can save you time and hassle. Below are 8 easy ways to pin and unpin a text message on different platforms: