CS 336: Project #5

Fish-schooling with Pthreads

On NSCC, create a proj05 directory.

In this project, you will be making a threaded version of the fish-schooling code. You will simulate each school in parallel, not worrying about parallelizing the code within a school's simulation.

  1. Copy all of your code from the sequential fish simulation. Update the Makefile to include -lpthread in the LFLAGS. Note: If there is any part of the sequential code that you would like from Stephanie, just let her know and she will share.
  2. Update the code to use rand_r.

    rand() is not thread-safe, so we must use a function that is. rand_r() is the reentrant version of rand, and it takes as input a pointer to a seed.

    Test your simulations with sim_slow.

  3. Parallelize runSimulations, by making each thread run runRandomSimulationWithStatistics in a loop (to run its chunk of the simulations). Create a new file named collect_stats.c (and its header collect_stats.h). Remove runSimulations from fish_support.c and fish_support.h and rewrite it in collect_stats.c. Update sim_stats_1r.c, sim_stats_nr.c, and Makefile to include collect_stats (the c files need to include it, the Makefile needs it added to the list of object files).

    Test it with sim_stats_1r and sim_stats_nr.

    Update the code in sim_stats_nr to test more values of r. Here is a snippet of code to do that concisely:

      float r[] = {0.25, 1.0, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 2.0, 16.0};
      int numR = sizeof(r)/sizeof(float);
    
    With both fishStep extensions, here are my results, as shown by disp_stats:

    figure

    Without extension 2 (correction for turning too much), the results are suspect (the elongation just keeps growing). So, ask me for extension 2 or implement it.

  4. Analyze performance. Treat NUM_THREADS=8, N=100, numTrials=32, and r={0.25, 1, 2, 16} as your default values. Time your results when NUM_THREADS is varied. Do you get the expected speed-up? Do the run-times reveal to you how many processors you have?
  5. Analyze the output (e.g. the effect of r on elongation and polarization).

Extensions

Invent your own extension. Find some aspect of one this week's work that interests you and expand on it. Or, maybe figure out why your code from proj 2 differed from someone else's code in timing or performance.

Writeup and Handin

To hand in your project, you will gather all of the necessary files into a proj05 directory in your turnin directory:

  1. Create a file named README.txt for your project write-up. Include the analysis outlined earlier. The more thorough the analysis, the higher your grade will be.
  2. You should hand in all code necessary to run your solutions. Place all necessary .h, .c, and Makefile files in the proj05 directory. Stephanie will probably want to compile and run the code. It should be possible to do so without looking for any more files.

Email Stephanie to let her know your project is ready for grading.