Friday, May 13, 2011

An Introduction to Threading and Multi-core Processors

An Introduction to Threading and Multi-core Processors


With the increasing popularity of multi-core processors, most people imagine that all their programs will run much faster. While this is true, it is not always the case. Sure, the operating system boots up faster, but what about Firefox, or Blender? Some people have noticed they just stay the same speed. So why do these programs run so slowly on such a “fast” processor?

The answer to this question lies in threads. So what is a thread? One can think of a thread as a lightweight process, where it can execute in a process and access the process’s resources, along with sharing information with other of its kin. Seems a bit bizarre? Do not fear, threads are quite simple to understand when you put them in an analogy. Imagine a room. In this room, there is only a filing cabinet will all the documents employees of Department A need to process. Joe, our hard-working employee works in this office, which all people of Department A share. Now, Joe’s office has a small problem - it is missing an essential piece of furniture: a desk. Without it, Joe has nowhere to place any of his work. Not to worry, the company did ensure that Joe the employee can work. So they ordered a desk. Unfortunately, they were a bit low on budget and could only afford one desk, so all the employees must share this one table. Problem is, the desk is so small that only one person can work on it. This means while Joe is filling out paperwork, Alice, an employee from a different department, must wait until Joe is finished. This may seem like it is very unproductive (and it is), but it gets worse. By a company policy, employees can only work on 10 documents during one session at the desk so other employees can use the desk too. This makes their work time slower as more people line up to use the desk if they have more than 10 documents, as they need to make trips back to the . Joe often needs to work on hundreds of documents. To increase the efficiency of his department The obvious solution is to hire another employee, and put him in the same department as Joe. So the company hires Steve and places him in Department A. Joe can then give half his work to Steve and he can wait behind Joe and as soon as Joe is finished working on 10 documents, Steve can jump to the table while Joe returns the end of the line. By employing another worker, the Department is performing faster. This technique is called multi-threading in programming.

In this analogy, Joe and Steve are threads (employees) of the same process (Department A). They can both access the documents (instructions) in their room and also work at the same pace. Now Alice who works in Department B is a thread in a different process and she can only get documents from her department. This is how processes work. Threads of one process can only access instructions and data from the process in which they were created.

Now back at the office, the Board of Directors thought it would be a good idea to increase productivity. The next day, all the employees walked into the workplace for a surprise: the company is now the proud owner of two desks. Now any of the employees can work at 2 times the efficiency. Now, to the board of directors, they had expected all the work to be done in half the time. Why was it that Alice’s department, Department B still worked at the same speed as it did before the second desk. The answer is that there is still only one employee in Department B and regardless of how many free desks, Alice can only work so fast. So single threaded applications such as Firefox are like Alice, where there is only one employee who needs to share the desk without sharing the work. This fact makes them unable to use the additional core as efficient as other programs such as Apophysis (a Fractal Renderer) that have multi-threading capabilities.

In this scenario, adding another desk is equivalent to adding an additional core to the processor. This is where you get dual core processors from Intel and AMD. This covers the basic theory of threads. This is also where I am going to finish of this article. Of course, being a introduction, it does not cover some of the more advanced topics such as:

Hyper-Threading - Where the workers literally share the desk by drawing a line down the middle and have two people work on it at the same time
Process Isolation - If there is a murder in Department A, Alice from Department B does not need to know about it.
Deadlocks - Steve is waiting for Joe to give him some data so he can finish his work but Joe is waiting on Steve to give himself data so he can give Steve the data he needs.



So if you want to know more about them, Wikipedia is a great resource. Cheers!

Wednesday, May 4, 2011

Beginning to code

Are all your friends are programming making you feel left out? Do you think that all those things that you use online can be made better? Have you ever complained that a game could be so much better if they just changed that level? Fear not! The power of programming will solve all those problems! Or will they?

Many of the representations of coding scare or fool many non-programmers. Being a programmer myself, it is easy for me to say that scenes of sitting in front of a terminal with flowing text (See hackertyper for reference). This is by far the most common view I have heard of whilst mingling with normal folk. To know the true experience of programming, one must sit down and start learning. Yet, the beginning programmer faces another hurdle. The world of programming has many languages to choose from, each with its strengths and weaknesses. The most common languages is C and C++, with the majority of all programs and operating systems written using them. While it is the most common, don’t get fooled into thinking that you must learn them first, or even at all. Of course, there must be a reason for the advocacy of C and C++, otherwise, why do so many use it. The first step to programming is picking the right language for the job. An in depth article on picking a programming language will be covered in a future post, as I intend this article as a primer to programming.

Before you start on programming, you should think about how you will see programming and what to expect. You may be programming as a hobby, or you may be stepping into the world of the computer industry. These two different views will affect how you program. A professional programmer will spend much more time, and will usually have more resources than a hobbyist. This leads into the next topic: Your Environment. All programmers have usually have a specific environment they like to work in. The most common environment is the Integrated Development Environment (IDE), which is a pre-made set of software where all the development can be done within a window. Others may like a text editor only, or even just cat and append. Consideration of these elements of programming is very important to the beginning programmer, as it will help with learning the language and make the development smooth.

So, what next? There are many paths that one can take when programming. The main branch is software vs web programming, and each of those split into smaller sub-branches. It is up to you to pick where to go next. It can be developing a website or making a game. As a final not, I encourage you not to stick to only one type of programming, play around with different styles and methods. Learning and programming in narrow views is limiting the amount of work you do. Read what others say, try new concepts. All of it will be helpful to you, and last but not least, enjoy yourself. Remember: more than once a programmer’s problem is resolved by an epiphany overnight.