NOTE: Asmt 3 will solve the same problem, but using OpenMP instead of pthreads.
WARNING: This assignment has been used before and all previous submissions have been retained to compare with, so don't be tempted to copy a former student's code!
Marking for this assignment (breaking down the 15%):
Software that does not compile, link, and execute on lockhart will receive 0%. The report does not have to be beautifully bound, but should be presentable and not be hand written.
The "Red/Blue computation" is described in textbook exercise #10 on p. 111, and you need to read that.
Be aware that, despite some similarities, this is not a "game of life": the respective quantities of coloured cells do not change as the program runs. Rather, the initial colours migrate around the board. For Part 1, design a
The purpose of this part is to instigate some intelligent planning based on applying the theory and techniques we have studied. It will not be marked before you proceed to Part 2; however, the Instructor may give you feedback if it appears your understanding of the problem is seriously awry. Not getting feedback at this stage does not imply that the Instructor believes your pseudocode to be perfect! You're still responsible for program correctness.
Again, start coding your program as soon as you have some decent pseudocode to work from; don't wait until handing in Part 1 to start Part 2!
Conceptually, all legal red moves must happen simultaneously for the entire board, followed by all legal blue moves. Of course, you have to produce that "conceptual effect" by means of the usual iterative program looping.
Consider the following 8-column row (@=space here, but you should print a real space):
As a human in the 3rd dimension eyeballing the row, you can see that only one red is a candidate to move right. Thus, the legal result of the red half-step is:
Don't go over the same row again and again to move reds into spaces that open up! That would produce:
The two conditions should be tested after each full step, and the density is tested first:
The maximum colour density condition is looking for a point where enough red or blue cells have shifted together so that some tile has become more than c% red or blue. We'll define this unambiguously so that our programs all stop at the same time when given the same initial board:
If a tile is t*t cells, then the maximum number of coloured cells for continuing the computation = t*t*c/100,
For example, if tiles are 8 cells wide, and c is 70%, then up to 8*8*70/100 = 44 red cells or blue cells are allowed, but 45+ of either red or blue in any tile will stop the program.
Thinking through the effect, we know that random initial assignment means that any board will have around 1/3 of each colour cells overall, though for small tiles, their coloured fractions could vary widely. Therefore, specifying c<<33 will likely stop the computation immediately, while c>>33 will rarely be achieved--except with small tiles. So for large tiles, the useful range of c is probably a narrow band above 33. Try it and see.
Past experience has shown that for a given combination of board and tile sizes, there's a certain c% that can be reached "soon", meaning in under 200 iterations. Call that percentage "D" for max. density. If you want to go above D, by even 1%, you won't reach it no matter how long you run. It would be very interesting to find some formula that gives D as a function of board and tile sizes.
The second condition, reaching a maximum number of full steps, is straightforward. It is only checked if the density condition was not triggered.
Your output for Part 1 is a paper submission consisting of:
In this part, the pseudocode, including Peril-L conventions, and the report will carry equal weight.
Code your solution to run on lockhart using pthreads in Intel Parallel Studio XE 2016 according to the program specification below. You'll find the following APIs in
The easiest way to access documention for these POSIX functions is to Google "man function " or type that at a Linux command prompt. You can also try "man pthreads" but it may be too high-level.
You may initially develop/debug on Linux if you like. In that case, if you want to use barriers, you may need to define a feature macro. Since it causes a warning in Windows pthreads (though it does not seem to do any harm), you can avoid the warning like this:
#ifndef _WIN32 // if we're not on Windows
#define _POSIX_C_SOURCE 200112L // get barriers on Linux
#endif
You can assume that the maximum no. of threads is 16, for the purpose of defining data structures. The TA will not run your program with >16 threads. However, you may wish to allow for more threads, to observe whether "oversubscribing" the 16 cores helps performance or not. You may also choose to spawn additional threads that are not for computation and are not "bound" to cores.
The program is called
Print an error and abort if p<1, b <2, the tile width t is not a factor of the board width b, or the colour percentage is out of range. If a
seed
is specified, use it with srand(
seed
) to generate the initial board layout (specified below), otherwise, obtain a seed by reading the system clock.
"t" is an interesting parameter: With t1, the program will terminate immediately. (Why?) With tN=bN, running the program makes no difference to the colour density, so it will either terminate immediately or else run until the max. steps are exhausted. Also, note that "c100" effectively deactivates the colour stopping condition.
Initialize the board by calling rand()%3 for each cell in row major order. The values are: 0=white, 1=red, 2=blue. However, you do not have to fill the cells with integers 0, 1, and 2; you can think of another (more efficient?) encoding for the white, red, and blue if you wish, since we only care about the textual output.
When started in
automatic
mode, the program will run until it satisfies either stopping condition, and then print the final board contents on the file
For this assignment, the part that we want to time is just the simulation, excluding board initialization and final output. By disregarding a major inherently serial part in this way, more obvious speedup should be observed. Use the StartTime() and EndTime() functions supplied in
The purpose of interactive mode is to allow you to eyeball the results of one (half) step, and so to conveniently check your program's correctness. It is understood that interactive mode is for debugging and amusement using small boards. It is only worth 1 mark.
When started in interactive mode, the program will initialize the board, print a prompt and then loop accepting commands:
The <Enter> and # commands should "resynchronize" by doing a blue half step if preceding "h" commands ended on a red half step. For example, if I initially enter "h" then "5", the program will do one red half step, followed by one blue half step and 4 full steps.
If a command cannot be interpreted, print the word "Error" and reprompt. There is no need for fancy error messages.
After any stepping command, print the board followed by the same numerical status as at the end of the program using iterations so far, and current maximum of tiles' single-colour percentage. You can easily obtain
p[varies 1-16] b875 t125 c100 m2000 s3090
c% is intentionally set so that the full 2000 iterations run.
Bonuses of 15% (=entire assignment mark * 1.15), 10%, and 5% will be given for the 3 fastest programs (disregarding initialization and file output, as explained above). A benchmark set of arguments
NOTE: Rematch provision! The 2nd and/or 3rd place runners-up may request a rematch. In that event, block time (exclusive use of machine) will be available for further profiling and tuning prior to the runoff match using a set of arguments with different "TBA" values. The bonuses will be redistributed among the 3 contenders according to the new times.
Marks for Assignment #2 Part 2
For the software, mark is 0-6 based chiefly on functionality:
5 = Parallel program that produces correct results and reports timings for any tested args
+1 for interactive version in colour
1 = Parallel program that runs cleanly, and is coded to address the problem, but results are all incorrect or missing
0 = Fails to compile and link, or crashes without producing any results
In addition, 1/2 mark penalties will be deducted for specific "sins": numerous compiler remarks/warnings, file headers missing/incomplete, Visual Studio "solution" folder incomplete, program name wrong, arguments mishandled, output filename incorrect, etc.
The documentation starts with 4 marks, then deduct 1 mark for any item missing, or 1/2 mark for any lame or poorly-done item. If you didn't have any working software to graph, you'll lose at least 1 more mark to be fair to those who conducted timing and produced their graphs.