【问题标题】:Matrix Multiplication with specific number of threads [duplicate]具有特定线程数的矩阵乘法[重复]
【发布时间】:2016-04-17 13:43:07
【问题描述】:

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

我的方法基于以下尝试解决类似任务的 java 实现: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. 根据我的目标,我不知道我是否走在正确的道路上,即用户可以指定应该使用多少线程。

如果有人能指导我找到解决方案,我将不胜感激。

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

【问题讨论】:

  • i 变量在循环的整个生命周期中引用相同的内存位置。检查此答案将解决您的问题:stackoverflow.com/questions/34319303/…
  • @HenkHolterman 确实,Parallel.For() 将是一种非常舒适的方法,但我不得不为用户提供定义特定线程数的可能性。

标签: c# multithreading matrix


【解决方案1】:

如果您是新手,线性代数很难从线程开始。我建议先了解 map/reduce 并在 C# 中实现它。

想象一下,如果您只有一个内核并且想要执行长时间的计算。操作系统调度了多个线程,以便一个做一些工作,然后下一个是轮流,等等。做思想实验很容易,发现上下文切换会使问题比单线程版本慢。那里没有真正的并行化。

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

您能做的最好的事情是每个核心一个线程并像this 那样对矩阵进行分区。

这里有一个想法:在您担心多线程之前,请先使用 Matrix 类,并确保每个操作都可以在单个线程中正常工作。如果您的代码不能为单个线程产生正确的答案,那么担心多线程是没有意义的。让它工作,然后弄清楚如何在多个线程之间划分问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-23
    • 1970-01-01
    • 2020-05-08
    • 1970-01-01
    • 2020-04-02
    • 2012-02-07
    相关资源
    最近更新 更多