Question on Speed...

  • (2 Pages)
  • +
  • 1
  • 2

29 Replies - 983 Views - Last Post: 23 May 2011 - 09:56 AM Rate Topic: -----

#1 tsotne1990  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 230
  • Joined: 01-November 10

Question on Speed...

Posted 10 May 2011 - 11:28 AM

Is there any way to achieve the same (or close to) speed in C# matrix multiplication algorithm as in MATLAB? It takes 2-3 minutes to multiply 1000x1000 matrix in C# when in MATLAB it only takes seconds. I believe some multithreading should be used. But what would you recommend to make a code faster? Thanks

This post has been edited by tsotne1990: 10 May 2011 - 11:29 AM

Is This A Good Question/Topic? 0
  • +

Replies To: Question on Speed...

#2 tlhIn`toq  Icon User is offline

  • Closing in on 5,000
  • member icon

Reputation: 4928
  • View blog
  • Posts: 10,465
  • Joined: 02-June 10

Re: Question on Speed...

Posted 10 May 2011 - 11:33 AM

Let's see your code
Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Closing in on 5,000
  • member icon

Reputation: 4928
  • View blog
  • Posts: 10,465
  • Joined: 02-June 10

Re: Question on Speed...

Posted 10 May 2011 - 11:39 AM

2-3 minutes for 1000x1000???
You've got something screwy going on.

        private void btnMatrixMultiply_Click(object sender, EventArgs e)
        {
            Console.WriteLine("Start time: " + DateTime.Now.ToString("HH:mm:ss.fff"));
            for (int X = 0; X < 1000; X++)
            {
                for (int Y = 0; Y < 1000; Y++)
                {
                    int dummyvalue = X * Y;
                }
            }
            Console.WriteLine("End time: " + DateTime.Now.ToString("HH:mm:ss.fff"));

        }




Console output said:

Start time: 13:37:01.084
End time: 13:37:01.156


To me it looks like it took 72 milliseconds.
Was This Post Helpful? 2
  • +
  • -

#4 Curtis Rutland  Icon User is online

  • (╯°□°)╯︵ (~ .o.)~
  • member icon


Reputation: 3803
  • View blog
  • Posts: 6,410
  • Joined: 08-June 10

Re: Question on Speed...

Posted 10 May 2011 - 12:02 PM

That's not really matrix multiplication though:

http://en.wikipedia...._multiplication

Not that it should take you anywhere near 2 minutes.

Cough up the code, and let's see what's going on.
Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is offline

  • Closing in on 5,000
  • member icon

Reputation: 4928
  • View blog
  • Posts: 10,465
  • Joined: 02-June 10

Re: Question on Speed...

Posted 10 May 2011 - 12:12 PM

Thanks for clarification. To me a matrix is a grid of rows and columns. A bitmap is a matrix for example.

Given the complexity of the subject, I agree with Curtis Rutland that we need to see your attempt to do this before anyone can help you with it. Otherwise it because just another case of "Can someone give me code to do xxxxx"

http://msdn.microsof...x.multiply.aspx

This post has been edited by Curtis Rutland: 10 May 2011 - 12:13 PM

Was This Post Helpful? 0
  • +
  • -

#6 tsotne1990  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 230
  • Joined: 01-November 10

Re: Question on Speed...

Posted 10 May 2011 - 01:05 PM

