-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Clean up comments in matmul_test.cpp
- Loading branch information
Showing
1 changed file
with
43 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,47 +5,49 @@ | |
Princeton University | ||
[email protected] | ||
Sample Matrix-Matrix multiplication code for CoDaS-HEP Summer School | ||
Sample Matrix-Matrix multiplication code for CoDaS-HEP Summer School | ||
Purpose: | ||
To use as a example to profile with performance tuning tools such as VTune. The code does | ||
not do anything useful and is for illustrative/educational use only. It is not meant to be | ||
exhaustive or demonstrating optimal matrix-matrix multiplication techniques. | ||
Purpose: | ||
To use as a example to profile with performance tuning tools such as VTune. | ||
The code does not do anything useful and is for illustrative/educational | ||
use only. It is not meant to be exhaustive or demonstrating optimal | ||
matrix-matrix multiplication techniques. | ||
Description: | ||
Code generates two matrices with random numbers. They are stored in 2D arrays (named A and B) as | ||
well as 1D arrays (a and b). There are 4 functions that multiply A and B and store the result in | ||
matrix C (or c in the 1D case). It is possible to set the percentage of time each function is | ||
called via the command line. | ||
Code generates two matrices with random numbers. They are stored in 2D | ||
arrays (named A and B) as well as 1D arrays (a and b). There are 4 | ||
functions that multiply A and B and store the result in matrix C (or c in | ||
the 1D case). It is possible to set the percentage of time each function | ||
is called via the command line. | ||
Command line arguments: | ||
Command line arguments: | ||
N <threshold 1> <threshold 2> <threshold 3> | ||
N: size of NxN matrix | ||
**optional**: | ||
thresholds: 4 functions are distributed between 0 and 1. | ||
thresholds: 4 functions are distributed between 0 and 1. | ||
mm1 is called between 0 and <threshold 1> | ||
mm2 is called between <threshold 1> and <threshold 2> | ||
mm3 is called between <threshold 2> and <threshold 3> | ||
mm4 is called between <threshold 3> and 1. | ||
Example: | ||
to call only mm1 use 1.0 0 0 | ||
to call only mm2 use 0 1.0 1.0 | ||
to call only mm3 use 0 0 1.0 | ||
to call only mm4 use 0 0 0 | ||
*******************************************************************************************************/ | ||
|
||
#include "mm.h" | ||
|
||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
|
||
int matrix_size; //N*N matrix | ||
double thresh_1, thresh_2, thresh_3; | ||
|
||
//read command line input | ||
//set various paramaters | ||
if(argc<2) { | ||
|
@@ -55,46 +57,46 @@ int main(int argc, char *argv[]) | |
else { | ||
matrix_size=atoi(argv[1]); | ||
} | ||
|
||
if(argc<5) { | ||
thresh_1=0.1; | ||
thresh_2=0.3; | ||
thresh_2=0.3; | ||
thresh_3=0.6; | ||
cout<<"Using default thresholds: "; | ||
} | ||
else { | ||
thresh_1=atof(argv[2]); | ||
thresh_2=atof(argv[3]); | ||
thresh_3=atof(argv[4]); | ||
thresh_1=atof(argv[2]); | ||
thresh_2=atof(argv[3]); | ||
thresh_3=atof(argv[4]); | ||
cout<<"Using user supplied thresholds: "; | ||
} | ||
|
||
|
||
cout<<thresh_1<<" "<<thresh_2<<" "<<thresh_3<<endl; | ||
cout<<"using matrix size:"<<matrix_size<<endl; | ||
|
||
mkl_set_num_threads(1); //needed to prevent mkl (mm4) from grabbing all available cores | ||
|
||
|
||
double **A, **B, **C; //2D arrays | ||
double *a, *b, *c; //equivalent 1D arrays | ||
|
||
A = new double*[matrix_size]; | ||
B = new double*[matrix_size]; | ||
C = new double*[matrix_size]; | ||
a = new double[matrix_size*matrix_size]; | ||
b = new double[matrix_size*matrix_size]; | ||
c = new double[matrix_size*matrix_size]; | ||
|
||
|
||
for (int i = 0 ; i < matrix_size; i++) { | ||
A[i] = new double[matrix_size]; | ||
B[i] = new double[matrix_size]; | ||
C[i] = new double[matrix_size]; | ||
} | ||
|
||
int idx; | ||
|
||
//initialize values crudely between 0 and 1 | ||
//(we don't really care what they are) | ||
for (int i=0; i<matrix_size; i++) { | ||
|
@@ -109,7 +111,7 @@ int main(int argc, char *argv[]) | |
} | ||
} | ||
|
||
|
||
int max_iters=50; //number of times to call a matrix-matrix (mm) function | ||
double random_choice; //random number from rng | ||
srand (time(NULL)); | ||
|
@@ -118,9 +120,10 @@ int main(int argc, char *argv[]) | |
gettimeofday(&t1, NULL); //get starting time | ||
|
||
|
||
|
||
//Depending on random number and earlier set thresholds call a matrix-multiplication | ||
//function mm1,mm2,mm3,mm4. This is done to purposely obfuscate the "hotspots" | ||
|
||
//Depending on random number and earlier set thresholds call a | ||
//matrix-multiplication function mm1,mm2,mm3,mm4. | ||
//This is done to purposely obfuscate the "hotspots" | ||
for (int r=0; r < max_iters; r++) { | ||
random_choice = ((double) rand() / (RAND_MAX)); | ||
if (random_choice<thresh_1) { | ||
|
@@ -140,27 +143,26 @@ int main(int argc, char *argv[]) | |
mm4(a,b,c,matrix_size); | ||
} | ||
} | ||
|
||
gettimeofday(&t2, NULL); //get ending time | ||
timersub(&t2, &t1, &t); //get elapsed time | ||
cout <<"Loop finished in " | ||
<< t.tv_sec + t.tv_usec/1000000.0 | ||
<< t.tv_sec + t.tv_usec/1000000.0 | ||
<< " seconds" << endl; | ||
|
||
|
||
|
||
|
||
|
||
|
||
for (int i = 0 ; i < matrix_size; i++) { | ||
delete A[i]; | ||
delete B[i]; | ||
delete C[i]; | ||
} | ||
|
||
|
||
delete A; | ||
delete B; | ||
delete C; | ||
|
||
|
||
return 0; | ||
} |