Showing posts with label cuda. Show all posts
Showing posts with label cuda. Show all posts

Monday, June 8, 2009

CUDA errors propagate

Sometimes if a kernel or a cudaMemcpy fails it will take CUDA some time to recover, this often causes kernels or memcpys that follow the failing one to also fail, or at least not execute.

It is therefore important to always include error checking after of all CUDA calls (as mentioned before here: http://www.herikstad.net/2009/05/cuda-kernel-errors.html)or alternatively use CUDA Utility Library. Also if you get several failures in a row, fix the first error first, since the others might just be because of the first call failing.


To use cutil you need to include the cutil_inline.h file located in /NVIDIA_CUDA_SDK/common/inc in your home directory. After doing so, you can enclose all your communication with the CUDA device with different inline functions that will catch errors. The functions are:
cutilDrvSafeCallNoSync(err)
cutilDrvSafeCall(err)
cutilDrvCtxSync()
cutilSafeCallNoSync(err)
cutilSafeCall(err)
cutilSafeThreadSync()
cufftSafeCall(err)
cutilCheckError(err)
cutilCheckMsg(msg)
cutilSafeMalloc(mallocCall)
cutilCondition(val)
cutilExit(argc, argv)


To use, simply do the following for a kernel:
kernelCall<<>>(d_Data, dataSize);
cutilCheckMsg("kernelCall failed");


Or for a memcpy:
cutilSafeCall( cudaMemcpy(h_Data, d_Data, dataSize, cudaMemcpyDeviceToHost) );


Note: The CUDA Utility Library is a "wrapper" that makes it easier to utilize functions such as __cudaSafeCall()

Tuesday, May 5, 2009

CUDA and double precision floating point numbers

If you are in any way using CUDA to computations involving doubles, you need to perform a few adjustments to have it work. By default CUDA has double support disabled, this entails that all doubles are silently converted into floats inside kernels and any double precision calculations computed are incorrect.

To enable the use of doubles inside CUDA kernels you first need to make sure you have a CUDA Compute 1.3-capable card. These are the newer versions of the nVidia CUDA cards such as the GTX 260, GTX 280, Quadro FX 5800, and Tesla S1070 and C1060.
Thereby you have to add a command line options to the nvcc compiler: --gpu-architecture sm_13 .

Note: you can use sm_11 and sm_12 to enable functionality added in CUDA 1.1 and CUDA 1.2 respectively.

Source: https://www.cs.virginia.edu/~csadmin/wiki/index.php/CUDA_Support/Enabling_double-precision and http://forums.nvidia.com/index.php?showtopic=84999&pid=481809&mode=threaded&start=#entry481809

Friday, May 1, 2009

CUDA and SSE2 intrinsics

Using SSE2 intrinsic calls may speed up your program execution substantially. However, nvcc seems to be unable to compile SSE2 code. For example including emmintrin.h or equivalent will give errors like this:

/usr/lib/gcc/x86_64-linux-gnu/4.1.2/include/mmintrin.h(48): error: identifier "__builtin_ia32_emms" is undefined
/usr/lib/gcc/x86_64-linux-gnu/4.1.2/include/mmintrin.h(61): error: identifier "__builtin_ia32_vec_init_v2si" is undefined
/usr/lib/gcc/x86_64-linux-gnu/4.1.2/include/mmintrin.h(90): error: identifier "__builtin_ia32_vec_ext_v2si" is undefined
/usr/lib/gcc/x86_64-linux-gnu/4.1.2/include/mmintrin.h(114): error: identifier "__builtin_ia32_packsswb" is undefined
/usr/lib/gcc/x86_64-linux-gnu/4.1.2/include/mmintrin.h(129): error: identifier "__builtin_ia32_packssdw" is undefined
/usr/lib/gcc/x86_64-linux-gnu/4.1.2/include/mmintrin.h(144): error: identifier "__builtin_ia32_packuswb" is undefined
/usr/lib/gcc/x86_64-linux-gnu/4.1.2/include/mmintrin.h(158): error: identifier "__builtin_ia32_punpckhbw" is undefined
/usr/lib/gcc/x86_64-linux-gnu/4.1.2/include/mmintrin.h(172): error: identifier "__builtin_ia32_punpckhwd" is undefined
/usr/lib/gcc/x86_64-linux-gnu/4.1.2/include/mmintrin.h(186): error: identifier "__builtin_ia32_punpckhdq" is undefined
.
.
.
Error limit reached.
100 errors detected in the compilation of "/tmp/tmpxft_000010b9_00000000-4_template.cpp1.ii".
Compilation terminated.
make: *** [obj/release/template.cu_o] error 255


To come around this problem, you need to compile the code using SSE2 using gcc and your CUDA code using nvcc and then link them together afterwards.

So create a separate .c and .h file where you create a function that execute the SSE2 intrinsic calls. Include the emmintrin.h file in the .c file, since doing so in the .h will get you the same result as above because nvcc will read the .h file.

To use the SSE2 intrinsic function from your .cu file, you need to include the new .h, but in extern brackets like this:

extern "C" {
#include "yourfile.h"
}


Finally, you need to compile the files separately using nvcc and gcc and then link them together:

gcc cpuCode.c -o cpuCode.o
nvcc cudaCode.cu -o cudaCode.o
gcc cudaCode.o cpuCode.o -o progExe


Note: This is just an illustration, these 3 lines won't work by themselves, you need to include libaries etc.

CUDA "unspecified launch failure"

The error "unspecified launch failure" usually means the same as "segment fault" for host code. Check that your code does not try to access any areas outside the arrays being used. A common mistake is using 'the whole idx' instead of just the thread id to access shared memory. Here's an example:

int idx = blockIdx.x * blockDim.x + threadIdx.x;
shared[idx] = input[idx];


will give you an error and should look like this:

int idx = blockIdx.x * blockDim.x + threadIdx.x;
int tid = threadIdx.x;
shared[tid] = input[idx];

CUDA kernel errors

To print any errors that may be returned when you execute a kernel in a human readable way (you get the errors defined in the programming guide), add the following lines after your kernel call:

err = cudaGetLastError();
if (err != cudaSuccess) printf("%s\n", cudaGetErrorString( err ) );

Monday, April 27, 2009

Setting up a CUDA environment in Ubuntu Jaunty

To write code for CUDA devices you need to set up your system properly. The following explanation is taken from Life of a Programmer Geek. First of all make sure you have a CUDA enabled graphics device, Wikipedia has a list of supported devices.

Install the build tools we need
Open a console window and type the following:
sudo apt-get install build-essential libglut3-dev

Install the NVIDIA drivers
Go to the CUDA ZONE download page and download the CUDA driver. (The Ubuntu 8.04 will work for Ubuntu 9.04 Jaunty).
Press CTRL+ALT+F1 to go to a terminal, log in with your username and password.

Navigate to the folder that you placed the driver file in and type the following:
chmod +x NVIDIA-Linux-x86_64-180.22-pkg2.run
This will first make the driver installer runnable. We then need to stop Xwindows (remember to save any open files). (if you are using KDE use kdm instead of gdm (which is for gnome)):
sudo /etc/init.d/gdm stop
Next we run the driver installation (Usually selecting yes to all questions is ok):
sudo ./NVIDIA-Linux-x86_64-180.22-pkg2.run
Finally we restart the Xwindows session.
sudo /etc/init.d/gdm start

A new error for Ubuntu Jaunty is that upon reboot you might get a message similar to:
(EE)Failed to load module "type1" (module does not exist,0)
(EE)Failed to load module "freetype" (module does not exist,0)
(EE) NVIDIA(0) Failed to load the NVIDIA Kernel Module
(EE) NVIDIA ***Aborting***
(EE) Screen(s) found, but none have a usable configuration.


To fix this problem edit the file /etc/modprobe.d/lrm-video and comment out the line install nvidia /sbin/lrm-video nvidia $CMDLINE_OPTS by putting a # in front of it.
Solution found from: http://ubuntuforums.org/showthread.php?t=950777

Install the CUDA Toolkit
Go to the CUDA ZONE download page again and download the CUDA Toolkit. Open a console window and navigate to the directory containing the file, type the following:
chmod +x cudatoolkit_2.1_linux64_rhel5.2.run
sudo ./cudatoolkit_2.1_linux64_rhel5.2.run

Use the default options suggested by the installer (press enter).

Note: The following steps need to be taken by all users

Add environment variables
This is very important step.
Open a console window to your home directory and edit the file .bashrc, this is the settings file for your console window. Add the following lines to the bottom of the file:
PATH=$PATH:/usr/local/cuda/bin
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda/lib
export PATH
export LD_LIBRARY_PATH

These settings will not take effect before you open a new window so remember to do that.

Install the CUDA SDK
download the CUDA SDK from the CUDA ZONE download page. As with the Toolkit, open a console window, navigate to the directory with the files and type the following:
chmod +x cuda-sdk-linux-2.10.1215.2015-3233425.run
./cuda-sdk-linux-2.10.1215.2015-3233425.run

Accept the default options. The installer will create a folder called NVIDIA_CUDA_SDK in your home directory.

Compile and run an example
Open a console window, navigate to the NVIDIA_CUDA_SDK folder in your home directory, type:
make
./bin/linux/release/fluidsGL

This should open a window with a fluid dynamics simulation.

Writing code for CUDA devices
When writing code for CUDA GPUs its is useful to begin from the examples included in the SDK, the source code can be found in ~/NVIDIA_CUDA_SDK/projects/ (the directory in your home directory). Copy one of the directories and start editing the files in it. To compile the program type make.

If you want to put the files in a different directory or rename or add files to your project, you need to edit the Makefile file in your project directory and possibly also the common.mk file located in ~/NVIDIA_CUDA_SDK/common/ folder.