2

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
  • put the TIMES for loop inside SET for loop.
    – user8234870
    Commented Oct 27, 2020 at 8:29

2 Answers 2

1

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
         }
     }
 }
2
  • May I also ask, does avgTimeClassical print out the average of the average of totalTime Classical? Meaning avgTimeClassical=(totalTimeClassical/TIMES)/SETS? Or have I done this part incorrectly?
    – buns
    Commented Oct 27, 2020 at 8:45
  • 1
    Maybe you can remove these totalTimeClassical = totalTimeClassical / TIMES; lines, but update avgTimeClassical = totalTimeClassical / SETS; lines as avgTimeClassical = totalTimeClassical / (SETS * TIMES);
    – lkamal
    Commented Oct 27, 2020 at 8:52
1

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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.