miércoles, 15 de febrero de 2012

Compiling Cuda C code in Linux

If you don't know how to do it, here I'll explain in short how do you install the CUDA toolkit and what do you need in order to compile CUDA C code.

First of all, this is just to COMPILE, not to run, I can't do that part of the guide because I don't own a Nvidia graphics card or whatsoever, and of course I'm not going to post someone else's guide to that if I can't test if it works. Knowing that lets begin.

First, download the CUDA Toolkit 4.1 from here. I downloded the Ubuntu 10.04 version because I have Ubuntu 10.10 which is an earlier version and I didn't had any problem with it. It weights around 189 MB so be patient while it downloads.

When the download finishes, run the downloaded package. It should be called something like cudatoolkit_4.1.28_linux_ubuntu10.04.run(depending on the version you download, and your OS this will change obviously), give it executing permissions using chmod a+x cudatoolkit_4.1.28_linux_ubuntu10.04.run and run it with ./_ubuntu10.04.run.

If you can't, use sudo su and type your password to identify yourself as root, and run it again. This should produce an output like this:


As we can see, it will ask you the install directory, if we don't really care just press enter and it will be installed on /usr/local/cuda.

When it finishes we will probably need to set the PATH to this directory, or else we will not be able to compile our code, to check if your PATH is set try using the nvcc command, if you don't have the PATH set, the terminal will output something like "No command 'nvcc' found...", in that case we will need to set the PATH using: export PATH=$PATH:/usr/local/cuda/bin. Like this:


As we can see at first it didn't recognize the nvcc command, but after setting the PATH the nvcc command was recognized.

Now we can try to compile our own code in CUDA C, for this you can use the following Hello World example:


Try to compile it with nvcc filename.cu(notice the .cu extension). It should just produce a file named filename.cu.cpp.ii. Like this:
If you get the "Error trying to exec 'cc1plus'" message, install g++ and that should fix it.

As a compiler, if the code has syntax errors, or something like that, it shows you the line and the type of error the code has. For example making a simple ";" missing error produces the following feedback at compiling.(Which in this case doesn't tell anything about the ";" missing)


And that is all about compiling CUDA, for other weekly reports in class, I will probably try to compile some Merge Sort, Bubble Sort, or something like that in CUDA and I will ask someone else to run it for me.

No hay comentarios:

Publicar un comentario