Created
January 26, 2013 02:20
-
-
Save thomasjungblut/4639695 to your computer and use it in GitHub Desktop.
mat mult benchmark
This file contains hidden or 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
package de.jungblut.benchmark; | |
import java.util.Random; | |
import com.google.caliper.Param; | |
import com.google.caliper.Runner; | |
import com.google.caliper.SimpleBenchmark; | |
public class MatMultBenchmark extends SimpleBenchmark { | |
@Param({ "10", "100", "300", "1000" }) | |
private int size; | |
@Param | |
Type type; | |
public enum Type { | |
NORMAL, CACHE_OBLIVIOUS, IKJ | |
} | |
final Random r = new Random(); | |
double[][] matrix; | |
double[][] otherMatrix; | |
double[][] output; | |
@Override | |
protected void setUp() throws Exception { | |
matrix = new double[size][size]; | |
for (int i = 0; i < size; i++) { | |
for (int j = 0; j < size; j++) { | |
matrix[i][j] = r.nextDouble(); | |
} | |
} | |
otherMatrix = new double[size][size]; | |
for (int i = 0; i < size; i++) { | |
for (int j = 0; j < size; j++) { | |
otherMatrix[i][j] = r.nextDouble(); | |
} | |
} | |
output = new double[size][size]; | |
} | |
public void timeQuery(int reps) { | |
final int m = size; | |
final int n = size; | |
final int p = size; | |
int sum = 0; | |
for (int rep = 0; rep < reps; rep++) { | |
if (type == Type.NORMAL) { | |
for (int j = p; --j >= 0;) { | |
for (int i = m; --i >= 0;) { | |
double s = 0; | |
for (int k = n; --k >= 0;) { | |
s += matrix[i][k] * otherMatrix[k][j]; | |
} | |
output[i][j] = s + matrix[i][j]; | |
} | |
} | |
sum++; | |
} else if (type == Type.CACHE_OBLIVIOUS) { | |
for (int k = 0; k < n; k++) { | |
for (int i = 0; i < m; i++) { | |
for (int j = 0; j < p; j++) { | |
output[i][j] = output[i][j] + matrix[i][k] * otherMatrix[k][j]; | |
} | |
} | |
} | |
sum++; | |
} else if (type == Type.IKJ) { | |
for (int i = 0; i < m; i++) { | |
for (int k = 0; k < n; k++) { | |
for (int j = 0; j < p; j++) { | |
output[i][j] += matrix[i][k] * otherMatrix[k][j]; | |
} | |
} | |
} | |
sum++; | |
} | |
} | |
System.out.println(sum); | |
} | |
public static void main(String[] args) { | |
Runner.main(MatMultBenchmark.class, args); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment