Friday, April 11, 2014

CUDA installation and programming in CUDA and OpenCL

Introduction to CUDA:-

 CUDA (Compute Unified Device Architecture) is a parallel computing platform and programming model created by NVIDIA and implemented by the graphics processing units (GPUs) that enables dramatic increases in computing performance by harnessing the power of the graphics processing unit (GPU).
CUDA was developed with several design goals in mind:
  • Provide a small set of extensions to standard programming languages, like C, that enable a straightforward implementation of parallel algorithms. With CUDA C/C⁠+⁠+, programmers can focus on the task of parallelization of the algorithms rather than spending time on their implementation.
  • Support heterogeneous computation where applications use both the CPU and GPU. Serial portions of applications are run on the CPU, and parallel portions are offloaded to the GPU. As such, CUDA can be incrementally applied to existing applications. The CPU and GPU are treated as separate devices that have their own memory spaces. This configuration also allows simultaneous computation on the CPU and GPU without contention for memory resources.

Installing CUDA on Ubuntu 12.04:-

1. System Requirements

To use CUDA on your system, you will need the following installed:

2. Pre-installation Actions

Some actions must be taken before the CUDA Toolkit and Driver can be installed on Linux:
  • Verify the system has a CUDA-capable GPU
  • Verify the system is running a supported version of Linux.
  • Verify the system has gcc installed.
  • Download the NVIDIA CUDA Toolkit. 
You can see here for these pre-installation actions. 


3. Package Manager Installation:-


The installation is a two-step process. First the small repository configuration package must be downloaded from the NVIDIA CUDA download page, and installed manually. The package sets the package manager database to include the CUDA repository. Then the CUDA Toolkit is installed using the package manager software.

3.1. Prerequisites

  • The package manager installations (RPM/DEB packages) and the stand-alone installer installations (.run file) of the NVIDIA driver are incompatible. Before using the distribution-specific packages, uninstall the NVIDIA driver with the following command:
sudo /usr/bin/nvidia-uninstall
 
·         For x86 to ARMv7 cross development, the host system must be an Ubuntu 12.04 system. 

3.2. Ubuntu

  1. Perform the pre-installation actions.
  2. Enable armhf foreign architecture, if necessary
The armhf foreign architecture must be enabled in order to install the cross-armhf toolkit. To enable armhf as a foreign architecture, the following commands must be executed:
On Ubuntu 12.04,
$ sudo sh -c \
  'echo "foreign-architecture armhf" >> /etc/dpkg/dpkg.cfg.d/multiarch'
$ sudo apt-get update
On Ubuntu 12.10 or greater,
$ sudo dpkg --add-architecture armhf
$ sudo apt-get update
  1. Install repository meta-data
When using a proxy server with aptitude, ensure that wget is set up to use the same proxy settings before installing the cuda-repo package.
$ sudo dpkg -i cuda-repo-<distro>_<version>_<architecture>.deb
  1. Update the Apt repository cache
$ sudo apt-get update
  1. Install CUDA
$ sudo apt-get install cuda
  1. Add libcuda.so symbolic link, if necessary
The libcuda.so library is installed in the /usr/lib/nvidia-331 directory. For pre-existing projects which use libcuda.so, it may be useful to add a symbolic link from libcuda.so in the /usr/lib directory.
  1. Perform the post-installation actions.

4. Post-installation Actions

Some actions must be taken after installing the CUDA Toolkit and Driver before they can be completely used:
  • Setup evironment variables.
  • Install a writable copy of the CUDA Samples.
  • Verify the installation.

4.1. Environment Setup

The PATH variable needs to include /usr/local/cuda-6.0/bin
The LD_LIBRARY_PATH variable needs to contain /usr/local/cuda-6.0/lib on a 32-bit system, and /usr/local/cuda-6.0/lib64 on a 64-bit system
·         To change the environment variables for 32-bit operating systems:
·         $ export PATH=/usr/local/cuda-6.0/bin:$PATH
$ export LD_LIBRARY_PATH=/usr/local/cuda-6.0/lib:$LD_LIBRARY_PATH
·         To change the environment variables for 64-bit operating systems:
·         $ export PATH=/usr/local/cuda-6.0/bin:$PATH
$ export LD_LIBRARY_PATH=/usr/local/cuda-6.0/lib64:$LD_LIBRARY_PATH

4.2. (Optional) Install Writable Samples

In order to modify, compile, and run the samples, the samples must be installed with write permissions. A convenience installation script is provided:
$ cuda-install-samples-6.0.sh <dir>
This script is installed with the cuda-samples-6-0 package. The cuda-samples-6-0 package installs only a read-only copy in /usr/local/cuda-6.0/samples.

4.3. Verify the Installation

Before continuing, it is important to verify that the CUDA toolkit can find and communicate correctly with the CUDA-capable hardware. To do this, you need to compile and run some of the included sample programs.
Note: Ensure the PATH and LD_LIBRARY_PATH variables are set correctly.

4.3.1. Verify the Driver Version

If you installed the driver, verify that the correct version of it is installed.
This can be done through your System Properties (or equivalent) or by executing the command
cat /proc/driver/nvidia/version
Note that this command will not work on an iGPU/dGPU system.

4.3.2. Compiling the Examples

The version of the CUDA Toolkit can be checked by running nvcc -V in a terminal window. The nvcc command runs the compiler driver that compiles CUDA programs. It calls the gcc compiler for C code and the NVIDIA PTX compiler for the CUDA code.
The NVIDIA CUDA Toolkit includes sample programs in source form. You should compile them by changing to ~/NVIDIA_CUDA-6.0_Samples and typing make. The resulting binaries will be placed under ~/NVIDIA_CUDA-6.0_Samples/bin.

4.3.3. Running the Binaries

After compilation, find and run deviceQuery under ~/NVIDIA_CUDA-6.0_Samples.



Programming in CUDA

A simple “Hello, world” program using CUDA C is given here

In file hello.cu:

#include "stdio.h"

int main()

{

printf("Hello, world\n");

return 0;

}

On our Tesla machine, you can compile and this with:

$ nvcc hello.cu

$ ./a.out

You can change the output file name with the –o flag: nvcc –o hello hello.cu

Programming using OpenCL

We should write the header as follows for Linux

#ifdef _LINUX_
#include <OpenCL/opencl.h>
#else
#include <CL/cl.h>
#endif
To run "Hello.c"
> gcc -I /path-to-NVIDIA/OpenCL/common/inc -L /path-to-NVIDIA/OpenCL/common/lib/Linux64 -o hello hello.c -lOpenCL (64-bit Linux)


That is all you have to do .I hope this blog is helpful to beginners. !!





 







 

No comments:

Post a Comment