0% found this document useful (0 votes)
89 views3 pages

Hajara-Yasmin Isa: CS 261 Machine Organization Lab 11 (20 Points)

This document provides instructions for completing Lab 11 in CS 261 Machine Organization. It asks the student to: 1. Record runtimes for a C code before and after applying different optimizations without and with loop unrolling. 2. Modify the given labw11 function to apply code motion and memory reference optimizations, without unrolling, and record the new runtime. 3. Further modify labw11 to apply loop unrolling and record the final optimized runtime. Well commented code for the optimized labw11 function is required. The goal is to optimize the given C code through various techniques and measure the impact on runtime performance. Instructions for using time commands to measure runtime are also provided.

Uploaded by

Giant don
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
89 views3 pages

Hajara-Yasmin Isa: CS 261 Machine Organization Lab 11 (20 Points)

This document provides instructions for completing Lab 11 in CS 261 Machine Organization. It asks the student to: 1. Record runtimes for a C code before and after applying different optimizations without and with loop unrolling. 2. Modify the given labw11 function to apply code motion and memory reference optimizations, without unrolling, and record the new runtime. 3. Further modify labw11 to apply loop unrolling and record the final optimized runtime. Well commented code for the optimized labw11 function is required. The goal is to optimize the given C code through various techniques and measure the impact on runtime performance. Instructions for using time commands to measure runtime are also provided.

Uploaded by

Giant don
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Hajara-Yasmin Isa

Name: ____________________________
670821591
UIN: ___________

CS 261 Machine Organization Lab 11 (20 points)


Review lecture 20 on Optimization from the learning content in week 12 on Blackboard
and go through Chapter 6.

Note:
1. Do not change the pdf form, just fill in the blanks (eg. do not print and scan the
file).
2. Make sure that your pdf is not empty in Gradescope after submission. If it is empty,
print your pdf as a pdf. If you still have any problems, let us know by Friday.
3. Do not forget to submit your VoiceThread video, not just upload it, you need to
click submit. If you are unsure about how to, go through the following tutorial on
submitting VoiceThread videos.
https://docs.google.com/document/d/1HXVSdd1uKORZOP5RGwuVzOd3ws20
nAE-ew9mglz_nvU/edit?usp=sharing
4. Students are expected to submit the lab by Friday, the deadline is Saturday.

For this lab we will be recording the time for the given C code, and optimize the given
code using code motion, Memory references and loop unrolling and record the time
taken after optimization.

To record the time spent running the program use the time command:

gcc lab11.c
time ./a.out
The result are 3 runtimes, we will be using the run time specified by ‘user’ to record the
times.

1. Record the times and fill the table below as you finish each part of the lab (3 points).

Unoptimized time Optimized time Optimized time


(no modifications to file) (no loop unrolling) (with loop unrolling)

user 0m2.184s user 0m0.595s user 0m0.528s


2. Modify the labw11 function by applying optimizations mentioned in class (Code
Motion, Memory References), but do not apply any loop unrolling. Show your well-
commented code below for labw11 function, highlighting the changes you’ve made, then
compile it and record your user time above(q1):
(7 points)
void labw11(long myMat1[][get_dim()], long myMat2[][get_dim()], long result[][get_dim()])
{
int i;
int j;
int k;

// added this local variable as an accumulator that will be used in the third
// for loop instead of accessing result[i][j] everytime.
// This helps with memory accessing instead we can just request for a specific
// element in result such as result[i][j] one time at the end after total has compiled the
// the needed calculation

long total;

// added this local variable so that the get_dim function does not have to copmute
// the necessary calculation over and over again each time the for loops need to
// to evaluate the comparision and determine the termination point for the loop.

long length = get_dim();

for ( i = 0 ; i < length ; i++ ){


for ( j = 0 ; j < length; j++ ){

// this calers out total everytime so that we have an accurate reading when
// computing the total

total = 0;
for ( k = 0 ; k < length ; k++ ){
total += myMat1[i][k] * myMat2[k][j];
}

// this assignment was added so that the accumulation total contains can be
// in result[i][j]

result[i][j] = total;
}
}
}
3. Modify the labw11 function further by applying loop unrolling as mentioned in class.
Play around with how many times you unroll or how many accumulators you use to see
how much you can further improve the speed of your code. Show your well-commented
code below for labw11 function, highlighting the changes you’ve made, then compile it
and record your user time above (q1) (10 points)

// The changes made in the previous problem are also applied here for the same reasons.

void labw11(long myMat1[][get_dim()], long myMat2[][get_dim()], long result[][get_dim()])


{
int i;
int j;
int k;

long total;

long length = get_dim();

// I added this to hold the new length I will be iterating over in the main for loop
long limit = ((length / 5) * 5);

for ( i = 0 ; i < length ; i++ ){


for ( j = 0 ; j < length; j++ ){
total = 0;
for ( k = 0 ; k < limit ; k = k + 5 ){

// this line of code is used to reduce the amount of time the for loop
// is executed. This helps with the number of times we have to increment and
// compare

total += myMat1[i][k] * myMat2[k][j];


total += myMat1[i][k + 1] * myMat2[k + 1][j];
total += myMat1[i][k + 2] * myMat2[k + 2][j];
total += myMat1[i][k + 3] * myMat2[k + 3][j];
total += myMat1[i][k + 4] * myMat2[k + 4][j];
}

// This takes into consideration when length amount is not divisible by 5

for (k = limit; k < length; ) {


total += myMat1[i][k] * myMat2[k][j];
}
result[i][j] = total;

}
}
}

You might also like