Category Archives: Posts

Making a little progress with Erl-ray

I’ve had some time over the holidays to work on the ErlRay project.

I finally got a simple ASE parser working, finished writing the ray-triangle intersection code and did some major refactoring of the tracer code.

The project is still only shooting the first rays, no reflections, no materials, no refractions; just a basic ray caster.

I’m still trying to decide if I should go with something more complex, like Monte Carlo methods and bidirectional path tracing or plain old ray tracing with some global illumination. I hope I’ll decide by the time I have time to work on it again.

Enough talk, here is the latest render:


To get a feeling for the direction I’m going in, here is this scene rendered with Mental Ray:


Erl-ray, distributed renderer written in Erlang

I’m going to be working on a new project in my spare time. A ray-tracer written in Erlang.

It’s hosted on GoogleCode:

The project is intended to be educational, not to create the best render engine or to research some new ray-tracing algorithm.

It will be capable of running on a cluster, rendering a piece of the scene on each node.

It will have a high resistance to errors and will run on any system where the Erlang run-time is supported.

The project is still at the beginning. It doesn’t do very much right now, only producing images with random colored pixels, but it does that in a distributed manner :).

It will not have releases, so if you want the code you will have to check out a copy from the repo.

SUSE Studio

A couple of days ago, I received my invitation to SUSE Studio. I’ve been very excited to try that service; it looked really cool.

SUSE Studio is a service where you can build appliances based on OpenSUSE or SUSE Linux Enterprise Server.

You can do it all online, via a very user-friendly web-app. The coolest thing is that you can even test-drive your appliance from the browser, no fuss, no download, just hit a button and go.

If you’re a Linux shop and you like OpenSUSE then give SUSE Studio a try, it’s a very useful tool.

QBash, displaying quotes with Qt and C++

What’s QBash?

A couple of days ago I started work on a small C++ app in the hope of learning a little bit more about Qt.

It took me about 4 hours to relearn C++ and get used to Qt. In the end I had a working app. Today, I polished it a little and managed to build it on Windows.

The app is small and useless. It gets a page from and displays one quote. You hit a button and it gets a new quote.

I thought it would be a nice project for learning Qt, because it uses networking, regular expressions and the Qt string class.

QBash running on KDE

The thing that impressed me with Qt, was that I was able to build the app on Windows without changing a single line of code. I expect that bigger apps will have some problems.

Get the code

As with all my little projects, you can have the code: qbash.tar.gz.

Now, don’t laugh. My C++ is a bit rusty, I haven’t used it in a couple of years. I’m still getting readjusted to manual memory management.

Django deployed through CGI, on shared hosting

After a couple of hours of work, I finally got Django to work on my shared host.

First the link:, give it a try. (moved hosts and broke the script)

How it works

Django is a Python web framework, it’s MVC, it has a nice ORM abstraction layer, nice form handling and a lot of other goodies for us web developers.

I started playing with it a couple of days ago and I like what I’m seeing.

The only problem is that it needs a proper web host.

To solve this problem, I needed to load the whole framework from a CGI script.

The script I used is:

The code works, but it is a horrible hack and using it will slow any non trivial application to a grind under the slightest of loads.

The python interpreter, the Django framework and your application code is loaded on each request, only to be thrown out after the request’s life ends.

Sounds a little like PHP, the only difference is that mod_php doesn’t create a new process like the CGI protocol does. Don’t get me wrong this difference is significant.

Why did I waste two hours trying to get this thing to work if I can’t use it? Well, I’m a geek, and like any geek, I like these kind of things.

If you want proper Django support, try WebFaction, I’ve been using them for a few years and they’re great.

OpenSUSE easter egg

I think I found an easter egg, LOL. Although I am pretty sure that I’m not the first who stumbles across this, it’s new to me.

OpenUSE installer easter egg

To get this screen just pop in the openSUSE 11.1 DVD and on the boot screen hit F8.

Color replacement in PHP, pixel by pixel

If you’re crazy enough to process an image pixel by pixel in PHP, then you can replace a color with another.

You can get decent results for vector graphics, but it’s slow.

On this one, I won’t explain the code, I’ll just post it and let you figure it out, it has some comments.

The code could be better, but I don’t really have the time or the will to  improve it.

A quick example

Let’s say you have this image (the Ubuntu wallpaper):

Original image

And for some reason, you want to replace the oranges with some sort of violet. You can use Photoshop, or Gimp, or a PHP script to get this:

Image with colors replaced by the PHP script

Above is the result of the script, after 3 seconds of processing.

Download the PHP code

Here is the script: color.tar.bz2. If you can find a use for it, by all means use it.

I have to give credit to Paul Bourke for the HSVToRGB and RGBToHSV functions that I ported to PHP.

Race conditions in PHP and how to guard against them

What do you think? Does PHP need inter-process synchronization?

Although PHP doesn’t support running multiple threads, it runs in a multi-threaded or multi-process environment.

PHP needs some form of mutex

PHP needs mutexes. Sometimes, we need to create critical sections.

You can’t create threads from your PHP scripts, but that’s not a problem; if you need threads to generate some HTML, then something is very wrong.

If you look at the bigger picture, things start to get a little messy – your scripts could run at the same time.

Scripts are started by the web server in response to different, concurrent, HTTP requests. Because of this we need to think about shared resources and some form of locking.

In a normal PHP script, the shared resources are usually files, but they could be anything that’s global and shared by all your scripts.

If you find yourself using shared resources, you’d better start thinking about locking start analyzing your code as if you were writing it in a multithreaded environment.

Don’t believe me? well… take a look in the PHP source at the session handling code; why are they locking the session files?

A quick example

Let’s assume that we run this thing on a server with 2 physical processors.

It just happens that Apache gets 2 requests for our script at the exact same moment. The kernel just happens to schedule Apache’s processes to run on the 2 processors at the exact same time.

Here’s what will happen:

  1. Instance 1 starts and reads the value from the file (assume it’s 0).
  2. Instance 2 starts and reads the value from the file (it’s still 0).
  3. Instance 1 increments the value to 1 and saves it back.
  4. Instance 2 increments the value to 1 and saves it back.
  5. The file contains the 1, not 2.

What you can do about it

If you understood my crummy example above, you’ll notice that there is one function in PHP which can ensure that we don’t use the file from multiple scripts at the same time. I’m talking about flock().

So, we can use flock() to create a critical section and everything will be OK? Well, actually, yes! We can and we should.

Let’s fix the previous example:

  1. Instance 1 starts and locks the file.
  2. Instance 1 reads the value from the file (assume it’s 0).
  3. Instance 2 starts and blocks while locking the file.
  4. Instance 1 increments the value to 1 and saves it back.
  5. Instance 1 unlocks the file and exits.
  6. Instance 2 is resumed and locks the file.
  7. Instance 2 reads the value from the file. The value is 1.
  8. Instance 2 increments the value to 2 and saves it back.
  9. Instance 2 unlocks the file and exits.
  10. The file contains the 2.

That wasn’t so hard! was it?

Just keep in mind that flock() has its limitations. Check out its manual page for more details.

Another thing: keep your critical sections small, or you’ll get a bottleneck where requests are waiting for the same lock.

Use this mutex class

I’ve written a mutex class that you can use: