简体   繁体   中英

Matrix multiplication using threads

I am trying to multiply 2 matrices together by using 1 thread for each cell of output.
I am using c++/g++ on unix.
How would I go about doing this?
Can I do this in a loop?

Here's my suggestion:

  1. Write a function that will compute one output cell. Give it parameters indicating which cell to compute.
  2. Write a single-threaded program that uses a loop to compute every cell (calling the function from "1"). Store all the results and don't write them out until you have finished computing all cells.
  3. Modify the program so that instead of each loop calling the function, each loop creates a thread to execute the function.
  4. Figure out how to make the "main" program wait until all threads have finished before writing out all the results.

I think that will give you a strategy to work out a solution, without me doing your homework for you.

If you have a go and it doesn't work, post your code on here and people will help you debug it. The important part is not for you to get a good answer, it is for you to learn how to solve this type of problem -- so it won't really help you if somebody just gives you the answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM