CUDA, Objective-C and Xcode — Part 2
Posted: July 22nd, 2009 | Author: Thomas | Filed under: Apple, CUDA, Development | Tags: Cocoa, CUDA, Development, Objective-C, Xcode | No Comments »In the last blog entry it was discussed what needs to be installed to get a combination of CUDA, Objective-C and Xcode up and running. This part of the series shows how to actually build a very simple project that uses CUDA and Objective-C.
It is assumed that CUDA 2.2, the NVCuda plug-in and the template are installed at their default locations. Furthermore a basic understanding of Objective-C, Xcode and the IB is required.
This is a step by step guide, how to build the simple project:
- Launch Xcode and create a cocoa application project
File > New Project… > Mac OS X > Applications > Cocoa Application - Name the project “CUDA_Demo_1”.
- Drag the file “/usr/local/cuda/lib/libcudart.dylib” into the project (under the blue icon in the Groups & Files window of Xcode). Copying is not required, but does not hurt either.
- Create a new file where we put the CUDA code into.
File > New File… > Mac OS X > Other > Empty File - Name the new file “cudaCompute.cu”.
- Copy the “Listing One” of Part 1 from Dr. Dobbs excellent CUDA tutorial into the newly created file cudaCompute.cu. Change the line:
int main(void)
into the following line
extern “C” void cudaCompute(void)
And do not forget the closing bracket in the last line. It is missing in the code of Dr. Dobb’s “Listing One”. - Add a new class that will be the AppController instance
File > New File… > Mac OS X > Cocoa > Objective-C class - Name the new class files (.m and .h) AppController
- Declare a IBAction method that will handle a button press in AppController.h
- (IBAction)buttonPressed:(id)sender; - Declare the cudaCompute function in AppController.h
void cudaCompute(void); - Implement buttonPressed: in Appcontroller.m
- (IBAction)buttonPressed:(id)sender
{
cudaCompute();
NSLog(@"The function cudaCompute was called.");
} - Open the MainMenu.xib, put an Object (from the Controller Group) into the xib, set the object to be of the class AppController. Add a button to the window and hook the button to the buttonPressed: action of the AppController class.
- Finally set some options before building the project:
Project > Edit Project Settings > Build - NVIDIA CUDA – CodeGeneration > Device Emulation > Off
- NVIDIA CUDA – CodeGeneration > Host Compilation > C
- Linking > Runpath Search Paths > /usr/local/cuda/lib
- Linking > Other Linker Flags > -lcuda -lcudart
- Search Paths > Library Search Paths > /usr/local/cuda/lib
- Search Paths > User Header Search Paths > /usr/local/cuda/include (activate the recursive flag – it will display /usr/local/cuda/include/**)
- Build the project. Look at the output. Not much to see there. But this is actually a working CUDA with Objective-C example.
Add some printf statements to see the output the input values of the assert command in cudaCompute.cu. This might help to understand and verify that the application worked as it is supposed to. The project file that can be downloaded here – CUDA_Demo_1 – includes a version of cudaCompute.cu with some additional output and comments.
Some notes about this project:
- It is a very simple example and not even using a true CUDA kernel. The example is about mixing CUDA, Objective-C and Xcode — not about state of the art GPU programming.
- The project settings for the section NVIDIA CUDA – CodeGeneration will only be displayed if there is at least one .cu file included in the project.
- libcudart.dylib is the cuda runtime dynamic library
- Xcode seems to remember some settings as new defaults. So I might have missed to mention a change to a required option. Please inform me if you have some problems with the example, I will try to figure it out.
- Hint: Have a look at the CUDA C:C++ App template for the correct build settings.
- NVIDIA released CUDA 2.3 SDK today. Everything above is valid for CUDA 2.2 SDK. No guarantees for 2.3 – though looking at the release notes there seem to be no obvious show stoppers.
- Thanks to “TheBaron” from the #cuda channel of irc.freenode.net for answering my questions with lots of patience.
The next part of this series will most likely deal with the question, how to move data between Cocoa NSArrays and plain C arrays. And I have to learn more about CUDA. So when looking a my schedule, do not expect Part 3 to arrive soon™.
CUDA, Objective-C and Xcode — Part 2
CUDA, Objective-C and Xcode — Part 1

