Cambridge Raspberry Jam: A Box of 200 Rasperry Pi’s…

I’ll put up another article or 2 if I have time about the Cambridge Raspberry Jam event held on the 14th of July 2012… However…

One of the things that struck me was a comment from Eben that he had a box of 200 Raspberry Pis (for sale) and he noted how easy it was to carry it and made a comment about the potential computing power.

It struck a note with me, as some time back (1988-1995!) I worked for a UK based supercomputer company called Meiko Scientific and one of the projects I worked on was a 256-node supercomputer installed at the Lawrence Livermore National Laboratories in California… (It was one of Meikos CS2 units – Computing Surface mk 2)

This is a picture of that Supercomputer, which at the time was the fastest supercomputer on the planet:

The Meiko CS2 at LLNL

The Meiko CS2 at LLNL

Now, without knowing what you’re looking at, it’s hard to get an idea of how big that is… The cabinets are 2 metres high. The whole is arranged into bays of 3-module widths (4 bays) and 2-module width (the smaller one on a diagonal in the middle). It’s that big “L” shape due to the nature of the cabling between the modules – it goes diagonally under the floor. (The middle sections of each bay is a set of network switches) The stand-alone black boxes are RAID arrays with (from memory) 20 x 4GB drives each.. (they were later moved inside some of the modules, but at that time the mechanicals weren’t in-place)

This was 1993/1994 or thereabouts.

Inside that, amongst other things are 256 compute nodes. 4 to one of the smaller modules that you see. Each smaller module is rated at up to 1 KW in power consumption terms. Those compute modules were about 14 inches square and contained a dual-processor sparc at 66MHz and 128MB of RAM. It ran a variant of Solaris. It booted over the network, but each node also had a local 1GB SCSI drive. (There was an additional control bus and “stuff” associated with that to give us node control like power, monitoring and console which added a little to the board, as well as the high speed data network)

So think about that for a moment… A single Raspberry Pi is probably faster than each of those sparc boards. (and at 700MHz vs 66MHz, it’s a lot faster!) The Pi also has double the RAM. The CS2 boards did have a blisteringly fast (for the time) communications network – 750Mbits/sec with a very low latency between nodes. A lot faster than the Pi’s max. of 100Mbits/sec. into a switched Ethernet network, however I could build an equivalent “supercomputer” based on 256 Raspberry Pis, put it under my desk and power it from a 13A socket. And that’s just using the ARMs – not even thinking of the GPUs…

Still – it’s somewhat interesting to think that Eben can carry a box of 200 Pi’s representing something that 20 years ago was simply incomprehensible to think about at the time.

(Minor update to change network speeds to bits/sec!)

7-Segment LED displays and the Raspberry Pi

Chatting as you do about “stuff” on the Raspberry Pi, someone on the #raspberrypi IRC channel mentioned LEDs and driving them. Subsequently someone else on the forums was asking about then too… And so another little project was born…

So off to my (now usual!) online store, SKPang and a 4-digit, 7-segment display module found its way into my order. (Actually, since it has decimal points, then technically it’s an 8-segment display, but for some reason they’re always referred to as 7-segment displays!)

Raspberry Pi with 7-segment LED display

Raspberry Pi with 7-segment LED display

There are many strategies for driving 7-segment displays – I’ve chosen to use one of the lower-level ones, doing more in software than in hardware to keep the hardware design simple – so as you can see here, it’s almost as simple as it can get. We have 8 GPIO outputs going to the 8 segments of the display (white + yellow wires) and 4 GPIO outputs going to the individual digits common line. (the black and green wires) This is a common anode display, so the anodes of each of the 8 LEDs in a digit are connected together.

To drive the display, we start by setting all GPIO pins to outputs, set the 4 digit GPIO pins to logic zero and the 8 segment GPIO pins to logic 0. Now we need to light up the display one segment at a time…We do this by selecting the digit and driving it’s common connection to logic 1 (this is the common anode), then drive the segments one at a time to logic 0 to illuminate them, pause for a brief period of time (I’m using 500 μS here), then turn it off by setting it to logic 1 and moving on to the next segment. 4 digits, 8 segments per digit, 500 μS per segment means that the total scan time will be 16mS, or just over 60 times a second. That ought to be fast enough to eliminate flicker.

