Question: Weve implicitly assumed that each call to Compute next value requires roughly the same amount of work as the other calls. How would you change
Weve implicitly assumed that each call to Compute next value requires roughly the same amount of work as the other calls. How would you change your answer to the preceding question if call i = k requires k + 1 times as much work as the call with i = 0? So if the first call (i = 0) requires 2 milliseconds, the second call (i = 1) requires 4, the third (i = 2) requires 6, and so on.
Write an implementation of the best version of your proposed algorithm that you came up with for Problem 1-2, in which you needed to assign work to each core such that the workload was as balanced as possible across all cores. Prepare your code in one of the usual languages we study in the CSCI curriculum (Java, C++, or MATLAB). If by chance you already did this in advance of today's class, then please use a different language for today's exercise. Your code should not only correctly implement your algorithm, but it should also output a version of the table shown in the homework instructions (e.g., using tab-separated values) to the console, and it should work for the following combinations of inputs, at a minimum:
Input variation #1: n = 12, p = 4
Input variation #2: n = 23, p = 5
I will also test your code using a different set of input values for n and p (which I will not reveal) to ensure your code isn't "hard-coded" to generate correct results. These input values can be hard-coded in your code -- you don't have to prompt the user to enter n and p. Just make sure you test the above combinations for n and p when running your code. Your output table does NOT necessarily need to be formatted exactly the same as what is shown in the original homework instructions and hints -- as long as your output clearly shows which values are assigned to each core, the work required for each assignment, AND the total work performed by each core, then that will suffice for the purpose of today's lab. However, if you are able to output a version of the table that looks more like the one shown in the instructions, I may give extra credit depending on how clean the output is.
/* * assignment[my_rank][k] is the kth value of i assigned to core my_rank * (in the example above, since there are n/p = 3 values of i assigned to each * core, then each cores assignments would be indexed 0, 1, and 2) * * total_work[my_rank] is the total amount of work assigned to core my_rank * p = number of cores, n = number of calls * In the tables above, k would correspond to a COLUMN index, * and my_rank would correspond to a ROW index */
// initialize values my_rank = k = 0;
// iterate through the n calls to hypothetical function Compute_next_value()
// where i = index of the current function call that needs to be assigned
// to one of our p cores for ( i = 0 ; i < n ; i++ ) { assignment[ my_rank ][ k ] = i; work[ my_rank ][ k ] = 2*(i+1);
// e.g., for i = 0, work = 2*(0+1) = 2 total_work[ my_rank ] += work[ my_rank ][ k ];
// update k for the next assignment to my_rank, assuming there
// are any remaining assignments to be made. One (clever) way to do
// this is to increment k by 1, but when k > (n/p), k will reset to 0: k = ( k + 1 ) % (n/p);
// if current k = 0, then new k = (1 % 3) = 1 [for this example, n/p = 3] // if current k = 1, then new k = (2 % 3) = 2
// if current k = 2, then new k = (3 % 3) = 0
// update my_rank (the row index) after finishing all assignments
// for the current my_rank (when k resets to 0 in the previous step,
// that means were ready to move onto the next value of my_rank): if ( k == 0 ) { my_rank++; }
// end if }
// end for
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
