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
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™.
Xcode is the preferred integrated development enviroment (IDE) for most Cocoa developers. It offers good support for a variety of languages, esp. C, C++ and Objective-C. It is possible to extend Xcode to handle matters which Apple does not support out of the box. This post shows how to integrate CUDA with Xcode.
This howto guide was written for Xcode 3.1.3, Mac OS X 10.5.7 and CUDA 2.2. Keep in mind, that later versions may break the following procedure.
To use CUDA the basic files from NVIDIA are required.
CUDA Toolkit 2.2 for Mac OS
CUDA SDK 2.2.1 code samples for Mac OS
(optional) CUDA Visual Profiler v1.2 for Mac OS
When installing these files, take care to choose custom install and mark the CUDA.kext to be installed. See the note on my earlier post about this.
In the next step two files are required: a plug-in to introduce .cu files to Xcode and a template to set up Xcode for CUDA programming. You can download both files from the NVIDA forum in the CUDA on Mac OS X section, specifically posting #29 in this thread. Some very talented guy called Paul T. Thompson created a plug-in and a template. Download the two files labeled “CUDA_Projects.zip” and “NVCuda.pbplugin.zip”.
As described by Paul, these files need to be copied to specific places in your $HOME directory. Here’s the summary:
“CUDA_Projects.zip”
Extract the file CUDA_Projects.zip
Copy the folder CUDA Projects to the folder $HOME/Library/Application Support/Developer/Shared/Xcode/Project Templates/
If some of the folders do not exist in your $HOME folder, simply create them.
“NVCuda.pbplugin.zip”
Extract the file NVCuda.plugin.zip
Copy the file NVCuda.plugin to the folder $HOME/Library/Application Support/Developer/Shared/Xcode/Plug-ins/
Again, if some folders do not exist in your $HOME folder, simply create them.
Basically, that is all, what needs to be done. Launch Xcode and verify the correct installation of CUDA and the plug-in as well as the template. When creating a New Project… , there should be a new choice if the template was copied to the correct location:
User Templates > CUDA Projects > CUDA C:C++ App
Select this and create a new CUDA C:C++ App. To verify the NVCuda.pbplugin, select
Project > Edit Project Settings > Build
Near the bottom of the list should be a section named NVIDIA CUDA – CodeGeneration. If you see this, the plug-in was installed correctly. By default, Device Emulation is active. In the screenshot below it is deactivated.
So far — so good. In a soon™ to be published follow up I will explain how to combine Cocoa and CUDA.
The iPhone OS 3.0 will be released soon. From my experience with the beta of iPhone OS, ColorRef version 1.0.1 should still be usable with iPhone OS 3.0. You are going to notice some quirks, but no real show stoppers.
An updated version of ColorRef will be available soon after the release of the iPhone OS 3.0. Currently the internal build is working fine and could already be released … if, well … actually Apple does not allow any application built with the beta SDK into the App Store. So all I can do is test but not release.
I guess, during the WWDC we are going to learn about the time schedule of the final SDK and the iPhone OS 3.0. I am going to release an update as soon as possible. Hopefully the App Store review team will not drown under the torrent of apps rebuilt for iPhone OS 3.0. But with over 35.000 applications available I see some long shifts coming for the review team during the next weeks.
I had some trouble to put the iPhone OS 3.0 beta5 on my first generation iPhone. Without going to much into details, I thought that I had bricked my phone.
Fortunatly, there are some usefull hints in the Apple iPhone Developer Forum. One recommendation was using iTunes’ restore function instead of the Xcode organizer. I guessed the difference lies in using a simple update (Xcode) or a full restore (iTunes) which possibly includes some DFU-Mode access.
So, I wanted to know, what exactly is happening and took a look at the Console log. Look what I found:
iTunes uses Jedi mind tricks to … to trick my MobileDevice. Amazing. Someone at the iPhone/iTunes team at Apple seems to be a huge star wars fan.
CUDA is the Compute Unified Device Architecture from NVIDIA. It’s an environment for using the massive parallel capabilities of your video card for general purpose computing. If this sounds strange to you, just check out the description at the wikipedia.
In April version 2.2 of CUDA was made available at the CUDA Zone. I was eager to try this on my new MacBook Pro — since it is my first x86 Macintosh that uses an NVIDIA GPU. The installation of CUDA is described in the “CUDA SDK Getting Started” document. Here is one pitfall you might want to avoid.
There was no previous installation of CUDA on my system (Mac OS X 10.5.7). The default setting did not install the CUDA kernel extension to /System/Library/Extensions/CUDA.kext. I had to select custom install and check mark the CUDA Extension in the installer dialogue.
Without the extension, the examples to verify the correct installation run only in the emulator. So check the installation settings and make sure that the CUDA extension is installed, too. Oh, and don’t forget to update your .bash_profile as shown in the ”getting started“ PDF from NVIDIA.
Once you have everything up and running, try the smokeParticles application. It’s really awesome. I didn’t find a video of the smokeParticles demo, so I created one. BTW, it uses all three mouse buttons — so check your system setting for your mouse.
Before the availibility of Objective-C 2.0 the usage of Key-Value Coding accessors required some boiler plate code. Objective-C 2.0 introduced the @property and @synthesize directives and reduced a lot of boiler plate code into two simple statements. Which is kind of nice.
Nevertheless there might be circumstances that force you to implement KVC accessors manually. But you do not have to type all the accessor methods code by yourself. Xcode can take care of this task. Here is how to do this.
Within you .h file, create the property you need for your application. Let us assume you need an array like this:
…
NSArray *someArray;
…
Select the whole line where you just defined your NSArray instance variable and go to the script menu (it is the one with the ancient scroll icon). Select the command
Script Menu > Code > Place Accessor Decls on Clipboard
Next, paste the declarations to your .h file. Select the whole line with your NSArray instance variable again, this time select the command
Script Menu > Code > Place Accessor Defs on Clipboard
and paste the content of the clipboard to your .m file in the @implementation part of your code.
While this is only a minor help for properties of the NSArray class, try the same for the NSMutableArray class. Now we are talking about time saving. Thanks, Xcode team!
Due to my day job, I’m a little bit late with my announcement — ColorRef 1.0.1 has changed to “Your application status is Ready for Sale” on March, 2nd 2009. At least this is what the e-mail from Apple claimed. To be closer to the truth, ColorRef is still priced at zero $, so there isn’t much of a sale. The changes and added features are described in my previous post, just in case you want to look at it.
Some of you might have been a little bit confused last week because the screenshots and description at the App Store showed the 1.0.1 release while the actual binary was still the 1.0 release.
I don’t know if I made a mistake within iTunes Connect, but the description and the screenshots have been updated nearly immediatly, while the binary took some time for the review. Only the text describing the changes was updated at the same time as the release of the binary. This happened although I entered all information into the sections for “update”. Well, you live, you learn.
So my advise for using iTunes Connect and updating your app: Change the images and the description not before the release of your updated binary. It might confuse your customer if the sceenshots and description don’t match your application.
ColorRef 1.0.1 was submitted to the App Store today. Usually it takes it takes Apple only a few days to review an app. You can expect to get the update via iTunes by the end of the week.
What has changed from the initial release? A brief run down:
Themes can be renamed.
Themes can be reordered.
Colors can be edited.
Colors can be reordered.
The color picker has a new RGB/Hex control.
No more built in themes.
To be honest, I gave ColorRef 1.0.1 the codename “Missing Feature Release”. The omission of editing controls — reordering, editing names or colors — was quite obvious. Adding these features got high priority on my to do list for ColorRef.
In order to implement these features, controls are required to activate the editing mode. The natural place for this is a toolbar at the bottom of the screen. So I dumped the idea of the tab bar — which actually brought ColorRef closer to the recommendations of the HIG.
A tab bar gives users the ability to switch among different modes or views in an application.
For my understanding emphasis should be placed on “modes”. If you look at the iPod application on your iPhone, the black tab bar switches not only between views but between play modes. The clock application uses the tab bar to switch between different modes – not between different data sets.
The tab bar is gone, the toolbar takes it place and there is the space for additional controls. Like — for now — the edit button. From this button features like reordering, deleting and editing of tables and their entries can be accessed. All these features have been implemented as standard controls — this should reduce the amount of learning to use the controls to zero. One of the benefits of using standards.
So, I hope you like the “Missing Feature Release”. Actually the screenshots at the App Store have already been updated. The binary file … well, soonish. You can preview the ColorRef 1.0.1 release at the App Store. If you encounter any errors or unexpected behaviour please send me an e-mail or leave a comment.
First things first – because my target audience is international I use the english language from now on in the blog entries.
OK, next up: I succeded on the second part of Aaron Hillegass’ Challenge No. 35. Feeling kind of proud – and very motivated.
Finally: Thanks for the feedback on ColorRef. There are some valid issues I’m going to address in a upcoming release.
An option for editing theme names and editing colors will be added.
The reordering of entries will be implemented.
Adding colors in hexadecimal format — should be possible.
Moving the EM/SE-Theme to the favorites — very likely.
About the EM/SE-Theme: These are from my employer and originally I used ColorRef only for the purpose of having a handy “digital color reference” app. It evolved into a little more usage scenarios. This evolution left some traces, like the Tab-Bar at the lower section. This will very likely change soon.
Again, thanks to everyone who took her/his valuable time and supplied me with feedback. Your help is very appreciated.
P.S.
You can delete a theme name or a color with a horizontal swipe. I expected this to be obvious — but in fact I was wrong on this. I added a note to the description in the App Store.