It’s not that I’m so smart, it’s just that I stay with problems longer.
— Albert Einstein

CUDA, Objective-C and Xcode — Part 2

Posted: July 22nd, 2009 | Author: Thomas | Filed under: Apple, CUDA, Development | Tags: , , , , | 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:

  1. Launch Xcode and create a cocoa application project
    File > New Project… > Mac OS X > Applications > Cocoa Application
  2. Name the project “CUDA_Demo_1”.
  3. 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.
  4. Create a new file where we put the CUDA code into.
    File > New File… > Mac OS X > Other > Empty File
  5. Name the new file “cudaCompute.cu”.
  6. 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”.
  7. Add a new class that will be the AppController instance
    File > New File… > Mac OS X > Cocoa > Objective-C class
  8. Name the new class files (.m and .h) AppController
  9. Declare a IBAction method that will handle a button press in AppController.h
    - (IBAction)buttonPressed:(id)sender;
  10. Declare the cudaCompute function in AppController.h
    void cudaCompute(void);
  11. Implement buttonPressed: in Appcontroller.m
    - (IBAction)buttonPressed:(id)sender

    {
    cudaCompute();
    NSLog(@"The function cudaCompute was called.");
    }
  12. 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.
  13. Finally set some options before building the project:
    Project > Edit Project Settings > Build
  14. NVIDIA CUDA – CodeGeneration > Device Emulation > Off
  15. NVIDIA CUDA – CodeGeneration > Host Compilation > C
  16. Linking > Runpath Search Paths >  /usr/local/cuda/lib
  17. Linking > Other Linker Flags > -lcuda -lcudart
  18. Search Paths > Library Search Paths > /usr/local/cuda/lib
  19. Search Paths > User Header Search Paths > /usr/local/cuda/include (activate the recursive flag – it will display /usr/local/cuda/include/**)
  20. 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


CUDA, Objective-C and Xcode — Part 1

Posted: July 20th, 2009 | Author: Thomas | Filed under: Apple, CUDA, Development | Tags: , , , | 5 Comments »

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”

  1. Extract the file CUDA_Projects.zip
  2. 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”

  1. Extract the file NVCuda.plugin.zip
  2. 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

CUDA Project Template

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.

CUDA Project Settings

So far — so good. In a soon™ to be published follow up I will explain how to combine Cocoa and CUDA.


Installing CUDA on a Mac

Posted: May 25th, 2009 | Author: Thomas | Filed under: Apple, CUDA, Development, Technology | Tags: , , , , | 1 Comment »

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.