Getting started with CUDA (using VS2017)
Tags: code, cpp, cuda, gpgpu
Published: 2018-03-06 17:20:02

---
title: "Getting started with CUDA (using VS2017)"
published: true
tags: Code,cpp,cuda,gpgpu
canonical_url: http://www.elemarjr.com/en/2018/03/getting-tarted-with-cuda-using-vs2017/
---

If you are interested in performance, you need to know more about CUDA.

From the [official website](https://developer.nvidia.com/cuda-zone):

> CUDA® is a parallel computing platform and programming model developed by NVIDIA for general computing on graphical processing units (GPUs). With CUDA, developers are able to dramatically speed up computing applications by harnessing the power of GPUs.  
> In GPU-accelerated applications, the sequential part of the workload runs on the CPU – which is optimized for single-threaded performance – while the compute intensive portion of the application runs on thousands of GPU cores in parallel. When using CUDA, developers program in popular languages such as C, C++, Fortran, Python and MATLAB and express parallelism through extensions in the form of a few basic keywords.

In this post, I will guide you through your first steps with CUDA. Also, I will show you how to move some basic processing from the CPU to the GPU.

Let’s start getting some bytes.

## Environment setup

I am using Visual Studio 2017 (version 15.6.0). To code with CUDA, you will need to download and install the [CUDA Toolkit.](https://developer.nvidia.com/cuda-toolkit) The Toolkit includes Visual Studio project templates and the NSight IDE (which it can use from Visual Studio).

Also, you will need to install the VC++ 2017 toolset (CUDA is still not compatible with the latest version of Visual Studio).

![](http://www.elemarjr.com/wp-content/uploads/2018/03/cuda_setup_1.png)

## Starting a new project using CUDA

The easiest way to start a project that uses CUDA is utilizing the CUDA template.

![](http://www.elemarjr.com/wp-content/uploads/2018/03/CUDA-New-Project.png)

To be able to compile this, you will need to change the Project Properties to use the Visual Studio 2015 toolset.

![](http://www.elemarjr.com/wp-content/uploads/2018/03/platform_toolset.png)

I recommend you to clean the template’s boilerplate changing the content of the file _kernel.cu_ to this_:_

```cpp
#include "cuda_runtime.h" 
#include "device_launch_parameters.h"
```

## Basic CPU processing

Let’s add two arrays (_a_ and _b_) putting the results in a third array (_c_).

```cpp
#include "cuda_runtime.h" 
#include "device_launch_parameters.h" 

#include <stdio.h> 

void add_arrays_cpu(int* a, int* b, int* c, const int count)
{
	for (auto i = 0; i < count; i++)
	{
		c[i] = a[i] + b[i];
	}
} 

int main()
{
	const auto count = 5; 
	int a[] = { 1, 2, 3, 4, 5 }; 
	int b[] = { 10, 20, 30, 40, 60 }; int c[count]; 
	add_arrays_cpu(a, b, c, count); 
	for (auto i = 0; i < count; i ++)
	{
		printf("%d ", c[i]);
	} 
	getchar(); 
	return 0;
}
```

This code is pretty simple. But it is tough to parallelize.

Whenever we want to use parallelism, we need to create functions that could be executed independently. The function _add_arrays_cpu_ has a _for-loop_ that runs the _add process_ in a sequential fashion. But, there is no reason to work in this way. Let’s change it:

```cpp
#include "cuda_runtime.h"
#include "device_launch_parameters.h"

#include <stdio.h>;

void add_array_element_cpu(int* a, int * b, int* c, const int index)
{
	c[index] = a[index] + b[index];
}

int main()
{
	const auto count = 5;
	int a[] = { 1, 2, 3, 4, 5 };
	int b[] = { 10, 20, 30, 40, 60 };
	int c[count];

	add_arrays_cpu(a, b, c, count);

	for (auto i = 0; i < count; i++)
	{
		add_array_element_cpu(a, b, c, i);
	}

	for (auto i = 0; i < count; i ++)
	{
		printf("%d ", c[i]);
	}

	getchar();

	return 0;
}

```

The function _add_array_element_cpu_ can be executed independently, and that is great. We could start a thread for each position of the array, and that would work fine (I am not saying that would be the right thing to do with CPUs, just as an example).

## Bringing CUDA to the game

The next logical step is to start using the GPU to run our code. We will not move all the functions to the GPU, but only the functions we think we could be parallelized.

```cpp
#include "cuda_runtime.h"
#include "device_launch_parameters.h"

#include <stdio.h>;

__global__ void add_arrays_gpu(int* a, int *b, int* c)
{
	c[threadIdx.x] = a[threadIdx.x] + b[threadIdx.x];
}

int main()
{
	const auto count = 5;
	int host_a[] = { 1, 2, 3, 4, 5 };
	int host_b[] = { 10, 20, 30, 40, 60 };
	int host_c[count];

	int *device_a, *device_b, *device_c;
	const int size = count * sizeof(int);
	cudaMalloc(&amp;device_a, size);
	cudaMalloc(&amp;device_b, size);
	cudaMalloc(&amp;device_c, size);

	cudaMemcpy(
		device_a, host_a,
		size,
		cudaMemcpyHostToDevice
	);

	cudaMemcpy(
		device_b, host_b,
		size,
		cudaMemcpyHostToDevice
	);

	add_arrays_gpu <<< 1, count >>> (device_a, device_b, device_c);

	cudaMemcpy(
		host_c, device_c,
		size,
		cudaMemcpyDeviceToHost
	);


	for (auto i = 0; i < count; i ++)
	{
		printf("%d ", host_c[i]);
	}

	getchar();

	return 0;
}
```

We marked the _add_arrays_gpu_ function as ___global___. Using CUDA terminology, this function is a _kernel,_ and that will be executed from the GPU.  It runs on the GPU, called from the CPU (there is another qualifier location qualifier, ___device___that should be used with functions that run on GPU, called from the GPU).

___global___ functions are invoked using the special <<<…>>> syntax. The parameters we used indicates that we are running the kernel function in _1 block of count (5) threads_.  We will talk more about it in the future.

Note that we don’t need to inform an _index_ anymore – we retrieve the index position from  _threadIdx_.x. _threadIdx_ is a special variable, provided by CUDA runtime, that informs the position of the current thread in the thread block (we are using five threads, one thread block to run our code. Because of that, we were able to use this information as the index in the array).

I would like to make a special consideration about an vital pattern that you will see a lot of times when doing CUDA programming. To run, GPU code is not allowed to access CPU memory (and vice-versa). So we will need:

1. copy the data we need to process from the CPU to the GPU memory
2. do the processing
3. copy results from the GPU to the CPU memory.

## Final words

I know, I know! Adding two arrays with only five elements each is not an exciting example. But, in this post, we did the setup to use CUDA on our machines. Then, I helped you to move your could from sequential running on CPU to parallel on GPU. In the future, let’s return to it and get some real benefits.


The post [Getting started with CUDA (using VS2017)](http://www.elemarjr.com/en/2018/03/getting-tarted-with-cuda-using-vs2017/) appeared first on [Elemar JR](http://www.elemarjr.com/en).