Integration with XCODE? Anyone created a CUDA template project?

I got it to build the test project without error (in both DEBUG and RELEASE mode)

but trying build-and-go

got this error in RELEASE mode:

dyld: Library not loaded: @rpath/libcudart.dylib
Referenced from: /Users/rg/Downloads/XcodeTest/build/Release/XcodeTest
Reason: image not found

The Debugger has exited due to signal 5 (SIGTRAP).The Debugger has exited due to signal 5 (SIGTRAP).

and this error in DEBUG mode:

dyld: Library not loaded: /Volumes/Builds/nv/build/rel/gpu_drv/r180/r180_00/drivers/gpgpu/bin/i386_Darwin_release/libcuda.dylib
Referenced from: /Users/rg/Downloads/XcodeTest/build/Debug/XcodeTest
Reason: image not found

I tried running the DEBUG product from the command line and got

/Users/rg/Downloads/XcodeTest/build/Debug/XcodeTest
Beginning test…
The total difference between the arrays is -0.000000 for 16 elements
c:
0.885087 0.810088 0.564484 0.966075
0.771194 0.655279 0.955544 0.787532
1.386714 1.213439 1.319069 1.561755
0.837728 0.429731 0.826081 0.526407
reference:
0.885087 0.810088 0.564484 0.966075
0.771194 0.655279 0.955544 0.787532
1.386714 1.213439 1.319069 1.561756
0.837728 0.429731 0.826081 0.526407
Test finished

which looks like it might be right but I think its using the emulator.
Trying the Release product got:

/Users/rg/Downloads/XcodeTest/build/Release/XcodeTest
Beginning test…
The total difference between the arrays is 381667.000000 for 16 elements
c:
1198.885010 979.810059 1130.564453 1220.966064
1198.771240 979.655273 1130.955566 1220.787598
1199.386719 980.213440 1131.319092 1221.561768
107663.843750 53912.429688 93926.828125 112585.531250
reference:
0.885087 0.810088 0.564484 0.966075
0.771194 0.655279 0.955544 0.787532
1.386714 1.213439 1.319069 1.561756
0.837728 0.429731 0.826081 0.526407
Test finished

which looks like it failed.

Have you got round to setting up any of the other examples in Xcode? I don’t understand how to get the project files working in Xcode

RickG,

I got stuck at the same point that you did. You can get xcode to run the executable by changing the active target’s runtime search path in the linking section to be either the project directory (I’m guessing) or /usr/local/cuda/lib (I did) which probably both contain libcudart.dylib.

I also got the issue where there was a disagreement between what the calculation yielded on the device and in CPU emulation. I’ll look at that later tonight probably.

Hope this helps.

-chuck

Hi,

I managed to fix this on my machine by setting the environment variable DYLD_LIBRARY_PATH to “/usr/local/cuda/lib”, as per:

http://www.ikaros-project.org/articles/2008/performance/

Still no luck with the “no rule to process file…” problem in Release.

T.

Fixed this by setting Architectures to “Native Architecture of Build Machine” in the Project info. I’m guessing it’s because there’s no PPC support for the Cuda compiler…?

The example works fine for me now (running in emulation mode). Thanks for the plugin! Hopefully this helps anyone else running into similar problems.

T.

This might be obvious to those experienced with Xcode, but here’s how I got the CUDA examples into Xcode and got them to compile and run…

Create a new project and drag the .cu, .cpp, and .h files into it.

Open up the Project Info and add “/usr/local/cuda/include” and “/Developer/CUDA/common/inc” to Header Search Paths (don’t check recursive). Also, set Architectures to “Native Architecture of Build Machine”.

Then just find the libraries you need (i.e. libcuda.dylib, libcufft.dylib) and drag them into the Xcode project.

Finally, if you click “Build and Go” you’re going to have to make sure the console variable DYLD_LIBRARY_PATH is set to “/usr/local/cuda/lib” (you also need to add ‘/Developer/CUDA/lib’ and ‘/Developer/CUDA/common/lib/darwin’ if you use dynamic libraries from those locations). You can do this by right clicking on the executable in Xcode and going to the Arguments tab.

note:
the libraries for cuda are in ‘/usr/local/cuda/lib’, ‘/Developer/CUDA/lib’ or ‘/Developer/CUDA/common/lib/darwin’
the opengl libraries for examples like nbody are in ‘/Developer/SDKs/MacOSX10.5.sdk/usr/X11/lib’. you need libGLU, libGL, libglut, libGLEW, and libparamgl for those examples

I was able to run it just fine. I had to create the folder for the plugin though. I used the newest version of xcode and CUDA 2.2.