blic static double[,] MMult(double[,] matrixA, double[,] matrixB)
        {
            int n = matrixA.GetLength(0);  //rows
            int m = matrixA.GetLength(1);  //columns
            int a = matrixB.GetLength(0);  //rows
            int b = matrixB.GetLength(1);  //columns
            double[,] MM = new double[n, b];

            if (m == a)
            {
                int k = 0;

                for (int i = 0; i < n; i++)
                {
                    for (int j = 0; j < b; j++)
                    {
                        while (k < m)
                        {
                            MM[i, j] += matrixA[i, k] * matrixB[k, j];
                            k++;
                        }
                        k = 0;
                    }
                }
                return MM;
            }
            else
            {
                MatrixDimensionsException ex = new MatrixDimensionsException(DateTime.Now,
                    "Columns of the first matrix is not equal to the rows of the second matrix!",
                    "Matrix-Multiplication Error Message");
                throw ex;



Here is the code.

And this is the threading which makes it even faster. But I wonder the power of C# in terms of speed. Can I achieve MATLAB speed?(even if it is hard)


 static double[,] Multiply(double[,] matrixA, double[,] matrixB, int nPartitions)
        {
            int nrowsA = matrixA.GetLength(0);
            int ncolsA = matrixA.GetLength(1);

            int nrowsB = matrixB.GetLength(0);
            int ncolsB = matrixB.GetLength(1);

            double[,] resM = new double[nrowsA, ncolsB];
            int rowPartitionSize = nrowsA / nPartitions;

            Task[] tasks = new Task[nPartitions];

            // I prefer to use this in case I want to add a scheduler later on, i.e. to restrict
            // the degree of parallelism
            TaskFactory factory = new TaskFactory();

            Action<int, int> multiplier = new Action<int, int>
            (
                (int fromIndex, int toIndex) =>
                {
                    for (int i = fromIndex; i < toIndex; i++)
                    {
                        for (int k = 0; k < ncolsB; k++)
                        {
                            double sum = 0;
                            for (int l = 0; l < nrowsB; l++)
                            {
                                sum += matrixA[i, l] * matrixB[l, k];
                            }

                            // locking shouldn't be required - no concurrent access to
                            // same element in array possible
                            resM[i, k] = sum;
                        }
                    }
                }
            );

            for (int iTask = 0; iTask < nPartitions; iTask++)
            {
                int start = iTask * rowPartitionSize;
                int end = (iTask + 1) * rowPartitionSize;
                tasks[iTask] = factory.StartNew(() => multiplier(start, end));
            }

            Task.WaitAll(tasks );
            return resM;

        }

Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq  Icon User is offline

  • Closing in on 5,000
  • member icon

Reputation: 4928
  • View blog
  • Posts: 10,465
  • Joined: 02-June 10

Re: Question on Speed...

Posted 10 May 2011 - 01:16 PM

I'm just curious how your coded solution compares to the built-in function for Matrix.Multiply that I linked in an earlier post.

Is your solution faster than the .NET function?

All things being equal I would assume the .NET function to be working at a level as close to assembly as possible and this be as fast as an entire team of professionals could create.
Was This Post Helpful? 4
  • +
  • -

#8 tsotne1990  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 230
  • Joined: 01-November 10

Re: Question on Speed...

Posted 11 May 2011 - 01:01 AM

No I meant the second code is faster than my first code. Could you please point me to some free Microsoft mathematical libraries? I'd really appreciate it since I don't know such sources.
Was This Post Helpful? 0
  • +
  • -

#9 tlhIn`toq  Icon User is offline

  • Closing in on 5,000
  • member icon

Reputation: 4928
  • View blog
  • Posts: 10,465
  • Joined: 02-June 10

Re: Question on Speed...

Posted 11 May 2011 - 03:56 AM

And still you don't say if you even tried the built-in function that I gave you a link for.

I don't know any math libraries off the top of my head. I'd have to perform the same Google search you would and could not recommend one over the other based on {a lack of} experience with any of them.
Was This Post Helpful? 0
  • +
  • -

#10 tsotne1990  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 230
  • Joined: 01-November 10

Re: Question on Speed...

Posted 11 May 2011 - 05:22 AM

That's because I didn't quite well understood what you mean in build in...

???
Was This Post Helpful? 0
  • +
  • -

#11 tlhIn`toq  Icon User is offline

  • Closing in on 5,000
  • member icon

Reputation: 4928
  • View blog
  • Posts: 10,465
  • Joined: 02-June 10

Re: Question on Speed...

Posted 11 May 2011 - 05:29 AM

View Posttsotne1990, on 11 May 2011 - 06:22 AM, said:

That's because I didn't quite well understood what you mean in build in...

???


Look at post #5 again. There is a link to the Matrix.Multiple method built into the .NET framework.
http://msdn.microsof...x.multiply.aspx

private void multiplicationExample()
{

    Matrix matrix1 = new Matrix(5, 10, 15, 20, 25, 30);
    Matrix matrix2 = new Matrix(2, 4, 6, 8, 10, 12);

    // matrixResult is equal to (70,100,150,220,240,352) 
    Matrix matrixResult = Matrix.Multiply(matrix1, matrix2);

    // matrixResult2 is also
    // equal to (70,100,150,220,240,352) 
    Matrix matrixResult2 = matrix1 * matrix2;


}

Was This Post Helpful? 0
  • +
  • -

#12 raheelmarwat  Icon User is offline

  • D.I.C Head

Reputation: -38
  • View blog
  • Posts: 53
  • Joined: 20-April 11

Re: Question on Speed...

Posted 11 May 2011 - 05:37 AM

i beleav you should try some multi threading in this case to run your code in to 2 segments..hope that can speed up the process..
Was This Post Helpful? -1
  • +
  • -

#13 tlhIn`toq  Icon User is offline

  • Closing in on 5,000
  • member icon

Reputation: 4928
  • View blog
  • Posts: 10,465
  • Joined: 02-June 10

Re: Question on Speed...

Posted 11 May 2011 - 05:43 AM

View Postraheelmarwat, on 11 May 2011 - 06:37 AM, said:

i beleav you should try some multi threading in this case to run your code in to 2 segments..hope that can speed up the process..


The OP has already posted code showing that when run as parallel tasks it runs faster than as a single task.

If you are unfamiliar with parallel processing it is a means to divide the work across several threads and several cores of a multi-core computer, which most modern computers are.

The OP has already supplied code that goes beyond multi-threading and into multi-thread and multi-core.

http://msdn.microsof...93(VS.100).aspx

This post has been edited by tlhIn`toq: 11 May 2011 - 05:45 AM

Was This Post Helpful? 2
  • +
  • -

#14 NotarySojac  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 49
  • View blog
  • Posts: 424
  • Joined: 30-September 10

Re: Question on Speed...

Posted 11 May 2011 - 08:08 AM

MATLAB supposedly uses some SSE registers to get improvements in some of its functions (net necessarily for your matrix multiplication scenario, but probably).

Mono uses SIMD since 09 so you might try compiling for that runtime too (something on my todo list).

http://tirania.org/b...008/Nov-03.html
Was This Post Helpful? 1
  • +
  • -

#15 lordofduct  Icon User is online

  • I'm a cheeseburger
  • member icon


Reputation: 2067
  • View blog
  • Posts: 4,061
  • Joined: 24-September 10

Re: Question on Speed...

Posted 11 May 2011 - 10:19 AM

View PosttlhIn`toq, on 10 May 2011 - 07:12 PM, said:

Thanks for clarification. To me a matrix is a grid of rows and columns. A bitmap is a matrix for example.



I'd just like to make further clear for you.

You are right to think a Bitmap is a matrix, because it fits into the definition of a matrix.

It's just that matrices have several different versions of multiplication/products. The common 'matrix multiplication' is the one outlined in this thread, this is the common form because it's the most useful in Linear Algebra.

There's several other multiplication methods for matrices, all with their own names and rules. Some that only work on certain sized matrices, or relationships between the two matrices in the operation.

This post has been edited by lordofduct: 11 May 2011 - 10:23 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2