Wednesday, April 19

Why does parallelism need to be so complicated?

Creating applications that use multiple threads in languages such as C# and Java is easier than in previous languages, but still quite a complex process. This means that people only use it if it's vitally important for a special case.

As clock frequencies in microprocessors are reaching their limits, they are increasingly using multi-core technology to gain increased performance. It is likely that in a few years time, processors may have 16-32 cores, but at similar clock frequencies as today.

This means that creating applications that do things in parallel needs to be easier, but programmers don't want to learn a new language.

If you have some code like


{
doA();
doB();
doC();
}
doD();


and you don't care what order the functions are executed in, could you use a new form of brackets?


{[
doA();
doB();
doC();
]}
doD();


This will execute all 3 functions. They may be executed in any order, sequentially or in parallel. How they are executed will depend on the compiler and operating system. It may create new threads, or if the overhead for this is too great it may not. When all 3 functions finish, the function doD() will be executed.

You may also have for loops, but don’t care in which order the iterations are performed.

If you have a for loop, this could work in a similar way.


for(int i=1;i<=10;i++)
{[
doE(i);
]}
doF();


doE() may be executed in any order. DoF() will be executed when all the calls to doE() are completed.

This will mean whenever you write code, you can think "Does it matter what order the statements are performed in?" If the answer to this question is "No", then using the new notation will (if the compiler and operating system deem it more efficient) perform them in parallel using separate threads.

If you later find the parallelism is causing problems, you can easily revert to the original sequential notation and the operations will be executed sequentially.

If you find code is running slowly, you can add the new notation and the operations may be performed in parallel.

No comments: