Hello world with CUDA

CUDA is becoming one of the essential tools to learn. It is going to help you even you are not in Deep Learning world. Gaming industry, Finance Industry and even there are databases which runs on GPU.

In this blog post we will try to get started quickly with CUDA. At the end of the blog there are some resources which can help you learn CUDA in detail.

Launch GPU on aws.

If you already know or you have a GPU instance, then you can skip this section.

First we will see that how do we spin up a gpu instance on aws to write our first CUDA code. We need a CUDA supported GPU. I know I know aws is a bit costly and there are many alternatives like paperspace, FloydHub etc but we will stick to aws because let’s say I ain’t spending my money ūüėõ

When you login to aws console, you will be asked to select an AMI(Amazon Machine Image). I prefer “Deep Learning AMI(ubuntu)” because it comes with all the pre installed packages I need and it is free!

Screenshot 2019-03-17 at 8.07.20 AM.png¬†The second step here is to select the instance type. I have selected “p2.xlarge”.

Screenshot 2019-03-17 at 8.20.48 AM.png

after this, you can directly do “review and finish”. Don’t forget to download “.pem” file.

Then give your .pem right permissions.

chmod 400 name-of-your-key.pem

Now we are set to login to our gpu instance –

ssh -i name-of-your-key.pem ubuntu@your-instance-ip

Yay! we are in.

Screenshot 2019-03-17 at 8.34.19 AM.png

Writing your first CUDA code.

Congratulation! you have come this far. In this section we will write our first hello world CUDA program in cpp(You can also write the code in C).

Screenshot 2019-03-17 at 8.48.11 AM.png

Now, Let’s try to understand the above code. The¬†__global__¬†specifier indicates a function that runs on GPU. These function can be called through host code, e.g. the¬†main()¬†function in the example, and is also known as “kernels“.¬† When a kernel is called, its execution configuration is provided through¬†<<<...>>>¬†syntax, e.g.¬†cuda_hello<<<1,1>>>(). This is called “kernel launch“.

Keep in mind that if you compile the above file, say “hello-world.cu”. It will give you printf not found error. To resolve this problem, include #<stdio.h> rather than #include<iostream>.

The extension for CUDA files is “.cu” .

Compiling CUDA code.

nvcc hello-world.cu -o hello

Further Reading –

I hope you enjoyed reading this blog post and If there are any doubts then feel free to reach out. This blog was meant to be a kickstarter we will try to dive deep into CUDA in coming blogs.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s