Question

1) Write a script(in Bash shell) that characterizes the application performance. Specifically, your script should automatically...

1) Write a script(in Bash shell) that characterizes the application performance. Specifically, your script should automatically test both algorithms of the matrix math program for each of the array sizes shown in Table. Also, extend your script to automatically parse the output of the program.

Table (Array Sizes in Test Suite)

Algorithm 1 Algorithm 2
256 256
512 512
768 768
1024 1024
1280 1280
1536 1536
1792 1792
2048 2048

C source file

// Adapted from https://gustavus.edu/+max/courses/F2011/MCS-284/labs/lab3/
// Max Hailperin, Gustavus Adolphus College

// This program measures the speed of the matrix computation C = C + A * B,
// which is a slight generalization of matrix multiplication. (If C is
// initially 0, then this would be matrix multiplication.) The three
// matrices A, B, and C are all of size n*n, where n is passed to this
// program on its command line. The reported result is the number of
// double-precision (64-bit) floating point operations per second.
// The operational speed is unlikely to be significantly influenced
// by the particular numbers being multiplied and added; the matrices are
// initialized to a specific pseudo-random sequence for repeatability.

#include
#include
#include
#include

#include "matrix_math.h"

// Generate a pseudo-random double-precision floating point number
double randomDouble()
{
return random() / (double) 0x7fffffff;
}

int main(int argc, char* argv[])
{
int n, i;
int algorithm;

if(argc != 3)
{
printf("Program usage: %s \n", argv[0]);
return 1;
}

algorithm = atoi(argv[1]);
n = atoi(argv[2]);

if(algorithm < 1 || algorithm > 2)
{
printf("Error: Algorithm must be 1 or 2\n");
return 1;
}

if(n << 24 >> 24 != 0)
{
printf("Error: Maxtrix dimension must be divisible by 256\n");
return 1;
}

printf("Configuration: Algorithm %i, array size %i\n", algorithm, n);

// Allocate memory for three n*n arrays of
// double-precision floating point numbers.

// Note on matrix storage in memory:
// Each matrix is stored as a sequence of n^2 values. The first n
// constitute the first row of the matrix, the next n the second row, etc.
// (This is called "row major order".) As such, given that we are numbering the
// rows and columns starting from 0, to reach a position that is in row
// number i requires skipping over i full rows of n elements.
// Thus, array[row][column] is the same as array[row*n+column]

printf("Total size of all 3 arrays: %.2g MB\n", 3*sizeof(double)*n*n/1024.0/1024.0);

double *a = malloc(n*n*sizeof(double));
assert(a != NULL);
double *b = malloc(n*n*sizeof(double));
assert(b != NULL);
double *c = malloc(n*n*sizeof(double));
assert(c != NULL);

// Reset the pseudo-random number generator to a known value
// so experiments are *repeatable*
srandom(284);

// Initialize the arrays with a fixed sequence of pseudo-random numbers.
for(i = 0; i < n*n; i++)
{
a[i] = randomDouble();
b[i] = randomDouble();
c[i] = randomDouble();
}

// Get resource consumption information before the matrix
// computation as a baseline.
struct rusage before;
assert(getrusage(RUSAGE_SELF, &before) == 0);

// Now comes the matrix computation itself
if(algorithm == 1)
multiply_1(a, b, c, n);
else if(algorithm == 2)
multiply_2(a, b, c, n);

// Get the resource consumption information after the matrix computation.
struct rusage after;
assert(getrusage(RUSAGE_SELF, &after) == 0);

printf("Result for sanity checking: c[%i][%i]=%g\n",
   n-1, n-1, c[n*n-1]);

// Print the number of floating point operations per second.
// This is calculated based on the total user-mode CPU time elapsed and
// the fact that 2*n*n*n floating point operations are performed (one
// floating point multiplication and one floating point addition each
// of the n*n*n times that the line marked above is executed).
printf("Floating-point ops/sec: %.2E\n",
   2.0 * n * n * n /
   (((after.ru_utime.tv_usec - before.ru_utime.tv_usec) * 1e-6) +
   (after.ru_utime.tv_sec - before.ru_utime.tv_sec)));


// Exit normally.
return 0;
}

