#### Performance Assignment

This performance assignment is based on the one by Bryant and O’Hallaron for Computer Systems: A Programmer’s Perspective, Third Edition

Due: Wednesday, October 4, 11:59pm

This assignment deals with optimizing memory intensive code. Image processing offers many examples of functions that can benefit from optimization. In this lab, we will consider two image processing operations: pinwheel, which rotates quadrants of an image counter-clockwise by 90 degrees while also converting to grayscale, and motion, which “blurs” an image to simulate motion toward the top-left of the image.

These instructions are long, but the lab itself may not be too time-consuming to get the threshold results required for full credit. The potential upside for clever optimizations is anyone’s guess.

##### Image Operations

For this lab, we will consider an image to be represented as a size followed by a two-dimensional matrix M, where Mi,j denotes the value of (i,j)th pixel of M. Pixel values are triples of red, green, and blue (RGB) values. We will only consider square images. Let N denote the number of rows (or columns) of an image. Rows and columns are numbered, in C-style, from 0 to N-1.

Given this representation, the pinwheel operation in the first quadrant can be implemented quite simply as the combination of the following three matrix operations on that quadrant:

• Transpose: For each (i,j) pair, Mi,j and Mj,i are interchanged.

• Exchange rows: Row i is exchanged with row N-1-i.

• Grayscale: Set the red, green, and blue components all to the average of the three components.

For example, applying pinwheel to

produces

The motion operation is implemented by replacing every pixel value with a combination of nine pixels: the pixels that form a 3x3 block with the target pixel in the top left. Pixels in the source image are weighted as follows:

 0.6 0.03 0 0.03 0.3 0.03 0 0.03 0.1

That is, the new value of Mi,j is computed as

 Mi,j×0.60 + Mi+1,j+1×0.30 + Mi+2,j+2×0.10 + Mi,j+1×0.03 + Mi+1,j×0.03 + Mi+1,j+2×0.03 + Mi+2,j+1×0.03

For the purposes of computing Mi,j’s value, neighbor pixels beyond the edge of the image are treated as black.

For example, applying motion to

produces

##### Setup

Start by copying perflab-handout.zip to a protected directory in which you plan to do your work. Then, run the command:

\$ unzip perflab-handout.zip

This will cause a number of files to be unpacked into the directory. The only file you will be modifying and handing in is "kernels.c". The "driver.c" program is a driver program that allows you to evaluate the performance of your solutions. Use the command make driver to generate the driver code and run it with the command ./driver.

Looking at the file "kernels.c" you’ll notice a C structure student into which you should insert the requested identifying information about yourself. Do this right away so you don’t forget.

##### Data Structures

The core data structure deals with image representation. A pixel is a struct as shown below:

 typedef union { struct { unsigned short red;   /* R value */ unsigned short green; /* G value */ unsigned short blue;  /* B value */ }; int dim; } pixel;

An image I is represented as a one-dimensional array of pixels. The first “pixel” in an image uses dim to report the dimension of the image (i.e., the height and width, which are the same). Each subsequent pixel uses the red, green, and blue fields for one pixel’s 16-bit RGB values. The (i,j)th pixel of an image I is I[RIDX(i,j,I->dim)], where RIDX is a macro defined as follows:

 #define RIDX(i,j,n) (1+(i)*(n)+(j))

See the file "defs.h" for this code.

The pinwheel and rotate functions receive two pixel* pointers representing source and destination images. The source image must not be changed, and the destination image must be filled with the result of transforming the source. The source and destination images have the same dimensions, and the destination dimension is already filled in when pinwheel or rotate is called.

##### Pinwheel

