英文:
How to test 1000 sets of data 20 times each for nxn matrices up to n=2^i?
问题
public static void main(String[] args)
{
final int SETS = 1;
final int TIMES = 2;
final int POWER = 1;
long timeStart, timeEnd;
int[][] a, b;
int n;
for (int i = 1; i <= POWER; i++)
{
n = (int) Math.pow(2, i);
long totalTimeClassical = 0;
long totalTimeDivideConquer = 0;
long totalTimeStrassen = 0;
for (int k = 1; k <= SETS; k++)
{
for (int j = 1; j <= TIMES; j++)
{
a = buildMatrix(n);
b = buildMatrix(n);
timeStart = System.nanoTime();
classicalMatrixMult(a, b, n);
timeEnd = System.nanoTime();
totalTimeClassical += timeEnd - timeStart;
timeStart = System.nanoTime();
divideConquerMatrixMult(a, b, n);
timeEnd = System.nanoTime();
totalTimeDivideConquer += timeEnd - timeStart;
timeStart = System.nanoTime();
strassenMatrixMult(a, b);
timeEnd = System.nanoTime();
totalTimeStrassen += timeEnd - timeStart;
outputMatrix(a, n);
outputMatrix(b, n);
System.out.println(" -----------");
}
}
long avgTimeClassical = totalTimeClassical / (SETS * TIMES);
long avgTimeDivideConquer = totalTimeDivideConquer / (SETS * TIMES);
long avgTimeStrassen = totalTimeStrassen / (SETS * TIMES);
System.out.print("For " + n + "x" + n + ":");
System.out.println("\nClassical Matrix multiplication took "
+ avgTimeClassical + " nanoseconds to run."
+ "\nDivide and Conquer Matrix multiplication took "
+ avgTimeDivideConquer + " nanoseconds to run."
+ "\nStrassen's Matrix Multiplication took "
+ avgTimeStrassen + " nanoseconds to run."
+ "\n");
}
}
英文:
I am working on a project that tests different methods of matrix multiplication. The goal is to test 1000 sets of data, 20 times each, for n x n matrices up to n = 2^i, where in my case I am going up to n = 256.
My methods for matrix multiplication all work just fine, but I cannot get it to test the correct amount of times.
Below is my main method, where I am attempted to test this feature for correctness in output.
public static void main(String[] args)
{
final int SETS = 1;
final int TIMES = 2;
final int POWER = 1;
long timeStart, timeEnd;
long totalTimeClassical = 0;
long totalTimeDivideConquer = 0;
long totalTimeStrassen = 0;
long avgTimeClassical = 0;
long avgTimeDivideConquer = 0;
long avgTimeStrassen = 0;
int[][] a, b;
int n;
for (int i=1; i<=POWER; i++)
{
n = (int) Math.pow(2, i);
for (int j=1; j<=TIMES; j++)
{
for (int k=1; k<=SETS; k++)
{
a = buildMatrix(n);
b = buildMatrix(n);
timeStart = System.nanoTime();
classicalMatrixMult(a, b, n);
timeEnd = System.nanoTime();
totalTimeClassical += timeEnd - timeStart;
timeStart = System.nanoTime();
divideConquerMatrixMult(a, b, n);
timeEnd = System.nanoTime();
totalTimeDivideConquer += timeEnd - timeStart;
timeStart = System.nanoTime();
strassenMatrixMult(a, b);
timeEnd = System.nanoTime();
totalTimeStrassen += timeEnd - timeStart;
outputMatrix(a,n);
outputMatrix(b,n);
System.out.println(" -----------");
}
totalTimeClassical = totalTimeClassical / TIMES;
totalTimeDivideConquer = totalTimeDivideConquer / TIMES;
totalTimeStrassen = totalTimeStrassen / TIMES;
}
avgTimeClassical = totalTimeClassical / SETS;
avgTimeDivideConquer = totalTimeDivideConquer / SETS;
avgTimeStrassen = totalTimeStrassen / SETS;
System.out.print("For " + n + "x" + n + ":");
System.out.println("\nClassical Matrix multiplication took "
+ avgTimeClassical + " nanoseconds to run."
+"\nDivide and Conquer Matrix multiplcation took "
+ avgTimeDivideConquer + " nanoseconds to run."
+"\nStrassen's Matrix Multiplication took "
+ avgTimeStrassen + " nanoseconds to run."
+ "\n");
}
}
Above is my main method, where I currently have it set to print out the input matrices so I can see if it is running the correct amount of times with the correct number of data sets. With the current values I have plugged in, I want it to test 1 SET of 2 x 2 matrices, 2 TIMES. Instead, it tests 2 SETS of 2 x 2 matrices, as the printed out values are different.
9 -9
-4 4
-2 0
3 8
-----------
-5 10
-1 1
-2 -8
-5 -5
-----------
For 2x2:
Classical Matrix multiplication took 2514 nanoseconds to run.
Divide and Conquer Matrix multiplcation took 10575 nanoseconds to run.
Strassen's Matrix Multiplication took 1356 nanoseconds to run.
I have tried to rearrange the loops but end up breaking the code. How do I format this so it tests a set of data a specified amount of times?
答案1
得分: 1
我认为你在代码中初始化矩阵的位置不正确。当前你正在为每个集合创建一个新矩阵。我认为需要进行以下操作:
for (int j=1; j<=TIMES; j++)
{
a = buildMatrix(n);
b = buildMatrix(n);
for (int k=1; k<=SETS; k++)
{
// 缩减的代码部分,用于简洁起见
}
// 缩减的代码部分,用于简洁起见
}
一个主要的注意事项:按这种方式进行操作需要确保 SETS 循环中的代码不会修改数组 a
和 b
。如果这些数组被修改,你可能需要在 SETS 循环开始时创建它们的副本。
英文:
I think you're initializing your matrices at the wrong point in your code. Right now you're creating a new matrix for each set. What I think needs to happen is:
for (int j=1; j<=TIMES; j++)
{
a = buildMatrix(n);
b = buildMatrix(n);
for (int k=1; k<=SETS; k++)
{
// Your code cut for brevity
}
// Your code cut for brevity
}
One major caveat: doing it this way requires that the code in the SETS loop does not modify arrays a
and b
. If those arrays are modified you should probably make copies of them at the beginning of the SETS loop.
答案2
得分: 1
看起来你已经错误地排列了矩阵的创建和循环乘法部分。
首先,你需要根据"SETS"值的数量创建相应数量的矩阵。
然后在该循环内,你需要根据"TIMES"的数量运行操作。
for (int i=1; i<=POWER; i++)
{
n = (int) Math.pow(2, i);
for (int j=1; j<=SETS; j++)
{
a = buildMatrix(n);
b = buildMatrix(n);
for (int k=1; k<=TIMES; k++)
{
// 操作代码
}
}
}
英文:
It looks like you have ordered the matrix creation and times loops incorrectly.
First you need to create the number of matrices for the number of "SETS" values.
Then inside that loop, you need to run the operations for the number of "TIMES".
for (int i=1; i<=POWER; i++)
{
n = (int) Math.pow(2, i);
for (int j=1; j<=SETS; j++)
{
a = buildMatrix(n);
b = buildMatrix(n);
for (int k=1; k<=TIMES; k++)
{
// operations code
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论