CS Games 2015 Parallelism Challenge - A Simple Solution
What’s up people!
Now that a month has passed, surely you have found a, at least simple, solution to the challenge? Now, as I promised last month, let’s go over a simple solution for this challenge…
See, after pondering the thought for a while, I’ve decided against publishing a full solution to the challenge. I think people should be able to come up with their own without any shortcuts. So instead of giving away a solution, I’ll be giving off some tips on how you can make the most of this challenge without losing your mind.
Tip 0: Rage quit
Winning a war doesn’t necessarily means winning every single battle. For some teams, it’s better to focus on other challenges or just get some rest before the next day’s challenges (this challenge was given quite late in the evening, when many were possibly burned out).
Also, the basic client given to the competitors already send random answers back to the server. So, who knows, you might get lucky (but you probably won’t).
With that being said, let’s go over some real tips for the ones
crazy brave enough to step up to the challenge.
Tip 1: Get some data
Quick question: can you solve a problem without any data?
So with this in mind, the first thing you should do is capture some problems of each type that the server can send. With this you’ll able to easily verify that your solutions are, in fact, correct.
Tip 2: Solve the problem sequentially first
Given that this was a parallelism challenge, some might be tempted to go all out with a parallel algorithm right from the start. If this is your case, please calm yourself. It is far easier to solve a problem sequentially. Remember, you not only have to produce answers to the problems sent fast, but these answers also have to be correct.
To illustrate this, let’s take the problem of finding a value in an array. If we choose to implement a parallel solution first, we might end up with something like this.
In short, we want to start as many threads as possible. To do so, we split an array so as to give each thread an (approximatively) equal share of the work to be done. Then, we start the threads by giving each of them a piece of the array. Then, we join them. Then, we give an answer based on the what the threads found. Finally, we start it all over for every other array that was part of the problem.
It’ll work, but this is seriously not a solution that you should consider during the competition for two big reasons. First, its size makes it error prone (especially in a high stress environment like the CS Games). So you’ll waste a lot of time getting it right. Second, creating and destroying threads, which this solution does a lot of, takes time, which you don’t really have in this context.
Instead, you should concentrate on first solving the problem in a sequential fashion. For example, a simple sequential solution can be written in way less lines of code using the
And not only is it shorter to write, it is easier to see that it is correct. So there you have it, a correct solution that you can then speed up with multiple threads.
Tip 3: Parallelize at the coarsest level possible
For the few that got the idea to write a sequential solution first, it seemed that the tentation to write a solution akin to the first one shown in the tip above was too great to resist. If this is your case, calm your tits.
As I’ve already mentionned, there is a time penalty associated with creating and destroying threads. So when you have to parallelize a time-constrained application, be sure to keep this overhead in mind. Especially when some challenge’s authors might reduce the time available for computation when evaluating the competitors’ solutions (tough they would be real assholes to do it).
One way to minimize the number of threads used is to parallelize at the coarsest level possible like in the code below.
As you can see, changing the granularity of the threads made us use 4 times less threads than in the previous parallel solution. It also gave us a easier to reason about solution which could probably be programmed in less than half the time it’d likely take to program the other parallel solution.
tl;dr: Be smart when designing a multithreaded application.
Coming up next month: An even more efficient way to crush the competition.
This article is Part 2 in a 3-Part Series.
- Part 1 - CS Games 2015 Parallelism Challenge - Introduction
- Part 2 - This Article
- Part 3 - CS Games 2015 Parallelism Challenge - A More Efficient Solution