20145-5SumII_CSC407_assign1.html CSC 407: Computer Systems II: 2015 Summer II, Assignment #1 Last Modified 2015 July 21Purpose: To go over issues related to how the compiler and the linker serve you, the programmer. Computing Please ssh into ctilinux1.cstcis.cti.depaul.edu, or use your own Linux machine. Compiler optimization (45 Points) Consider the following program. /* q1.c */ #include <stdlib.h> #include <stdio.h> #define unsigned int uint #define LENGTH ((uint) 512*64) int initializeArray (uint len, int* intArray ) { uint i; for (i = 0; i < len; i++) intArray[i] = (rand() % 64); } uint countAdjacent (int maxIndex, int* intArray, int direction ) { uint i; uint sum = 0; for (i = 0; i < maxIndex; i++) if ( ( intArray[i] == (intArray[i+1] + direction) ) && ( intArray[i] == (intArray[i+2] + 2*direction) ) ) sum++; return(sum); } uint funkyFunction (uint len, int* intArray ) { uint i; uint sum = 0; for (i = 0; i < len-1; i++) if ( (i % 8) == 0x3 ) sum += 7*countAdjacent(len-2,intArray,+1); else sum += 17*countAdjacent(len-2,intArray,-1); return(sum); } int main () { int* intArray = (int*)calloc(LENGTH,sizeof(int)); initializeArray(LENGTH,intArray); printf("funkyFunction() == %d\n",funkyFunction(LENGTH,intArray)); free(intArray); return(EXIT_SUCCESS); } (8 Points) Compile it for profiling but with no extra optimization with: $ gcc -o q1None -pg q1.c # Compiles q1.c to write q1None to make profile info $ ./q1None # Runs q1None $ gprof q1None # Gives profile info on q1None Be sure to scroll all the way to the top of gprof output! What are the number of self seconds taken by: FunctionSelf secondsinitializeBigArray()__________countAdjaceent()__________funkyFunction()__________ (8 Points) How did it do the operation (i % 8) == 0x3? Was it done as a modulus (the same as an expensive division, but returns the remainder instead of the quotient) or something else? Show the assembly language for this C code using gdb to dissassemble funkyFunction() of q1None. Hint: do: $ gdb q1None . . . (gdb) disass funkyFunction Dump of assembler code for function funkyFunction: . . . and then look for the code that sets up the calls to countAdjacent(). The (i % 8) == 0x3 test is done before either countAdjacent() call. (8 Points) Compile it for profiling but with optimization with: $ gcc -o q1Compiler -O1 -pg q1.c # Compiles q1.c to write q1Compiler to make profile info $ ./q1Compiler # Runs q1Compiler $ gprof q1Compiler # Gives profile info on q1Compiler What are the number of self seconds taken by: FunctionSelf secondsinitializeBigArray()__________countAdjacent()__________funkyFunction()__________(8 Points) Use gdb to dissassemble countAdjacent() of both q1None and q1.