To increase scan frequency, we could light up more than one segment, but as they are all going via the same resistor then the segments will get dimmer the more we turn on… To prevent this, we could use 8 resistors, however then the combined current of 8 GPIO pins being fed from one GPIO pin would be too much for the Pi’s SoC. We could then use a seprate drive transistor to boost the current, but we’re trying to keep things as simple as possible here…

So, we illuminate each segment of each digit in-turn and if we do it fast enough then our persistence of vision will fool us into thinking they’re all turned on all the time.

So now we have a problem… How do we scan through the segments fast enough while at the same time allowing our program to run? And, what happens if,  while scanning the segments, Linux decides to go off and so something else? (Linux is a pre-emptive, multi-tasking, multi-user operating system, after-all!)

This is when we really need a real-time operating system, and while there are some patches to the Linux kernel, they’re not standard in the Pi kernels, and even if they were, they still might not be suitable for use here – to do it “properly” really does require very tight control over the hardware, interrupts, peripherals and so on, and I am not convinced that Linux will give us all that control.

However, there are some simple things we can do to improve things – to the point of making a task like this relatively easy and do-able.

This first thing we can do is tell Linux that we want to have a higher scheduling priority and that we want to be considered for real-time scheduling too. We used the sched_setscheduler() system call to effect this. It’s not perfect – a higher priority process can interrupt your program (and if you want to maintain things like keyboard entry and allowing other programs to run, then you must allow this!) but it’s a good way to give your program a good boost.

The other thing we can do is to use Posix Threads and run what’s effectively a 2nd program concurrently with your main program, and have that 2nd program (although it’s actually a function inside your main program rather than a 2nd program) manage the LED updates.

As long as the LED display routine calls delay() every now and then, then the rest of the system will carry on which the display is kept updating. (It can use other methods to de-schedule itself, but calling a wait function is easiest – I actually call delayMicroseconds() in the display code).

All that remains is to establish a way to communicate between our main program and the display routine and I use a simple global variable here: A string array which I can write to in the main program and read from in the display routine – we don’t need any fancy locking, etc. for something as simple as this.

Even after that, setting the real-time priorities and running the separate display thread, I still see the occasional flicker or glitch on the display. It’s probably no real issue for a display, but imagine if we were driving a stepper motor… One reason I still maintain that Linux is really not the right tool to do that form of “hard” real-time control, but for LED displays? it’s OK.

Any down-sides? Well the overhead of keeping the display updated is between 15 and 20% of the CPU usage on my Pi! So it has to be said that the Raspberry Pi really isn’t the right device to be driving such a display, but it’s good to know that if we had to then we can.

And I’m sure some of the tricks in my program will be useful in other applications. You can find the software here.

Finally another of my most excellent videos:

LAMP on a Raspberry Pi

So after a conversation on IRC where the general gist was that apache was bloated and slow vs. lighttpd, I thought I’d try a little experiment today – see how long it might take to compile up Apache, PHP and MySQL and get it going on a Raspberry Pi… Then stick a wordpress on it.

(And in-case you found this looking for a comparison, sorry – I don’t have one, lighttpd may well be smaller & faster than Apache, but I’ve not done any tests myself – I was just interested in how well or badly my “usual” apache/mysql/php setup I employ on my server fared on the Raspnerry Pi)

So I decided to compile them from sources – which is what I often do anyway rather then use the Debian supplied versions – mostly to see how long it would take doing it that way, but also to try to only compile in the modules/features, etc. that I needed – hopefully to make it as “lean” as possible while still maintaining enough functionality to run WordPress.

My Pi was running Raspbian with a 3.2.21 kernel, and the memory split at 244MB.

Versions of the “AMP” stack are: Apache: 2.2.22, MySQL: 5.1.61 and PHP: 5.3.10.

