Showing posts with label program. Show all posts
Showing posts with label program. Show all posts

Sunday, 4 November 2012

Installing CUDA 5 on Kubuntu

Introduction

So today I decided to install the CUDA 5 toolkit on my linux machine so that I can test my OpenCL applications on it. This turned out to be much more of a pain than I had originally thought as there is no definitive guide on how to install CUDA under linux and also the installer is quite finicky and really needs a rethink. So I'm posting some tips on how to install the toolkit so that other people don't make the same mistakes I did. Just so you guys know the linux version that I use is Kubuntu 12.10. The main issue with installing the toolkit is that the driver they package with it requires you to have gcc version 4.7 but the toolkit installation requires that you have version 4.4, I'm sure you can all see why this would be an issue. Before reading this guide I advise watching these videos:


So the first piece of advice I have for you guys is to install the driver FIRST and then the toolkit/samples after.

DISCLAIMER: This guide is offered without any guarantees, I am not responsible if you end up bricking your install. You have been warned!

1. Installing the Driver

First install the following libraries & Tools:
sudo apt-get install freeglut3-dev build-essential libx11-dev libxmu-dev libxi-dev libgl1-mesa-glx libglu1-mesa libglu1-mesa-dev

Next we will blacklist some modules(drivers), in terminal enter:
sudo kate /etc/modprobe.d/blacklist.conf

Add the following to the end of the file(one per line like so):
blacklist amd76x_edac
blacklist vga16fb
blacklist nouveau
blacklist rivafb
blacklist nvidiafb
blacklist rivatv

Save the file and close the editor.
Now we want to get rid of any nvidia residuals, in terminal:
sudo apt-get remove --purge nvidia*

Next you need to restart your machine (sudo reboot). At this point, if you log back it to your machine you may find that the window manager isn't working correctly (window borders are missing etc.). Before worrying about that I advise installing the driver portion of the toolkit

0) Press Ctrl+Alt+F1 at login screen(you don't have to login, we'll have to restart later anyway), then log in.
1) sudo service lightdm stop
2) cd Downloads
3) chmod +x <cuda>.run (your toolkit filename)
4) sudo ./<cuda>.run

When asked if you wish to install the driver enter y, but do NOT install the toolkit or samples. After the installation execute “sudo service lightdm start” to restart the x server. Log in to the machine, if your window manager is still screwed I solved this by running “sudo apt-get install kubuntu-desktop”. If you are running another version of linux this command will differ from mine.

Round about here you've installed the driver and (hopefully) your system is running fine. Now for the easy bit, installing the toolkit and samples.

2. Making GCC 4.4 the default version

In this section we are going to use update-alternatives to make gcc-4.4 the default version on your system. In the terminal execute the following commands

sudo apt-get install gcc-4.4
sudo update-alternatives --remove-all gcc
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.7 10
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.4 20
sudo update-alternatives --config gcc

Choose gcc-4.4

gcc --version

You should have gotten some output saying your version number is 4.4.x

3. Installing the Toolkit/Samples

Now you are ready to install the toolkit. All you need to do is navigate to your downloads folder(from the terminal of course) and run “sudo ./<cuda>.run” the same as before but this time when it asks if you want to install the driver select no and install the toolkit and samples instead. This installation worked for me first time but I have heard of some people getting error messages about missing glut libraries (and possibly others), if this happens you will need to find out where the glut library is on your computer and make a symbolic link to /usr/lib using ln (the link tool). Anywhoooo... assuming your install was a success you have very little else to do apart from set up your environment variables and test the install.

Add Environmental variables:
export PATH=/usr/local/cuda/bin:$PATH

#for 64-bit machines:
export LD_LIBRARY_PATH=/usr/local/cuda/lib64:/usr/local/cuda/lib:$LD_LIBRARY_PATH

#for 32-bit machines:
export LD_LIBRARY_PATH=/usr/local/cuda/lib:$LD_LIBRARY_PATH

Now we want to test did the toolkit install properly:
nvcc --version

If you get some output telling you the version number of nvcc then congrats! You are now ready to develop for CUDA and OpenCL!!!

Thursday, 1 November 2012

Setting up OpenCL in Visual Studio

Introduction

As a relatively new OpenCL user I was looking online for a guide to setting up OpenCL in visual studio. I have about 9 years development experience, so I thought this wouldn’t be much of an issue but I did have a little trouble due to a few details of the setup not being common knowledge. In this article I’m going to provide step by step instructions on setting up OpenCL for both nVidia and ATI graphics cards in Visual Studio 2010. I assume that you have an OpenCL capable graphics card, if you are unsure please check your manufacturers website or leave a comment and I’ll check for you. In this tutorial I will be using an nVidia card but I will provide text instructions for AMD cards.

