For some reason I've started looking at the sample code for Microsoft's C++ AMP: Accelerated Massive Parallelism. The temptation to offload work onto the GPU has finally proved too much. Actually, I've looked at various ways to do that in the past, but then slowly walked away...
This time I got as far as calculating the Mandelbrot set by means of C++ AMP, but I won't bore you with that, and besides, Microsoft have provided their own sample code and it's probably better than my version. But when I looked that their code for calling C++ AMP from C# code I was reminded how simple it is to call C++ code from .Net, and I started to wonder why I don't do this more often - even without any Accelerated Massive Parallelism trickery. For example, say you're doing some repetitive calculations, why not do the maths in a small bit of C++ code and get a huge speed improvement? It's not that difficult to do a simple calculation in C++ and you don't need lots more code.
So I spent a lunchbreak writing a sample project to calculate the biggest distance found between an array of points (x,y coordinates). I implemented the same calculation in both C# and C++ and then ran it on an array of 10,000 random points. Using the C++ code, without any help from AMP, it was about ten times faster - even thought I was calling it from within my C# console app:
Wow. That's quite a big difference. Note: I'm computing the distance between every combination of two points in the array and returning the biggest distance found.
Now ... I may not be able to do this type of stuff during my day job, where everything needs to be done in C# only, but I think that I may be tempted to do this type of stuff in my own time when I'm just messing around.
UPDATE: you'll probably need to recompile the library from source, read here for instructions.
So, here is the Raspberry Pi GPIO shared library that I have been working on. When you look at the code, you'll see that
I can't really take the credit, the bulk of the code is the same as shown here on the Raspberry Pi wiki.
What I have done is fiddle with some compiler options and done some minor tinkering in the code to suit my purposes and style.
And it seems to work OK, I'm mainly using it from Mono. It's not the only way to get GPIO access using Mono (another approach is demonstrated
here for example), but it's the approach that I've chosen.
It means that I can carry on tinkering with the C library without recompiling my Mono code. You could also build versions
of the same library for other hardware and then run the same Mono code on different machines. I've made a stub version of
the library which allows me to test Mono code on the vitual machine I've set up to run Debian.
The source code for the library is in this zip file. You can recompile from source on the Raspberry Pi (instructions are
in the C code) or you can just copy the
libpigpio.so file to your /lib folder. You can't hold me responsible
if you blow up your own Raspberry Pi though! I have not yet tested the code that gets values from the GPIO, I've only
tested the setting of pins at this point. You have been warned.
Of course, you don't have to use this library in Mono, you can use it in C programs too (as well as any other
language that supports calling shared libraries). But I've only tested calling it from C and Mono.
Anyway, I hope that it's useful to somebody ... or maybe just interesting. At some point I'll post an example C#/Mono program that uses the library. Have fun!
I am currently in the queue to receive a Raspberry Pi. Apparently my machine is about 5 weeks away. So I have decided that I should begin making some preparations. I have downloaded an ISO image of Debian "squeeze" (the version with the LXDE desktop). So at least I should know my way round the OS in advance.
After installing the Debian ISO on a virtual machine, the first thing that I did was install the monodevelop IDE which I don't think will actually work on the Raspberry Pi ... but should enable me to try some things out and then just compile them from the command line on the Pi (hopefully).
Monodevelop seemed to work like a charm on Debian, so I grabbed my C# webserver example to try out some code.
Amazingly, the only real thing that I had to change was add the following delegate declaration:
public delegate TResult Func<in T1, out TResult>(T1 arg1);
...apart from that the code worked without any changes. Here is a screenshot of it all working:
So I am hopeful that I will be able to run my little slimline webserver on my Raspberry Pi when it arrives.