The following C function computes the result of pinwheeling the source image src and stores the result in destination image dst. It implements all three transformations (transpose, exchange, and grayscale) in a single pass.

 void naive_pinwheel(pixel *src, pixel *dest) { int qi, qj, i, j; /* Loop over 4 quadrants: */ for (qi = 0; qi < 2; qi++) for (qj = 0; qj < 2; qj++) /* Loop within quadrant: */ for (i = 0; i < src->dim/2; i++) for (j = 0; j < src->dim/2; j++) { int s_idx = RIDX((qj * src->dim/2) + i, j + (qi * src->dim/2), src->dim); int d_idx = RIDX((qj * src->dim/2) + src->dim/2 - 1 - j, i + (qi * src->dim/2), src->dim); dest[d_idx].red = (src[s_idx].red + src[s_idx].green + src[s_idx].blue) / 3; dest[d_idx].green = (src[s_idx].red + src[s_idx].green + src[s_idx].blue) / 3; dest[d_idx].blue = (src[s_idx].red + src[s_idx].green + src[s_idx].blue) / 3; } }

The above code scans the rows of the source image matrix, copying to the columns of the destination image matrix. Your task is to rewrite this code to make it run as fast as possible using techniques like code motion, loop unrolling and blocking.

See the file "kernels.c" for this code.

##### Motion

The motion-blurring function takes as input a source image src and returns the blurred result in the destination image dst. Here is part of an implementation:

 void naive_motion(pixel *src, pixel *dst) { int i, j; for (i = 0; i < src->dim; i++) for (j = 0; j < src->dim; j++) dst[RIDX(i, j, src->dim)] = weighted_combo(src->dim, i, j, src); }

The function weighted_combo performs the weighted combination of the pixels around the (i,j)th pixel. Your task is to optimize motion (and weighted_combo) to run as fast as possible. (Note: The function weighted_combo is a local function and you can get rid of it altogether to implement motion in some other way.)

This code and an implementation of weighted_combo are in the file "kernels.c".

##### Performance measures

Our main performance measure is CPE or Cycles per Element. If a function takes C cycles to run for an image of size N×N, the CPE value is C/N2. When you build and driver its output shows CPE results for 5 different values of N. The baseline measurements were made on a CADE lab1-n machine.

The ratios (speedups) of the optimized implementation over the naive one will constitute a score of your implementation. To summarize the overall effect over different values of N, we will compute the geometric mean of the results for these 5 values. See Evaluation for more information on grading.

##### Assumptions

To make life easier, you can assume that N is a multiple of 32. Your code must run correctly for all such values of N but we will measure its performance only for the 5 values reported by driver.

##### Infrastructure

We have provided support code to help you test the correctness of your implementations and measure their performance. This section describes how to use this infrastructure. The exact details of each part of the assignment are described in the following section.

Note: The only source file you will be modifying is "kernels.c".

##### Versioning

You will be writing many versions of the pinwheel and motion routines. To help you compare the performance of all the different versions you’ve written, we provide a way of “registering” functions.

For example, the file "kernels.c" that we have provided you contains the following function:

 void register_pinwheel_functions() { add_pinwheel_function(&pinwheel, pinwheel_descr); }

This function contains one or more calls to add_pinwheel_function. In the above example, add_pinwheel_function registers the function pinwheel along with a string pinwheel_descr which is an ASCII description of what the function does. See the file "kernels.c" to see how to create the string descriptions. This string can be at most 256 characters long.

A similar function for your motion kernels is provided in the file motion.c.

##### Driver

The source code you will write will be linked with object code that we supply into a driver binary. To create this binary, you will need to execute the command

\$ make driver

You will need to re-make driver each time you change the code in "kernels.c".

To test your implementations, you can then run the command:

\$ ./driver

The driver can be run in four different modes:

• Default mode, in which all versions of your implementation are run.

• Autograder mode, in which only the pinwheel and motion functions are run. This is the mode we will run in when we use the driver to grade your handin.

• File mode, in which only versions that are mentioned in an input file are run.

• Dump mode, in which a one-line description of each version is dumped to a text file. You can then edit this text file to keep only those versions that you’d like to test using the file mode. You can specify whether to quit after dumping the file or if your implementations are to be run.

If run without any arguments, driver will run all of your versions (default mode). Other modes and options can be specified by command-line arguments to driver, as listed below:

