Click here to Skip to main content
15,889,877 members
Please Sign up or sign in to vote.
1.67/5 (3 votes)
See more:
Write a parallel function that will multiply two n by n matrices with a variable number of threads.
This should be an efficient by choosing the number of threads and number of blocks for each thread.

Thanks in advance!
Posted
Updated 7-Mar-12 2:19am
v3
Comments
Sergey Alexandrovich Kryukov 5-Mar-12 16:02pm    
Very good, but not a question.
Do you just want to order that someone do your job for your? Good luck, then.
Or do it by yourself and ask a question when and if you stuck.
--SA
Mhd Sami Almouhtasb 5-Mar-12 16:08pm    
i did it, but it doesn't work with me, in my opinion we should make either number of thread is constant or the dimension of array constant.....
don't know what i should do...
Sergey Alexandrovich Kryukov 6-Mar-12 0:21am    
First of all, I would solve the problem using Parallel Library, things like Parallel.ForEach and see.
You should understand that creation of thread is an expensive operation, so it's easy to make worse.
I think that matrix multiplication is a typical case where parallel processing helps, because input matrices stay intact and multiplication of one row by one column is independent.
So, what's the problem?
--SA
Nagy Vilmos 7-Mar-12 6:15am    
No work from you === no work from us.

1 solution


  1. Work out multiplication compatibility, (m x n) can multiply (n x o) matrices which will yield a (m x o) matrix. That is not really needed here since we are only looking at square matrices which are n x n and thus have the correct dimensions for multiplication.
  2. Choose block size: b
  3. Create a new matrix with the dimensions (m x o).
  4. Traversing the new matrix in a row by row (or column by column) fashion create tuples in batches size b.
    Example with b = 2 and m = n = o = 3 and array indices based on one:
    1. (1,1) (1,2)
    2. (1,3) (2,1)
    3. (2,2) (2,3)
    4. (3,1) (3,2)
    5. (3,3)
  5. Start a thread for each of the batches. Using Thread.Start you will pass the list of tuples to the thread as well as a reference to the the newly created m x o matrix as well as both original matrices in the order left hand side, right hand side.
  6. In the threads work method you'll do the multiplication of row x by column y for each tuple (x, y) of the list you passed to the thread. The resulting scalar is placed into the new matrix at position (x,y).
  7. In your main program wait for all threads to finish (Join). The multiplication result is now in the newly created matrix from step 3.


That is all there is to it really. The multiplication of a row by a column or matrix multiplication[^] is straight forward enough so I won't go into that.

Regards,

Manfred
 
Share this answer
 
v5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900