Showing posts with label How To. Show all posts
Showing posts with label How To. Show all posts

Tuesday, 4 March 2014

Set Up Visual Studio 2012 for CUDA Dynamic Parallelism

In this post i am going to show you how to set up Visual Studio 2012 to compile CUDA code which uses dynamic parallelism.

What is Dynamic Parallelism?


Dynamic parallelism enables the GPU to create new work for itself without ever having to involve the CPU by allowing CUDA kernels to call other CUDA kernels.

Nvidia 2013
This is a powerful tool for CUDA developers as there is now no longer the need to pass data between the host and device as launch configuration decisions can now be made at runtime by threads executing on the device.

In order to make use of dynamic parallelism you must have an Nvidia GPU with compute capability 3.5

Dynamic Parallelism Example


As with every first code example we will look at a simple 'hello world' program which uses dynamic parallelism. 


Here we have two kernels, the parent kernel and the child kernel. Out parent kernel calls the child, waits for it to complete its work and then carries out some work of its own. 

When we initially try and build this code using visual studio we will get the following error. 


Visual studio is complaining that we can only call kernels within kernels if we have a card of compute 3.5 or higher. To let visual studio know that we do infact have a suitable card we need to change a few properties. 

To do this we open up the project properties by right clicking on the project. 



The first setting we need to change is to tell CUDA to generate relocatable device code by selecting 'Yes (-rdc=true)'


Next we have to tell CUDA to use compute 3.5 by changing the code generation to 'compute_35,sm_35'


We then need to tell the runtime library to use the multi threaded library. If we are in debug mode we select 'Multi-Threaded Debug (/MTd)' or if we are in release mode 'Multi-Threaded (/MT)'


Finally inside the linker properties we add an additional dependency to 'cudadevrt.lib' 

After following these steps our project will now build successfully and we should get the following output. 


This has been to mostly help remind myself how to set up visual studio in the future if i forget but hopefully it can help out a few other people as well. 

Any questions or if you are struggling with anything feel free to send me an email at craigmcmillan01@outlook.com with blog in the subject and i will be happy to help. 

Sunday, 3 February 2013

How to Export to Android Using Unity


Here is how I was able to set up the android sdk and export from unity to my android phone. I am not sure if this is the correct way or the best way but it is how it worked for me.

Firstly you need to get yourself a pro version of unity you can either buy the expensive licences which are $1,500 for the pro version and $400 for the android license or you can get the 30 day free trial which is what I would recommend.

You can get unity by going to the unity website http://unity3d.com/unity/ and clicking on the download link in the top right corner. Once it downloads it will pop up with a choice for either a free license or a 30 day free trial for the pro version, select the pro version and make an account if you need to or log in with your email address and password.  

Next you need the android sdk which you can get from http://developer.android.com/sdk/index.html

Click on the blue button to download the ADT bundle for windows.


On the next screen agree to the terms and conditions and download the 32-bit version.  You could try 64 bit but I have heard of there being problems with using the emulator so I stuck with the 32-bit.


Once the zip folder downloads I extracted it to my C: drive.

Once you have done this if you open up the folder there should be 3 items, an eclipse folder, sdk folder and an application called SDK Manager.

If you open up the SDK Manager application it will have a list of all the different versions of android and two folders one called tools and one called Extras. You want to open up the extras folder and make sure that the Google USB Driver is installed as you may need this driver to use your phone properly.

Before you plug your phone in you need to enable USB debugging inside the developer options which can be found within the settings on your phone.

If you now plug in your android phone, it should automatically install the drivers for you and your phone should be recognised as an Android device in the device manager. If not you will need to install the Google usb driver that we just downloaded.

You can install the driver to your phone by selecting your phone in the device manager.
In my case my phone was under the portable devices.


If you right click and open up the properties and then go to drivers and then update driver and select “browse my computer for driver software” and find the following folder.

And hit next the driver should be installed and your phone should be recognised as an android device. If not then I’m sorry you are on your own I don’t know how to fix that L.

Once this is set up we should be able to make a new unity project which can be exported to the phone.

I am assuming you know how to do some basic things in unity, like make a new project, add objects and scripts to the project etc.

First thing to do is to make a new project in unity which we will use to export to our phone.

Inside this new project let’s add a cube and a plane and we will move the cube about using the accelerometers. 

So once you have added the cube and plane and a directional light you should have something that looks like this.

 Once you have this add a new c# script called MovableObject and input the following code


 Drag this script onto the cube object so that it is attached. This will allow us to move an object using the accelerometers.

Next we need to set up unity to get it ready to export to the phone. To do this we need to go to Edit->Project Settings->Player and you should be presented with the following screen.




You want to select the little android icon and set the default orientation to landscape left. And then select the Other Settings option lower down the page.



Here we need to make sure that the company name and product name match up with the bundle identifier. It should be com.CompanyName.ProductName so we need to change that to match the company name and product name we have chosen. This step is important or else it will crash when you try and export.

After this we are almost ready to build and run. We just need to set up the build settings.

To do this goto file->Build Settings


Here you need to select android and then add current to add the scene that we want to export to the build. Once you have done this your screen should look the same as the one above and we should be able to hit build and run!

Save the file anywhere you would like and name it whatever you want, Remember your phone needs to be plugged in.

The first time you run, unity will ask for the location of the android sdk, here find the folder that we downloaded earlier the one called adt-bundle-windows-x86_64 and select that.

It will come up saying it is building the scene and pushing to the device.



Unity should open up on your phone and there should be a cube which you can move about the screen by tilting the phone!

I hope this was helpful for you and made sense  it was done in a bit of a rush but if you have any questions email me at 10004794@napier.ac.uk and I will see if I can help.