• -g Run only pinwheel and motion functions (autograder mode).

• -f funcfile Execute only those versions specified in funcfile (file mode).

• -d dumpfile- Dump the names of all versions to a dump file called dumpfile, one line to a version (dump mode).

• -q Quit after dumping version names to a dump file. To be used in tandem with -d. For example, to quit immediately after printing the dump file, type ./driver -qd dumpfile.

• -i Write the main test images to files that end in ".image". Each file contains N twice to indicate the height and width of the image, and it contains the pixel values as a sequence of red, green, and blue numbers.

Combined with -m gradient or -m squares and viewed with a suitable decoder, these dumps can be helpful in debugging problems with your functions.

One way to view a file’s image is to run

\$ racket show-image.rkt filename

on a CADE machine, or you can use

\$ racket show-image.rkt --png filename

to generate a ".png" version.

• -I Write all the benchmarks images to files that end in ".image".

• -m mode Selects the starting image, where mode can be random, gradient, or squares. The default (and grading mode) is random.

• -h Print the command line usage.

##### Optimizing Pinwheel (50 points)

In this part, you will optimize pinwheel to achieve as low a CPE as possible. You should compile driver and then run it with the appropriate arguments to test your implementations.

For example, running driver with the supplied naive version (for pinwheel) generates the output shown below:

 \$ ./driver Name: Harry Q. Bovik Email: bovik@nowhere.edu Pinwheel: Version = naive_pinwheel: baseline implementation: Dim 64 128 256 512 1024 Mean Your CPEs 17.1 16.9 17.3 17.7 20.9 Baseline CPEs 17.1 16.9 17.3 17.7 20.8 Speedup 1.0 1.0 1.0 1.0 1.0 1.0

##### Optimizing Motion (50 points)

In this part, you will optimize motion to achieve as low a CPE as possible.

For example, running driver with the supplied naive version (for motion) generates the output shown below:

 \$ ./driver Motion: Version = naive_motion: baseline implementation: Dim 32 64 128 256 512 Mean Your CPEs 203.8 210.4 214.0 215.7 216.5 Baseline CPEs 204.0 210.0 214.0 215.0 216.0 Speedup 1.0 1.0 1.0 1.0 1.0 1.0

##### Coding Rules

You may write any code you want, as long as it satisfies the following:

• It must be in ANSI C. You may not use any embedded assembly language statements or special gcc directives.

• It must not interfere with the time measurement mechanism. You may also be penalized if your code prints any extraneous information.

You can only modify code in "kernels.c". You are allowed to define macros, additional global variables, and other procedures in these files.

##### Evaluation

Your solutions for pinwheel and motion will each count for 50% of your grade. The score for each will be based on the following:

• Correctness: You will get NO CREDIT for buggy code that causes the driver to complain! This includes code that correctly operates on the test sizes, but incorrectly on image matrices of other sizes. As mentioned earlier, you may assume that the image dimension is a multiple of 32.

• CPE: You will get full credit for your implementations of pinwheel and motion if they are correct and achieve mean CPEs improvements at or above thresholds 3.5 and 1.6, respectively. In more detail:

• pinwheel
• between 1.0–2.0 mean speedup: linear mapping to 0–30 points

• between 2.0–3.5 mean speedup: linear mapping to 30–50 points

• above 3.5 mean speedup: 50 points

• motion
• between 1.0–1.3 mean speedup: linear mapping to 0–30 points

• between 1.3–1.6 mean speedup: linear mapping to 30–50 points

• above 1.6 mean speedup: 50 points

##### Hand In Instructions

When you have completed the lab, you will hand in one file, "kernels.c", that contains your solution. Use Canvas to hand in your work.

• Make sure you have included your identifying information in the student struct in "kernels.c".

• Make sure that the pinwheel and motion functions correspond to your fastest implementations, as these are the only functions that will be tested when we use the driver to grade your assignment.

• Remove any extraneous print statements.

Good luck!