1.       Installing OpenCL

For nVidia graphics cards you need to install the CUDA toolkit and development drivers. As of the time of writing the current CUDA toolkit version was 5.0, this version includes the development drivers in the toolkit. It can be downloaded here:

For ATI graphics cards you will need to install the AMD APP SDK (formerly the AMD Stream SDK). This can be downloaded from:

2.       Setting up the Project

First thing to do is set up an empty VS project by choosing
  • ‘New Project->Visual C++->Win32 Console Application'
  • Enter a name for the project and choose OK
  • In the application creation wizard choose ‘Next’
  • Under ‘Additional options’ check the ‘Empty project’ box and click ‘Finish’

3.       Including OpenCL in the Project

The first step in including OpenCL is to create a C++ file, this enables the configuration options we will need.
  • Right click on the ‘Source Files’ folder in the solution explorer and select ‘Add-> New Item’
  • Select C++ File and give the file a name
  • Click the ‘Add’ button in the bottom right hand corner of the dialog box
  • Now we are ready to point the project to the include directories for OpenCL
  • Right click on the project name in the solution explorer and select ‘Properties’
  • From the ‘Configuration’ drop down box choose ‘All Configurations’
  • Navigate to ‘Configuration Properties-> C/C++ -> General’
  •  In the ‘Additional Include Directories’ field add the following information depending on your graphics card
    •  nVidia   -“$(CUDA_INC_PATH)”
    • AMD    -“$(AMDAPPSDKROOT)\include”

include path image

4.       Linking OpenCL

Now we are going to tell the project where to find the actual library file which contains OpenCL. This is where the actual implementation of OpenCL is contained.

Without closing the dialog box used above:
  • Choose ‘Linker-> General’
  • In  the ‘Additional Dependencies’ field enter the following, again this is based on your card vendor
    •  nVidia   - “$(CUDA_LIB_PATH)”
    • AMD      - “$(AMDAPPSDKROOT)\lib\x86” (for 64 bit users you may need to change the x86 to x86_64, I have no machine to test this on)

library path image
  • Still in the ‘Linker’ submenu, select ‘Input’
  • In the ‘Additional Dependencies’ field click on the arrow that appears at the end of the field and choose Edit…
  • In the dialog that appears enter “OpenCL.lib” (without the quotes obviously J)

library link image

  • Click OK in the dialog box
  • Click OK in the Properties dialog to bring you back to the main IDE
If you are a 32-bit user then your project is now set up for OpenCL (skip to the” Test Your Project” section), if you’re a 64-bit user then continue on to the next section.

5.       A Note for 64-Bit users

If you are using a 64-bit machine then you will need to add an x64 target to your project to be able to build it.
  • Again open the properties page of your project by right clicking on it and selecting ‘Properties’
  • Select ‘Configuration Manager…’ from the top right hand corner of the dialog (if it is greyed out expand ‘Configuration Properties’ on the left hand side to enable it)
  • Under ‘Active solution platforms’ select New...
  • Under ‘Type or select the new platform’ select x64 and click OK
  • Click Close
  • Click OK to get back to the main IDE


Your project should now be set up for OpenCL!! Wasn’t that a breeze ;)

6.       Testing your project

Below is the simplest OpenCL application to test that the install and configuration have worked. Copy this code into the C++ class that you created at the start of the tutorial:

Click the run button. If you get a console window that opens and then closes again, then congratulations!!! You have just taken the first step on your OpenCL/world domination journey. Happy coding guys! If I have made any errors please leave me a comment and I will correct it ASAP. 

Sunday, 28 October 2012

Hello World!!!

Welcome to my amazing technology and programming blog... OK I'm probably overselling this a little. Realistically this blog is a place where I can post tips/tricks/semi-interesting snippets from projects that I'm working on. Do you ever come across a problem when your programming, and when you've solved it, think to yourself "Why in the name of the flying spaghetti monster has someone not written an article about this!!! *sits angrily contemplating wasted time*"? Well I often do, so I'm going to write the solution to those problems here so that someone else may benefit from my hours of frustration.

A little about me

So you're probably thinking that this is all well and good but who is this guy and why should I listen to him? Well... let me tell you. My name is Chris McCabe, I'm a Masters student studying computer science at Queens University Belfast. My research topic is on multi-precision arithmetic in OpenCL, so you guys can probably expect a whole lot of OpenCL related posts. I was the 2008 recipient of the Liberty IT scholarship, and I went on to work at Liberty IT for over a year during my placement year. I am an Oracle Certified Professional Java programmer although my language of choice is C++. Currently I work part-time at the university as a teaching assistant which fits in well with my studies. In my spare time I'm an indie game developer and programming enthusiast. I think that's enough to be getting on with.

Check back here from time to time to see if anything interests you!