// Matrix multiply: C = C + A*B
// Algorithm 1
void multiply_1(double *a, double*b, double *c, int n)
{
int i, j, k;
for(i = 0; i < n; i++){
for(j = 0; j < n; j++){
for(k = 0; k < n; k++){
   c[i*n + j] += a[i*n + k] * b[k*n + j]; // <- this line is executed n*n*n times
}
}
}
}

// Matrix multiply: C = C + A*B
// Algorithm 2
void multiply_2(double *a, double*b, double *c, int n)
{
int i, j, k;

// OPTIMIZATION 1: Swapping order of loops to operate on adjacent elements
for(i = 0; i < n; i++){
for(k = 0; k < n; k++){
for(j = 0; j < n; j++){
   c[i*n + j] += a[i*n + k] * b[k*n + j]; // <- this line is executed n*n*n times
}
}
}
}

header file

#ifndef MATRIX_MATH_H
#define MATRIX_MATH_H

double randomDouble(void);
void multiply_1(double *a, double*b, double *c, int n);
void multiply_2(double *a, double*b, double *c, int n);
void multiply_3(double *a, double*b, double *c, int n);

#endif // MATRIX_MATH_H

0 0
Add a comment Improve this question Transcribed image text
Answer #1
  • As per the above problem statement i have done the script code for matrix_math.c
  • I am not attaching matrix_math.h and matrix_math.c file as you already attached the file above.

Script file:-

parser.sh

#!/bin/bash
i=1
size=256
if [ -f ./results.out ]
then
        rm results.out
fi
touch results.out
echo "Algorithm   Array size    FLOPS" >> results2.out
for i in 1 2
do
for size in {256..2048..256}
do
./matrix_math $i $size 2>&1 | tee temp.out

perl -ni -e 'print unless $. <= 3' temp.out

flops=$(awk '{last=match($0,"....E+..")
                s=substr($0,last)
                print s}' temp.out)
echo -e "    " $i "       " $size " \t" $flops >> results.out

rm temp.out
done
done

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

results.out

Algorithm   Array size    FLOPS
     1         256       1.33E+08
     1         512       6.11E+07
     1         768       1.14E+08
     1         1024      5.88E+07
     1         1280      1.28E+08
     1         1536      5.90E+07
     1         1792      1.29E+08
     1         2048      5.43E+07
     2         256       1.68E+09
     2         512       1.46E+09
     2         768       1.29E+09
     2         1024      9.21E+08
     2         1280      9.18E+08
     2         1536      9.01E+08
     2         1792      9.37E+08
     2         2048      9.15E+08

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

if you are satify with my answer then please ,please like it.

