Sunday, January 25, 2009

CUDA in Code::Blocks

I recently installed the Code::Blocks IDE at uni, and, after some initial success, suddenly came across a few issues when trying to use it in conjunction with NVIDIA's CUDA compiler "nvcc". Some quick 'net searching got me nowhere, and I realised my productivity boost may be short-lived. But I was not going to give up, and in a moment of caffeine-powered strength, I found the courage to push past the "Are you really sure you know what you're doing!?" warning messages and delve deep into the Code::Blocks settings dialogs. What I found changed the way I look at compiler flags forever...

The first problem I noticed was the fact that nvcc accepts compiler options in a different format to, say, gcc. The 'net quickly led me to the following syntax:

$compiler --compiler-options "$options" $includes -c $file -o $object

which can be plopped right into Code::Blocks's compile-command format setting. Bye-bye compiler whingeing.

The second problem became apparent when I discovered that error (and warning, etc.) messages were not being parsed and passed to me ("definitely not legal C++;" is definitely not legal C++, even according to nvcc). The syntax of messages reported by gcc (file: line: message) and nvcc (file (line): message) differ very slightly (who knows why :/), meaning Code::Blocks doesn't naturally pick up nvcc's messages. To make things worse, nvcc calls gcc to compile the non-CUDA parts of your code anyway, meaning you are returned error messages in both formats. The road ahead was climbing steeply...

Fortunately, Code::Blocks allows the exact parsing syntax to be defined manually, in the form of regular expressions. This immediately came to mind, as my ventures of only a couple of days before came to the rescue. [Warning: If you don't know regular expressions, look away now!]. I replaced

:([0-9]+):

with

[(:]([0-9]+)\)?:

in the compiler error, warning and "Instantiated from" message parsers, and was suddenly greeted with the delightful, if slightly confusing: "‘definitely’ was not declared in this scope".

I have a feeling there are more surprises lying in wait, but for now I have all the functionality I need. To make things a little more explicit, you can look at the relevant excerpt from my Code::Blocks config file here.

Let me know if you've found this useful, had similar experiences, or know of an existing solution!

5 comments:

Anonymous said...

dude can u add more detail on how to get codeblocks to use nvcc? i can't even select the nvcc compiler on codeblocks. help!

Ben said...

Thanks for pointing out my gross lack of detail! See the new post, I hope that helps!

Anonymous said...

oops.. i didn't see your latest post. i shall try them. thanks!

Anonymous said...

oh you just put them up! cheers :)

Anonymous said...

Thanks it helped me ;)