Friday, December 17, 2010

What is OpenMP? What does OpenMP code look like?

OpenMP is an easy cross platform way of making code multi-threaded. You just sprinkle in a magic pragma before a for loop, and presto, the cycles of the loop get spread across multiple cores.

Here’s a simple example (written in Visual Studio 2008).

#include "stdafx.h"
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

int _tmain (int argc, char *argv[])
{
#pragma omp parallel for
    for (int i = 0; i < 20; i++)
    {
        int threadId = omp_get_thread_num();
        printf("Hello World from thread %d\n", threadId);
        if ( threadId == 0 )
        {
            printf("%d threads\n", omp_get_num_threads());
            printf("In Parallel Region = %d\n", omp_in_parallel());
        }
    }

    _getch();
    return EXIT_SUCCESS;
}

Here’s the output on a machine with 12 cores:

Hello World from thread 0
There are 12 threads
In Parallel Region = 1
Hello World from thread 0
There are 12 threads
In Parallel Region = 1
Hello World from thread 11
Hello World from thread 3
Hello World from thread 3
Hello World from thread 9
Hello World from thread 1
Hello World from thread 1
Hello World from thread 10
Hello World from thread 7
Hello World from thread 7
Hello World from thread 8
Hello World from thread 6
Hello World from thread 6
Hello World from thread 4
Hello World from thread 4
Hello World from thread 2
Hello World from thread 2
Hello World from thread 5
Hello World from thread 5

There’s some interesting things to see about this output:

  • There are 12 threads – 0..11.
  • Some threads run one cycle (e.g. #10) and others run multiple cycles (e.g. #5).
  • You can see where thread 0 runs the conditional code.

Getting More Complicated

A simple loop like the one above is fine as it is. Once you start trying to do real work, you quickly bump into the problem that you need to think about whether variables are ‘shared between all threads’ or ‘private to each thread’.

I first recommend adding the ‘clause’ of ‘default(none)’. This will force the compiler to make you explicitly define whether variables are shared or private. Shared and private variables are then specified in a comma separated list. Here’s an example of what the pragma line now looks like:

#pragma omp parallel for default(none) private(myVar, foo) shared(someOtherVar, andAnother)

I’m no expert on OpenMP, and there’s a lot more to it that’s not covered here. The wikipedia page is pretty good for more info.

Some notes:

  • The omp.h header comes with Visual Studio. There’s no lib file needed.
  • You need to enable OpenMP in the project settings (C/C++ –> Language –> OpenMP Support –> Yes).

Thursday, December 09, 2010

How do I make a videogame?

Someone recently mailed to ask “I’ve got an idea for a game – how do I get started making it?”

The high level of the question, implies that the asker is completely new to making games.

Games tend to require a lot of time spent programming, making artwork, and then putting it all together into a game.

First, I recommend concentrating on learning how to build games in general – instead of focusing on building your dream game. In the early days of learning, it's great if you can just build 'Asteroids' - you can build your Quake-killer later on.

If you’re ‘pretty good with computers’, then a fun and free way to get a taste of game development is to install Unity and follow this tutorial. Following the tutorial is kind of like getting an ‘Airfix’ model airplane and assembling it – most of the hard work has been done, but there’s still challenge in working through the instructions.

Going through this will show you bits of program code, and how art and animations get combined to make a game.

From there, you could go on building things with Unity, or explore the many other options available - C++, XNA, Flash, and many many more. Each of the options has a bunch of pro’s and con’s associated with it. Choosing the right one is a matter of knowing what you want to make (e.g. 2D, 3D, Networked, Web page based …), which technologies you want to use / learn (e.g. C++, Flash, Javascript …), and what platforms (e.g. Windows, iPhone, Xbox …) you are targeting.

Thursday, December 02, 2010

Developing iPhone apps in Windows, in regular C++

apple_iphone_1When the iPhone development gold-rush started, I investigated a little but was soon put off by the need to buy an iPhone, a Mac, and work in Objective C++. It would have been a lot of money and time before I could even write a Hello World app.
I just became aware of the Airplay SDK (Thanks Tim). It allows you to develop for iPhone in C++, in Visual Studio, on a PC, and comes with an emulator for PC. And it’s free!
It’s an engine with a graphics pipeline, networking, user input, sound, and other things. The things which it might be considered lacking if you were comparing with (the somewhat similar) Unity3D are a 3D editor, and a more developed physics system (I’m only getting this from a quick glance at the website, so forgive me if I’m wrong…). I’m not knocking it though – Airplay appears very exciting.
I currently lack the time to play around with it, but it looks pretty interesting to me. For a small fee, you can also target other mobile platforms like Android, without having to make changes to your code. Very cool!
Check it out – www.airplaysdk.com

Update:

  • I recently found this good roundup of many different iOS development options.