Question: Modify the program_file.c code given below to add a second argument for the L3 cache size, and a printf to print it out. -------------------------------------------------------------------------------------------------------------------------------------------------------------------- #include

Modify the program_file.c code given below to add a second argument for the L3 cache size, and a printf to print it out.

--------------------------------------------------------------------------------------------------------------------------------------------------------------------

#include #include #include #include

int **genRandomMatrix(int n, int max) { int i, j; int **mat = malloc(n * sizeof(int *));

for (i = 0; i < n; i++) { mat[i] = malloc(n * sizeof(int));

for (j = 0; j < n; j++) { mat[i][j] = 1 + rand() % max; } }

return mat; }

void free_all(int **mat, int n) { int i;

for (i = 0; i < n; i++) { free(mat[i]); }

free(mat); }

float averageMat_v1(int **mat, int n) { int i, j, total = 0;

for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { // Note indexing: [i][j] total += mat[i][j]; } }

return (float) total / (n * n); }

float averageMat_v2(int **mat, int n) { int i, j, total = 0;

for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { // Note indexing: [j][i] total += mat[j][i]; } }

return (float) total / (n * n); }

int main(int argc, char **argv) { int i, n; int **matrix;

if (argc != 2) { fprintf(stderr, "usage: %s ", argv[0]); fprintf(stderr, "where is the dimension of the matrix "); return 1; }

n = strtol(argv[1], NULL, 10); srand(time(NULL));

matrix = genRandomMatrix(n, 100);

struct timeval tstart; struct timeval tend; float res, timer;

/* Time version 1. */ gettimeofday(&tstart, NULL); res = averageMat_v1(matrix, n); gettimeofday(&tend, NULL); timer = tend.tv_sec - tstart.tv_sec + (tend.tv_usec - tstart.tv_usec)/1.e6; printf("v1 average is: %.2f; time is %g ", res, timer);

/* Time version 2. */ gettimeofday(&tstart, NULL); res = averageMat_v2(matrix, n); gettimeofday(&tend, NULL); timer = tend.tv_sec - tstart.tv_sec + (tend.tv_usec - tstart.tv_usec)/1.e6; printf("v2 average is: %.2f; time is %g ", res, timer);

free_all(matrix, n); return 0; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!