tests: test-progs: matrix-mult: Add matrix multiplication code for testing

Introduce matrix multiplication test code which allows us to test
performance of various cache replacement algorithms.

This needs to compiled as follows
g++ -static-libgcc mm.cpp -O2 -o mm -lm libpthread.a m5op_x86.o

The pthread static library for x86 is available from this branch
https://github.com/tiwarianoop2/m5threads

and m5op_x86 needs to be generated as described here
http://pages.cs.wisc.edu/~david/courses/cs752/Fall2015/wiki/index.php?n=Main.Homework3

This has been tested only for x86.
This commit is contained in:
Sanchayan Maity 2017-01-24 10:36:25 +05:30
parent 6e058b9eb3
commit 99817b8de9

View file

@ -0,0 +1,82 @@
#include <iomanip>
#include <iostream>
#include <random>
#include "../../../util/m5/m5op.h"
#define BLOCK_SIZE 16
using namespace std;
void multiply(double **A, double **B, double **C, int size)
{
for (int k = 0; k < size; k += BLOCK_SIZE) {
for (int j = 0; j < size; j += BLOCK_SIZE) {
for (int i = 0; i < size; i++) {
for (int jj = j; jj < min(j + BLOCK_SIZE, size); jj++) {
for (int kk = k; kk < min(k + BLOCK_SIZE, size); kk++) {
C[i][jj] += A[i][kk] * B[kk][jj];
}
}
}
}
}
}
void printMatrix(double **A, int size)
{
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
cout << setprecision(3) << setw(8) << A[i][j] << " ";
}
cout << endl;
}
}
int main(int argc, char *argv[])
{
int size = 128;
cout << "Initalizing the matricies...";
random_device rd;
mt19937 gen(rd());
uniform_real_distribution<> dis(0, 1);
double *dataA = new double[size*size];
double *dataB = new double[size*size];
double *dataC = new double[size*size];
double **A = new double*[size];
double **B = new double*[size];
double **C = new double*[size];
for (int i = 0; i < size; i++) {
A[i] = &dataA[size * i];
B[i] = &dataB[size * i];
C[i] = &dataC[size * i];
for (int j = 0; j < size; j++) {
A[i][j] = dis(gen);
B[i][j] = dis(gen);
C[i][j] = 0;
}
}
cout << "Done." << endl;
cout << "Beginning multiply...";
m5_dumpreset_stats(0, 0);
multiply(A, B, C, size);
m5_dumpreset_stats(0, 0);
cout << "Done." << endl;
delete[] A;
delete[] B;
delete[] C;
delete[] dataA;
delete[] dataB;
delete[] dataC;
}