Add a comment
Know the answer?
Add Answer to:
1) Write a script(in Bash shell) that characterizes the application performance. Specifically, your script should automatically...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • If void * is a pointer to void is a "generic" pointer type, and a void...

    If void * is a pointer to void is a "generic" pointer type, and a void * can be converted to any other pointer type * without an explicit cast, why in the ,myarrcopy function the malloc is created like char and not like void? if we are going to work with different type of arrays? Here is de program: *This is a function that make a copy of any given array. * We then use this function make a...

  • System Programming in C

    Explain what the problem is within the program. Fix the problem when you create a child process per column. The code is given below. So basically, after the child processes have successfully excuted their code, the final print statement does not give the correct values. It prints the original matrix rather than the multiplied matrix.#include  #include  #include  #include  int main(int argc, char *argv[]){ int row = 0; int column = 0; row = atoi(argv[1]); column = atoi(argv[2]); int *A = ...

  • In Programming language C - How would I convert my words char array into a string...

    In Programming language C - How would I convert my words char array into a string array so I can use the strcmp() function and alphabetically sort the words? #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> int main(int argc, char*argv[]){ int i =0; int j =0; int count =0; int length = strlen(argv[1]); for(i =0; i < length; i++){ if(isalpha(argv[1][i]) == 0 ||isdigit(argv[1][i] != 0)){ count ++; } printf("%c",argv[1][i]); } char *strings; int wordNum =0; int charNum =0; strings...

  • OPERATING SYSTWM Question 31 What is the output of this C program? #include #include void main()...

    OPERATING SYSTWM Question 31 What is the output of this C program? #include #include void main() int mptr, *cptr mptr = (int*)malloc(sizeof(int)); printf("%d", "mptr); cptr = (int)calloc(sizeof(int),1); printf("%d","cptr); garbage 0 000 O garbage segmentation fault Question 8 1 pts If this program "Hello" is run from the command line as "Hello 12 3" what is the output? (char '1'is ascii value 49. char '2' is ascii value 50 char'3' is ascii value 51): #include<stdio.h> int main (int argc, char*argv[]) int...

  • How can I integrate these programs into this menu template: #define _CRT_SECURE_NO_WARNINGS #include #include #include #include...

    How can I integrate these programs into this menu template: #define _CRT_SECURE_NO_WARNINGS #include #include #include #include #include void program_one (); void program_two (); void program_three (); void program_four(); void program_five(); int main() { int menu_option = 0; while (menu_option != 9) { printf(" = 1\n"); //Change this to your first program name. Nothing else. printf(" = 2\n"); //Change this to your second program name. Nothing else. printf(" = 3\n"); //Change this to your third program name. Nothing else. printf(" =...

  • ​what is the output? Consider the following program: #include <stdio.h> #include <stdlib.h> #define size 3 void...

    ​what is the output? Consider the following program: #include <stdio.h> #include <stdlib.h> #define size 3 void func(int **a) {int tmp; for (int i = 0; i < size; i++) {for (int j = i; j < size, j++) {tmp = *(*(a+i)+j); *(*(a+i)+j) = *(*(a+j)+i); *(*(a+j)+i) = tmp;}}} int main() {int **arr = malloc(sizeof(int*) * size); for(int i = 0; i < size; i++) {arr[i] = malloc(sizeof(int) * size); for (int j = 0; j < size, j++) arr[i][j] = 2*i...

  • The following C code keeps returning a segmentation fault! Please debug so that it compiles. Also...

    The following C code keeps returning a segmentation fault! Please debug so that it compiles. Also please explain why the seg fault is happening. Thank you #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> // @Name loadMusicFile // @Brief Load the music database // 'size' is the size of the database. char** loadMusicFile(const char* fileName, int size){ FILE *myFile = fopen(fileName,"r"); // Allocate memory for each character-string pointer char** database = malloc(sizeof(char*)*size); unsigned int song=0; for(song =0; song < size;...

  • Need help with shuffle function and give 8 cards to user and computer from shuffle deck?...

    Need help with shuffle function and give 8 cards to user and computer from shuffle deck? #include <stdio.h> #include <stdbool.h> #include <string.h> #include <stdlib.h> #include <math.h> #include <time.h> typedef struct card_s{     char suit;     int face;     struct card_s *listp; } card; void card_create(card* thisNode, char cardSuit, int cardFace, card* nextLoc) {     thisNode->suit = cardSuit;     thisNode->face = cardFace;     thisNode->listp = nextLoc;     return; } void card_insertAfter(card* thisNode, card* newNode) {     card* tmpNext = NULL;    ...

  • Debug the following matrix program in C: // Program to read integers into a 3X3 matrix...

    Debug the following matrix program in C: // Program to read integers into a 3X3 matrix and display them #include <stdio.h> void display(int Matrix[3][3],int size); int main(void) {         char size;         double Matrix[size][size+1];         printf("Enter 9 elements of the matrix:\n");         int i;         for (i = 0: i <= size: i++)     {       int j = 0;       for (; j <= size++; j++){         scanf("%d", matrix[i--][4])       }     }         Display(Matrix,9);         return 0; void...

  • I am having problems getting the insertion sort function to output in descending order. Also, the...

    I am having problems getting the insertion sort function to output in descending order. Also, the initialize array function is not being called when I test it in the main function. I am wondering why it prints out the original array instead of initializing. Thank you. #include <stdio.h> void print_array(int *array, int length){ int i; for(i=0; i<length;i++) printf("%d"",",array[i]); printf("\n"); } void initialize_array(int *array, int length){ int i; for(i=0, i<length; i++;){ if (i%2 ==0) array[i]= 5; else array[i]= 0; } }...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT