Meling Ngo, Amir Kamel, Yatish Patel, Chen Chang, Frank Gennari, Guillermo Canas
This one is not a NAS parallel benchmark, but it serves to highlight irregular communication. You need to write UPC and Titanium codes to solve a 0-1 knapsack problem by dynamic programming.
Imagine that you have N old textbooks with integer weights Wi, for i from 1 to N. You also have a backpack with an integer weight capacity of C. There's a used book store down the street that will give you Pi dollars for book i. How much money can you make in one trip?
That's the 0-1 knapsack problem in a nutshell. The obvious solution of trying every possible combination is silly. A slightly better method is known as branch-and-bound; you run a breadth-first search of the combination space, but prune branches that cannot lead to optimal solutions.
A much, much better solution follows from expressing the problem as a recurrence relation and taking a dynamic programming approach. For the non-CS people (and the CS people who have forgotten), dynamic programming algorithms store common subproblems in a table and fill the table until they reach the solution.
The table T for this problem is indexed by a book and a capacity. Entry Ti, j is the maximum profit that can be obtained with a capacity j and books 1 through i. The capacities range from 1 to C, and the books range from 1 to N. Entry Ti, j is calculated by comparing the profit at capacity j without book i to the profit at capacity j - Wi with book i. More formally:
Ti, j = max { Ti-1, j , Ti - 1, j - Wi + Pi }
So what does that do? Assume that if you're examining book i and capacity j, you already know the maximum profit for all the capacities less than j for all the books less than i. Including book i decreases your remaining capacity by Wi, so you need to fill up capacity j - Wi in a way that maximizes the profit. Luckily, that's already in entry Ti-1, j-Wi. The total profit possible from including book i is thus Ti-1, j-Wi + Pi. Not including book i simply maintains the status quo, the profit of which is already in entry Ti-1, j. The larger of the two amounts will decide inclusion of i.
If those two amounts are the same, be a packrat and don't include i. You may find someone who's willing to pay more for that book later. This is illustrated in Figure 1.
Figure 1: To decide on packing a book, compare the previous total profit at the current capacity to the book's profit plus the profit at the current capacity minus the book's weight (here, capacity - 3). If the latter is larger, include the book.
There are a few corner cases to flesh out... What if j - Wi is negative? Then item i doesn't fit at all, and the choice has been made for you. And what if i is 1? That is, how do you bottom out the recursion? The entries in the first row are determined simply by whether or not book i fits in capacity 1. (You may want to start with the smallest weight.)
After all this, the total profit is in entry TN, C. You can find the books that achieve that profit by backtracking over the choices.
A pair of sample programs are provided in C and UPC. The UPC program is horribly inefficient when using a network back-end. The data is distributed as shown by the colors in Figure 1, ensuring too much communication at every step.
An efficient implementation will likely give each processor a contiguous block of bag capacities and access its columns via local pointers. You may put all the book profits on each processor if that's the most efficient (for large numbers of books). You may also want to investigate pipelining the computation and fetching the necessary total profits in bulk.
Figure 2: Pipelining the computation of totals may increase the computation per communication.
For the backtracking phase, you need to use more than one processor in some way. Do not simply send the entire table to one processor and have it do the backtracking. You don't have to be excessively clever, but think about making the back solve efficient.
I swiped the problem from Dr. Culler's CS258 class. You might find other useful tips there. It's a fun little problem.
Back to homework 3's main page.
Main CS267 page, and the TA's CS267 page
E. Jason Riedy