具有特定线程数的矩阵乘法 [英] Matrix Multiplication with specific number of threads

查看:117
本文介绍了具有特定线程数的矩阵乘法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在多线程领域是全新的.目前,我正在尝试实现一个命令行程序,该程序能够将两个大小相等的矩阵相乘.主要目标是用户可以输入特定数量的线程作为命令行参数,并使用恰好该数量的线程来解决乘法任务.

I am completely new in the field of multithreading. At the moment I am trying to implement a commandline program which is able to multiply two matrices of equal size. The main goal is that the user can enter a specific number of threads as a commandline argument and that the multiplication task is solved using exactly this number of threads.

我的方法基于尝试解决类似任务的以下Java实现: Java实现矩阵乘法多线程

My approach is based on the following java implementation which tries to solve a similar task: Java Implementation Matrix Multiplication Multi-Threading

我的当前状态如下:

using System;
using System.Threading;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading.Tasks;

class Program
{
    static int rows = 16;
    static int columns = 16;
    static int[] temp = new int[rows*columns];
    static int[,] matrixA = new int[rows, columns];
    static int[,] matrixB =  new int[rows, columns];
    static int[,] result = new int[rows, columns];
    static Thread[] threadPool;

    static void runMultiplication(int index){
        for(int i = 0; i < rows; i++){
            for(int j = 0; j < columns; j++){
                Console.WriteLine();
                result[index, i] += matrixA[index, j] * matrixB[j, i];
            }
        }
    }

    static void fillMatrix(){
        for (int i = 0; i < matrixA.GetLength(0); i++) {
            for (int j = 0; j < matrixA.GetLength(1); j++) {
                matrixA[i, j] = 1;
                matrixB[i, j] = 2;
            }
        }       
    }

    static void multiplyMatrices(){
        threadPool = new Thread[rows];

        for(int i = 0; i < rows; i++){
            threadPool[i] = new Thread(() => runMultiplication(i));
            threadPool[i].Start();
        }

        for(int i = 0; i < rows; i++){
            try{
                threadPool[i].Join();
            }catch (Exception e){
                Console.WriteLine(e.Message);
             }
        }
    }

    static void printMatrix(int[,] matrix) {
        for (int i = 0; i < rows; i++)
        {
            for (int j = 0; j < columns; j++)
            {
                Console.Write(string.Format("{0} ", matrix[i, j]));
            }
            Console.Write(Environment.NewLine + Environment.NewLine);
        }       
    }

    static void Main(String[] args){
        fillMatrix();
        multiplyMatrices();
        printMatrix(result);
    }
}

此刻我有两个问题:

  1. 我的结果矩阵中包含的值与有效结果相差甚远
  2. 我不知道我是否可以按照我的目标让用户指定应该使用多少个线程.

如果有人可以指导我解决问题,我将不胜感激.

I would be very grateful if anyone could guide me to a solution.

PS:我知道现有的帖子与我的相似,但是我的帖子中的主要挑战是允许用户设置线程数,然后再解决矩阵乘法.

PS: I know there are existing posts which are similar to mine, but the main challenge in my post is to allow the user to set the number of threads which will then solve the matrix multiplication.

推荐答案

如果您是新手,线性代数是一个很难从线程开始的地方.我建议先学习映射/减少,然后在C#中实现

Linear algebra is a difficult place to begin with threads if you're new. I'd recommend learning about map/reduce first and implementing that in C#.

想象一下,如果您只有一个核,并且想要执行较长的计算.操作系统安排了多个线程,因此一个线程可以完成一些工作,然后下一个线程轮流进行操作,等等.很容易进行思想实验并弄清楚上下文切换将使问题的处理速度比单线程版本慢.那里没有真正的并行化.

Imagine if you have just one core and you wanted to perform a long calculation. Multiple threads are scheduled by the operating system so that one does some work, then the next is giving a turn, etc. It's easy to do the thought experiment and figure out that context switching will make the problem go slower than the single threaded version. There's no true parallelization there.

问题在于大多数线性代数运算不容易并行化.它们不是彼此独立的.线程数多于内核数不会改善这种情况,并且可能使性能变差.

The problem is that most linear algebra operations are not easily parallelizable. They aren't independent of each other. More threads than cores will not improve the situation and may make performance worse.

您能做的最好的事情是每个内核一个线程,并对矩阵进行分区,例如

The best you can do is one thread per core and partitioning the matrix like this.

这里有个想法:在担心多线程之前,请学习Matrix类,并确保每个操作都可以在单个线程中正常工作.如果您的代码没有为单个线程提供正确的答案,则不必担心多线程.使该工作正常,然后找出如何在多个线程之间分配问题.

Here's a thought: Before you worry about multithreading, take your Matrix class and make sure that every single operation works properly with a single thread. There's no sense in worrying about multithreading if your code doesn't produce the right answers for a single thread. Get that working, then figure out how to partition the problem among multiple threads.

这篇关于具有特定线程数的矩阵乘法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