Here’s an Xcode 3.1.3 project template. Unzip the file, and drag the whole folder (titled “CUDA Projects”) to your ~/Library/Application Support/Developer/Shared/Xcode/Project Templates/ folder. You may have to create Project Templates, if it doesn’t already exist. This template is set up basically the same as llamatron’s test project, except that it uses gcc 4.2 and has OpenMP enabled by default. I’ve gotten projects made from it to compile on my machine, so hopefully it works for everyone, but I really can’t make any promises. I understand the template making process rather poorly right now, and this was kinda thrown together. Not sure if it will play nice with the debugger either, for that matter.

Anyway, enjoy, and if it breaks, someone please fix it!

CUDA_Projects.zip (85.6 KB)

Paul

EDIT Looks like it does work with the debugger, in fact.
EDIT2 The «PROJECTNAME» tags weren’t working correctly in the first version of the template, but I’ve fixed them now, and the above linked archive should work properly. Turns out files in a project template must be in Western (Mac OS Roman) encoding, rather than Unicode (UTF-8) in order for the replacement macros to work when you create a new project.

Ok, so I managed to get CUDA working in Xcode with OpenMP. It took me a while, so I figure I might as well post all the details to save others the trouble.

The first problem is that the current version of Xcode uses GCC 4.0.1 by default, which does not support OpenMP. Inside an Xcode project, we can specify which version of gcc to use, but NVCC doesn’t care about our choice, and goes ahead and uses the system default anyway. So you have two options: You can either install the latest version of gcc, as I did, or you can change the system default. Changing the system default to GCC 4.2 (which is included in the latest release of xcode) has the advantage that it is an Apple build, and therefore has all the extra Mac OS X stuff built in. Installing a new release has the advantage that you won’t have to change the system default.

To change the system default, navigate to ‘/usr/bin/’ and type:

‘sudo rm gcc’

then:

‘sudo ln -s /opt/local/bin/gcc-4.2 gcc’

This will delete the existing symbolic link, which points to gcc-4.0, and replace it with one that points to gcc-4.2. Your system default is now changed.

Going the other route, you can download and install macports, and the very nice gui front end, ‘Porticus’, and have it download and install the latest version of GCC (in my case, it was 4.4)'. The catch is that it will download the SOURCE for gcc, and then proceed (very VERY slowly) to compile it. It took about 3-4 hours on my second generation MacBook Pro, so be forewarned. Once the new version of GCC is installed (let’s assume that it put it in ‘/opt/local/bin’, as it did for me), navigate to the install directory and type:

‘sudo ln -s /opt/local/bin/gcc-mp-4.4 gcc’

to make the symbolic link you need (this assumes that you want the gcc-mp-4.4 binary to be the default for this installation, as I did).

Now, the next step is to enable Xcode to recognize .cu files, and let it know what the heck to do with them. For that, you need a slightly modified version of Llamatron’s awesome plugin, which I’m attaching below. The change is that I’ve added a project build option to allow you to pass options to the NVCC compiler directly. So download the NVCuda.pbplugin.zip file, decompress it (should happen automatically), and place it in your ‘/Users//Library/Application Support/Developer/Shared/Xcode/Plug-ins/’ directory. If the plug-ins folders doesn’t exist, just create it.

Next, download the CUDA project template file, ‘CUDA Projects.zip’, and decompress it. This folder goes (for Xcode 3.1) in ‘/User//Library/Application Support/Developer/Shared/Xcode/Project Templates/’. Again, if Project Templates doesn’t exist, just create it. Be sure that when you move the folder, that you move the ‘CUDA Projects’ folder, which contains ‘CUDA C/C++ App’, and not just the sub folder itself. If you forget the ‘CUDA Projects’ folder, Xcode probably won’t recognize the project.

Now it’s time to start Xcode. Once Xcode is up, choose ‘New Project…’ from the File menu, and you should see a ‘CUDA Projects’ category under User Templates. Choose the only option (CUDA C:C++ App), and hit ok.

Now, to test it out, lets add ‘#include <omp.h>’ to the .cu file. If you simply changed the system default compiler to GCC 4.2, then at this point you should be able to compile with no errors. If we forgot something, then you’ll probably get an error saying that omp.h doesn’t exist. In that case, you’re on your own to figure it out. :P sorry. Now, if you installed a new version of GCC, such as 4.4, as I did, then there’s one last step we have to take before we can compile. In the project window, under Groups & Files, on the left, click on your project file, and then the info icon. Go to the Build tab, and scroll all the way to the bottom. You should see a setting labeled, “Custom Options for NVCC”. If you don’t see it, then I screwed up somehow, and again, you’re on your own. Assuming you see it though, this is where we tell NVCC where to look for the new gcc compiler (which knows how to handle OpenMP). The command to insert is ‘-ccbin=/opt/local/bin’ (assuming that you installed the new gcc to /opt/local/bin, of course.) The -ccbin option is the same thing as ‘–compiler-bin-dir’, and just tells it where to look. So NOW, finally, you should be able to Build & Go with no errors. If it doesn’t work, then, yes, you’re on your own. But if I did screw this stuff up somehow, and someone figures it out, by all means let me know and I’ll try to fix it.

