<commit_before>---
layout: post
title:  "NVIDIA CUDA Sample"
date:   2018-06-12
---

<p class="intro"><span class="dropcap">N</span>VIDIA CUDA Practice</p>
The purpose of this post is to get started with simple CUDA program and add more materials over time.
Assuming you have NIVIDA cuda toolkit installed, meaning <i>nvcc</i> works)


<b>Sample Code— </b>
The program adds to arrays x and y using CUDA. 
{% highlight c++ %}

#include <iostream>
#include <math.h>
// Kernel function to add the elements of two arrays
//global identifies this function as cuda kernel
__global__
void add(int n, float *x, float *y)
{
  int index = threadIdx.x;
  int stride = blockDim.x;
  for (int i = index; i < n; i += stride)
      y[i] = x[i] + y[i];
}

int main(void)
{
  int N = 1<<20;
  float *x, *y;

  // Allocate Unified Memory – accessible from CPU or GPU
  cudaMallocManaged(&x, N*sizeof(float));
  cudaMallocManaged(&y, N*sizeof(float));

  // initialize x and y arrays on the host
  for (int i = 0; i < N; i++) {
    x[i] = 1.0f;
    y[i] = 2.0f;
  }

  // Run kernel on 1M elements on the GPU
  add<<<1, 256>>>(N, x, y);
  // <<< >>>notation to supply grid/block sizes

  // Wait for GPU to finish before accessing on host
  cudaDeviceSynchronize();

  // Check for errors (all values should be 3.0f)
  float maxError = 0.0f;
  for (int i = 0; i < N; i++)
    maxError = fmax(maxError, fabs(y[i]-3.0f));
  std::cout << "Max error: " << maxError << std::endl;

  // Free memory
  cudaFree(x);
  cudaFree(y);
  
  return 0;
}

{% endhighlight %}


<b>Run— </b><br>
Run the following command to compile <br>
nvcc add.cu -o add_cuda

<br>
This command to execute <br>
./add_cuda

<br>
And this command to profile (clock) it <br>
nvprof ./add_cuda


<b>References— </b>
1. Special thanks to <NAME>, https://www.youtube.com/watch?v=1cHx1baKqq0
<br> 
1a. https://github.com/llSourcell/An_Introduction_to_GPU_Programming
<br>
2. CUDA (https://developer.nvidia.com/cuda-downloads)<commit_msg>abc of cuda programming/update<commit_after>---
layout: post
title:  "NVIDIA CUDA Sample"
date:   2018-06-12
---

<p class="intro"><span class="dropcap">N</span>VIDIA CUDA Practice</p>
The purpose of this post is to get started with simple CUDA program and add more materials over time.
Assuming you have NIVIDA cuda toolkit installed, meaning <i>nvcc</i> works)

If you are ready, a library of parallel programs-
https://www.cs.cmu.edu/%7Escandal/nesl/algorithms.html#sort

<br>
Cuda specific material<br> 
https://www.shodor.org/media/content/petascale/materials/UPModules/matrixMultiplication/moduleDocument.pdf

<b>Sample cuda Code— </b>
The following program(<b>add.cu</b>) adds two arrays x and y using CUDA. 
{% highlight c++ %}
/*filename add.cu*/
#include <iostream>
#include <math.h>
// Kernel function to add the elements of two arrays
//global identifies this function as cuda kernel
__global__
void add(int n, float *x, float *y)
{
  int index = threadIdx.x;
  int stride = blockDim.x;
  for (int i = index; i < n; i += stride)
      y[i] = x[i] + y[i];
}

int main(void)
{
  int N = 1<<20;
  float *x, *y;

  // Allocate Unified Memory – accessible from CPU or GPU
  cudaMallocManaged(&x, N*sizeof(float));
  cudaMallocManaged(&y, N*sizeof(float));

  // initialize x and y arrays on the host
  for (int i = 0; i < N; i++) {
    x[i] = 1.0f;
    y[i] = 2.0f;
  }

  // Run kernel on 1M elements on the GPU
  add<<<1, 256>>>(N, x, y);
  // <<< >>>notation to supply grid/block sizes

  // Wait for GPU to finish before accessing on host
  cudaDeviceSynchronize();

  // Check for errors (all values should be 3.0f)
  float maxError = 0.0f;
  for (int i = 0; i < N; i++)
    maxError = fmax(maxError, fabs(y[i]-3.0f));
  std::cout << "Max error: " << maxError << std::endl;

  // Free memory
  cudaFree(x);
  cudaFree(y);
  
  return 0;
}

{% endhighlight %}


<b>Run— </b><br>
&#9658; Run the following command to compile <br>
<pre>nvcc add.cu -o add_cuda</pre>

&#9658; This command to execute <br>
<pre>./add_cuda</pre>

&#9658; And this command to profile (clock/runtime) it, bsically how long it takes to run<br>
<pre>nvprof ./add_cuda</pre>


<b>References— </b>
1. Special thanks to <NAME>, https://www.youtube.com/watch?v=1cHx1baKqq0
<br> 
1a. https://github.com/llSourcell/An_Introduction_to_GPU_Programming
<br>
2. CUDA (https://developer.nvidia.com/cuda-downloads)
{'fraction_non_alphanumeric': 0.15044653079917564, 'fraction_numerical': 0.01900618273414243, 'mean_word_length': 3.5452653485952133, 'pattern_counts': {'":': 0, '<': 91, '<?xml version=': 0, '>': 69, 'https://': 8, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2002727', 'n_tokens_mistral': 1715, 'n_tokens_neox': 1614, 'n_words': 526}