In: Computer Science
Programming Assignment 1 Performance Assessment
Objective:
To write a C program (not C++) that calculates the average CPI, total processing time (T), and MIPS of a sequence of instructions, given the number of instruction classes, the CPI and total count of each instruction type, and the clock rate (frequency) of the machine.
The following is what the program would look like if it were run interactively. Your program will read values without using prompts.
Inputs:
• Clock rate of machine (MHz) • Number of instruction classes (types) • CPI of each type of instruction • Total instruction count for each type of instruction
Specification:
The program calculates the output based on choosing from a menu of choices, where each choice calls the appropriate procedure, where the choices are:
1) Enter parameters 2) Print Results 3) Quit
“Print Results” Output Format:
The Print Results option prints out the following information as shown below. Finally use upper case letters and tabs so that your inputs and outputs look just like the example.
FREQUENCY (MHz) input value
INSTRUCTION DISTRIBUTION
CLASS CPI COUNT input value input value input value input value input value input value input value input value input value
PERFORMANCE VALUES
AVERAGE CPI computed value TIME (ms) computed value MIPS computed value
Test Run:
An example of what a test run should look like when run on a terminal on Linux is shown below. All labels should be in CAPS (as shown in the example) and blank lines between input prompts and output lines should be used as in the example.
$ ./aout
(Program Execution Begin)
1) Enter Parameters 2) Print Results 3) Quit
Enter Selection: 1
Enter the frequency of the machine (MHz): 200
Enter the number of instruction classes: 3
Enter CPI of class 1: 2 Enter instruction count of class 1 (millions): 3
Enter CPI of class 2: 4 Enter instruction count of class 2 (millions): 5
Enter CPI of class 3: 6 Enter instruction count of class 3 (millions): 7
Performance assessment:
1) Enter Parameters 2) Print Results 3) Quit
Enter Selection: 2
FREQUENCY (MHz): 200
INSTRUCTION DISTRIBUTION
CLASS CPI COUNT
1 2 3
2 4 5
3 6 7
PERFORMANCE VALUES
AVERAGE CPI 4.53
TIME (ms) 340.00
MIPS 44.12
Dr. George Lazik (display your full name, not mine)
Performance assessment:
1) Enter Parameters 2) Print Results 3) Quit
Enter selection: 3
Program Terminated Normally
Notes:
Make sure all calculations are displayed truncated to 2 decimal fractional places, using the format “%.2f” in the printf statements.
Be sure that execution time is measured in milliseconds (ms).
To typecast an int x to a float y, use y = (float)x or simply y = 1.0*x
To create proper spacing, use “\t” to tab and “\n” for a new line.
Your Submission to the zyLab:
The source code as a single file named: assignment_1.c, submitted to the zyLab for Assignment 1. It will be graded when you do. You can submit your solution up to 3 times and the best score will be recorded.
You can use any editor and/or compiler you wish, but make sure your code compiles and executes under the gcc compiler on the zyLab; otherwise you will receive 0 points.
Sample Test Bench Run on a zyLab. Note that all input prompts have been eliminated from this version.
The input values:
1 200 3 2 3 4 5 6 7 2 3
The outputs:
Note that spacing is irregular due to publishing quirks here. Use TABS for spacing and you will get correct results.
FREQUENCY (MHz): 200
INSTRUCTION DISTRIBUTION
CLASS CPI COUNT
1 2 3
2 4 5
3 6 7
PERFORMANCE VALUES
AVERAGE CPI 4.53
TIME (ms) 340.00
MIPS 44.12
PROGRAM TERMINATED NORMALLY
Assignment 1 Skeleton
#include <stdio.h>
#include <stdlib.h>
//initialize values
//
int * cpi_i; //define cpi_i as a pointer to one of more
integers
//
// procedure to read all input parameters
//
void enter_params()
{
//initialize counter variables
//
int i;
cpi_sum=0;
instr_total=0;
scanf("%d", &mhz);// input frequency
scanf("%d",&classes);// input number of instruction classes
cpi_i = (int *)malloc(classes*sizeof(int)); //dynamically
allocate an array
count_i = (int *)malloc(classes*sizeof(int));//dynamically allocate
a second array
instr_total =0;
for (i=1; i <= classes; i++)
{
scanf("%d", &cpi_i[i]);// input
instruction's cpi
scanf("%d", &count_i[i]);//
input instruction's count
instr_total += count_i[i];
cpi_sum = cpi_sum + (cpi_i[i] *
count_i[i]);
}
printf("\n");
return;
}
//function computes average cpi
//
float calc_CPI()
{
}
//function computes execution time
//
float calc_CPU_time()
{
}
//function computes mips
//
float calc_MIPS()
{
}
//procedure prints input values that were read
//
void print_params()
{
}
//procedure prints calculated values
//
void print_performance()
{
}
//main program keeps reading menu selection and dispatches
accordingly
//
int main()
{
void fee(cpi_i);//free up space previously allocated above
return 0;
}
Here I am initializing classes to 0 because when the user enters selection 2 before entering parameters then we will show an error message and return. If you think it's unnecessary put it in comments.
for void fee(cpi_i); So I used free(cpi_i) inside the function where free will free up the memory of cpi_i
I modified enter_params() function as there were instructions for each input. If not required then use your enter_params() function only.
Code:
#include <stdio.h>
#include<stdlib.h>
int *cpi_i;
int *count_i;
int cpi_sum;
int instr_total;
int mhz;
int classes=0;
void enter_params()
{
//initialize counter variables
//
int i;
cpi_sum=0;
instr_total=0;
printf("Enter the frequency of the machine (MHz):
");
scanf("%d", &mhz);// input frequency
printf("Enter the number of instruction classes:
");
scanf("%d",&classes);// input number of
instruction classes
cpi_i = (int *)malloc(classes*sizeof(int));
//dynamically allocate an array
count_i = (int
*)malloc(classes*sizeof(int));//dynamically allocate a second
array
instr_total =0;
for (i=1; i <= classes; i++)
{
printf("Enter CPI of class %d:
",i);
scanf("%d", &cpi_i[i]);// input instruction's
cpi
printf("Enter instruction count of class %d
(millions): ",i);
scanf("%d", &count_i[i]);// input instruction's
count
instr_total += count_i[i];
cpi_sum = cpi_sum + (cpi_i[i] * count_i[i]);
}
printf("\n");
return;
}
//function computes average cpi
//
float calc_CPI()
{
//avg_cpi= cpi_sum/total_instructions
float avg_cpi=1.0*cpi_sum/instr_total;
return avg_cpi;
}
//function computes execution time
//
float calc_CPU_time()
{
//CPI*instructions/frequency
float execution_time=calc_CPI()*instr_total*1000/mhz;
//in ms
return execution_time;
}
//function computes mips
//
float calc_MIPS()
{
//frequency/avg_CPI
float MIPS=mhz/calc_CPI();
return MIPS;
}
//procedure prints input values that were read
//
void print_params()
{
if(classes==0){
printf("THE PARAMETERS WERE NOT
ENTERED, KINDLY SELECT OPTION 1\n");
return;
}
printf("FREQUENCY (MHz):\t%d\n",mhz);
printf("INSTRUCTION DISTRIBUTION\n");
printf("CLASS CPI COUNT\n");
int i;
for(i=1;i<=classes;++i){
printf("%d\t%d\t%d\n",i,cpi_i[i],count_i[i]);
}
}
//procedure prints calculated values
//
void print_performance()
{
if(classes==0){
printf("THE PARAMETERS WERE NOT
ENTERED, KINDLY SELECT OPTION 1\n");
return;
}
printf("PERFORMANCE VALUES\n");
printf("AVERAGE CPI\t%.2f\n",calc_CPI());
printf("TIME (ms)\t%.2f\n",calc_CPU_time());
printf("MIPS\t%.2f\n",calc_MIPS());
}
//main program keeps reading menu selection and dispatches
accordingly
//
int main()
{
int choice;
while(1){
printf("1) Enter Parameters 2)
Print Results 3) Quit\n");
printf("Enter selection: ");
scanf("%d",&choice);
if(choice==1){
enter_params();
}
else if(choice==2){
print_params();
print_performance();
}
else if(choice==3){
printf("Program
Terminated Normally\n");
break;
}
else{
printf("INVALID
CHOICE\n");
}
}
void fee(cpi_i);//free up space previously allocated
above
return 0;
}
void fee(cpi_i){
free(cpi_i);
}
Output:
If you have any doubts or find any errors kindly ask me. Thank you