MySQL took some 2.5 hours to compile and resulted on about 260KB of swap being used. Apache was a bit quicker at 1.5 hours, but surprisingly it used a little more swap – up to 680KB. The configure and compile for PHP took it up to nearly 900KB. (however I did start up MySQL during this phase to finish the installation of it, but shut it down once I’d finialised the installation)

Watching the system with ‘top’ while it was compiling, it seems the GCC compiler doesn’t really need much more than 60-80MB of RAM, so the few little things that got swapped out are really just making more buffer space – so I imagine the compiler and associated programs stay cached in RAM for the duration while will speed things up somewhat. The bottleneck really seems to be the CPU, so I doubt putting the files on USB drive, NFS, etc. would make any difference to it all – the system runs at >95% CPU running gcc for a good few minutes for each file, so optimising the few seconds of disk IO doesn’t really seem worthwhile.

Typical of top during PHP being compiled:

KiB Mem:    222744 total,   185080 used,    37664 free,    10000 buffers
KiB Swap:   131068 total,      868 used,   130200 free,    71484 cached

  PID USER      PR  NI  VIRT  RES  SHR SWAP S  %CPU %MEM    TIME+  COMMAND                    
14053 root      20   0 83580  73m 6904    0 R  98.1 33.6   3:00.28 cc1

The whole process is very reminiscent of installing the LAMP stack on an older server – a PIII/800MHz many years ago. (And I had about the same amount of RAM then too!)

… Some time later and it’s running.

And so what can I say… It’s slow. I’ve tweaked the apache config somewhat and it’s still slow. What makes it slow… It seems to be a conbination of lots of things – PHP is quite large, so that eats up some RAM. MySQL likes to do fflush() operations which make the underlying filesystem somewhat slow. (however using phpMyAdmin on the server was OK, so maybe wordpress really is a bit big and bloaty afterall!)

Next week (if I have the time and enthusiasm!) I’ll start to try some of the newer servers and configurations and so on… Watch this space, but don’t hold your breath!

Another update to wiringPi

Thanks to Keith Wright for diligently testing it and pointing out bugs for me – fixed some issues in the gpio command, and while I was at it, I added in 2 new functions to it to allow for easy exporting/unexporting of the /sys/class/gpio interface – so now you can write a little script to do the exports for you, then run your own program that uses that interface (e.g.) a Python, shell, or php script, then un-export them afterwards, all without needing to be root (or use the sudo command)

WiringPi – Another bug, another day…

Just uploaded a new version of wiringPi  with a few bug fixes. the main one being in digitalWrite when the range check for the pin number was fine when it was wiringPi pins, but not fine for the newly introduced GPIO pin numbers.

I’ve also added in some additions – a shiftIn/Out module and a serial port access module. These are just handy additions, but I use them myself in other projects, so it’s a handy place to keep them.

As usual, fedback welcome!


Raspberry Pi – Game Port IO?

My little ladder game seems to have generated some interest – or was it my comment about the GPIO port standing for Game Port IO? Who knows, it’s fun anyhow…

But it’s got me thinking – what else can I do with a row of LEDs and a button. The Ladder game isn’t original, it was from a magazine published over 30 years ago, but what else can I think of to use on the same platform…

Some sort of reaction tester? shooter? Will crank up my imagination and see what comes up, but suggestions are welcome!


No updates for a while as I’ve spent most of this week cooking. If every you think that running a little baking hobby business is fun, then great, it is fun! Until you decide to take on doing an afternoon tea for 100 people…

More when I can get more time out of the kitchen… Lost count of the number of scones but I think it’s close to 200. 8 big cakes (Lemon Drizzle, Chocolate, Victoria sandwich and coffee & walnut), 100 mini cup-cakes, and 240 slices of bread into dainty sandwiches!

Then there’s tea, lemonade, lashing of ginger beer (non-alcoholic) and so on…

If only the weather was looking better… Ah well, we do have an indoor hall as well as a big garden!

— Quick update after the event. A huge success with most of the food eaten! If you want photos, they’re over on our Moorbakes site.