Enjoy, and good luck!

Paul

CUDA_Projects.zip (87.4 KB)

[attachment=9876:NVCuda.pbplugin.zip]

Thanks for the plug-in and the template. Both work fine on my system (Xcode 3.1.3, 10.5.7).

As a next step I tried to set up a project that mixes Objective-C and CUDA. The compile stage works OK, but the linking process fails. What I tried was the following:

• Starting with Apple’s Cocoa Application template, I included libcudart.dylib
• Next I tweaked the build setting (-lcuda, -lcudart, /usr/local/cuda/lib /usr/local/cuda/include, GCC 4.2, …) similar to your template.
• Set the Host Compilation to “C”

Next I wrote some pretty basic code to test things:

An AppController class with a simple method (buttonPressed:) to act on a button press. Within this method I call a function “cudaCompute();” that is defined in my .cu file.

But instead of a binary I only get the following message:

“_cudaCompute”, referenced from:
-[AppController buttonPressed:] in AppController.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

I guess, I make some stupid mistake here. But since I have relied on the build in templates up to now, I somewhat helpless with the problems generated here. Any ideas, how I could correct my code?

All the object files are created:
AppController.o
main.o
moveArrays.cu_o

So inside of moveArrays.cu_o there should be the symbol “_cudaCompute”. Or am I missing something? Any help is appreciated.

Thx
Thomas

edit: Some nice guy “thebaron” solved my problem in the chat. Defining “cudaCompute” must be done via
extern “C” void cudaCompute(void) { … }
Well, you live, you learn.

Could you zip and post both the working project and the not-quite-working version so we can see the specifics? It’s good to know about these issues. :D

Thanks

Paul

Sure, no problem.

The zip archive contains two folders “CUDA_Test_working” and “CUDA_Test_fail”. The only difference is within the file moveArrays.cu. As you can see from the comment, the only change is in line no. 13.

Not working: [font=“Courier New”]void cudaCompute(void) { … }[/font]

Working: [font=“Courier New”]extern “C” void cudaCompute(void) { … }[/font]

This is very simple example, using bits of code I found on the net. No additional stuff, like moving data between CUDA and CFArray or NSArray has been implemented.

I hope this helps to clarify my description from the first posting.

Cheers,

Thomas
CUDA_Test.zip (118 KB)

It looks like the plugin is no longer working, with the update to CUDA 2.3. Also, the project template needs to be modified to pass some flags to nvcc to enable OpenMP support.

Unfortunately the plugin is not working anymore with Snow Leopard and XCode 3.2. XCode crashes when launching:

Exception Type:

EXC_BAD_INSTRUCTION (SIGILL)
Exception Codes: 0x0000000000000001, 0x0000000000000000
Crashed Thread: 0 Dispatch queue: com.apple.main-thread

Application Specific Information:
objc[3530]: garbage collection is ON
objc[3530]: layout_bitmap_or: layout bitmap too short: NVCudaOutputParser
NSCFDictionary

Any ideas? Thanks.

Hi,

I wanted to try your xcode plugin attached here,

but when I clicked on the links, I was forwarded to a separate page with download id,

but actual download did not start.

Are you aware of this problem?

Is there any alternative way that I can get the plugin and template files?

thanks,

John

Sorry John, as mentioned above, the plugin no longer works with cuda 2.3 and snow leopard. I’m not sure why the files aren’t available anymore, either. Basically, we’re going to have to wait until someone who understands XCode’s plugin api rewrites the plugin. I’ve tried messing with it a bit, but I can’t really do much to it without the source code, which I don’t have. If you’d like the older, cuda 2.2 and Leopard compatible files, send me a msg and I’ll email them to you (assuming I can find them on my computer, of course).

Paul

hmm, none of the attachment links in this thread seem to be working for me… is anyone else having this problem?

The problem disapeared in the 2.2 version of the sdk release - I am also using the precompiled version of the plugin.

I think the issue may be that the newer XCode version on Snow Leopard is 64 bit, so the 32 plugin is not working.

I got the source and managed to build a 64 bit plugin, which is partially working. It builds and runs a test project. However, it does not currently show compiler settings.

If I get time, I’ll continue to play with it.

One other possibility is running XCode in 32 bit mode with the old plugin - I’ll also try that later on.

One of the problems that I noticed in moving from the 2.2 to 2.3 drivers and sdk is that the CUDA plugin seemed to continue to try to access the 2.2 version of nvcc, and downstream compilers, etc (the install location changed from 2.2 to 2.3, I think). That might need to be looked at. Better yet, it might be nice to be able to explicitly set the path to the bin folder… maybe. Just a thought.

Paul