CS267 Assignment 3: Parallelize Knapsack

Overview

The purpose of this assignment is introduction to programming in Partitioned Global Address Space (PGAS) languages. You need to parallelize a solution of the 0-1 knapsack problem.

You have N items with

• positive integer weights {w[1], ..., w[N]} and
• positive integer values {v[1], ..., v[N]}
The goal is to find a subset of the items such that its total weight does not exceed given bag capacity C and its total value is the largest possible.

We consider solving the problem using dynamic programming. Define T[i,j] to be the maximum value that can be attained with weight ≤ i using items {1, ..., j}. Then it satisfies the following recurrence relation:

T[i,j] = max( T[i,j-1], T[i-w[j],j-1]+v[j] ).

The value of the optimal solution is found at T[C,N]. Backtracking over the choices gives the items used in the solution.

Assignment

There are two parts to the assignment.
1. There is a bug in knapsack-race.upc. You will use the UPC-Thrille tool you learned about in Lecture 16 to identify a potential race condition. For part (1), you will run on Franklin, with the Portland Group compilers loaded (PrgEnv-pgi, the default). After downloading the source files, instrument the knapsack-race.upc code with UPC-Thrille by typing "make knapsack-race". Then, run the instrumented code with "qsub job-knapsack-race" from Bash (shell). If you use another shell (like csh, the default), you must call "qsub -S /bin/bash job-knapsack-race", or switch shells explictly (e.g., typing "bash" before calling "qsub"). Examine the resulting output file to find the race condition. Then, you must modify knapsack-race.upc to fix the potential race condition without altering correctness -- assume that if no data race occurs, then knapsack-race.upc gives the correct answer. In your writeup, you must show the output of the UPC-Thrille tool indicating detection of the potential race condition. You must clearly explain the bug and the fix you implemented. Lastly you must include the UPC-Thrille output showing that your fix indeed removed the race condition. You do not need to submit your (fixed) knapsack-race.upc
2. .
3. Implement knapsack.upc that solves the 0-1 knapsack problem -- your code must get the correct answer, like the provided serial and parallel (fixed) codes. You must use UPC - the Makefile is designed for the Cray UPC compiler (PrgEnv-cray) but the Berkeley UPC compiler (module load bupc) is also available. Explain your parallelization strategy in your write-up, especially the communication pattern between parallel processors. Run scaling experiments with 1 to 32 cores (1 to 8 nodes) of Franklin (at least test {1,2,3,4,5,8,16,24,32} cores). Your report should include performance plots demonstrating strong scaling (constant global problem size) and weak scaling (constant per-thread problem size). Try different local (per-thread) problem sizes - how do your strong and weak scaling curves change? Feel free to experiment on Hopper as well.

Source Code

 serial.cpp a serial implementation that you need to efficiently parallelize, knapsack-race.upc a naive and very inefficient parallel implementation in UPC, with a bug Makefile a Makefile for Franklin. Note that you must use PrgEnv-pgi for Part (1) ("make knapsack-race") and PrgEnv-cray for part (2) ("make serial" and "make knapsack"). job-serial, job-knapsack-race, job-knapsack sample batch files to launch jobs on Franklin. The third is for your own code, knapsack.upc.
The supplied (naive) parallel code uses a cyclic layout and computes entries corresponding to the same item (T[:,j]) in parallel. Barrier synchronization is used when proceding to next item. This means a lot of fine-grain communication - the parallel code may run slower than the serial code!

Your goal is to get better scaling vs. the serial code. To improve parallel efficiency, consider blocking rows and/or columns (of T), and using bulk communication and/or pipelined computation. Using UPC is a requirement.

You may work in groups of 2 or 3. Email the GSIs your write-up and source codes.

Resources

[ Back to CS267 Resource Page ]