This post has been edited by tsotne1990: 10 May 2011 - 11:29 AM
29 Replies - 983 Views - Last Post: 23 May 2011 - 09:56 AM
#1
Question on Speed...
Posted 10 May 2011 - 11:28 AM
Replies To: Question on Speed...
#3
Re: Question on Speed...
Posted 10 May 2011 - 11:39 AM
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:
End time: 13:37:01.156
To me it looks like it took 72 milliseconds.
#4
Re: Question on Speed...
Posted 10 May 2011 - 12:02 PM
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.
#5
Re: Question on Speed...
Posted 10 May 2011 - 12:12 PM
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
#6
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;
}
#7
Re: Question on Speed...
Posted 10 May 2011 - 01:16 PM
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.
#8
Re: Question on Speed...
Posted 11 May 2011 - 01:01 AM
#9
Re: Question on Speed...
Posted 11 May 2011 - 03:56 AM
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.
#10
Re: Question on Speed...
Posted 11 May 2011 - 05:22 AM
???
#11
Re: Question on Speed...
Posted 11 May 2011 - 05:29 AM
tsotne1990, on 11 May 2011 - 06:22 AM, said:
???
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;
}
#12
Re: Question on Speed...
Posted 11 May 2011 - 05:37 AM
#13
Re: Question on Speed...
Posted 11 May 2011 - 05:43 AM
raheelmarwat, on 11 May 2011 - 06:37 AM, said:
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
#14
Re: Question on Speed...
Posted 11 May 2011 - 08:08 AM
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
#15
Re: Question on Speed...
Posted 11 May 2011 - 10:19 AM
tlhIn`toq, on 10 May 2011 - 07:12 PM, said:
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
|
|

New Topic/Question
Reply




MultiQuote







|