Objective: to determine the MFLOPS for floating point addition, subtraction, multiplication and division on two different machines.

I Write a very simple C++ program:
1. Create three float arrays A, B and C with 500000000 elements
2. Call a random number generator to fill up A and B with numbers. There is a function called rand() or srand() in the library cstdlib in C++. There are some examples on the following website
3. Write the code segment that will do C = A + B for all the 500000000 elements and time this segment (see the example below)
4. Write the code segment that will do C = A - B for all the 500000000 elements and time this segment (see the example below)
5. Write the code segment that will do C = A * B for all the 500000000 elements and time this segment (see the example below)
6. Write the code segment that will do C = A / B for all the 500000000 elements and time this segment (see the example below)
7. Print the MFLOPS = (500000000/(time for operation * 106)) for
a. addition (step 3)
b. subtraction (step 4)
c. multiplication (step 5)
d. division (step 6)

>Create three float arrays A, B and C with 500000000 elements
Assuming that float is four bytes, you're looking at 6 gigs worth of storage. Your timing will be way off because of thrashing. A better solution would be to do a fraction of this and then multiply it into the whole or run the program several times and sum the results.

It's nice that you posted the requirements for your program, but where's your attempt? We're not going to do it for you.

Here is my attempt, could you look over it and tell me what I am doing wrong?

#include <cstdlib>
#include <iostream>
#include <ctime>
#include <cmath>
using namespace std;
void add(float[],float[],float[]);


int main()
{


float a[500000000],b[500000000],c[500000000];
for(int count =0; count<500000000;count++)
{
a[count]=rand();
b[count]=rand();
}


add(a,b,c);


return 0;
}
void add(float x[],float y[],float z[])
{
time_t begin,end;
float d=10;
float e=6;
int total=0;
for(long countk=0;countk<500000000;countk++)
{
z[countk]=0;
}


for(int j=0;j<10;j++)//test the loop ten time to get a more
accerate executing
//time
{
begin = time(NULL);//sequence of when the time begins
for(int county=0;county<500000000;county++)
{
z[county]=0;
}
for(long countu =1; countu<500000000;countu++)
{
z[countu]=x[countu]+y[countu];
end = time(NULL);//how long it took for the loop to finish
}


cout<<"time for loop is "<<end-begin<<"seconds"<<endl;
total=total+end-begin;
cout<<"total="<<total<<endl;


}//end first loop


cout<<"time for exexcuting c=a+b is"
<<500000000/(total*pow(d,6))<<endl;


}

Edited 3 Years Ago by happygeek: fixed formatting

This should be the output of the program:

hours since January 1, 1970 first = -4196164
hours since January 1, 1970 second = 1100010441
time for the loop is 13 seconds
total = 13
hours since January 1, 1970 second = 1100010454
time for the loop is 13 seconds
total = 26
hours since January 1, 1970 second = 1100010467
time for the loop is 13 seconds
total = 39
hours since January 1, 1970 second = 1100010480
time for the loop is 13 seconds
total = 52
hours since January 1, 1970 second = 1100010493
time for the loop is 13 seconds
total = 65
hours since January 1, 1970 second = 1100010506
time for the loop is 13 seconds
total = 78
hours since January 1, 1970 second = 1100010519
time for the loop is 13 seconds
total = 91
hours since January 1, 1970 second = 1100010532
time for the loop is 13 seconds
total = 104
hours since January 1, 1970 second = 1100010545
time for the loop is 13 seconds
total = 117
hours since January 1, 1970 second = 1100010558
time for the loop is 13 seconds
total = 130
time for executing the sum of 1 to 50000

This article has been dead for over six months. Start a new discussion instead.